putils.tcl 9.9 KB

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