state.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IO::Select;
  5. use File::Basename;
  6. use File::Temp qw( tempfile );
  7. use Sys::Hostname;
  8. use IPC::System::Simple qw( system capture );
  9. # Local modules
  10. use Cwd qw(abs_path);
  11. use File::Basename qw(dirname);
  12. use lib dirname(abs_path($0));
  13. use DMX;
  14. # Prototypes
  15. sub mtime($);
  16. # User config
  17. my %DISPLAY_DEVS = ('TV' => 1, 'PROJECTOR' => 1);
  18. my $DISPLAY = undef();
  19. my $STATE_TIMEOUT = 180;
  20. my $EXISTS_TIMEOUT = 900;
  21. my $START_DELAY = 15;
  22. my %MON_FILES = ();
  23. # Host-specific config
  24. # This is necessary to avoid contention on shared disks
  25. my $HOST = Sys::Hostname::hostname();
  26. if ($HOST =~ /loki/i) {
  27. # Ensure the media path is available before we get going
  28. my $MEDIA_PATH = undef();
  29. {
  30. my $mount = $ENV{'HOME'} . '/bin/video/mediaPath';
  31. $MEDIA_PATH = capture($mount);
  32. }
  33. if (!$MEDIA_PATH || !-d $MEDIA_PATH) {
  34. die("Unable to access media path\n");
  35. }
  36. # Garage door opener
  37. $MON_FILES{ $MEDIA_PATH . '/DMX/cmd/GARAGE_CMD' } = 'EXISTS-VALUE-CLEAR';
  38. }
  39. # Available state files
  40. {
  41. # Plex
  42. $MON_FILES{'PLEX'} = 'NONE';
  43. $MON_FILES{'GUI'} = 'NONE';
  44. $MON_FILES{'PLAYING'} = 'PLAYING';
  45. # RiffTrax
  46. $MON_FILES{'RIFF'} = 'VALUE-NOUPDATE';
  47. # Motion detection
  48. $MON_FILES{'MOTION'} = 'MTIME';
  49. $MON_FILES{'MOTION_STAIRS'} = 'MTIME-NOUPDATE';
  50. $MON_FILES{'NO_MOTION'} = 'EXISTS-NOUPDATE';
  51. # Projector
  52. $MON_FILES{'PROJECTOR'} = 'STATUS';
  53. $MON_FILES{'PROJECTOR_COLOR'} = 'VALUE-NOUPDATE';
  54. $MON_FILES{'PROJECTOR_INPUT'} = 'VALUE-NOUPDATE';
  55. $MON_FILES{'PROJECTOR_LAMP'} = 'VALUE-NOUPDATE';
  56. # Amplifier
  57. $MON_FILES{'AMPLIFIER'} = 'STATUS';
  58. $MON_FILES{'AMPLIFIER_VOL'} = 'VALUE-NOUPDATE';
  59. $MON_FILES{'AMPLIFIER_MODE'} = 'VALUE-NOUPDATE';
  60. $MON_FILES{'AMPLIFIER_INPUT'} = 'VALUE-NOUPDATE';
  61. $MON_FILES{'AUDIO_AMP'} = 'EXISTS-ON';
  62. $MON_FILES{'STEREO_CMD'} = 'EXISTS-ON';
  63. # A/V Effects
  64. $MON_FILES{'LIGHTS'} = 'EXISTS-TIMEOUT';
  65. $MON_FILES{'BRIGHT'} = 'EXISTS-TIMEOUT';
  66. $MON_FILES{'RAVE'} = 'EXISTS';
  67. $MON_FILES{'EFFECT'} = 'EXISTS';
  68. # Equipment
  69. $MON_FILES{'FAN_CMD'} = 'EXISTS-ON';
  70. # OS State
  71. $MON_FILES{'FRONT_APP'} = 'VALUE-NOUPDATE';
  72. $MON_FILES{'COLOR'} = 'VALUE-NOUPDATE';
  73. $MON_FILES{'AUDIO'} = 'VALUE-NOUPDATE';
  74. $MON_FILES{'AUDIO_STATE'} = 'VALUE-NOUPDATE';
  75. # TV
  76. $MON_FILES{'TV'} = 'STATUS';
  77. $MON_FILES{'TV_VOL'} = 'VALUE';
  78. }
  79. my %EXTRAS = (
  80. 'PLAYING' => {
  81. 'URL' => qr/^file:(.+)$/m,
  82. 'YEAR' => qr/^year:(\d+)/m,
  83. 'SERIES' => qr/^showtitle:(.+)$/m,
  84. 'SEASON' => qr/^season:(\d+)/m,
  85. 'EPISODE' => qr/^episode:(\d+)/m,
  86. 'TITLE' => qr/^title:(.+)$/m,
  87. 'LENGTH' => qr/^duration:(\d+)/m,
  88. 'POSITION' => qr/^time:(\d+(?:\.\d+)?)/m,
  89. 'TYPE' => qr/^type:(.+)$/m,
  90. 'IMAGE' => qr/^fanart:(.+)$/m,
  91. 'THUMB' => qr/^thumbnail:(.+)$/m,
  92. 'ALBUM' => qr/^album:(.+)$/m,
  93. 'ARTIST' => qr/^artist:(.+)$/m,
  94. 'SELECTION' => qr/^selection:(.+)$/m,
  95. 'WINDOW' => qr/^window:(.+)$/m,
  96. 'WINDOWID' => qr/^windowid:(\d+)/m,
  97. }
  98. );
  99. # App config
  100. my $SOCK_TIMEOUT = 5;
  101. my $DATA_DIR = DMX::dataDir();
  102. my $CMD_FILE = 'STATE';
  103. my $MAX_CMD_LEN = 4096;
  104. my $RESET_CMD = $ENV{'HOME'} . '/bin/video/dmx/reset.sh';
  105. my $PUSH_TIMEOUT = 20;
  106. # Debug
  107. my $DEBUG = 0;
  108. if ($ENV{'DEBUG'}) {
  109. $DEBUG = 1;
  110. }
  111. # Command-line arguments
  112. my ($DELAY) = @ARGV;
  113. if (!$DELAY) {
  114. $DELAY = 0.5;
  115. }
  116. # Socket init
  117. my $select = DMX::stateSocket($CMD_FILE);
  118. # Add the extras to the main file list
  119. foreach my $file (keys(%EXTRAS)) {
  120. if (exists($MON_FILES{$file})) {
  121. foreach my $extra (keys(%{ $EXTRAS{$file} })) {
  122. $MON_FILES{ $file . '_' . $extra } = 'NONE';
  123. }
  124. }
  125. }
  126. # Init the file tracking structure
  127. my %files = ();
  128. foreach my $name (keys(%MON_FILES)) {
  129. my %file = (
  130. 'name' => basename($name),
  131. 'type' => $MON_FILES{$name},
  132. 'path' => $DATA_DIR . $name,
  133. 'update' => 0,
  134. 'value' => 0,
  135. 'last' => 0,
  136. );
  137. # Allow absolute paths to override the $DATA_DIR path
  138. if ($name =~ /^\//) {
  139. $file{'path'} = $name;
  140. }
  141. # Record the directory name for folder monitoring
  142. $file{'dir'} = dirname($file{'path'});
  143. # Set the attribute bits, for use in later update handling logic
  144. my %attr = (
  145. 'available' => 0,
  146. 'status' => 0,
  147. 'exists' => 0,
  148. 'value' => 0,
  149. 'clear' => 0,
  150. 'clear_off' => 0,
  151. 'mtime' => 0,
  152. 'playing' => 0,
  153. 'no_update' => 0,
  154. );
  155. if ($file{'type'} =~ /\bSTATUS\b/i) {
  156. $attr{'status'} = 1;
  157. }
  158. if ($file{'type'} =~ /\bEXISTS\b/i) {
  159. $attr{'exists'} = 1;
  160. }
  161. if ($file{'type'} =~ /\bVALUE\b/i) {
  162. $attr{'value'} = 1;
  163. }
  164. if ($file{'type'} =~ /\bCLEAR\b/i) {
  165. $attr{'clear'} = 1;
  166. }
  167. if ($file{'type'} =~ /\bON\b/i) {
  168. $attr{'clear_off'} = 1;
  169. }
  170. if ($file{'type'} =~ /\bTIMEOUT\b/i) {
  171. $attr{'clear_timeout'} = 1;
  172. }
  173. if ($file{'type'} =~ /\bMTIME\b/i) {
  174. $attr{'mtime'} = 1;
  175. }
  176. if ($file{'type'} =~ /\bPLAYING\b/i) {
  177. $attr{'playing'} = 1;
  178. }
  179. if ($file{'type'} =~ /\bNOUPDATE\b/i) {
  180. $attr{'no_update'} = 1;
  181. }
  182. # Cross-match some data types for easy of use
  183. if ($attr{'value'} || $attr{'playing'}) {
  184. $attr{'status'} = 1;
  185. }
  186. if ($attr{'status'} || $attr{'exists'}) {
  187. $attr{'mtime'} = 1;
  188. }
  189. # Push ATTR into the file hash
  190. $file{'attr'} = \%attr;
  191. # Push FILE into the files hash
  192. $files{$name} = \%file;
  193. }
  194. # Delete any existing input files, to ensure our state is reset
  195. foreach my $file (values(%files)) {
  196. if (-e $file->{'path'}) {
  197. if ($DEBUG) {
  198. print STDERR 'Deleting ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  199. }
  200. unlink($file->{'path'});
  201. }
  202. }
  203. # Reset all dependents at starts
  204. system($RESET_CMD);
  205. # Subscribers
  206. my @subscribers = ();
  207. # State
  208. my $state = 'INIT';
  209. my $stateLast = $state;
  210. my $status = '';
  211. my $statusLast = $status;
  212. my $updateLast = 0;
  213. my $timeSinceUpdate = 0;
  214. my $startTime = time() + $START_DELAY;
  215. my $pushLast = 0;
  216. # Loop forever
  217. while (1) {
  218. # Provide a method to force updates even when the state does not change
  219. my $update = 0;
  220. # Check for queued commands
  221. my @ready_clients = $select->can_read($DELAY);
  222. foreach my $fh (@ready_clients) {
  223. # Grab the inbound text
  224. my $path = undef();
  225. $fh->recv($path, $MAX_CMD_LEN);
  226. $path =~ s/^\s+//;
  227. $path =~ s/\s+$//;
  228. $path =~ s/\W/_/g;
  229. if ($DEBUG) {
  230. print STDERR 'Got path: ' . $path . "\n";
  231. }
  232. # Open the new socket
  233. my $sub = eval { DMX::clientSock($path); };
  234. if (!$sub) {
  235. print STDERR 'Unable to open socket: ' . $path . ": ${@}\n";
  236. next;
  237. }
  238. # Add the socket to our subscriber list
  239. my %tmp = (
  240. 'path' => $path,
  241. 'socket' => $sub,
  242. );
  243. push(@subscribers, \%tmp);
  244. # Force an update
  245. $update = 1;
  246. }
  247. # Use opendir/readdir in each folder to ensure fresh results
  248. my %folders = ();
  249. foreach my $file (values(%files)) {
  250. if (!$folders{ $file->{'dir'} }) {
  251. my $dh = undef();
  252. opendir($dh, $file->{'dir'})
  253. or die('Unable to open directory: ' . $file->{'dir'});
  254. if ($DEBUG) {
  255. print STDERR 'Refreshed folder: ' . $file->{'dir'} . "\n";
  256. }
  257. my @files = readdir($dh);
  258. closedir($dh);
  259. $folders{ $file->{'dir'} } = 1;
  260. }
  261. }
  262. # Monitor files of all types
  263. foreach my $file (values(%files)) {
  264. # Skip "NONE" files -- they are data stores handled in other actions
  265. if ($file->{'type'} eq 'NONE') {
  266. next;
  267. }
  268. # Always record the previous status and clear the new one
  269. $file->{'last'} = $file->{'value'};
  270. $file->{'value'} = 0;
  271. # Track available/unavailable status in each cycle
  272. {
  273. my $wasAvailable = $file->{'attr'}->{'available'};
  274. $file->{'attr'}->{'available'} = -r $file->{'path'} ? 1 : 0;
  275. if ($wasAvailable != $file->{'attr'}->{'available'}) {
  276. # Reset and extras for this file
  277. if (exists($EXTRAS{ $file->{'name'} })) {
  278. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  279. my $name = $file->{'name'} . '_' . $extra;
  280. $files{$name}{'last'} = $files{$name}{'value'};
  281. $files{$name}{'value'} = 0;
  282. }
  283. }
  284. # Determine which display device we have (if any)
  285. if (exists($DISPLAY_DEVS{ $file->{'name'} })) {
  286. $DISPLAY = $file->{'name'};
  287. if ($DEBUG) {
  288. print STDERR 'Selecting display device: ' . $DISPLAY . "\n";
  289. }
  290. }
  291. if ($DEBUG) {
  292. if ($file->{'attr'}->{'available'}) {
  293. print STDERR 'Added file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  294. } else {
  295. print STDERR 'Dropped file ' . $file->{'name'} . ': ' . $file->{'path'} . "\n";
  296. }
  297. }
  298. }
  299. }
  300. # Skip unavailable files
  301. if (!$file->{'attr'}->{'available'}) {
  302. next;
  303. }
  304. # Record the last update for MTIME files
  305. if ($file->{'attr'}->{'mtime'}) {
  306. my $mtime = mtime($file->{'path'});
  307. # All EXISTS files have MTIME set, and mtime returns 0 if the file does not exist, so we can overload this check
  308. if ($file->{'attr'}->{'exists'}) {
  309. if ($mtime > 0) {
  310. $file->{'update'} = $mtime;
  311. $file->{'value'} = 1;
  312. } elsif ($file->{'last'}) {
  313. $file->{'update'} = time();
  314. }
  315. } else {
  316. $file->{'update'} = $mtime;
  317. }
  318. if ($DEBUG) {
  319. print STDERR 'Mtime: ' . $file->{'name'} . ': ' . $file->{'value'} . ':' . $file->{'update'} . "\n";
  320. }
  321. }
  322. # Grab the state from STATUS files
  323. if ($file->{'attr'}->{'status'}) {
  324. my $text = '';
  325. {
  326. if (!open(my $fh, $file->{'path'})) {
  327. warn('Unable to open ' . $file->{'path'} . "\n");
  328. } else {
  329. local $/;
  330. $text = <$fh>;
  331. close($fh);
  332. }
  333. }
  334. # Parse the value for the file
  335. if ($file->{'attr'}->{'value'}) {
  336. $text =~ s/\n$//;
  337. $file->{'value'} = $text;
  338. } elsif ($file->{'attr'}->{'playing'}) {
  339. if ($text =~ /^playing:1/m) {
  340. $file->{'value'} = 1;
  341. }
  342. } else {
  343. if ($text =~ /1/) {
  344. $file->{'value'} = 1;
  345. }
  346. }
  347. if ($DEBUG) {
  348. print STDERR 'Status: ' . $file->{'name'} . ': ' . $file->{'value'} . "\n";
  349. }
  350. # Handle extras, if any
  351. if (exists($EXTRAS{ $file->{'name'} })) {
  352. foreach my $extra (keys(%{ $EXTRAS{ $file->{'name'} } })) {
  353. my $name = $file->{'name'} . '_' . $extra;
  354. $files{$name}{'last'} = $files{$name}{'value'};
  355. $files{$name}{'value'} = 0;
  356. $files{$name}{'update'} = $file->{'update'};
  357. if ($text =~ $EXTRAS{ $file->{'name'} }{$extra}) {
  358. $files{$name}{'value'} = $1;
  359. }
  360. if ($DEBUG) {
  361. print STDERR 'Status (extra): ' . $name . ': ' . $files{$name}{'value'} . "\n";
  362. }
  363. }
  364. }
  365. }
  366. }
  367. # Simulate the old GUI indicator
  368. {
  369. my $gui = 0;
  370. if (exists($files{'PLAYING_WINDOW'}) && $files{'PLAYING_WINDOW'}->{'value'} ne 'Fullscreen video') {
  371. $gui = 1;
  372. }
  373. $files{'GUI'}->{'value'} = $gui;
  374. }
  375. if (exists($files{'PLAYING'})) {
  376. $files{'GUI'}->{'update'} = $files{'PLAYING'}->{'update'};
  377. }
  378. # Ignore the MOTION file if NO_MOTION is set
  379. if (exists($files{'NO_MOTION'}) && $files{'NO_MOTION'}->{'value'}) {
  380. if (exists($files{'MOTION'})) {
  381. $files{'MOTION'}->{'value'} = 0;
  382. $files{'MOTION'}->{'update'} = 0;
  383. }
  384. }
  385. # Set the global update timestamp, excluding files marked NOUPDATE
  386. foreach my $file (values(%files)) {
  387. if ( $file->{'update'} > $updateLast
  388. && !$file->{'attr'}->{'no_update'}
  389. && $file->{'update'} > $startTime)
  390. {
  391. $updateLast = $file->{'update'};
  392. }
  393. }
  394. $timeSinceUpdate = time() - $updateLast;
  395. # Calculate the PLEX state
  396. $files{'PLEX'}->{'last'} = $files{'PLEX'}->{'value'};
  397. $files{'PLEX'}->{'value'} = 0;
  398. if (exists($files{'FRONT_APP'})) {
  399. if ( $files{'FRONT_APP'}->{'value'} =~ 'com.plexapp.plexhometheater'
  400. || $files{'FRONT_APP'}->{'value'} eq 'com.apple.ScreenSaver.Engine')
  401. {
  402. $files{'PLEX'}->{'value'} = 1;
  403. }
  404. }
  405. if ($files{'PLEX'}->{'last'} != $files{'PLEX'}->{'value'}) {
  406. $files{'PLEX'}->{'update'} = time();
  407. }
  408. # Determine some intermediate state data
  409. my $playing = 0;
  410. if (exists($files{'PLAYING'}) && $files{'PLAYING'}->{'value'}) {
  411. $playing = 1;
  412. }
  413. my $video = 0;
  414. if (exists($files{'PLAYING_TYPE'}) && $files{'PLAYING_TYPE'}->{'value'} ne 'audio') {
  415. $video = 1;
  416. } elsif (!exists($files{'PLAYING_TYPE'})) {
  417. $video = 1;
  418. }
  419. # Non-plex apps are always "playing" and "video"
  420. if (exists($files{'PLEX'}) && !$files{'PLEX'}->{'value'}) {
  421. $playing = 1;
  422. $video = 1;
  423. }
  424. # Calculate the new state
  425. $stateLast = $state;
  426. if (!defined($DISPLAY) || !exists($files{$DISPLAY})) {
  427. # If there's no display the state is always PLAY or PAUSE, as indicated by the master
  428. if ($playing) {
  429. $state = 'PLAY';
  430. } else {
  431. $state = 'PAUSE';
  432. }
  433. } elsif ($files{$DISPLAY}->{'value'}) {
  434. # If a display exists and is on:
  435. # PLAY when video is active and playing
  436. # PAUSE any other time (including when audio is active)
  437. if ($playing && $video) {
  438. $state = 'PLAY';
  439. } else {
  440. $state = 'PAUSE';
  441. }
  442. } else {
  443. # If the display exists but is off, check the timeouts to determine MOTION vs. OFF
  444. if ($timeSinceUpdate > $STATE_TIMEOUT) {
  445. $state = 'OFF';
  446. } elsif ($timeSinceUpdate < $STATE_TIMEOUT) {
  447. $state = 'MOTION';
  448. }
  449. }
  450. if ($state eq 'INIT') {
  451. $state = 'OFF';
  452. }
  453. # Clear EXISTS_ON files when the main state is "OFF" (and before we append their status)
  454. foreach my $file (values(%files)) {
  455. if ( $file->{'attr'}->{'clear_off'}
  456. && $file->{'value'}
  457. && $state eq 'OFF')
  458. {
  459. unlink($file->{'path'});
  460. $file->{'status'} = 0;
  461. if ($DEBUG) {
  462. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  463. }
  464. }
  465. }
  466. # Clear EXISTS_TIMEOUT files when the main state is "OFF" and EXISTS_TIMEOUT has expired (and before we append their status)
  467. foreach my $file (values(%files)) {
  468. if ( $file->{'attr'}->{'clear_timeout'}
  469. && $file->{'value'}
  470. && $state eq 'OFF'
  471. && $timeSinceUpdate > $EXISTS_TIMEOUT)
  472. {
  473. unlink($file->{'path'});
  474. $file->{'status'} = 0;
  475. if ($DEBUG) {
  476. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  477. }
  478. }
  479. }
  480. # Calculate the new status
  481. $statusLast = $status;
  482. {
  483. my @statTime = ();
  484. foreach my $file (values(%files)) {
  485. my $text = $file->{'value'};
  486. $text =~ s/\s/ /g;
  487. $text =~ s/\|/-/g;
  488. push(@statTime, $file->{'name'} . '|' . $text . '|' . $file->{'update'});
  489. }
  490. $status = "\n" . join("\n", @statTime);
  491. }
  492. # Clear EXISTS_CLEAR files immediately (but after we append their status)
  493. foreach my $file (values(%files)) {
  494. if ($file->{'attr'}->{'clear'} && $file->{'value'}) {
  495. unlink($file->{'path'});
  496. $file->{'status'} = 0;
  497. if ($DEBUG) {
  498. print STDERR 'Clearing exists flag for: ' . $file->{'name'} . "\n";
  499. }
  500. }
  501. }
  502. # Force updates on a periodic basis
  503. if (time() - $pushLast > $PUSH_TIMEOUT) {
  504. $update = 1;
  505. }
  506. # Update on any status change
  507. foreach my $file (values(%files)) {
  508. if ($file->{'value'} ne $file->{'last'}) {
  509. $update = 1;
  510. last;
  511. }
  512. }
  513. # Force updates on any state change
  514. if ($stateLast ne $state || $statusLast ne $status) {
  515. if ($DEBUG) {
  516. print STDERR 'State change: ' . $stateLast . $statusLast . ' => ' . $state . $status . "\n";
  517. }
  518. $update = 1;
  519. }
  520. # Push the update
  521. if ($update) {
  522. # Note the push
  523. $pushLast = time();
  524. # Send notifications to all subscribers
  525. foreach my $sub (@subscribers) {
  526. # Drop subscribers that are not available
  527. if (!eval { $sub->{'socket'}->send($state . $status) }) {
  528. print STDERR 'Dropping bad socket from subscriber list: ' . $sub->{'path'} . "\n";
  529. my @new_subscribers = ();
  530. foreach my $new (@subscribers) {
  531. if ($new->{'socket'} eq $sub->{'socket'}) {
  532. next;
  533. }
  534. push(@new_subscribers, $new);
  535. }
  536. @subscribers = @new_subscribers;
  537. }
  538. }
  539. # Save the state and value to disk
  540. my ($fh, $tmp) = tempfile($DATA_DIR . 'STATE.XXXXXXXX', 'UNLINK' => 0);
  541. print $fh $state . "\n";
  542. close($fh);
  543. rename($tmp, $DATA_DIR . 'STATE');
  544. }
  545. }
  546. sub mtime($) {
  547. my ($file) = @_;
  548. my $mtime = 0;
  549. if (-r $file) {
  550. (undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), undef(), $mtime, undef(), undef(), undef()) = stat($file);
  551. }
  552. return $mtime;
  553. }