; -----------------------------------------------------------------------------
; Remotely Manage mIRC Files
; -----------------------------------------------------------------------------
; -----------------------------------------------------------------------------
; Constants
; -----------------------------------------------------------------------------
; rm.site
; The host URL to be retrieving script files from
alias -l rm.host { return www.changeme.com }
; rm.path
; Path on said site to retrieve script files from
alias -l rm.path { return /change/me/ }
; rm.hash
; Hash table name (used to maintain file updates)
alias -l rm.hash { return rmlist }
; rm.hfile
; Hash file store path
alias -l rm.hfile { return system\hash\rmlist.hsh }
; -----------------------------------------------------------------------------
; Menu System
; -----------------------------------------------------------------------------
menu nicklist {
Remote mIRC
.$iif($ulist($$1,admin),Remove,Add) User: {
if ($ulist($$1,admin)) .ruser admin $$1
else /auser -a admin $$1
}
}
; -----------------------------------------------------------------------------
; Hash Table System
; -----------------------------------------------------------------------------
alias -l rm.save {
if ($hget($rm.hash)) {
var %t = 1, %dir = $nofile($rm.hfile), %tok = $iif($pos(%dir,/),47,92), %n = $numtok(%dir,%tok)
while (%t < %n) {
var %path = $gettok(%dir,$iif(%t == 1,%t,$+(1-,%t)),%tok), %t = %t + 1
if (!$isdir(%path)) /mkdir %path
}
/hsave $rm.hash $rm.hfile
}
}
on *:START: {
var %dir_list = system\hash system\backup system\scripts, %d = 1
while ($gettok(%dir_list,%d,32)) {
var %t = 1, %dir = $v1, %n = $numtok(%dir,92), %d = %d + 1
while (%t < %n) {
var %path = $gettok(%dir,$iif(%t == 1,%t,$+(1-,%t)),92), %t = %t + 1
if (!$isdir(%path)) /mkdir %path
}
}
if (!$hget($rm.hash)) /hmake $rm.hash 10
if ($isFile($rm.hfile)) /hload $rm.hash $rm.hfile
}
on *:EXIT: {
/rm.save
}
; -----------------------------------------------------------------------------
; Sockets
; -----------------------------------------------------------------------------
alias -l rm.cmd {
if ($1 == echo) return /echo -s *
return /msg $1
}
alias -l rm.chk_update {
var %cmd = $rm.cmd($1), %file = $replace($2-,$chr(32),$+(%,20)), %lupdate = $hget($rm.hash,%file)
if (%lupdate) {
/sockopen rm.update $rm.host 80
/sockmark rm.update CHKUPDATE $1 %file
[ %cmd ] Checking for update of %file $+ ...
}
else [ %cmd ] Could not update file: Previous record not found.
}
alias -l rm.update {
var %cmd = $rm.cmd($1), %file = $replace($2-,$chr(32),$+(%,20)), %lupdate = $hget($rm.hash,%file)
/sockopen rm.update $rm.host 80
/sockmark rm.update UPDATE $1 %file
[ %cmd ] Downloading update of %file $+ ...
}
on *:SOCKOPEN:rm.update: {
var %sock = $sockname, %mark = $sock(%sock).mark
var %what = $gettok(%mark,1,32), %cmd = $rm.cmd($gettok(%mark,2,32)), %file = $gettok(%mark,3-,32)
/set %rm.update.header 1
if ($right($rm.path,1) == /) var %gfile = $rm.path $+ %file
else var %gfile = $rm.path $+ / $+ %file
if ($sockerr) [ %cmd ] Could not check for update: $sock(%sock).wsmsg
else if (%what == CHKUPDATE) {
/sockwrite -n %sock HEAD %gfile HTTP/1.0
/sockwrite -n %sock Host: $rm.host
/sockwrite -n %sock User-Agent: Mozilla/4.0 (mIRC Remote)
/sockwrite -n %sock Accept: */*
/sockwrite -n %sock If-Modified-Since: $hget($rm.hash,%file)
/sockwrite -n %sock $crlf
}
else if (%what == UPDATE) {
/sockwrite -n %sock GET %gfile HTTP/1.0
/sockwrite -n %sock Host: $rm.host
/sockwrite -n %sock User-Agent: Mozilla/4.0 (mIRC Remote)
/sockwrite -n %sock Accept: */*
/sockwrite -n %sock $crlf
}
else {
[ %cmd ] Unknown action %what
/sockclose %sock
}
}
on *:SOCKREAD:rm.update: {
var %sock = $sockname, %mark = $sock(%sock).mark, %read
var %what = $gettok(%mark,1,32), %cmd = $rm.cmd($gettok(%mark,2,32)), %file = $gettok(%mark,3-,32)
if (%rm.update.header) {
/sockread %read
if ($len(%read) == 0) {
/unset %rm.update.header
return
}
}
else {
/sockread &data
}
; HEADERS
if (%rm.update.header) {
var %reg_http = /HTTP[\/0-9\.]+ ([0-9]+) .*/i
var %reg_lmod = /Last-Modified: (.*)/i
if (%what == CHKUPDATE) {
if ($regex(%read,%reg_http)) {
var %code = $regml(1)
if (%code == 304) [ %cmd ] %file has not been updated
else if (%code == 200) /set %rm.update.modified 1
else if (%code == 404) [ %cmd ] File could not be found. Check the file name, and try again.
}
else if (($regex(%read,%reg_lmod)) && (%rm.update.modified)) {
var %date = $gettok(%read,2-,32)
[ %cmd ] %file was last modified %date $+ .
/unset %rm.update.modified
}
}
else if (%what == UPDATE) {
if ($regex(%read,%reg_http)) {
var %code = $regml(1)
if (%code == 200) {
/set %rm.update.download 1
[ %cmd ] Found %file $+ , downloading...
}
else if (%code == 404) [ %cmd ] File could not be found. Check the file name, and try again.
}
else if ($regex(%read,%reg_lmod)) {
var %date = $regml(1)
if (!$hget($rm.hash,%file)) /set %rm.upload.newfile 1
/hadd $rm.hash %file %date
/rm.save
}
}
else if ($regex(%read,%reg_http)) {
var %code = $regml(1)
if (%code == 400) [ %cmd ] Could not retrieve file: bad request
else [ %cmd ] Unknown error: %read
}
}
else if (%rm.update.modified || %rm.update.download) {
var %savefile = $replace(%file,$+(%,20),$chr(32))
/echo -s /bwrite $+(",%savefile,") -1 -1 &data
/bwrite $+(",%savefile,") -1 -1 &data
/set %rm.update.file %savefile
}
}
on *:SOCKCLOSE:rm.update: {
var %sock = $sockname, %mark = $sock(%sock).mark
var %what = $gettok(%mark,1,32), %cmd = $rm.cmd($gettok(%mark,2,32)), %file = $gettok(%mark,3-,32)
/echo -s %mark
/echo -s % $+ rm.* vars: $var(%rm.*,0)
if ((%what == UPDATE) && (%rm.update.file)) {
[ %cmd ] Download complete. Loading script...
var %savefile = %rm.update.file
if ($isFile($+("system\scripts\,%savefile,"))) .copy -o $+("system\scripts\,%savefile,") $+("system\backup\,%savefile,")
.copy -o $+(",%savefile,") $+("system\scripts\,%savefile,")
.remove $+(",%savefile,")
if ($script($+("system\scripts\,%savefile,"))) /reload -rs $+("system\scripts\,%savefile,")
else /load -rs $+("system\scripts\,%savefile,")
if (*Loaded* !isin $line(Status Window,$calc($line(Status Window,0) - 1))) {
[ %cmd ] Error loading %file $+ : $line(Status Window,$calc($line(Status Window,0) - 1))
}
else [ %cmd ] %file loaded successfully.
/unset %rm.update.*
}
}
; -----------------------------------------------------------------------------
; BOT Commands
; -----------------------------------------------------------------------------
on admin:TEXT:!*:#,?: {
var %target = $iif($target == $me,$nick,$chan)
;
; !download <file>
;
; Downloads the specified file from a web server (provided), then loads the
; file.
;
if ($1 == !download) {
if ($2) {
var %file = $2-
/msg %target Downloading $+(,%file,...)
/rm.update %target %file
}
else /msg %target Insufficient parameters: !download <file>
}
;
; !update <file>
;
; Checks for an update of the file on the specified web server.
;
else if ($1 == !update) {
if ($2) {
var %file = $2-
/msg %target Checking $+(,%file,) for update...
/rm.chk_update %target %file
}
else /msg %target Insufficient parameters: !update <file>
}
;
; !unload <file>
;
; Unloads the script file
;
else if ($1 == !unload) {
if ($2) {
var %file = $2-
if ($script(%file)) {
/unload -rs %file
/msg %target %file has been unloaded.
}
else /msg %target %file is not loaded.
}
else /msg %target Insufficient parameters: !unload <file>
}
}