cims_interconnect.tcl 13 KB

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