Advertisement
Kelven

mIRCron

Nov 13th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 10.02 KB | None | 0 0
  1. ; mIRCron version 1.0
  2. ; By Sarten-X
  3. ; Copyright © 2007-2008 Sarten-X. All rights reserved.
  4. ;
  5. ; This is based on the popular cron daemon. It provides the ability to schedule
  6. ; a command to be run at a given time. It reads the crontab.txt file, located
  7. ; in the main mIRC directory. This file is formatted like its Linux/Unix/BSD
  8. ; counterpart, with the limitations specified below. For details on using cron,
  9. ; a search on Google or Wikipedia will help, or you can read the man page at
  10. ; http://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=5
  11. ;
  12. ; I've also added in a few extra features not included in "standard" cron that
  13. ; should make it more suitable for IRC.
  14. ;
  15. ; Differences between mIRCron and cron:
  16. ; Commands are normal mIRC commands, not system commands.
  17. ; Environment variables must be set using normal /SET commands with a schedule,
  18. ;  such as: * * * * * set -u0 %RunningCron $true
  19. ; No usernames. No reason for them, either.
  20. ; %'s do not need to be escaped. They will be evaluated when the command runs.
  21. ; Names of days cannot be used in the day field in the crontab.
  22. ;
  23. ; Added features:
  24. ; Server-specific commands: if the first character of a command is a equals
  25. ;  sign (=), then the word is assumed to be an extra field, specifying what
  26. ;  server the command will be executed on. The field can be a wildcard, such as
  27. ;  0 * * * * =*.webchat.org echo -a It's the top of the hour on WebChat!
  28. ;
  29. ; Network-specific commands: The same as server-specific commands, but checking
  30. ;  the network name instead. This may or may not be provided by your network of
  31. ;  choice. To use a network-specific command, prefix the command with a field
  32. ;  of at (@) followed by the network name. This field can also be a wildcard.
  33. ;  0 * * * * @freenode echo -a It's the top of the hour on freenode!
  34. ;
  35. ; If a server or network field is specified, the command will be run on each
  36. ; instance of that server or network. If no field is given, the command will be
  37. ; run on whatever server is running the mIRCron daemon. To run a command on all
  38. ; servers, specify a server-specific command with just a wildcard to match.
  39. ;
  40. ; Known bugs:
  41. ; Specifically on laptops, cron may run more than once per minute if the timer
  42. ;  is delayed for any reason. This is due to the -c switch, which ensures the
  43. ;  timer runs on schedule during normal operation.
  44. ;
  45. ;
  46. ; *****************************************************************************
  47. ; Actual code starts here. Do not edit. If you do, don't come whining to me.
  48. ; *****************************************************************************
  49.  
  50. ; On startup, make an initial timer to run next minute. This will synchronize
  51. ; the daemon to run at the start of each minute.
  52. on *:start: {
  53.   synchronizeCron
  54.   ; Once the timer is established, run the initial "reboot" run of the daemon.
  55.   croncheck reboot
  56. }
  57.  
  58. ; This alias will synchronize Cron to the next minute.
  59. alias -l synchronizeCron {
  60.   .timerCronStartup -ohi $asctime($calc($ctime - $ctime % 60 + 60),HH:nn) 1 0 if ($isalias(CronCheck)) CronCheck
  61. }
  62.  
  63. ; This alias does the bulk of the processing for the mIRCron system.
  64. alias croncheck {
  65.   ; Open the debugging window. This line is commented out, to remove all
  66.   ; debugging output. All echoes check to see if the window exists. If you want
  67.   ; to enable the debugging information, simply uncomment this line. All output
  68.   ; will go into a hidden window.
  69.   ;!window -ehw0 @Cron
  70.  
  71.   ; If this is the special "reboot" run of the daemon, mark a flag so we can
  72.   ; check it later.
  73.   if ($1 == reboot) var %RebootRun = $true
  74.  
  75.   ; If the timer isn't running, start it. It's written so if the script is
  76.   ; unloaded, the timer will stop itself, rather than run invalid commands.
  77.   ; The normal timers in mIRC don't seem to be accurate enough to get correct
  78.   ; timing, so I've been forced to use a multimedia timer.
  79.   ; This timer only runs 29 times. On its last run, the timer will be
  80.   ; re-synchronized. This should re-synchronize every 30 minutes.
  81.   if ((!$timer(CronCheck)) && (!%RebootRun)) .timerCronCheck -cohi 29 60000 if ($isalias(CronCheck)) CronCheck $chr(124) else .timerCronCheck off
  82.   if ($timer(CronCheck).reps == 0) { synchronizeCron }
  83.  
  84.   ; Quickly tokenize the current time in the crontab format.
  85.   !tokenize 32 $asctime($calc($ctime + 5),n HH dd mm) $replace($left($asctime($calc($ctime +5),ddd),2),Su,0,Mo,1,Tu,2,We,3,Th,4,Fr,5,Sa,6)
  86.  
  87.   if ($window(@cron)) !echo @Cron Checking crontab... Current time is $1-
  88.  
  89.   ; Copy time into field variables
  90.   !var %Minute = $1, %Hour = $2, %Date = $3, %Month = $4, %Day = $5
  91.  
  92.   ;Initialize counter, and for speed we'll also store the lines in the crontab.
  93.   !var %lines = $lines($locate(crontab.txt)),%n = 1
  94.  
  95.   ; Iterate through each line in the crontab file.
  96.   while (%n <= %lines) {
  97.     !var %line = $read($locate(crontab.txt),nt,%n)
  98.     !inc %n
  99.     ; Skip blank lines, and only parse non-commented lines
  100.     if ($len(%line) == 0) !continue
  101.     if ($left(%line,1) != $chr(35)) {
  102.       ; Get the command out of the crontab line before it's modified
  103.       !var %Command = $iif($left(%line,1) == @,$gettok(%line,2-,32),$gettok(%line,6-,32))
  104.       ; Throw the crontab line into the parameter variables. This should be a
  105.       ; quick replacement for using a lot of $gettok calls
  106.       !tokenize 32 $replace(%line, @yearly,0 0 1 1 *, @annually,0 0 1 1 *, @monthly,0 0 1 * *, @weekly,0 0 * * 0, @daily,0 0 * * *, @midnight,0 0 * * *, @hourly,0 * * * *, $chr(44),;)
  107.       if ($window(@cron)) !echo @Cron Checking line: $1-
  108.  
  109.       if ($window(@cron)) !echo @Cron Date is %date and day is $+(%day,.) (( $3 != * && $5 != * ) && ( $NumberMatch(%Date,$3) || $NumberMatch(%Day,$replace($5,7,0)) )) || $&
  110.         ( $NumberMatch(%Date,$3) && $NumberMatch(%Day,$replace($5,7,0)) )
  111.  
  112.       ; Check the crontab's time against the current time. This includes the
  113.       ; rules for matching, as defined in the cron man page.
  114.       if (($1 == @reboot && %RebootRun == $true) || $&
  115.         ($NumberMatch(%Minute,$1) && $NumberMatch(%Hour,$2) && $NumberMatch(%Month,$4) && ( $&
  116.         (($3 != * && $5 != *) && ($NumberMatch(%Date,$3) || $NumberMatch(%Day,$replace($5,7,0)))) || $&
  117.         ($NumberMatch(%Date,$3) && $NumberMatch(%Day,$replace($5,7,0))) $&
  118.         ) )) {
  119.  
  120.         ; If all the times matched, begin the additional processing.
  121.         if ($window(@cron)) !echo @Cron Matched! Will run %Command
  122.         ; Again, throw data into $1- for easy parsing
  123.         !tokenize 32 %Command
  124.         ; First check for server-specific commands
  125.         if ($left($1,1) == =) {
  126.           ; Check each open connection
  127.           !var %s = $scon(0)
  128.           while (%s) {
  129.             ; If the server matches the server in crontab, then make the
  130.             ; connection active and run the command.
  131.             if ($mid($1,2) iswm $scon(%s).$server) {
  132.               !scid $scon(%s)
  133.               RunCronCommand $2-
  134.               !scid -r
  135.             }
  136.             !dec %s
  137.           }
  138.         }
  139.         ; This code is nearly identical to the server code, but using
  140.         ; $network instead.
  141.         elseif ($left($1,1) == @) {
  142.           !var %s = $scon(0)
  143.           while (%s) {
  144.             if ($mid($1,2) iswm $scon(%s).$network) {
  145.               !scid $scon(%s)
  146.               RunCronCommand $2-
  147.               !scid -r
  148.             }
  149.             !dec %s
  150.           }
  151.         }
  152.         ; If there's no special processing fields, just run the command
  153.         ; like normal cron
  154.         else { RunCronCommand $1- }
  155.       }
  156.     }
  157.   }
  158. }
  159.  
  160. alias -l NumberMatch {
  161.   ; This checks to see if a tested number is in the ranges given by the crontab
  162.   ; fields.
  163.  
  164.   ; $1 = number being tested
  165.   ; $2 = semicolon-separated list of numbers & ranges
  166.  
  167.   ; Count backwards through each range in the list
  168.   !var %n = $numtok($2,59)
  169.   while (%n) {
  170.     !var %MatchNumber = $gettok($2,%n,59)
  171.     ; Wildcards always match
  172.     if (%MatchNumber == *) !return $true
  173.     ; If  there's a slash in the range, then use mIRC's built-in factoring
  174.     ; operator to decide whether to do further processing.
  175.     if ((/ isin %MatchNumber) && ($gettok(%MatchNumber,2,47) // $1)) {
  176.       ; If the tested number is in the range, then the time matches the range.
  177.       if (($IsRolloverNum($1,$gettok(%MatchNumber,1,47))) || ($gettok(%MatchNumber,1,47) == *)) !return $true
  178.     }
  179.     ; If there isn't a slash, and the number's in the range, then it matches.
  180.     elseif ($IsRolloverNum($1,%MatchNumber)) !return $true
  181.     ; If nothing matched (since if something matched, it wouldn't reach this
  182.     ; point), then decrement the counter and go to the next range in the list.
  183.     !dec %n
  184.   }
  185.   ; If we get theough the whole list without matching anything, return false.
  186.   !return $false
  187. }
  188.  
  189. alias -l IsRolloverNum {
  190.   ; This alias works like the isnum condition operator, but it allows ranges to
  191.   ; "roll over" a maximum.
  192.   ; $1 = Number to be tested
  193.   ; $2 = Range
  194.   if ($numtok($2,45) == 1 || $gettok($2,1,45) < $gettok($2,2,45)) { !return $iif($1 isnum $2,$true,$false) }
  195.   else !return $iif($1 >= $gettok($2,1,45) || $1 <= $gettok($2,2,45),$true,$false)
  196. }
  197.  
  198. ; This alias just runs the passed parameters. This is to allow variables to be
  199. ; evaluated outside the context of mIRCron.
  200. alias -l RunCronCommand { $eval($1-,2) }
  201.  
  202. ; Due to the new file locations in mIRC 6.3 and higher, it can't be guaranteed
  203. ; that files will be found correctly. Specifically, reading and writing files
  204. ; without a specific path is now assumed to be intended for the user's own
  205. ; directory, rather than a system-wide directory. This alias searches several
  206. ; locations for a requested file.
  207. alias -l locate {
  208.   ; First, the default directory. Let mIRC do what it wants.
  209.   if ($exists($1)) return $1
  210.   ; Second, the settings directory.
  211.   var -s %% = $+($mircdir,$1)
  212.   if ($exists(%%)) return %%
  213.   ; Third, mIRC's executable's directory.
  214.   var -s %% = $+($regsubex($mircexe,/(.*\\).*/,\1),$1)
  215.   if ($exists(%%)) return %%
  216.   ; Finally, this script's location.
  217.   var -s %% = $+($scriptdir,$1)
  218.   if ($exists(%%)) return %%
  219.   return $1
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement