putils.tcl 10 KB

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