BT.TopWords.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #########################################################################
  2. ## BlackTools - The Ultimate Channel Control Script ##
  3. ## One TCL. One smart Eggdrop ##
  4. #########################################################################
  5. ########################### TOPWORDS TCL ############################
  6. #########################################################################
  7. ## ##
  8. ## BlackTools : http://blacktools.tclscripts.net ##
  9. ## Bugs report : http://www.tclscripts.net/ ##
  10. ## Online Help : irc://irc.undernet.org/tcl-help ##
  11. ## #TCL-HELP / UnderNet ##
  12. ## You can ask in english or romanian ##
  13. ## ##
  14. #########################################################################
  15. proc topwords:delexcept {user nick host hand chan chan1} {
  16. global black
  17. set user [join $user]
  18. set show_user [split $user]
  19. if {[onchan $user $chan]} {
  20. set getuser [nick2hand $user]
  21. if {$getuser != "*"} {
  22. set user $getuser
  23. }
  24. }
  25. if {![validuser $user]} {
  26. blacktools:tell $nick $host $hand $chan $chan1 gl.nomem none
  27. return
  28. }
  29. if {![matchattr $user "-|w" $chan]} {
  30. blacktools:tell $nick $host $hand $chan $chan1 topwords.20 "$show_user"
  31. return
  32. }
  33. chattr $user -|-w $chan
  34. blacktools:tell $nick $host $hand $chan $chan1 topwords.21 "$show_user"
  35. }
  36. proc topwords:addexcept {user nick host hand chan chan1} {
  37. global black
  38. set user [join $user]
  39. set show_user [split $user]
  40. if {![onchan $user $chan]} {
  41. blacktools:tell $nick $host $hand $chan $chan1 gl.usernotonchan $show_user
  42. return
  43. }
  44. set mask [return_mask [return_host_num "topwords" $chan] [getchanhost $user $chan] $user]
  45. if {[validuser $user]} {
  46. set user $user
  47. } else {
  48. if {[nick2hand $user] == "*"} {
  49. set user $user
  50. } else {
  51. set user [nick2hand $user]
  52. }
  53. }
  54. foreach usr [userlist] {
  55. set hst [getuser $usr hosts]
  56. foreach hhost $hst {
  57. if {[string match -nocase $hhost $mask] && (![string match -nocase $usr $user])} {
  58. blacktools:tell $nick $host $hand $chan $chan1 add.5 [split $usr]
  59. set user $usr
  60. set show_user [split $usr]
  61. }
  62. }
  63. }
  64. if {[validuser $user]} {
  65. if {[matchattr $user -|w $chan]} {
  66. blacktools:tell $nick $host $hand $chan $chan1 topwords.17 $show_user
  67. return
  68. }
  69. chattr $user -|+w $chan
  70. blacktools:tell $nick $host $hand $chan $chan1 topwords.15 $show_user
  71. } else {
  72. adduser $user $mask
  73. chattr $user -|+w $chan
  74. blacktools:tell $nick $host $hand $chan $chan1 topwords.16 "$show_user $mask"
  75. }
  76. }
  77. proc topwords {nick host hand chan arg} {
  78. global black lastbind
  79. set next [lindex [split $arg] 0]
  80. set cmd ""
  81. set type 0
  82. set chan1 "$chan"
  83. set return [blacktools:mychar $lastbind $hand]
  84. if {$return == "0"} {
  85. return
  86. }
  87. if {[string equal -nocase $next "reset"] && [matchattr $hand nmo|M $chan]} {
  88. set cmd "reset"
  89. }
  90. if {[string equal -nocase $next "total"]} {
  91. set next [lindex [split $arg] 1]
  92. set cmd "total"
  93. }
  94. if {[string equal -nocase $next "add"] && [matchattr $hand nmo|M $next]} {
  95. set next [lindex [split $arg] 1]
  96. set cmd "add"
  97. }
  98. if {[string equal -nocase $next "del"] && [matchattr $hand nmo|M $next]} {
  99. set next [lindex [split $arg] 1]
  100. set cmd "del"
  101. }
  102. if {[regexp {^[&#]} $next] && [matchattr $hand nmo|MAOV $next]} {
  103. set chan $next
  104. set next [lindex [split $arg] 1]
  105. if {[string equal -nocase $next "reset"] && [matchattr $hand nmo|M $next]} {
  106. set cmd "reset"
  107. }
  108. if {[string equal -nocase $next "total"]} {
  109. set next [lindex [split $arg] 2]
  110. set cmd "total"
  111. }
  112. if {[string equal -nocase $next "add"] && [matchattr $hand nmo|M $next]} {
  113. set next [lindex [split $arg] 2]
  114. set cmd "add"
  115. }
  116. if {[string equal -nocase $next "del"] && [matchattr $hand nmo|M $next]} {
  117. set next [lindex [split $arg] 2]
  118. set cmd "del"
  119. }
  120. }
  121. topwords:process $nick $host $hand $chan $chan1 $cmd $type $next
  122. }
  123. proc topwords:process {nick host hand chan chan1 cmd type next} {
  124. global botnick black
  125. set cmd_status [btcmd:status $chan $hand "topwords" 0]
  126. if {$cmd_status == "1"} {
  127. return
  128. }
  129. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  130. return
  131. }
  132. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  133. return
  134. }
  135. if {![validchan $chan]} {
  136. blacktools:tell $nick $host $hand $chan $chan1 gl.novalidchan none
  137. return
  138. }
  139. if {[string equal -nocase $next "list"]} {
  140. set list [userlist "-|w" $chan]
  141. blacktools:tell $nick $host $hand $chan $chan1 topwords.18 "none"
  142. if {$list == ""} {set list "NONE"}
  143. blacktools:tell $nick $host $hand $chan $chan1 topwords.19 "$list"
  144. return
  145. }
  146. if {[string equal -nocase $cmd "del"]} {
  147. if {$next == ""} {
  148. switch $type {
  149. 0 {
  150. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "topwords"
  151. }
  152. 1 {
  153. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "topwords"
  154. }
  155. 2 {
  156. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "topwords"
  157. }
  158. }
  159. return
  160. }
  161. topwords:delexcept $next $nick $host $hand $chan $chan1
  162. return
  163. }
  164. if {[string equal -nocase $cmd "add"]} {
  165. if {$next == ""} {
  166. switch $type {
  167. 0 {
  168. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "topwords"
  169. }
  170. 1 {
  171. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "topwords"
  172. }
  173. 2 {
  174. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "topwords"
  175. }
  176. }
  177. return
  178. }
  179. topwords:addexcept $next $nick $host $hand $chan $chan1
  180. return
  181. }
  182. if {[string equal -nocase $cmd "reset"]} {
  183. set reset [topwords:reset $chan]
  184. if {$reset == "1"} {
  185. blacktools:tell $nick $host $hand $chan $chan1 topwords.8 none
  186. } else {
  187. blacktools:tell $nick $host $hand $chan $chan1 topwords.10 none
  188. }
  189. return
  190. }
  191. array set topwordslist [list]
  192. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  193. set temp "$black(tempdir)/topwords_temp.$timestamp"
  194. set counter 0
  195. set file [open $black(topwords_file) "r"]
  196. set w [read -nonewline $file]
  197. close $file
  198. set data [split $w "\n"]
  199. foreach line $data {
  200. set read_chan [lindex [split $line] 0]
  201. set enc_chan [encoding convertfrom utf-8 $read_chan]
  202. if {[string match -nocase $enc_chan $chan]} {
  203. if {$cmd == "total"} {
  204. set words_count [lindex [split $line] 4]
  205. } else {
  206. set words_count [lindex [split $line] 5]
  207. }
  208. set read_time [lindex [split $line] 31]
  209. set read_nick [lindex [split $line] 1]
  210. if {$words_count > 0} {
  211. lappend topwordslist($words_count) $read_nick:$read_time
  212. }
  213. }
  214. }
  215. set tempwrite [open $temp w]
  216. set latestuser ""
  217. set latesttime 0
  218. foreach words [lsort -integer -decreasing [array names topwordslist]] {
  219. set counter [expr $counter + 1]
  220. foreach user $topwordslist($words) {
  221. set split_user [split $user ":"]
  222. set theuser [lindex $split_user 0]
  223. set thetime [lindex $split_user 1]
  224. if {$thetime > $latesttime} {
  225. set latesttime $thetime
  226. set latestuser $theuser
  227. } else {
  228. set latesttime $latesttime
  229. set latestuser $theuser
  230. }
  231. }
  232. puts $tempwrite "$latestuser $words $counter"
  233. }
  234. close $tempwrite
  235. set file [open $temp "r"]
  236. set w [read -nonewline $file]
  237. close $file
  238. set data [split $w "\n"]
  239. file delete $temp
  240. if {$w == ""} {
  241. if {$cmd == "total"} {
  242. blacktools:tell $nick $host $hand $chan $chan1 topwords.7 none
  243. } else {
  244. blacktools:tell $nick $host $hand $chan $chan1 topwords.14 none
  245. }
  246. return
  247. }
  248. if {$cmd == "total"} {
  249. module:getinfo $nick $host $hand $chan $chan1 $type $data "topwords:total" "0" $next
  250. } else {
  251. module:getinfo $nick $host $hand $chan $chan1 $type $data "topwords" "0" $next
  252. }
  253. }
  254. proc topwords:module {nick host hand chan arg} {
  255. global black
  256. set arg [split $arg]
  257. set mask [return_mask [return_host_num "topwords" $chan] $host $nick]
  258. set handle [nick2hand $nick]
  259. if {[setting:get $chan topwords]} {
  260. if {[isbotnick $nick]} {
  261. return
  262. }
  263. if {[validuser $handle]} {
  264. if {[matchattr $handle -|w $chan]} {
  265. return
  266. }
  267. }
  268. if {[setting:get $chan accessonly]} {
  269. if {![validuser $hand]} {
  270. return
  271. }
  272. if {![matchattr $hand $black(exceptflags) $chan]} {
  273. return
  274. }
  275. }
  276. set time [unixtime]
  277. set list_hours {00:0 01:0 02:0 03:0 04:0 05:0 06:0 07:0 08:0 09:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0}
  278. set found_entry 0
  279. set h_counter 0
  280. set h [clock format $time -format %H]
  281. set file [open $black(topwords_file) "r"]
  282. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  283. set temp "$black(tempdir)/topwords_temp.$timestamp"
  284. set tempwrite [open $temp w]
  285. while {[gets $file line] != -1} {
  286. set read_chan [lindex [split $line] 0]
  287. set read_host [lindex [split $line] 2]
  288. set real_read_host [string map [list \[ {\[} \] {\]} \? {\?} \\ {\\}] $read_host]
  289. set enc_chan [encoding convertfrom utf-8 $read_chan]
  290. if {[string equal -nocase $enc_chan $chan] && [string equal -nocase $real_read_host $mask]} {
  291. set found_entry 1
  292. set nr_letter [lindex [split $line] 3]
  293. set nr_words [lindex [split $line] 4]
  294. set words_today [lindex [split $line] 5]
  295. set nr_lines [lindex [split $line] 6]
  296. set hours [lrange [split $line] 7 30]
  297. foreach hour $hours {
  298. set split_hour [split $hour ":"]
  299. set the_hour [lindex $split_hour 0]
  300. set counter [lindex $split_hour 1]
  301. if {$the_hour == $h} {
  302. set h_counter $counter
  303. }
  304. }
  305. continue
  306. } else {
  307. puts $tempwrite $line
  308. }
  309. }
  310. set chars [string length [string map {" " ""} $arg]]
  311. set words [llength $arg]
  312. set lines 1
  313. if {$found_entry == "1"} {
  314. set list_hours2 [topwords:return_hours $h_counter $h $hours]
  315. puts $tempwrite "$chan $nick $mask [expr $nr_letter + $chars] [expr $nr_words + $words] [expr $words_today + $words] [expr $nr_lines + $lines] $list_hours2 $time"
  316. } else {
  317. set list_hours2 [topwords:return_hours 0 $h $list_hours]
  318. puts $tempwrite "$chan $nick $mask $chars $words $words $lines $list_hours2 $time"
  319. }
  320. close $tempwrite
  321. close $file
  322. file rename -force $temp $black(topwords_file)
  323. }
  324. }
  325. proc topwords:module:me {nick host hand chan keyword arg} {
  326. global black
  327. topwords:module $nick $host $hand $chan $arg
  328. }
  329. proc topwords:reset {chan} {
  330. global black
  331. set found_entry 0
  332. set file [open $black(topwords_file) "r"]
  333. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  334. set temp "$black(tempdir)/topwords_temp.$timestamp"
  335. set tempwrite [open $temp w]
  336. while {[gets $file line] != -1} {
  337. set read_chan [lindex [split $line] 0]
  338. set enc_chan [encoding convertfrom utf-8 $read_chan]
  339. if {[string equal -nocase $chan $enc_chan]} {
  340. set found_entry 1
  341. continue
  342. } else {
  343. puts $tempwrite $line
  344. }
  345. }
  346. close $tempwrite
  347. close $file
  348. file rename -force $temp $black(topwords_file)
  349. return $found_entry
  350. }
  351. proc topwords:day:reset {minute hour day month year} {
  352. global black
  353. set file [open $black(topwords_file) "r"]
  354. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  355. set temp "$black(tempdir)/topwords_temp.$timestamp"
  356. set tempwrite [open $temp w]
  357. while {[gets $file line] != -1} {
  358. set chan [lindex [split $line] 0]
  359. set nick [lindex [split $line] 1]
  360. set host [lindex [split $line] 2]
  361. set time [lindex [split $line] 31]
  362. set nr_letter [lindex [split $line] 3]
  363. set nr_words [lindex [split $line] 4]
  364. set words_today [lindex [split $line] 5]
  365. set nr_lines [lindex [split $line] 6]
  366. set hours [lrange [split $line] 7 30]
  367. puts $tempwrite "$chan $nick $host $nr_letter $nr_words 0 $nr_lines $hours $time"
  368. }
  369. close $tempwrite
  370. close $file
  371. file rename -force $temp $black(topwords_file)
  372. }
  373. proc topwords:return_hours {num hour hours} {
  374. global black
  375. set thehours $hours
  376. if {[lsearch -glob $thehours "$hour:*"] > -1} {
  377. set position [lsearch -glob $thehours "$hour:*"]
  378. set thehours [lreplace $thehours $position $position]
  379. set thehours [linsert $thehours $position "$hour:[expr $num + 1]"]
  380. }
  381. return $thehours
  382. }
  383. ##############
  384. #########################################################################
  385. ## END ##
  386. #########################################################################