Guest User

Untitled

a guest
Mar 22nd, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.00 KB | None | 0 0
  1. #### mIRCStats Eggdrop Logger 2.0
  2. #### (c)1999-2004 Jules <[email protected]>
  3. #### Multiple Channel Support by SirSir <[email protected]>
  4. #### Unix/Linux Support by T_D <[email protected]> #cracks2000@EFNet
  5. ####
  6. #### This tcl creates mIRC-style channel logfiles for use with the popular
  7. #### mIRCStats <http://www.mircstats.com> program.
  8. #### This script is written for Tcl 8.1 or higher and Eggdrop 1.6.0 or higher.
  9. #### Running this script on any lower version of Tcl or Eggdrop will cause your
  10. #### eggdrop to die.
  11. ####
  12. #### For version history see history.txt
  13.  
  14.  
  15. #### Change your settings below ####
  16.  
  17. #### Check readme.txt for explanation of these settings
  18.  
  19. namespace eval mel {
  20.  
  21. # Location of logfiles
  22. variable statslogdir mel/logs
  23.  
  24. # Location of configuration files
  25. variable statsdir mel
  26.  
  27. # Static logkeeper
  28. variable staticlogkeeper fenris_
  29.  
  30. # Adds datestamp in logfile name
  31. variable dodatestamp 1
  32.  
  33. # Strips '#' from logfile name
  34. variable unixnames 1
  35.  
  36. # Enable/disable host blocking
  37. variable bhosts 0
  38.  
  39. # Day of week (1 is monday) <- for weekly rotation
  40. variable weekly 1
  41.  
  42. # Notify static logkeeper on logfile rotation?
  43. variable notify 1
  44.  
  45. # Ignore nicknames
  46. variable ignorenicks {
  47. chanserv
  48. andthisnickeither
  49. }
  50. }
  51.  
  52. # # # # # # # # # # # # DO NOT CHANGE ANYTHING BELOW HERE # # # # # # # # # # #
  53.  
  54. bind dcc * mel ::mel::dcc
  55. bind pubm - * ::mel::chatter
  56. bind join - * ::mel::enter
  57. bind sign - * ::mel::signoff
  58. bind topc - * ::mel::ctopic
  59. bind kick - * ::mel::kicked
  60. bind nick - * ::mel::nickchange
  61. bind mode - * ::mel::cmode
  62. bind part - * ::mel::parting
  63. bind ctcp - "ACTION" ::mel::action
  64.  
  65. namespace eval mel {
  66.  
  67. variable settings settings.mel
  68. variable enabled 0
  69. variable version "mEL 2.0"
  70. variable channels
  71. variable macmode 1
  72. variable rotate week
  73. variable keepers
  74. variable deadchanfile deadchans.mel
  75. variable nextswitch
  76. variable weekdays
  77. variable skipmac 0
  78. array set weekdays {
  79. 1 monday
  80. 2 tuesday
  81. 3 wednesday
  82. 4 thursday
  83. 5 friday
  84. 6 saturday
  85. 7 sunday
  86. }
  87.  
  88. variable actives [list statslogdir statsdir settings staticlogkeeper dodatestamp skipmac macmode rotate weekdays weekly nextswitch unixnames enabled version channels keepers deadchanfile bhosts notify ignorenicks]
  89.  
  90. proc starttests {} {
  91. variable actives
  92. variable starterror
  93. foreach v $actives {variable $v}
  94. if {$::tcl_version >= "8.1"} {
  95. putlog "$version - Tcl version $::tcl_version detected... OK."
  96. } else {
  97. variable starterror "Tcl version $::tcl_version detected... FAILED! I need at least Tcl 8.1 to function properly."
  98. return 1
  99. }
  100. if {[string range $::numversion 0 2] >= 106} {
  101. putlog "$version - Eggdrop version [lindex $::version 0] detected... OK."
  102. } else {
  103. variable starterror "Eggdrop version [lindex $::version 0] detected... FAILED! I need at least Eggdrop 1.6.0 to function properly."
  104. return 1
  105. }
  106. if {![file exists $statsdir] || ![file isdirectory $statsdir]} {
  107. variable starterror "Directory $statsdir is not found!"
  108. return 1
  109. } else {
  110. putlog "$version - $statsdir exists and is a directory... OK."
  111. }
  112. if {![file exists $statslogdir] || ![file isdirectory $statslogdir]} {
  113. variable starterror "Directory $statslogdir is not found!"
  114. return 1
  115. } else {
  116. putlog "$version - $statslogdir exists and is a directory... OK."
  117. }
  118. if {[validuser $staticlogkeeper]} {
  119. variable starterror "$staticlogkeeper is not a user on this bot (handle not found)"
  120. return 1
  121. }
  122. if {[lsearch [string tolower [userlist b]] [string tolower $staticlogkeeper]] != -1} {
  123. variable starterror "$staticlogkeeper is registered as a bot!"
  124. return 1
  125. }
  126. putlog "$version - $staticlogkeeper is a valid user on this bot... OK."
  127. return 0
  128. }
  129.  
  130.  
  131. proc dcc {handle idx args} {
  132. set args [split [lindex $args 0]]
  133. variable actives
  134. foreach v $actives {variable $v}
  135. if {![item find keep $handle]} {
  136. putlog "#$handle# mel $args"
  137. putidx $idx "$version - You are not allowed to perform maintenance."
  138. putidx $idx "$version - Contact $staticlogkeeper for more information."
  139. return 0
  140. }
  141. switch -- [lindex [split $args] 0] {
  142. on {
  143. putlog "#$handle# mel on"
  144. if {![array exists channels]} {
  145. putidx $idx "$version - You should add channels before turning mEL on!"
  146. return 0
  147. }
  148. if {$enabled == 1} {
  149. putidx $idx "$version - mEL already is on. Try \'.mel status\' for a status overview"
  150. return 0
  151. }
  152. variable enabled 1
  153. putidx $idx "$version - mEL is now turned on!"
  154. return 0
  155. }
  156. off {
  157. putlog "#$handle# mel off"
  158. if {$enabled == 0} {
  159. putidx $idx "$version - mEL already is off!"
  160. return 0
  161. }
  162. variable enabled 0
  163. putidx $idx "$version - mEL is now turned off!"
  164. return 0
  165. }
  166. rotation {
  167. putlog "#$handle# mel rotation"
  168. if {$enabled == 0} {
  169. putidx $idx "$version - mEL is disabled. Unable to change rotation settings."
  170. return 0
  171. }
  172. switch -- [lindex $args 1] {
  173. mac {
  174. if {$macmode == 0} {
  175. putlog "$version - Switching to mAC mode..."
  176. foreach search [binds time] {
  177. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  178. unbind time - "[lindex $search 2]" ::mel::timer
  179. }
  180. }
  181. putlog "$version - Event timers removed..."
  182. switchlogs
  183. putlog "$version - Created new logfiles for all channels..."
  184. variable macmode 1
  185. variable rotate mac
  186. arraywrite
  187. putidx $idx "$version - Now running in mAC mode"
  188. } else {
  189. putidx $idx "$version - Already running in mAC mode"
  190. return 0
  191. }
  192. }
  193. week {
  194. if {$rotate == "week"} {
  195. putidx $idx "$version - Already rotating every week"
  196. return 0
  197. }
  198. if {$macmode == 0} {
  199. variable rotate week
  200. set t [clock format [clock scan "now next $weekdays($weekly)"] -format "00 00 %d %m %Y"]
  201. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  202. arraywrite
  203. foreach search [binds time] {
  204. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  205. unbind time - "[lindex $search 2]" ::mel::timer
  206. }
  207. }
  208. if {[string length [expr [lindex $t 3] -1]] == 1} {
  209. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  210. } else {
  211. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  212. }
  213. bind time - $tt ::mel::timer
  214. putidx $idx "$version - Now rotating logfiles every week"
  215. return 0
  216. }
  217. putlog "$version - Disabling mAC mode..."
  218. variable macmode 0
  219. variable rotate week
  220. variable nextswitch {}
  221. checktimer
  222. switchlogs
  223. putlog "$version - mAC mode disabled"
  224. return 0
  225. }
  226. month {
  227. if {$rotate == "month"} {
  228. putidx $idx "$version - Already rotating every month"
  229. return 0
  230. }
  231. if {$macmode == 0} {
  232. variable rotate month
  233. set t [clock format [clock scan "now next month"] -format "00 00 01 %m %Y"]
  234. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3]01T000000"]
  235. arraywrite
  236. foreach search [binds time] {
  237. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  238. unbind time - "[lindex $search 2]" ::mel::timer
  239. }
  240. }
  241. if {[string length [expr [lindex $t 3] -1]] == 1} {
  242. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  243. } else {
  244. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  245. }
  246. bind time - $tt ::mel::timer
  247. putidx $idx "$version - Now rotating logfiles every month"
  248. return 0
  249. }
  250. putlog "$version - Disabling mAC mode..."
  251. variable macmode 0
  252. variable rotate month
  253. variable nextswitch {}
  254. checktimer
  255. switchlogs
  256. putlog "$version - mAC mode disabled"
  257. return 0
  258. }
  259. default {
  260. if {$macmode == 0} {
  261. if {[string is integer [lindex $args 1]]} {
  262. if {[expr [lindex $args 1] < 1]} {
  263. putidx $idx "$version - Invalid argument, expecting integer greater than zero"
  264. return 0
  265. }
  266. variable rotate [lindex $args 1]
  267. set t [clock format [clock scan "now next $rotate days"] -format "00 00 %d %m %Y"]
  268. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  269. arraywrite
  270. foreach search [binds time] {
  271. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  272. unbind time - "[lindex $search 2]" ::mel::timer
  273. }
  274. }
  275. if {[string length [expr [lindex $t 3] -1]] == 1} {
  276. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  277. } else {
  278. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  279. }
  280. bind time - $tt ::mel::timer
  281. putidx $idx "$version - Now rotating logfiles every [lindex $args 1] days"
  282. return 0
  283. }
  284. putidx $idx "$version - Invalid argument, expecting integer"
  285. return 0
  286. }
  287. putlog "$version - Disabling mAC mode..."
  288. variable macmode 0
  289. variable rotate [lindex $args 1]
  290. variable nextswitch {}
  291. checktimer
  292. switchlogs
  293. putlog "$version - mAC mode disabled"
  294. return 0
  295. }
  296. }
  297. }
  298. status {
  299. putlog "#$handle# mel status"
  300. if {[lindex $args 1] == "all"} {
  301. showstatus [::hand2idx $handle] all
  302. } else {
  303. showstatus [::hand2idx $handle] norm
  304. }
  305. }
  306. +chan {
  307. set whatchan [lindex $args 1]
  308. if {$whatchan != ""} {
  309. putlog "#$handle# mel +chan"
  310. if {[string index $whatchan 0] != "#"} {set whatchan "#$whatchan"}
  311. if {[item find chan $whatchan]} {
  312. putidx $idx "$version - $whatchan is already being logged"
  313. return 0
  314. }
  315. set cflag 0
  316. foreach c [::channels] {
  317. if {[string compare -nocase $whatchan $c] == 0} {set cflag 1}
  318. }
  319. if {$cflag == 0} {
  320. putidx $idx "$version - Can't add $whatchan - I am not on that channel"
  321. return 0
  322. }
  323. variable enabled 1
  324. if {![info exists nextswitch]} {
  325. variable nextswitch 0
  326. }
  327. variable skipmac 1
  328. item add chan $whatchan
  329. variable skipmac 0
  330. putidx $idx "$version - Now logging channel $whatchan"
  331. return 0
  332. }
  333. putidx $idx "$version - You forgot to include a channel!"
  334. return 0
  335. }
  336. -chan {
  337. set whatchan [lindex [split $args] 1]
  338. if {$whatchan != ""} {
  339. putlog "#$handle# mel -chan"
  340. if {[string index $whatchan 0] != "#"} {set whatchan "#$whatchan"}
  341. if {![array exists channels]} {
  342. putidx $idx "$version - Can't remove any channel because no channels have been added yet!"
  343. return 0
  344. }
  345. if {![item find chan $whatchan]} {
  346. putidx $idx "$version - Can't remove channel $whatchan because channel hasn't been added yet!"
  347. return 0
  348. }
  349. item del chan $whatchan
  350. putidx $idx "$version - No longer logging channel $whatchan"
  351. putidx $idx "$version - Use \'.mel erase\' to remove the logfiles for channel $whatchan"
  352. return 0
  353. }
  354. putidx $idx "$version - You forgot to include a channel!"
  355. return 0
  356. }
  357. +keep {
  358. set who [lindex [split $args] 1]
  359. if {$who != ""} {
  360. putlog "#$handle# mel +keep"
  361. if {[string compare -nocase $handle $staticlogkeeper] != 0} {
  362. putidx $idx "$version - You can't add or remove logkeepers"
  363. return 0
  364. }
  365. if {![::validuser $who]} {
  366. putidx $idx "$version - $who is not a valid user on this bot"
  367. return 0
  368. }
  369. if {[item find keep $who]} {
  370. putidx $idx "$version - $who already is a logkeeper"
  371. return 0
  372. }
  373. item add keep $who
  374. putidx $idx "$version - $who has been added as a logkeeper"
  375. return 0
  376. }
  377. putidx $idx "$version - You forgot to specify a username!"
  378. return 0
  379. }
  380. -keep {
  381. set who [lindex [split $args] 1]
  382. if {$who != ""} {
  383. putlog "#$handle# mel -keep"
  384. if {[string compare -nocase $handle $staticlogkeeper] != 0} {
  385. putidx $idx "$version - You can't add or remove logkeepers"
  386. return 0
  387. }
  388. if {[string compare -nocase $who $staticlogkeeper] == 0} {
  389. putidx $idx "$version - You can't remove the static logkeeper from the partyline"
  390. return 0
  391. }
  392. if {![item find keep $who]} {
  393. putidx $idx "$version - $who is not registeren as a logkeeper"
  394. return 0
  395. }
  396. item del keep $who
  397. putidx $idx "$version - $who has been removed as a logkeeper"
  398. return 0
  399. }
  400. putidx $idx "$version - You forgot to specify a username!"
  401. return 0
  402. }
  403. erase {
  404. putlog "#$handle# mel erase"
  405. logfile kill [::hand2idx $handle]
  406. return 0
  407. }
  408. default {
  409. putidx $idx "\002Usage:\002 .mel <on/off/rotation/status/+chan/-chan/+keep/-keep/erase>"
  410. putidx $idx "For more details refer to the helpfile or type \'.help mel\'"
  411. return 0
  412. }
  413. }
  414. }
  415.  
  416. proc justify {args} {
  417. if {![info exists ::spacer]} {set spc " "} else {set spc [string index $::spacer 0]}
  418. set msg [lindex $args 1]
  419. set width [lindex $args end]
  420. switch -- [lindex $args 0] {
  421. l {
  422. if {$width <= [string length $msg]} {return $msg}
  423. return [string replace [string repeat $spc $width] 0 [expr [string length $msg] -1] $msg]
  424. }
  425. r {
  426. if {$width <= [string length $msg]} {return $msg}
  427. return [string replace [string repeat $spc $width] [expr $width - [string length $msg]] end $msg]
  428. }
  429. c {
  430. set startat [expr [expr $width/2] - [expr [string length $msg]/2]]
  431. set lastchar [expr $startat + [expr [string length $msg] -1]]
  432. if {$startat<=0} {return $msg}
  433. return [string replace [string repeat $spc $width] $startat $lastchar $msg]
  434. }
  435. lr {
  436. set msg1 $msg
  437. set msg2 [lindex $args 2]
  438. if {$width <= [expr [string length $msg1] + [string length $msg2]]} {return "$msg1$msg2"}
  439. return [string replace [string replace [string repeat $spc $width] 0 [expr [string length $msg1] -1] $msg1] [expr $width - [string length $msg2]] end $msg2]
  440. }
  441. }
  442. }
  443.  
  444. proc showstatus {idx mode} {
  445. variable actives
  446. foreach v $actives {variable $v}
  447. set fline "+[string repeat - 58]+"
  448. putidx $idx $fline
  449. putidx $idx "|[justify c "$version Status Overview" 58]|"
  450. putidx $idx $fline
  451. putidx $idx "|[justify l " Today is \002[clock format [clock scan now] -format %c]\002" 60]|"
  452. if {$mode == "all"} {
  453. if {$enabled == 1} {set st "\002enabled\002"} else {set st "\002disabled\002"}
  454. putidx $idx "|[justify l " mEL is currently $st" 60]|"
  455. if {$bhosts == 1} {set st "\002enabled\002"} else {set st "\002disabled\002"}
  456. putidx $idx "|[justify l " Host blocking is $st" 60]|"
  457. if {$macmode == 1} {set st "\002enabled\002"} else {set st "\002disabled\002"}
  458. putidx $idx "|[justify l " mAC mode is currently $st" 60]|"
  459. }
  460. putidx $idx $fline
  461. if {[array size channels] > 0} {
  462. putidx $idx "|[justify l " Currently logging the following channels:" 58]|"
  463. foreach c [lsort [array names channels]] {
  464. putidx $idx "|[justify l " \002$channels($c)\002" 60]|"
  465. }
  466. putidx $idx $fline
  467. } else {
  468. putidx $idx "|[justify l " Currently no channels are being logged" 58]|"
  469. putidx $idx $fline
  470. }
  471. if {$mode == "all"} {
  472. putidx $idx "|[justify l " Current logkeepers:" 58]|"
  473. putidx $idx "|[justify l " \002$staticlogkeeper\002 (static)" 60]|"
  474. if {[array size keepers] > 0} {
  475. foreach k [lsort [array names keepers]] {
  476. putidx $idx "|[justify l " \002$keepers($k)\002" 60]|"
  477. }
  478. }
  479. putidx $idx $fline
  480. }
  481. if {$macmode == 0} {
  482. if {[string is integer $rotate]} {
  483. putidx $idx "|[justify l " Logfiles will rotate every \002$rotate day(s)\002" 60]|"
  484. } else {
  485. putidx $idx "|[justify l " Logfiles will rotate every \002$rotate\002" 60]|"
  486. }
  487. putidx $idx $fline
  488. putidx $idx "|[justify l " Next logfile rotation will be at:" 58]|"
  489. putidx $idx "|[justify l " \002[clock format $nextswitch -format %c]\002" 60]|"
  490. putidx $idx $fline
  491. }
  492. if {$mode == "all"} {
  493. if {[file pathtype $statsdir] == "relative" } {
  494. putidx $idx "|[justify l " Storing settings in:" 58]|"
  495. putidx $idx "|[justify l " \002[file nativename [pwd]/$statsdir]\002" 60]|"
  496. } else {
  497. putidx $idx "|[justify l " Storing settings in:" 58]|"
  498. putidx $idx "|[justify l " \002[file nativename $statsdir]\002" 60]|"
  499. }
  500. if {[file pathtype $statslogdir] == "relative" } {
  501. putidx $idx "|[justify l " Storing logfiles in:" 58]|"
  502. putidx $idx "|[justify l " \002[file nativename [pwd]/$statslogdir]\002" 60]|"
  503. } else {
  504. putidx $idx "|[justify l " Storing logfiles in:" 58]|"
  505. putidx $idx "|[justify l " \002[file nativename $statslogdir]\002" 60]|"
  506. }
  507. putidx $idx $fline
  508. }
  509. }
  510.  
  511. proc logfile {action chan} {
  512. variable actives
  513. foreach v $actives {variable $v}
  514. switch -- $action {
  515. create {
  516. set starttime [clock format [clock scan now] -format "%a %b %d %T %Y"]
  517. set time_current [clock format [clock scan now] -format "%H:%M"]
  518. if {[expr $unixnames < 1]} {
  519. set curFilename [string tolower ${chan}.log]
  520. } else {
  521. set curFilename [string tolower [string range $chan 1 end].log]
  522. }
  523. if {![item find chan $chan]} {
  524. putlog "$version - Unable to create logfile for channel not in array! Channel: $chan"
  525. return 0
  526. }
  527. if {![file exists [file join $statslogdir $curFilename]]} {
  528. variable skipmac 1
  529. writelog sesdata 0 1 2 $chan "Session Start: $starttime"
  530. writelog sesdata 0 1 2 $chan "\[$time_current\] *** Now talking in $chan"
  531. variable skipmac 0
  532. putlog "$version - Created logfile for $chan"
  533. } else {
  534. putlog "$version - Can't create logfile for channel $chan - Appending to existing logfile"
  535. return 0
  536. }
  537. return 0
  538. }
  539. close {
  540. set stoptime [clock format [clock scan now] -format "%a %b %d %T %Y"]
  541. if {[expr $unixnames < 1]} {
  542. set curFilename [string tolower ${chan}.log]
  543. } else {
  544. set curFilename [string tolower [string range $chan 1 end].log]
  545. }
  546. if {![item find chan $chan]} {
  547. putlog "$version - Cannot close logfile for $chan. Channel not in array!"
  548. return 0
  549. }
  550. putlog "$version - Channel $chan has logfile [file join $statslogdir $curFilename]"
  551. if {![file exists [file join $statslogdir $curFilename]]} {
  552. putlog "$version - Cannot close logfile for $chan. File does not exist!"
  553. return 0
  554. }
  555. writelog sesdata 0 1 2 $chan "Session Close: $stoptime"
  556. putlog "$version - Closed logfile for $chan"
  557. return 0
  558. }
  559. kill {
  560. set handle $chan
  561. set dffile [file join $statsdir $deadchanfile]
  562. if {![file exists $dffile]} {
  563. putidx $handle "All redundant logfiles already removed!"
  564. return 0
  565. }
  566. putidx $handle "Deleting redundant logfiles..."
  567. set read_dead [open $dffile r]
  568. while {![eof $read_dead]} {
  569. set data [gets $read_dead]
  570. if {[eof $read_dead]} {break}
  571. if {[expr $unixnames < 1]} {
  572. set curFilename [string tolower $data].log"
  573. } else {
  574. set curFilename [string tolower [string range $data 1 end]]
  575. }
  576. foreach dead_chan [glob -nocomplain [file join $statslogdir $curFilename]*] {
  577. file delete -force $dead_chan
  578. if {[file pathtype $dead_chan] == "relative"} {putidx $handle "Deleted [pwd]/$dead_chan..."} else {putidx $handle "Deleted $dead_chan..."}
  579. }
  580. }
  581. close $read_dead
  582. file delete -force $dffile
  583. putidx $handle "\nFinished deleting redundant logfiles..."
  584. return 0
  585. }
  586. datestamp {
  587. set starttime [clock format [clock scan now] -format "%a %b %d %T %Y"]
  588. if {![array exists channels] || [array size channels] < 1} {
  589. putlog "$version - No channels defined. Use +chan first."
  590. variable enabled 0
  591. return 0
  592. }
  593. foreach c [lsort [array names channels]] {
  594. writelog sesdata 0 1 2 $channels($c) "Session Time: $starttime"
  595. }
  596. return 0
  597. }
  598. }
  599. }
  600.  
  601. proc switchlogs {} {
  602. variable actives
  603. foreach v $actives {variable $v}
  604. if {$enabled == 0} {return 0}
  605. foreach c [lsort [array names channels]] {
  606. set curChan $channels($c)
  607. logfile close $curChan
  608. if {[expr $unixnames < 1]} {
  609. set curFilename [string tolower $curChan]
  610. } else {
  611. set curFilename [string tolower [string range $curChan 1 end]]
  612. }
  613. set date [clock format [clock scan "now yesterday"] -format %m.%d.%Y]
  614. if {$dodatestamp == 1} {
  615. file rename -force [file join $statslogdir $curFilename].log [file join $statslogdir $curFilename].${date}.log
  616. } else {
  617. file rename -force [file join $statslogdir $curFilename].log [file join $statslogdir $curFilename].bak
  618. }
  619. logfile create $curChan
  620. }
  621. if {$notify == 1} {
  622. sendnote mEL $staticlogkeeper "$version - Logfiles have rotated at [clock format [clock scan now] -format %c]"
  623. }
  624. }
  625.  
  626. proc checktimer {} {
  627. variable actives
  628. foreach v $actives {variable $v}
  629. if {$nextswitch == ""} {
  630. putlog "$version - No rotation date found - Binding new timer"
  631. switch -- $rotate {
  632. week {
  633. set t [clock format [clock scan "now next $weekdays($weekly)"] -format "00 00 %d %m %Y"]
  634. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  635. }
  636. month {
  637. set t [clock format [clock scan "now next month"] -format "00 00 01 %m %Y"]
  638. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3]01T000000"]
  639. }
  640. default {
  641. set t [clock format [clock scan "now next $rotate days"] -format "00 00 %d %m %Y"]
  642. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  643. }
  644. }
  645. if {[string length [expr [lindex $t 3] -1]] == 1} {
  646. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  647. } else {
  648. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  649. }
  650. bind time - $tt ::mel::timer
  651. arraywrite
  652. return 0
  653. }
  654. if {[expr [clock scan now] > $nextswitch]} {
  655. putlog "$version - Missed a switch at [clock format $nextswitch -format %c]... switching now"
  656. switchlogs
  657. switch -- $rotate {
  658. week {
  659. set t [clock format [clock scan "now next $weekdays($weekly)"] -format "00 00 %d %m %Y"]
  660. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  661. }
  662. month {
  663. set t [clock format [clock scan "now next month"] -format "00 00 01 %m %Y"]
  664. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3]01T000000"]
  665. }
  666. default {
  667. set t [clock format [clock scan "now next $rotate days"] -format "00 00 %d %m %Y"]
  668. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  669. }
  670. }
  671. arraywrite
  672. }
  673. set t [clock format $nextswitch -format "00 00 %d %m %Y"]
  674. if {[string length [expr [lindex $t 3] -1]] == 1} {
  675. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  676. } else {
  677. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  678. }
  679. bind time - $tt ::mel::timer
  680. return 0
  681. }
  682.  
  683. proc timer {min hour day month year} {
  684. variable actives
  685. foreach v $actives {variable $v}
  686. if {$enabled == 0} {return 0}
  687. switchlogs
  688. foreach search [binds time] {
  689. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  690. unbind time - "[lindex $search 2]" ::mel::timer
  691. }
  692. }
  693. if {$enabled == 0} {
  694. putlog "$version - mEL is disabled - Not binding timed event"
  695. return 0
  696. }
  697. switch -- $rotate {
  698. week {
  699. set t [clock format [clock scan "now next $weekdays($weekly)"] -format "00 00 %d %m %Y"]
  700. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  701. }
  702. month {
  703. set t [clock format [clock scan "now next month"] -format "00 00 01 %m %Y"]
  704. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3]01T000000"]
  705. }
  706. default {
  707. set t [clock format [clock scan "now next $rotate days"] -format "00 00 %d %m %Y"]
  708. variable nextswitch [clock scan "[lindex $t 4][lindex $t 3][lindex $t 2]T000000"]
  709. }
  710. }
  711. arraywrite
  712. set t [clock format $nextswitch -format "00 00 %d %m %Y"]
  713. if {[string length [expr [lindex $t 3] -1]] == 1} {
  714. set tt [lreplace $t 3 3 0[expr [lindex $t 3] -1]]
  715. } else {
  716. set tt [lreplace $t 3 3 [expr [lindex $t 3] -1]]
  717. }
  718. bind time - $tt ::mel::timer
  719. putlog "$version - Next switch will occur at [clock format $nextswitch -format %c]"
  720. return 0
  721. }
  722.  
  723.  
  724.  
  725. proc arrayinit {} {
  726. variable actives
  727. foreach v $actives {variable $v}
  728. if {![file exists [file join $statsdir $settings]]} {
  729. variable enabled 0
  730. return 1
  731. }
  732. set readsettings [open [file join $statsdir $settings] r]
  733. set datatype 0
  734. while {![eof $readsettings]} {
  735. set data [gets $readsettings]
  736. if {[eof $readsettings]} {break}
  737. if {[string index [lindex $data 0] 0] == "\["} {
  738. regsub -all {[\[\]]} $data {} type
  739. switch -- $type {
  740. channels { set datatype 1 }
  741. keepers { set datatype 2 }
  742. rotation { set datatype 3 }
  743. switch { set datatype 4 }
  744. EOF { break }
  745. }
  746. continue
  747. }
  748. switch -- $datatype {
  749. 0 { continue }
  750. 1 { lappend allchans $data ; continue }
  751. 2 { lappend allkeeps $data ; continue }
  752. 3 { set tmac $data ; continue }
  753. 4 { set swdate $data ; continue }
  754. }
  755. }
  756. close $readsettings
  757. array unset channels
  758. if {[info exists allchans]} {
  759. set cnt 0
  760. foreach c $allchans {
  761. set channels($cnt) $c
  762. incr cnt
  763. }
  764. variable enabled 1
  765. } else {
  766. variable enabled 0
  767. return 0
  768. }
  769. array unset keepers
  770. if {[info exists allkeeps]} {
  771. set cnt 0
  772. foreach k $allkeeps {
  773. set keepers($cnt) $k
  774. incr cnt
  775. }
  776. }
  777. switch -- $tmac {
  778. mac {
  779. variable macmode 1
  780. }
  781. week {
  782. variable macmode 0
  783. variable rotate week
  784. }
  785. month {
  786. variable macmode 0
  787. variable rotate month
  788. }
  789. default {
  790. variable macmode 0
  791. variable rotate $tmac
  792. }
  793. }
  794. variable nextswitch $swdate
  795. return 0
  796. }
  797.  
  798. proc arraywrite {} {
  799. variable actives
  800. foreach v $actives {variable $v}
  801. if {[file exists [file join $statsdir $settings]]} {
  802. file rename -force [file join $statsdir $settings] [file join $statsdir $settings].old
  803. }
  804. set writesettings [open [file join $statsdir $settings] w]
  805. puts $writesettings "\[channels\]"
  806. foreach c [lsort [array names channels]] {
  807. puts $writesettings $channels($c)
  808. }
  809. puts $writesettings "\[keepers\]"
  810. foreach k [lsort [array names keepers]] {
  811. puts $writesettings $keepers($k)
  812. }
  813. puts $writesettings "\[rotation\]"
  814. puts $writesettings $rotate
  815. puts $writesettings "\[switch\]"
  816. puts $writesettings $nextswitch
  817. puts $writesettings "\[EOF\]"
  818. close $writesettings
  819. }
  820.  
  821. proc item {action type item} {
  822. variable actives
  823. foreach v $actives {variable $v}
  824. switch -- $action {
  825. find {
  826. switch -- $type {
  827. chan {
  828. if {[array exists channels]} {
  829. foreach c [array names channels] {
  830. if {[string compare -nocase $channels($c) $item] == 0} {
  831. return 1
  832. }
  833. }
  834. }
  835. return 0
  836. }
  837. keep {
  838. if {[string compare -nocase $item $staticlogkeeper] == 0} {
  839. return 1
  840. }
  841. if {[array exists keepers]} {
  842. foreach k [array names keepers] {
  843. if {[string compare -nocase $keepers($k) $item] == 0} {
  844. return 1
  845. }
  846. }
  847. }
  848. return 0
  849. }
  850. }
  851. }
  852. add {
  853. switch -- $type {
  854. chan {
  855. if {![array exists channels]} {
  856. set channels(0) $item
  857. } else {
  858. set channels([expr [array size channels] + 1]) $item
  859. }
  860. logfile create $item
  861. if {[file exists [file join $statsdir $deadchanfile]]} {
  862. set read [open [file join $statsdir $deadchanfile] r]
  863. while {![eof $read]} {
  864. set data [gets $read]
  865. if {[eof $read]} {break}
  866. if {[string compare -nocase $item $data] != 0} {
  867. set write [open [file join $statsdir $deadchanfile].tmp a]
  868. puts $write $data
  869. close $write
  870. }
  871. }
  872. close $read
  873. if {[file exists [file join $statsdir $deadchanfile].tmp]} {
  874. file rename -force [file join $statsdir $deadchanfile].tmp [file join $statsdir $deadchanfile]
  875. } else {
  876. file delete -force [file join $statsdir $deadchanfile]
  877. }
  878. }
  879. arraywrite
  880. }
  881. keep {
  882. if {![array exists keepers]} {
  883. set keepers(0) $item
  884. } else {
  885. set keepers([expr [array size keepers] + 1]) $item
  886. }
  887. arraywrite
  888. }
  889. }
  890. }
  891. del {
  892. switch -- $type {
  893. chan {
  894. foreach c [array names channels] {
  895. if {[string compare -nocase $channels($c) $item] == 0} {
  896. logfile close $item
  897. unset channels($c)
  898. set add_dead [open [file join $statsdir $deadchanfile] a]
  899. puts $add_dead $item
  900. close $add_dead
  901. if {[array names channels] == ""} {
  902. variable enabled 0
  903. foreach search [binds time] {
  904. if {[string compare [lindex $search 4] "::mel::timer"] == 0} {
  905. unbind time - "[lindex $search 2]" ::mel::timer
  906. }
  907. }
  908. putlog "$version - No channels are being logged. mEL turned off, existing time bindings removed."
  909. }
  910. arraywrite
  911. }
  912. }
  913. }
  914. keep {
  915. foreach k [array names keepers] {
  916. if {[string compare -nocase $keepers($k) $item] == 0} {
  917. unset keepers($k)
  918. arraywrite
  919. }
  920. }
  921. }
  922. }
  923. }
  924. }
  925. }
  926.  
  927. proc strip {args} {
  928. foreach char [split $args {}] {
  929. if {[string is graph $char] || $char == " "} {
  930. append printable $char
  931. }
  932. }
  933. return $printable
  934. }
  935.  
  936. proc writelog {args} {
  937. variable actives
  938. foreach v $actives {variable $v}
  939. if {$enabled == 0} {return 0}
  940. if {$bhosts == 1 && [isignore [lindex $args 2]]} {return 0}
  941. set curTime [clock format [clock scan now] -format "%H:%M"]
  942. set curChannel [lindex $args 4]
  943. if {[expr $unixnames < 1]} {
  944. set curFilename ${curChannel}.log
  945. } else {
  946. set curFilename [string range $curChannel 1 end].log
  947. }
  948. if {$skipmac == 0} {
  949. if {$macmode == 1 && ![file exists [file join $statslogdir $curFilename]]} {
  950. logfile create $curChannel
  951. }
  952. }
  953. switch -- [lindex $args 0] {
  954. chat {
  955. if {[lsearch -exact [string tolower $ignorenicks] [string tolower [lindex $args 1]]] == -1} {
  956. set addline "\[$curTime\] <[lindex $args 1]> [join [strip [lindex $args 5]]]"
  957. } else {
  958. return 0
  959. }
  960. }
  961. join {set addline "\[$curTime\] *** [lindex $args 1] ([lindex $args 2]) has joined $curChannel"}
  962. quit {set addline "\[$curTime\] *** [lindex $args 1] has quit IRC ([join [strip [lindex $args 5]]])"}
  963. topic {
  964. if {[lindex $args 1] == "*"} {return}
  965. set addline "\[$curTime\] *** [lindex $args 1] changes topic to '[join [strip [lindex $args 5]]]'"
  966. }
  967. kick {set addline "\[$curTime\] *** [lindex $args 5] was kicked by [lindex $args 1] ([lindex $args 6])"}
  968. nick {set addline "\[$curTime\] *** [lindex $args 1] is now known as [lindex $args 5]"}
  969. mode {set addline "\[$curTime\] *** [lindex $args 1] sets mode: [lindex $args 5] [lindex $args 6]"}
  970. part {set addline "\[$curTime\] *** [lindex $args 1] ([lindex $args 2]) has left [lindex $args 4] ([lindex $args 5])"}
  971. action {
  972. if {[lsearch -exact [string tolower $ignorenicks] [string tolower [lindex $args 1]]] == -1} {
  973. set addline "\[$curTime\] * [lindex $args 1] [join [strip [lindex $args 5]]]"
  974. } else {
  975. return 0
  976. }
  977. }
  978. sesdata {set addline [lindex $args 5]}
  979. }
  980. set writeout [open [file join $statslogdir $curFilename] a]
  981. puts $writeout $addline
  982. close $writeout
  983. }
  984.  
  985. proc chatter {nick host handle channel text} {writelog chat $nick $host $handle $channel $text}
  986. proc enter {nick host handle channel} {writelog join $nick $host $handle $channel}
  987. proc signoff {nick host handle channel partmsg} {writelog quit $nick $host $handle $channel $partmsg}
  988. proc ctopic {nick host handle channel topic} {writelog topic $nick $host $handle $channel $topic}
  989. proc kicked {nick host handle channel target reason} {writelog kick $nick $host $handle $channel $target $reason}
  990. proc nickchange {nick host handle channel newnick} {writelog nick $nick $host $handle $channel $newnick}
  991. proc cmode {nick host handle channel change victim} {writelog mode $nick $host $handle $channel $change $victim}
  992. proc parting {nick host handle channel partmsg} {writelog part $nick $host $handle $channel $partmsg}
  993. proc action {nick host handle destination keyword arg} {writelog action $nick $host $handle $destination $arg}
  994.  
  995. }
  996. putlog "Initializing $::mel::version...."
  997. putlog "$::mel::version - Running startup tests..."
  998. if {[::mel::starttests]} {
  999. putlog $::mel::starterror
  1000. die "Fatal error - Can not continue"
  1001. } else {
  1002. ::mel::arrayinit
  1003. ::mel::logfile datestamp all
  1004. if {$::mel::macmode == 0 && $::mel::enabled == 1} {
  1005. ::mel::checktimer
  1006. }
  1007. loadhelp mel.help
  1008. putlog "$::mel::version successfully initialized..."
  1009. }
  1010.  
  1011. ### http://mel.sourceforge.net
  1012. ### Feel free to alter this tcl to your personal flavour... just don't forget who wrote the
  1013. ### original code... Also, if you find bugs, or even fix some.. then please let me know!
Advertisement
Add Comment
Please, Sign In to add comment