cims_interconnect.tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # CIMS: Community IRC Messaging Service
  2. # CIMS formerly known as MNET (Message Network)
  3. # Multiple Network Interconnection MASTER Script
  4. # Copyright (C) 2004 Paul-Dieter Klumpp
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. namespace eval ::cims::interconnect {
  20. variable layout
  21. variable sshcommand {ssh}
  22. variable host {stomp@b4r.org}
  23. variable mnet_interconnect_version "ic!0.1"
  24. }
  25. proc ::cims::interconnect::connect {} {
  26. variable sshcommand
  27. variable host
  28. variable pipe
  29. set command $sshcommand
  30. lappend command $host
  31. putlog "command is $command"
  32. if {[catch {
  33. if {![info exists pipe] || $pipe == ""} {
  34. set pipe [open "|$command" r+]
  35. fconfigure $pipe -translation binary -blocking 0 -buffering line
  36. } else {
  37. putlog "already connected here: $pipe"
  38. }
  39. } error]} {
  40. set pipe ""
  41. putlog "error $error"
  42. #conect again in a time
  43. } else {
  44. # read from console input
  45. ::cims::interconnect::readloop
  46. # check if connection is alive.. if not, reset it.
  47. ::cims::interconnect::pingloop
  48. }
  49. }
  50. proc ::cims::interconnect::connect_from_bind {nick mask hand chan text} {
  51. ::cims::interconnect::connect
  52. }
  53. proc ::cims::interconnect::disconnect {} {
  54. variable pipe
  55. ::cims::interconnect::send "PART"
  56. close $pipe
  57. set pipe ""
  58. }
  59. proc ::cims::interconnect::disconnect_from_bind {nick mask hand chan text} {
  60. ::cims::interconnect::disconnect
  61. }
  62. proc ::cims::interconnect::reconnect {} {
  63. ::cims::interconnect::disconnect
  64. ::cims::interconnect::connect
  65. }
  66. proc ::cims::interconnect::reconnect_from_bind {nick mask hand chan text} {
  67. ::cims::interconnect::reconnect
  68. }
  69. proc ::cims::interconnect::send {msg} {
  70. variable pipe
  71. if {$msg != "" && $pipe != ""} {
  72. putlog "writing $msg to pipe '$pipe'"
  73. if {[catch {
  74. puts $pipe "$msg"
  75. } error]} {
  76. close $pipe
  77. set pipe ""
  78. # well, it seems, our connection is broken, let's reconnect in 2 seconds.
  79. utimer 2 "::cims::interconnect::connect"
  80. }
  81. }
  82. }
  83. proc ::cims::interconnect::send_from_bind {nick mask hand chan text} {
  84. ::cims::interconnect::send "$text"
  85. }
  86. proc ::cims::timeout_reply_from_ic_for_netbot {netbot network freqname source} {
  87. set bcid $::cims::interconnect::sources($freqname,$source)
  88. if {[info exists ::cims::interconnect::receive_count($bcid)]} {
  89. set output "$::cims::interconnect::receive_count($bcid)"
  90. }
  91. # Give him a reply.
  92. ::cims::put_bot $netbot "mnet_interconnect_answer $network $freqname $source $output"
  93. }
  94. proc ::cims::timeout_reply_from_local_for_ic {bcid network freqname source} {
  95. variable mnet_reached_users
  96. variable mnet_reached_userlist
  97. variable mnet_reached_chans
  98. # clean $name and $source
  99. set freqname [::putils::kill_spaces $freqname]
  100. set source [::putils::kill_spaces $source]
  101. # easify variables
  102. set userlist $mnet_reached_userlist($freqname,$source)
  103. set user_cnt [llength $userlist]
  104. set mnet_reached_users($freqname,$source) $user_cnt
  105. set chan_cnt $mnet_reached_chans($freqname,$source)
  106. # inzwischen drfte auch die antwort gekommen sein.. also jetzt mnet_reached_* auswerten nach timeout..
  107. # make stats channel-dependent
  108. #putlog "::cims:: * userlist is finally: $userlist "
  109. putlog "::cims:: * After ALL: Count_Users: $user_cnt Count_Channels: $mnet_reached_chans($freqname,$source)"
  110. # Give me some reply.
  111. ::cims::interconnect::send "BC_RE $bcid Users=$user_cnt,Channels=$chan_cnt"
  112. }
  113. # this proc is a new proc for cims.
  114. # the message came from interconnect and injects into the netbots and own channels
  115. proc ::cims::message_from_interconnect {network freqname source nick text} {
  116. variable mnet_reached_users
  117. variable mnet_reached_userlist
  118. variable mnet_reached_chans
  119. #putlog "freqname: $freqname"
  120. #putlog "source: $source"
  121. #putlog "nick: $nick"
  122. #putlog "text: $text"
  123. # reset stats .. because he is just freshly sending
  124. set mnet_reached_users($freqname,$source) "0"
  125. set mnet_reached_userlist($freqname,$source) ""
  126. set mnet_reached_chans($freqname,$source) "0"
  127. ::cims::history_queue $network,$freqname $nick $source $text
  128. # send them first, we await a reply!
  129. # single sends TO ALL REMOTE BOTS with their remote CHANNELS...
  130. # let's see which mode for sending is used...
  131. ::cims::message_to_netbots $network $freqname $source $nick $text
  132. # SEND TO ALL OWN CHANNELS...
  133. # send to all own channels, except to the channel the message originating from:
  134. ::cims::message_to_own_channels $network $freqname $source $nick $text
  135. }
  136. proc ::cims::interconnect::add_up {bcid item count} {
  137. variable receive_count
  138. # schon drin?
  139. if {![info exists receive_count($bcid)]} {
  140. set receive_count($bcid) ""
  141. }
  142. set position [lsearch $receive_count($bcid) $item]
  143. if {$position > -1} {
  144. #putlog "item: $item is drin, weil posi $position"
  145. set the_old_count [lindex $receive_count($bcid) [expr $position + 1]]
  146. # the_old_count gets increased by $count
  147. incr the_old_count $count
  148. # we set it ..
  149. lset receive_count($bcid) [expr $position + 1] $the_old_count
  150. } else {
  151. #putlog "item: $item nicht drin, weil posi $position"
  152. lappend receive_count($bcid) "$item" "$count"
  153. }
  154. #putlog "hum: $receive_count($bcid)"
  155. }
  156. # called right before the output...
  157. proc ::cims::timeout_reply_from_local_for_netbots_plugin {freqname source} {
  158. upvar output output
  159. set bcid $::cims::interconnect::sources($freqname,$source)
  160. if {[info exists ::cims::interconnect::receive_count($bcid)]} {
  161. set output "$output $::cims::interconnect::receive_count($bcid)"
  162. }
  163. }
  164. # this is the stuff we read on the console.
  165. proc ::cims::interconnect::work {inputline} {
  166. variable broadcasts
  167. variable sources
  168. variable receive_count
  169. #putlog "work that out: $inputline"
  170. regexp -lineanchor {^.*\d: (.*)} $inputline matched sub
  171. putlog "subline: $sub"
  172. if {[regexp -all {^PONG} $sub] > 0} {
  173. #putlog "interconnect pong received"
  174. } elseif {[regexp -lineanchor -all {^(BC_ID) (.+) for: (.+),(.+),(.+)$} $sub whole command bcid network freqname source] > 0} {
  175. # this tells us, we sent a REQ_BC before.
  176. set broadcasts($bcid) [list "$network" "$freqname" "$source"]
  177. set ::cims::interconnect::sources($freqname,$source) "$bcid"
  178. #putlog "bcid vars saved: $broadcasts($bcid) "
  179. # reset receive counters.
  180. set receive_user_count($bcid) 0
  181. set receive_item_count($bcid) 0
  182. } elseif {[regexp -lineanchor -all {^(BC_RE) (.+) (.+)=(\d+),(.+)=(\d+)$} $sub whole command bcid user_string user_count item_string item_count] > 0} {
  183. #BC_RE f4k3h4sh 5,3
  184. # central does some routing over central to make sure, the BC_RE message is only sent to me
  185. # ok cool .. i hope we sent a message with that $bcid before. And
  186. #.. we check on $bcid - find out the correct cims-vars (chan, freq, etc)
  187. # and add user_count, item_count to them.
  188. # now, let's validate that bcid .. we check if that bcid exists.. so the originating message is from me.
  189. if {[info exists broadcasts($bcid)] == 1} {
  190. set that $broadcasts($bcid)
  191. set network [join [lindex $that 0]]
  192. set freqname [join [lindex $that 1]]
  193. set source [join [lindex $that 2]]
  194. # count up
  195. ::cims::interconnect::add_up "$bcid" "$user_string" "$user_count"
  196. ::cims::interconnect::add_up "$bcid" "$item_string" "$item_count"
  197. # ready the variable, so it can be read from the plugin.
  198. set ::cims::interconnect::receive_count($bcid) "$::cims::interconnect::receive_count($bcid)"
  199. }
  200. } elseif {[regexp -lineanchor -all {^(BC) (.+) (.+),(.+),(.+),'(.+)','(.+)'$} $sub whole command bcid network freqname source nick text] > 0} {
  201. # THIS IS FRESH INPUT. ESCAPE IT.
  202. set freqname [split $freqname]
  203. set source [split $source]
  204. set nick [split $nick]
  205. set text [split $text]
  206. putlog "freqname: $freqname"
  207. putlog "source: $source"
  208. putlog "nick: $nick"
  209. putlog "text: $text"
  210. # parse here above.. or just use:
  211. #set network "QDEV"
  212. #set freqname "-dev-"
  213. #set source "http://qwnu"
  214. #set nick "testnickname"
  215. #set text "testmessage"
  216. # here, we read a message from central and put it to all local channels and netbots.
  217. ::cims::message_from_interconnect $network $freqname $source $nick $text
  218. # in the meantime, we even get all replies and counts (by ::cims::).
  219. # Now, we should get that count and send it to the central as a reply. Hitting the original sender.
  220. utimer 4 "::cims::timeout_reply_from_local_for_ic $bcid $network $freqname $source"
  221. } elseif {[regexp -all {^WHO.*} $sub whole] > 0} {
  222. # ignore this one - it's fully handled by central.
  223. } elseif {[regexp -all -nocase {^C (.*): (.*)} $sub whole sub1 sub2] > 0} {
  224. # received, with command "C "
  225. if {"$sub2" != ""} {
  226. ::putils::put_local_msg "#aztest" "C $sub1: $sub2"
  227. }
  228. } else {
  229. # received, but it has no command
  230. #::putils::put_local_msg "#aztest" "not handled: $sub"
  231. }
  232. }
  233. proc ::cims::interconnect::read {} {
  234. variable pipe
  235. #putlog "into read"
  236. if {$pipe != ""} {
  237. #while { ![eof $pipe] } {
  238. gets $pipe inputline
  239. if {$inputline != ""} {
  240. ::cims::interconnect::work "$inputline"
  241. }
  242. #}
  243. }
  244. #putlog "ending of read"
  245. }
  246. proc ::cims::interconnect::readloop {} {
  247. variable pipe
  248. # check timers.. if old is running, don't start a new one
  249. set timerlist [utimers]
  250. set matched 0
  251. foreach {timerinfo} $timerlist {
  252. set timer_proc [join [lindex $timerinfo 1]]
  253. set timer_id [join [lindex $timerinfo 2]]
  254. if {[string match -nocase "::cims::interconnect::readloop" $timer_proc] == 1} {
  255. set matched 1
  256. }
  257. }
  258. if {$matched == 0 && $pipe != ""} {
  259. utimer 1 "::cims::interconnect::readloop"
  260. }
  261. # read a line
  262. ::cims::interconnect::read
  263. }
  264. proc ::cims::interconnect::ping {} {
  265. #putlog "into read"
  266. ::cims::interconnect::send "PING"
  267. }
  268. proc ::cims::interconnect::pingloop {} {
  269. variable pipe
  270. # check timers.. if old is running, don't start a new one
  271. set timerlist [utimers]
  272. set matched 0
  273. foreach {timerinfo} $timerlist {
  274. set timer_proc [join [lindex $timerinfo 1]]
  275. set timer_id [join [lindex $timerinfo 2]]
  276. if {[string match -nocase "::cims::interconnect::pingloop" $timer_proc] == 1} {
  277. set matched 1
  278. }
  279. }
  280. if {$matched == 0 && $pipe != ""} {
  281. utimer 120 "::cims::interconnect::pingloop"
  282. }
  283. # start a ping
  284. ::cims::interconnect::ping
  285. }
  286. # this proc is a plugin proc for cims. it only gets executed if it exists.
  287. # the message came from our bot.
  288. proc ::cims::message_from_local_plugin {network freqname source nick text} {
  289. # send the message into central
  290. putlog "wo bin ich da? $source"
  291. ::cims::interconnect::send "REQ_BC $network,$freqname,$source,'$nick','$text'"
  292. # save BC_ID for this combination of parameters - is done in the above "::work"
  293. # receive answers from the other networks
  294. # prepare stats so that the local stats-counter takes it. the local stats are getting put out
  295. # at the end of ::cims::messaging_public_from_bind by ::cims::reply_timeout
  296. }
  297. # this proc is a plugin proc for cims. it only gets executed if it exists.
  298. # we received a message from a netbot
  299. proc ::cims::message_from_netbot_to_plugin {netbot network freqname source nick text} {
  300. # send the message into central
  301. ::cims::interconnect::send "REQ_BC $network,$freqname,$source,'$nick','$text'"
  302. # receive answers from the other networks
  303. # in the meantime, we get all the BC_RE replies and counts.
  304. # Now, we should get that count and send it to originating netbot.
  305. utimer 5 "::cims::timeout_reply_from_ic_for_netbot $netbot $network $freqname $source"
  306. # send an own answer to the originating netbot
  307. }
  308. proc ::cims::interconnect::bindings {} {
  309. bind pub - !mnet_connect ::cims::interconnect::connect_from_bind
  310. bind pub - !mnet_disconnect ::cims::interconnect::disconnect_from_bind
  311. bind pub - !mnet_reconnect ::cims::interconnect::reconnect_from_bind
  312. bind pub - !mnet_input ::cims::interconnect::send_from_bind
  313. }
  314. proc ::cims::interconnect::main {} {
  315. variable mnet_interconnect_version
  316. variable pipe
  317. putlog "mnet! = mnet interconnection MASTER script loaded: $mnet_interconnect_version"
  318. ::cims::interconnect::bindings
  319. ::cims::interconnect::connect
  320. }
  321. namespace eval ::cims::interconnect {
  322. # timer weil $botnet-nick nicht sofort von eggdrop gesetzt wird
  323. utimer 3 "::cims::interconnect::main"
  324. }