putils.tcl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 330
  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 $fromdata]
  58. putlog "base64: $data"
  59. # make a hash of $data
  60. set md5 [::md5::md5 -hex $data]
  61. putlog "md5: $md5"
  62. set data_len [string length $data]
  63. putlog "data_len: $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 "remainder: $remainder real: $real"
  72. if {[expr $data_len % $calcmsglen] > 0} {
  73. set count_parts [expr $count_parts + 1]
  74. }
  75. putlog "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. puts "x is $x"
  85. # get the part
  86. set part [string range $data 0 $maxmsglen]
  87. # kill the part out of the original string
  88. set data [string replace $data 0 $maxmsglen ""]
  89. putbot $target_botnetnick [concat "rec_putils " $md5 " " [expr $x + 1] " " $count_parts " " $part]
  90. }
  91. putlog "::putils:: put_bot: + a message delivered to ${target_botnetnick}."
  92. } else {
  93. putlog "::putils:: put_bot: + a message couldn't be delivered. Bot ${target_botnetnick} is not linked"
  94. }
  95. }
  96. proc ::putils::rec_bot {sender_botnetnick cmd rec_data} {
  97. # cmd is "rec_putils"
  98. #putlog "inside rec_bot. we want to decode rec_data: $rec_data"
  99. set rec_data [split $rec_data]
  100. set message_hash [join [lindex $rec_data 0]]
  101. set current_part [join [lindex $rec_data 1]]
  102. #putlog "current_part: $current_part"
  103. set count_parts [join [lindex $rec_data 2]]
  104. #putlog "count_parts: $count_parts"
  105. set base64data [join [lindex $rec_data 3]]
  106. variable themessage
  107. variable kill_coordinates
  108. variable kill_timer_id
  109. # push to intermediary variable .. everything is there. it's our message stack.
  110. lappend themessage "$message_hash" "${current_part}" "$count_parts" "${base64data}"
  111. # kill old one, start a new one.
  112. if {[info exists kill_timer_id]} {
  113. set timerlist [utimers]
  114. foreach {timerinfo} $timerlist {
  115. set timer_id [join [lindex $timerinfo 2]]
  116. if {$timer_id == $kill_timer_id} {
  117. killutimer $kill_timer_id
  118. }
  119. }
  120. unset kill_timer_id
  121. unset kill_coordinates
  122. }
  123. set kill_timer_id [utimer 2 "::putils::timer_cleanup"]
  124. proc ::putils::timer_cleanup {} {
  125. set themessage ""
  126. set kill_coordinates ""
  127. }
  128. # .tcl namespace eval ::putils { unset themessage }
  129. set copymessage $themessage
  130. set my_count 0
  131. set before_part ""
  132. # build a null list with X-count-elements
  133. set base64 ""
  134. for {set x 1} {$x <= $count_parts} {incr x} {
  135. lappend base64 $x
  136. }
  137. set iteration 0
  138. set finish 0
  139. foreach {hash current_part count base64data} $copymessage {
  140. incr iteration
  141. if {$hash == $message_hash} {
  142. if {$current_part != $before_part} {
  143. incr my_count
  144. set before_part $current_part
  145. # insert the element into the right place of the X-count-element list
  146. lset base64 [expr $current_part - 1] $base64data
  147. # save the coordinates for later deletion.
  148. #1*4 - 4 3 2 1
  149. set kill_from [expr ($iteration * 4) - 4]
  150. set kill_to [expr ($iteration * 4) - 1]
  151. lappend kill_coordinates $kill_from $kill_to
  152. #putlog "current_part: $current_part kill_from: $kill_from kill_to: $kill_to"
  153. if {$my_count == $count} {
  154. regsub -all " " ${base64} "" base64
  155. #putlog "base64: $base64"
  156. set finish 1
  157. # now delete this 4 from the $themessage
  158. foreach {kill_from kill_to} $kill_coordinates {
  159. putlog "kill_from: $kill_from kill_to: $kill_to"
  160. for {set y $kill_from} {$y <= $kill_to} {incr y} {
  161. lset themessage $y "X"
  162. #putlog "themessage: $themessage"
  163. }
  164. }
  165. set kill_coordinates ""
  166. }
  167. }
  168. }
  169. }
  170. if {$finish == 1} {
  171. putlog "base64: $base64"
  172. # decode base64data ...
  173. set original_data [::base64::decode $base64]
  174. #putlog "original_data1: $original_data"
  175. #set original_data [split $original_data]
  176. putlog "original_data2: $original_data"
  177. set md5 [::md5::md5 -hex $base64]
  178. if {$md5 == $message_hash} {
  179. putlog "alright md5"
  180. # start the targetted procedure with the received data. if available.
  181. set bindlist [split [join [binds bot]]]
  182. foreach {type o cmd cnt procedure} $bindlist {
  183. putlog "cmd: $cmd procedure: $procedure"
  184. # FIXME: we currently don't check for permissions...
  185. if {$cmd == [join [lindex $original_data 0]]} {
  186. putlog "execute procedure $procedure $sender_botnetnick [join [lindex $original_data 0]] [join [lrange $original_data 1 end]]"
  187. #execute procedure
  188. $procedure $sender_botnetnick [join [lindex $original_data 0]] [join [lrange $original_data 1 end]]
  189. }
  190. }
  191. }
  192. }
  193. }
  194. bind bot - rec_putils ::putils::rec_bot
  195. # cleans the input text from all irc control codes... and even has
  196. # some spam-protection.
  197. proc ::putils::clean_txt {text} {
  198. #putlog "filter_A: ${text}"
  199. #regsub -all "\\" $text "\\\\" text
  200. # fixes many whitespace between words down to one space between words
  201. regsub -all "\\s+" $text " " text
  202. # filtering out all colorcodes (works well)
  203. regsub -all "\003\[0-9\]\{1,2\},\[0-9\]\{1,2\}" $text "" text
  204. regsub -all "\003\[0-9\]\{1,2\}" $text "" text
  205. regsub -all "\003" $text "" text
  206. # filtering out BOLD text
  207. regsub -all "\002" $text "" text
  208. # underline gets filtered too. (since +c on quakenet would suppress it ...)
  209. regsub -all "\037" $text "" text
  210. # replacing like !!!!!!!!!!!!! with !!!!! (5 letters)
  211. # s/(.?)\1{4,}/\1\1\1\1\1/g;
  212. # - max 5 same chars in a row
  213. regsub -all -nocase -expanded {(.)\1\1\1\1+} $text {\1\1\1\1\1} text
  214. #putlog "test: $text"
  215. set text [string trim $text]
  216. # putlog "filter_B: ${text}"
  217. return $text
  218. }
  219. # replace found umlauts with umlauts.. funny, eh? We have to do this to channel names with
  220. # umlauts, because of some encoding problem inside eggdrop.
  221. # Afterwards, you can check for channelnames - without errors.
  222. proc ::putils::umlauts {text} {
  223. # A REAL STRANGE BUG WORKAROUND WITH UMLAUTS
  224. regsub -all "Ä" ${text} "Ä" text
  225. regsub -all "Ü" ${text} "Ü" text
  226. regsub -all "Ö" ${text} "Ö" text
  227. regsub -all "ä" ${text} "ä" text
  228. regsub -all "ü" ${text} "ü" text
  229. regsub -all "ö" ${text} "ö" text
  230. return ${text}
  231. }
  232. # safe "bot-knows-the-channel-and-is-in-there"-function, returns boolean
  233. proc ::putils::botonchannel {chan} {
  234. if {[validchan $chan] == 1 && [botonchan $chan] == 1} {
  235. return 1
  236. } else {
  237. return 0
  238. }
  239. }
  240. proc ::putils::write_f_array {file thearraylist} {
  241. array set myinternalarray $thearraylist
  242. set array_string [array get myinternalarray]
  243. set fh [open "$file" "w"]
  244. puts $fh $array_string
  245. close $fh
  246. }
  247. proc ::putils::read_f_array {file} {
  248. set fh [open "$file" "r"]
  249. array set myinternalarray [gets $fh]
  250. close $fh
  251. return [array get myinternalarray]
  252. }
  253. return 1