BT.TopWords.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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:process {nick host hand chan chan1 cmd type next} {
  78. global botnick black
  79. set cmd_status [btcmd:status $chan $hand "topwords" 0]
  80. if {$cmd_status == "1"} {
  81. return
  82. }
  83. if {[matchattr $hand q]} { blacktools:tell $nick $host $hand $chan $chan1 gl.glsuspend none
  84. return
  85. }
  86. if {[matchattr $hand -|q $chan]} { blacktools:tell $nick $host $hand $chan $chan1 gl.suspend none
  87. return
  88. }
  89. if {![validchan $chan]} {
  90. blacktools:tell $nick $host $hand $chan $chan1 gl.novalidchan none
  91. return
  92. }
  93. if {[string equal -nocase $next "list"]} {
  94. set list [userlist "-|w" $chan]
  95. blacktools:tell $nick $host $hand $chan $chan1 topwords.18 "none"
  96. if {$list == ""} {set list "NONE"}
  97. blacktools:tell $nick $host $hand $chan $chan1 topwords.19 "$list"
  98. return
  99. }
  100. if {[string equal -nocase $cmd "del"]} {
  101. if {$next == ""} {
  102. switch $type {
  103. 0 {
  104. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "topwords"
  105. }
  106. 1 {
  107. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "topwords"
  108. }
  109. 2 {
  110. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "topwords"
  111. }
  112. }
  113. return
  114. }
  115. topwords:delexcept $next $nick $host $hand $chan $chan1
  116. return
  117. }
  118. if {[string equal -nocase $cmd "add"]} {
  119. if {$next == ""} {
  120. switch $type {
  121. 0 {
  122. blacktools:tell $nick $host $hand $chan $chan1 gl.instr "topwords"
  123. }
  124. 1 {
  125. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_nick "topwords"
  126. }
  127. 2 {
  128. blacktools:tell $nick $host $hand $chan $chan1 gl.instr_priv "topwords"
  129. }
  130. }
  131. return
  132. }
  133. topwords:addexcept $next $nick $host $hand $chan $chan1
  134. return
  135. }
  136. if {[string equal -nocase $cmd "reset"]} {
  137. set reset [topwords:reset $chan]
  138. if {$reset == "1"} {
  139. blacktools:tell $nick $host $hand $chan $chan1 topwords.8 none
  140. } else {
  141. blacktools:tell $nick $host $hand $chan $chan1 topwords.10 none
  142. }
  143. return
  144. }
  145. array set topwordslist [list]
  146. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  147. set temp "$black(tempdir)/topwords_temp.$timestamp"
  148. set counter 0
  149. set file [open $black(topwords_file) "r"]
  150. set w [read -nonewline $file]
  151. close $file
  152. set data [split $w "\n"]
  153. foreach line $data {
  154. set read_chan [lindex [split $line] 0]
  155. set enc_chan [encoding convertfrom utf-8 $read_chan]
  156. if {[string match -nocase $enc_chan $chan]} {
  157. if {$cmd == "total"} {
  158. set words_count [lindex [split $line] 4]
  159. } else {
  160. set words_count [lindex [split $line] 5]
  161. }
  162. set read_time [lindex [split $line] 31]
  163. set read_nick [lindex [split $line] 1]
  164. if {$words_count > 0} {
  165. lappend topwordslist($words_count) $read_nick:$read_time
  166. }
  167. }
  168. }
  169. set tempwrite [open $temp w]
  170. set latestuser ""
  171. set latesttime 0
  172. foreach words [lsort -integer -decreasing [array names topwordslist]] {
  173. set counter [expr $counter + 1]
  174. foreach user $topwordslist($words) {
  175. set split_user [split $user ":"]
  176. set theuser [lindex $split_user 0]
  177. set thetime [lindex $split_user 1]
  178. if {$thetime > $latesttime} {
  179. set latesttime $thetime
  180. set latestuser $theuser
  181. } else {
  182. set latesttime $latesttime
  183. set latestuser $theuser
  184. }
  185. }
  186. puts $tempwrite "$latestuser $words $counter"
  187. }
  188. close $tempwrite
  189. set file [open $temp "r"]
  190. set w [read -nonewline $file]
  191. close $file
  192. set data [split $w "\n"]
  193. file delete $temp
  194. if {$w == ""} {
  195. if {$cmd == "total"} {
  196. blacktools:tell $nick $host $hand $chan $chan1 topwords.7 none
  197. } else {
  198. blacktools:tell $nick $host $hand $chan $chan1 topwords.14 none
  199. }
  200. return
  201. }
  202. if {$cmd == "total"} {
  203. module:getinfo $nick $host $hand $chan $chan1 $type $data "topwords:total" "0" $next
  204. } else {
  205. module:getinfo $nick $host $hand $chan $chan1 $type $data "topwords" "0" $next
  206. }
  207. }
  208. proc topwords:module {nick host hand chan arg} {
  209. global black
  210. set arg [split $arg]
  211. set mask [return_mask [return_host_num "topwords" $chan] $host $nick]
  212. set handle [nick2hand $nick]
  213. if {[setting:get $chan topwords]} {
  214. if {[isbotnick $nick]} {
  215. return
  216. }
  217. if {[validuser $handle]} {
  218. if {[matchattr $handle -|w $chan]} {
  219. return
  220. }
  221. }
  222. if {[setting:get $chan accessonly]} {
  223. if {![validuser $hand]} {
  224. return
  225. }
  226. if {![matchattr $hand $black(exceptflags) $chan]} {
  227. return
  228. }
  229. }
  230. set time [unixtime]
  231. 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}
  232. set found_entry 0
  233. set h_counter 0
  234. set h [clock format $time -format %H]
  235. set file [open $black(topwords_file) "r"]
  236. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  237. set temp "$black(tempdir)/topwords_temp.$timestamp"
  238. set tempwrite [open $temp w]
  239. while {[gets $file line] != -1} {
  240. set read_chan [lindex [split $line] 0]
  241. set read_host [lindex [split $line] 2]
  242. set real_read_host [string map [list \[ {\[} \] {\]} \? {\?} \\ {\\}] $read_host]
  243. set enc_chan [encoding convertfrom utf-8 $read_chan]
  244. if {[string equal -nocase $enc_chan $chan] && [string equal -nocase $real_read_host $mask]} {
  245. set found_entry 1
  246. set nr_letter [lindex [split $line] 3]
  247. set nr_words [lindex [split $line] 4]
  248. set words_today [lindex [split $line] 5]
  249. set nr_lines [lindex [split $line] 6]
  250. set hours [lrange [split $line] 7 30]
  251. foreach hour $hours {
  252. set split_hour [split $hour ":"]
  253. set the_hour [lindex $split_hour 0]
  254. set counter [lindex $split_hour 1]
  255. if {$the_hour == $h} {
  256. set h_counter $counter
  257. }
  258. }
  259. continue
  260. } else {
  261. puts $tempwrite $line
  262. }
  263. }
  264. set chars [string length [string map {" " ""} $arg]]
  265. set words [llength $arg]
  266. set lines 1
  267. if {$found_entry == "1"} {
  268. set list_hours2 [topwords:return_hours $h_counter $h $hours]
  269. puts $tempwrite "$chan $nick $mask [expr $nr_letter + $chars] [expr $nr_words + $words] [expr $words_today + $words] [expr $nr_lines + $lines] $list_hours2 $time"
  270. } else {
  271. set list_hours2 [topwords:return_hours 0 $h $list_hours]
  272. puts $tempwrite "$chan $nick $mask $chars $words $words $lines $list_hours2 $time"
  273. }
  274. close $tempwrite
  275. close $file
  276. file rename -force $temp $black(topwords_file)
  277. }
  278. }
  279. proc topwords:module:me {nick host hand chan keyword arg} {
  280. global black
  281. topwords:module $nick $host $hand $chan $arg
  282. }
  283. proc topwords:reset {chan} {
  284. global black
  285. set found_entry 0
  286. set file [open $black(topwords_file) "r"]
  287. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  288. set temp "$black(tempdir)/topwords_temp.$timestamp"
  289. set tempwrite [open $temp w]
  290. while {[gets $file line] != -1} {
  291. set read_chan [lindex [split $line] 0]
  292. set enc_chan [encoding convertfrom utf-8 $read_chan]
  293. if {[string equal -nocase $chan $enc_chan]} {
  294. set found_entry 1
  295. continue
  296. } else {
  297. puts $tempwrite $line
  298. }
  299. }
  300. close $tempwrite
  301. close $file
  302. file rename -force $temp $black(topwords_file)
  303. return $found_entry
  304. }
  305. proc topwords:day:reset {minute hour day month year} {
  306. global black
  307. set file [open $black(topwords_file) "r"]
  308. set timestamp [clock format [clock seconds] -format {%Y%m%d%H%M%S}]
  309. set temp "$black(tempdir)/topwords_temp.$timestamp"
  310. set tempwrite [open $temp w]
  311. while {[gets $file line] != -1} {
  312. set chan [lindex [split $line] 0]
  313. set nick [lindex [split $line] 1]
  314. set host [lindex [split $line] 2]
  315. set time [lindex [split $line] 31]
  316. set nr_letter [lindex [split $line] 3]
  317. set nr_words [lindex [split $line] 4]
  318. set words_today [lindex [split $line] 5]
  319. set nr_lines [lindex [split $line] 6]
  320. set hours [lrange [split $line] 7 30]
  321. puts $tempwrite "$chan $nick $host $nr_letter $nr_words 0 $nr_lines $hours $time"
  322. }
  323. close $tempwrite
  324. close $file
  325. file rename -force $temp $black(topwords_file)
  326. }
  327. proc topwords:return_hours {num hour hours} {
  328. global black
  329. set thehours $hours
  330. if {[lsearch -glob $thehours "$hour:*"] > -1} {
  331. set position [lsearch -glob $thehours "$hour:*"]
  332. set thehours [lreplace $thehours $position $position]
  333. set thehours [linsert $thehours $position "$hour:[expr $num + 1]"]
  334. }
  335. return $thehours
  336. }
  337. ##############
  338. #########################################################################
  339. ## END ##
  340. #########################################################################