Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; mIRCron version 1.0
- ; By Sarten-X
- ; Copyright © 2007-2008 Sarten-X. All rights reserved.
- ;
- ; This is based on the popular cron daemon. It provides the ability to schedule
- ; a command to be run at a given time. It reads the crontab.txt file, located
- ; in the main mIRC directory. This file is formatted like its Linux/Unix/BSD
- ; counterpart, with the limitations specified below. For details on using cron,
- ; a search on Google or Wikipedia will help, or you can read the man page at
- ; http://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=5
- ;
- ; I've also added in a few extra features not included in "standard" cron that
- ; should make it more suitable for IRC.
- ;
- ; Differences between mIRCron and cron:
- ; Commands are normal mIRC commands, not system commands.
- ; Environment variables must be set using normal /SET commands with a schedule,
- ; such as: * * * * * set -u0 %RunningCron $true
- ; No usernames. No reason for them, either.
- ; %'s do not need to be escaped. They will be evaluated when the command runs.
- ; Names of days cannot be used in the day field in the crontab.
- ;
- ; Added features:
- ; Server-specific commands: if the first character of a command is a equals
- ; sign (=), then the word is assumed to be an extra field, specifying what
- ; server the command will be executed on. The field can be a wildcard, such as
- ; 0 * * * * =*.webchat.org echo -a It's the top of the hour on WebChat!
- ;
- ; Network-specific commands: The same as server-specific commands, but checking
- ; the network name instead. This may or may not be provided by your network of
- ; choice. To use a network-specific command, prefix the command with a field
- ; of at (@) followed by the network name. This field can also be a wildcard.
- ; 0 * * * * @freenode echo -a It's the top of the hour on freenode!
- ;
- ; If a server or network field is specified, the command will be run on each
- ; instance of that server or network. If no field is given, the command will be
- ; run on whatever server is running the mIRCron daemon. To run a command on all
- ; servers, specify a server-specific command with just a wildcard to match.
- ;
- ; Known bugs:
- ; Specifically on laptops, cron may run more than once per minute if the timer
- ; is delayed for any reason. This is due to the -c switch, which ensures the
- ; timer runs on schedule during normal operation.
- ;
- ;
- ; *****************************************************************************
- ; Actual code starts here. Do not edit. If you do, don't come whining to me.
- ; *****************************************************************************
- ; On startup, make an initial timer to run next minute. This will synchronize
- ; the daemon to run at the start of each minute.
- on *:start: {
- synchronizeCron
- ; Once the timer is established, run the initial "reboot" run of the daemon.
- croncheck reboot
- }
- ; This alias will synchronize Cron to the next minute.
- alias -l synchronizeCron {
- .timerCronStartup -ohi $asctime($calc($ctime - $ctime % 60 + 60),HH:nn) 1 0 if ($isalias(CronCheck)) CronCheck
- }
- ; This alias does the bulk of the processing for the mIRCron system.
- alias croncheck {
- ; Open the debugging window. This line is commented out, to remove all
- ; debugging output. All echoes check to see if the window exists. If you want
- ; to enable the debugging information, simply uncomment this line. All output
- ; will go into a hidden window.
- ;!window -ehw0 @Cron
- ; If this is the special "reboot" run of the daemon, mark a flag so we can
- ; check it later.
- if ($1 == reboot) var %RebootRun = $true
- ; If the timer isn't running, start it. It's written so if the script is
- ; unloaded, the timer will stop itself, rather than run invalid commands.
- ; The normal timers in mIRC don't seem to be accurate enough to get correct
- ; timing, so I've been forced to use a multimedia timer.
- ; This timer only runs 29 times. On its last run, the timer will be
- ; re-synchronized. This should re-synchronize every 30 minutes.
- if ((!$timer(CronCheck)) && (!%RebootRun)) .timerCronCheck -cohi 29 60000 if ($isalias(CronCheck)) CronCheck $chr(124) else .timerCronCheck off
- if ($timer(CronCheck).reps == 0) { synchronizeCron }
- ; Quickly tokenize the current time in the crontab format.
- !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)
- if ($window(@cron)) !echo @Cron Checking crontab... Current time is $1-
- ; Copy time into field variables
- !var %Minute = $1, %Hour = $2, %Date = $3, %Month = $4, %Day = $5
- ;Initialize counter, and for speed we'll also store the lines in the crontab.
- !var %lines = $lines($locate(crontab.txt)),%n = 1
- ; Iterate through each line in the crontab file.
- while (%n <= %lines) {
- !var %line = $read($locate(crontab.txt),nt,%n)
- !inc %n
- ; Skip blank lines, and only parse non-commented lines
- if ($len(%line) == 0) !continue
- if ($left(%line,1) != $chr(35)) {
- ; Get the command out of the crontab line before it's modified
- !var %Command = $iif($left(%line,1) == @,$gettok(%line,2-,32),$gettok(%line,6-,32))
- ; Throw the crontab line into the parameter variables. This should be a
- ; quick replacement for using a lot of $gettok calls
- !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),;)
- if ($window(@cron)) !echo @Cron Checking line: $1-
- if ($window(@cron)) !echo @Cron Date is %date and day is $+(%day,.) (( $3 != * && $5 != * ) && ( $NumberMatch(%Date,$3) || $NumberMatch(%Day,$replace($5,7,0)) )) || $&
- ( $NumberMatch(%Date,$3) && $NumberMatch(%Day,$replace($5,7,0)) )
- ; Check the crontab's time against the current time. This includes the
- ; rules for matching, as defined in the cron man page.
- if (($1 == @reboot && %RebootRun == $true) || $&
- ($NumberMatch(%Minute,$1) && $NumberMatch(%Hour,$2) && $NumberMatch(%Month,$4) && ( $&
- (($3 != * && $5 != *) && ($NumberMatch(%Date,$3) || $NumberMatch(%Day,$replace($5,7,0)))) || $&
- ($NumberMatch(%Date,$3) && $NumberMatch(%Day,$replace($5,7,0))) $&
- ) )) {
- ; If all the times matched, begin the additional processing.
- if ($window(@cron)) !echo @Cron Matched! Will run %Command
- ; Again, throw data into $1- for easy parsing
- !tokenize 32 %Command
- ; First check for server-specific commands
- if ($left($1,1) == =) {
- ; Check each open connection
- !var %s = $scon(0)
- while (%s) {
- ; If the server matches the server in crontab, then make the
- ; connection active and run the command.
- if ($mid($1,2) iswm $scon(%s).$server) {
- !scid $scon(%s)
- RunCronCommand $2-
- !scid -r
- }
- !dec %s
- }
- }
- ; This code is nearly identical to the server code, but using
- ; $network instead.
- elseif ($left($1,1) == @) {
- !var %s = $scon(0)
- while (%s) {
- if ($mid($1,2) iswm $scon(%s).$network) {
- !scid $scon(%s)
- RunCronCommand $2-
- !scid -r
- }
- !dec %s
- }
- }
- ; If there's no special processing fields, just run the command
- ; like normal cron
- else { RunCronCommand $1- }
- }
- }
- }
- }
- alias -l NumberMatch {
- ; This checks to see if a tested number is in the ranges given by the crontab
- ; fields.
- ; $1 = number being tested
- ; $2 = semicolon-separated list of numbers & ranges
- ; Count backwards through each range in the list
- !var %n = $numtok($2,59)
- while (%n) {
- !var %MatchNumber = $gettok($2,%n,59)
- ; Wildcards always match
- if (%MatchNumber == *) !return $true
- ; If there's a slash in the range, then use mIRC's built-in factoring
- ; operator to decide whether to do further processing.
- if ((/ isin %MatchNumber) && ($gettok(%MatchNumber,2,47) // $1)) {
- ; If the tested number is in the range, then the time matches the range.
- if (($IsRolloverNum($1,$gettok(%MatchNumber,1,47))) || ($gettok(%MatchNumber,1,47) == *)) !return $true
- }
- ; If there isn't a slash, and the number's in the range, then it matches.
- elseif ($IsRolloverNum($1,%MatchNumber)) !return $true
- ; If nothing matched (since if something matched, it wouldn't reach this
- ; point), then decrement the counter and go to the next range in the list.
- !dec %n
- }
- ; If we get theough the whole list without matching anything, return false.
- !return $false
- }
- alias -l IsRolloverNum {
- ; This alias works like the isnum condition operator, but it allows ranges to
- ; "roll over" a maximum.
- ; $1 = Number to be tested
- ; $2 = Range
- if ($numtok($2,45) == 1 || $gettok($2,1,45) < $gettok($2,2,45)) { !return $iif($1 isnum $2,$true,$false) }
- else !return $iif($1 >= $gettok($2,1,45) || $1 <= $gettok($2,2,45),$true,$false)
- }
- ; This alias just runs the passed parameters. This is to allow variables to be
- ; evaluated outside the context of mIRCron.
- alias -l RunCronCommand { $eval($1-,2) }
- ; Due to the new file locations in mIRC 6.3 and higher, it can't be guaranteed
- ; that files will be found correctly. Specifically, reading and writing files
- ; without a specific path is now assumed to be intended for the user's own
- ; directory, rather than a system-wide directory. This alias searches several
- ; locations for a requested file.
- alias -l locate {
- ; First, the default directory. Let mIRC do what it wants.
- if ($exists($1)) return $1
- ; Second, the settings directory.
- var -s %% = $+($mircdir,$1)
- if ($exists(%%)) return %%
- ; Third, mIRC's executable's directory.
- var -s %% = $+($regsubex($mircexe,/(.*\\).*/,\1),$1)
- if ($exists(%%)) return %%
- ; Finally, this script's location.
- var -s %% = $+($scriptdir,$1)
- if ($exists(%%)) return %%
- return $1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement