putils.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. # putils
  2. namespace eval ::putils {
  3. ## some internal variables for messaging and stuff :) not your business after all.
  4. set local_ver "0.4truncateSR"
  5. package require base64
  6. package require md5
  7. # if putils-variable (thus, putils!) is already loaded, then .. well .. load again, but, this time, quietly. ;-)
  8. if {![info exists putils]} {
  9. set putils(version) $local_ver
  10. putlog "::putils:: paul's utils for eggdrop bots in tcl - $putils(version) - loading..."
  11. }
  12. # set again .. perhaps we changed version while in development ;) - even while putils was already loaded.
  13. set putils(version) $local_ver
  14. }
  15. ### this "library" supplies the following procedures:
  16. # gets botnick, truncates it and lowercases it. If input-value of a botnick was
  17. # already correct, then it won't change anything. ;)
  18. proc ::putils::proper_botnick {botnick} {
  19. set temp [string tolower $botnick]
  20. # abfrage ob zu lang, dann fixen
  21. # putlog "::putils::proper_botnick: $botnick lowercase: $temp"
  22. if {[string length $botnick] > 9} {
  23. set temp [string range $temp 0 8 ]
  24. # putlog "::putils::proper_botnick: botnickname $botnick too long: capping to: $temp"
  25. }
  26. return $temp
  27. }
  28. proc ::putils::sleep {N} {
  29. after [expr {int($N * 1000)}]
  30. }
  31. proc ::putils::proper_channelname {channelname} {
  32. # channel names ARE NOT and SHOULD NOT BE CASE SENSITIVE!
  33. set temp [string tolower $channelname]
  34. set temp [::putils::umlauts $temp]
  35. # putlog "::putils:: $channelname lowercase: $temp"
  36. return $temp
  37. }
  38. # puts out a normal channel message.
  39. proc ::putils::put_local_msg {chan text} {
  40. putserv "PRIVMSG $chan :$text"
  41. putlog "::putils:: + normal message: ${chan} => '$text'"
  42. }
  43. # puts a NOTICE to a specified nickname with a specified message.
  44. proc ::putils::put_nick {nick msg} {
  45. putserv "NOTICE $nick :$msg"
  46. }
  47. proc ::putils::kill_spaces {text} {
  48. regsub -all "\\s+" $text "" text
  49. return $text
  50. }
  51. proc ::putils::put_bot {target_botnetnick fromdata} {
  52. # real maxlen for putbot is 400. but we have overhead.
  53. set maxmsglen 300
  54. set calcmsglen [expr $maxmsglen + 1]
  55. #debug
  56. #set fromdata "12345678901234567890"
  57. if {[islinked $target_botnetnick] == 1} {
  58. # prepare data ...! encode. send multiple data, if needed.
  59. # encode it, base64, all in one line! '-wrapchar ""'
  60. set data [::base64::encode -wrapchar "" $fromdata]
  61. #::putils::filelog "scripts/cims/cims.log" "put_bot to $target_botnetnick: full base64 $data"
  62. # make a hash of $data
  63. set md5 [::md5::md5 -hex $data]
  64. putlog "::putils::put_bot: md5 of full base64 message: $md5"
  65. set data_len [string length $data]
  66. putlog "::putils::put_bot: data_len of full base64 message: $data_len"
  67. # into how many pieces does this message to be splitted?
  68. # default 1
  69. set count_parts 1
  70. if {$data_len > $calcmsglen} {
  71. set count_parts [expr $data_len / $calcmsglen]
  72. set remainder [expr $data_len % $calcmsglen]
  73. set real [expr ${data_len}.00 / ${calcmsglen}.00]
  74. putlog "::putils::put_bot: remainder: $remainder real: $real"
  75. if {[expr $data_len % $calcmsglen] > 0} {
  76. set count_parts [expr $count_parts + 1]
  77. }
  78. putlog "::putils::put_bot: length: $data_len messagedata will be divided into $count_parts parts"
  79. }
  80. #MTIzNDU2Nzg5MDEyMzQ1Njc4OTA=
  81. #MTIzNDU2
  82. #Nzg5MDEy
  83. #MzQ1Njc4
  84. #OTA=
  85. # cut data into pieces and send!
  86. for {set x 0} {$x < $count_parts} {incr x} {
  87. #putlog "part number is $x"
  88. # get the part
  89. set part [string range $data 0 $maxmsglen]
  90. putlog "::putils::put_bot: part $x is $part"
  91. # kill the part out of the original string
  92. set data [string replace $data 0 $maxmsglen ""]
  93. #::putils::filelog "scripts/cims/cims.log" "put_bot to $target_botnetnick: $part"
  94. putbot $target_botnetnick [concat "rec_putils " $md5 " " [expr $x + 1] " " $count_parts " " $part]
  95. }
  96. putlog "::putils:: put_bot: + delivered a message part to ${target_botnetnick}."
  97. } else {
  98. putlog "::putils:: put_bot: + a message couldn't be delivered. Bot ${target_botnetnick} is not linked"
  99. }
  100. }
  101. proc ::putils::rec_bot {sender_botnetnick cmd rec_data} {
  102. # cmd is "rec_putils"
  103. #putlog "inside rec_bot. we want to decode rec_data: $rec_data"
  104. set rec_data [split $rec_data]
  105. set message_hash [join [lindex $rec_data 0]]
  106. set current_part [join [lindex $rec_data 1]]
  107. #putlog "current_part: $current_part"
  108. set count_parts [join [lindex $rec_data 2]]
  109. #putlog "count_parts: $count_parts"
  110. set base64data [join [lindex $rec_data 3]]
  111. variable themessage
  112. variable kill_coordinates
  113. variable kill_timer_id
  114. # push to intermediary variable .. everything is there. it's our message stack.
  115. lappend themessage "$message_hash" "${current_part}" "$count_parts" "${base64data}"
  116. # kill old one, start a new one.
  117. if {[info exists kill_timer_id]} {
  118. set timerlist [utimers]
  119. foreach {timerinfo} $timerlist {
  120. set timer_id [join [lindex $timerinfo 2]]
  121. if {$timer_id == $kill_timer_id} {
  122. killutimer $kill_timer_id
  123. }
  124. }
  125. unset kill_timer_id
  126. unset kill_coordinates
  127. }
  128. set kill_timer_id [utimer 2 "::putils::timer_cleanup"]
  129. proc ::putils::timer_cleanup {} {
  130. variable themessage
  131. variable kill_coordinates
  132. set themessage ""
  133. set kill_coordinates ""
  134. }
  135. # .tcl namespace eval ::putils { unset themessage }
  136. set copymessage $themessage
  137. set my_count 0
  138. set before_part ""
  139. # build a null list with X-count-elements
  140. set base64 ""
  141. for {set x 1} {$x <= $count_parts} {incr x} {
  142. lappend base64 $x
  143. }
  144. set iteration 0
  145. set finish 0
  146. foreach {hash current_part count base64data} $copymessage {
  147. incr iteration
  148. if {$hash == $message_hash} {
  149. if {$current_part != $before_part} {
  150. incr my_count
  151. set before_part $current_part
  152. # insert the element into the right place of the X-count-element list
  153. lset base64 [expr $current_part - 1] $base64data
  154. #putlog "building base64: $base64"
  155. # save the coordinates for later deletion.
  156. #1*4 - 4 3 2 1
  157. set kill_from [expr ($iteration * 4) - 4]
  158. set kill_to [expr ($iteration * 4) - 1]
  159. lappend kill_coordinates $kill_from $kill_to
  160. #putlog "current_part: $current_part kill_from: $kill_from kill_to: $kill_to"
  161. if {$my_count == $count} {
  162. regsub -all " " ${base64} "" base64
  163. #putlog "finished base64: $base64"
  164. set finish 1
  165. # now delete this 4 from the $themessage
  166. foreach {kill_from kill_to} $kill_coordinates {
  167. #putlog "kill_from: $kill_from kill_to: $kill_to"
  168. for {set y $kill_from} {$y <= $kill_to} {incr y} {
  169. lset themessage $y "X"
  170. #putlog "themessage: $themessage"
  171. }
  172. }
  173. set kill_coordinates ""
  174. }
  175. }
  176. }
  177. }
  178. if {$finish == 1} {
  179. #::putils::filelog "scripts/cims/cims.log" "rec_bot from $sender_botnetnick base64: $base64"
  180. # decode base64data ...
  181. set original_data [::base64::decode $base64]
  182. #putlog "original_data: $original_data"
  183. set md5 [::md5::md5 -hex $base64]
  184. if {$md5 == $message_hash} {
  185. putlog "::putils::rec_bot: received decoded base64 message with correct md5"
  186. # start the targetted procedure with the received data. if available.
  187. set bindlist [split [join [binds bot]]]
  188. foreach {type o cmd cnt procedure} $bindlist {
  189. #putlog "::putils::put_bot: cmd: $cmd procedure: $procedure"
  190. # FIXME: we currently don't check for permissions...
  191. if {$cmd == [join [lindex $original_data 0]]} {
  192. putlog "::putils::rec_bot: execute procedure: $procedure $sender_botnetnick [join [lindex $original_data 0]] [join [lrange $original_data 1 end]]"
  193. #execute procedure
  194. $procedure $sender_botnetnick [join [lindex $original_data 0]] [join [lrange $original_data 1 end]]
  195. }
  196. }
  197. }
  198. }
  199. }
  200. bind bot - rec_putils ::putils::rec_bot
  201. # cleans the input text from all irc control codes... and even has
  202. # some spam-protection.
  203. proc ::putils::clean_txt {text} {
  204. #putlog "filter_A: ${text}"
  205. #regsub -all "\\" $text "\\\\" text
  206. # fixes many whitespace between words down to one space between words
  207. regsub -all "\\s+" $text " " text
  208. # filtering out all colorcodes (works well)
  209. regsub -all "\003\[0-9\]\{1,2\},\[0-9\]\{1,2\}" $text "" text
  210. regsub -all "\003\[0-9\]\{1,2\}" $text "" text
  211. regsub -all "\003" $text "" text
  212. # filtering out BOLD text
  213. regsub -all "\002" $text "" text
  214. # underline gets filtered too. (since +c on quakenet would suppress it ...)
  215. regsub -all "\037" $text "" text
  216. # replacing like !!!!!!!!!!!!! with !!!!! (5 letters)
  217. # s/(.?)\1{4,}/\1\1\1\1\1/g;
  218. # - max 5 same chars in a row
  219. regsub -all -nocase -expanded {(.)\1\1\1\1+} $text {\1\1\1\1\1} text
  220. #putlog "test: $text"
  221. set text [string trim $text]
  222. # putlog "filter_B: ${text}"
  223. return $text
  224. }
  225. # replace found umlauts with umlauts.. funny, eh? We have to do this to channel names with
  226. # umlauts, because of some encoding problem inside eggdrop.
  227. # Afterwards, you can check for channelnames - without errors.
  228. proc ::putils::umlauts {text} {
  229. # A REAL STRANGE BUG WORKAROUND WITH UMLAUTS
  230. regsub -all "Ä" ${text} "Ä" text
  231. regsub -all "Ü" ${text} "Ü" text
  232. regsub -all "Ö" ${text} "Ö" text
  233. regsub -all "ä" ${text} "ä" text
  234. regsub -all "ü" ${text} "ü" text
  235. regsub -all "ö" ${text} "ö" text
  236. return ${text}
  237. }
  238. #
  239. # Proc to generate a string of (given) characters
  240. # Range defaults to "ABCDEF...wxyz'
  241. #
  242. proc ::putils::randomRangeString {length {chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"}} {
  243. set range [expr {[string length $chars]-1}]
  244. set txt ""
  245. for {set i 0} {$i < $length} {incr i} {
  246. set pos [expr {int(rand()*$range)}]
  247. append txt [string range $chars $pos $pos]
  248. }
  249. return $txt
  250. }
  251. # safe "bot-knows-the-channel-and-is-in-there"-function, returns boolean
  252. proc ::putils::botonchannel {chan} {
  253. if {[validchan $chan] == 1 && [botonchan $chan] == 1} {
  254. return 1
  255. } else {
  256. return 0
  257. }
  258. }
  259. proc ::putils::write_f_array {file thearraylist} {
  260. array set myinternalarray $thearraylist
  261. set array_string [array get myinternalarray]
  262. set fh [open "$file" "w"]
  263. puts $fh $array_string
  264. close $fh
  265. }
  266. proc ::putils::read_f_array {file} {
  267. set fh [open "$file" "r"]
  268. array set myinternalarray [gets $fh]
  269. close $fh
  270. return [array get myinternalarray]
  271. }
  272. proc ::putils::filelog {file txt} {
  273. # write all to it
  274. set timestamp [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S"]
  275. #open handle
  276. set fh [open "$file" "a"]
  277. # put to handle
  278. puts $fh "$timestamp $txt"
  279. # close handle
  280. close $fh
  281. }
  282. return 1