putils.tcl 10 KB

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