cims_interconnect.tcl 13 KB

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