cims.tcl 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. # CIMS: Community IRC Messaging Service
  2. # CIMS formerly known as MNET (Message Network)
  3. # Copyright (C) 2004 Paul-Dieter Klumpp
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. namespace eval ::cims {
  19. set configs_file "scripts/cims/cims_conf.tcl"
  20. #your notes:
  21. # - Requirements:
  22. # -> eggdrop bot version >= 1.6.17
  23. # -> using TCL version >= 8.3
  24. # -> TCLLib version >= 1.6
  25. # -> TCLXML version >= 2.6
  26. #my notes:
  27. # ?- aliasing of channels!
  28. # ?- alternative configs! backupconf..
  29. # ?- xml:dom!
  30. # ?- better http get implementation!
  31. # ?- korrekte Statistiken der User usw?
  32. # ?- wordbanning?
  33. # Now, hands away pls :) except you know what you are doing.
  34. ######
  35. #package prefer stable
  36. # we need a nice tcl version
  37. package require Tcl 8.3
  38. # it's an eggdrop script, right? :)
  39. package require eggdrop 1.6
  40. # we need the tclxml package.. at least 2.6 i think
  41. package require xml
  42. # tclxml needs uri of tcllib.. at least 1.1 i think
  43. package require uri
  44. # include my/our little eggdrop utilities and helper library. ;-) Putils!
  45. if {[file isfile "scripts/cims/putils.tcl"] == 1} {
  46. source "scripts/cims/putils.tcl"
  47. }
  48. ############ XML CONFIGURATION PARSING FOLLOWS!! WTF! :) #########
  49. variable mnet_networks
  50. variable mnet_bots
  51. variable mnet_channels
  52. variable mnet_freqs
  53. variable mnet_bans
  54. variable mnet_colors
  55. variable mnet_colors_optional
  56. variable mnet_reached_users
  57. variable mnet_reached_userlist
  58. variable mnet_reached_chans
  59. variable mnet_http_config
  60. variable mnet_local_config
  61. variable mnet_histories
  62. variable mnet_netconfig_timer
  63. variable currentNetworkDesc
  64. variable currentNetworkSig
  65. variable startNetworkInstance
  66. variable currentBot
  67. variable currentOwner
  68. variable startBotInstance
  69. variable currentFreqName
  70. variable currentFreqPrefix
  71. variable currentFreqHelpmsg
  72. variable currentFreqDesc
  73. variable startFrequencyInstance
  74. global botnet-nick
  75. # list
  76. set mnet_local_config ""
  77. unset mnet_local_config
  78. # list
  79. set mnet_http_config ""
  80. unset mnet_http_config
  81. set mnet_cfg_reload_interval 0
  82. # list
  83. #set mnet_freqs_onoff "lala lala"
  84. #unset mnet_freqs_onoff
  85. }
  86. proc ::cims::clean_netconfig {} {
  87. variable mnet_bots
  88. variable mnet_channels
  89. variable mnet_freqs
  90. variable mnet_bans
  91. variable mnet_colors
  92. variable mnet_networks
  93. # secure deletion of all config-arrays..
  94. if {[array exists mnet_bots]} {
  95. array unset mnet_bots
  96. }
  97. if {[array exists mnet_channels]} {
  98. array unset mnet_channels
  99. }
  100. if {[array exists mnet_freqs]} {
  101. array unset mnet_freqs
  102. }
  103. if {[array exists mnet_bans]} {
  104. array unset mnet_bans
  105. }
  106. #if {[array exists mnet_freqs_onoff]} {
  107. # array unset mnet_freqs_onoff
  108. #}
  109. if {[array exists mnet_colors]} {
  110. array unset mnet_colors
  111. }
  112. if {[array exists mnet_colors_optional]} {
  113. array unset mnet_colors_optional
  114. }
  115. # list
  116. set mnet_networks ""
  117. unset mnet_networks
  118. # list
  119. set mnet_freqs "lala lala"
  120. unset mnet_freqs
  121. set mnet_bans "lala lala"
  122. unset mnet_bans
  123. # list FIXME (this kills the history upon every .mnet_reload ... :( .. not good)
  124. if {[array exists mnet_histories]} {
  125. array unset mnet_histories
  126. }
  127. set mnet_histories ""
  128. unset mnet_histories
  129. }
  130. proc ::cims::initvars {} {
  131. variable currentNetworkDesc
  132. variable currentNetworkSig
  133. variable startNetworkInstance
  134. variable currentBot
  135. variable currentOwner
  136. variable startBotInstance
  137. variable startFrequencyInstance
  138. variable mnet_networks
  139. variable mnet_freqs
  140. variable mnet_bans
  141. variable mnet_networks_by_prefix
  142. global botnet-nick
  143. set currentNetwork ""
  144. set currentSig ""
  145. set startNetworkInstance ""
  146. set currentBot ""
  147. set currentOwner ""
  148. set startBotInstance ""
  149. set startFrequencyInstance ""
  150. }
  151. ::cims::initvars
  152. proc ::cims::xmlElementStart {tagname attlist args} {
  153. variable mnet_networks
  154. variable mnet_bots
  155. variable mnet_channels
  156. variable mnet_freqs
  157. variable mnet_bans
  158. variable mnet_colors
  159. variable mnet_colors_optional
  160. variable mnet_freqs_onoff
  161. variable currentNetworkDesc
  162. variable currentNetworkSig
  163. variable startNetworkInstance
  164. variable currentBot
  165. variable currentOwner
  166. variable startBotInstance
  167. variable currentFreqName
  168. variable currentFreqPrefix
  169. variable currentFreqHelpmsg
  170. variable currentFreqDesc
  171. variable startFrequencyInstance
  172. global botnet-nick
  173. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  174. # putlog "::cims:: attr: '$attlist' arg: '$args'"
  175. # getting NETWORK NAME
  176. if {"$tagname" == "mnet"} {
  177. set currentNetworkDesc ""
  178. set currentNetworkSig ""
  179. foreach {key value} $attlist {
  180. if {"$key" == "sig"} {
  181. set currentNetworkSig $value
  182. }
  183. if {"$key" == "desc"} {
  184. set currentNetworkDesc $value
  185. }
  186. if {"$currentNetworkSig" != ""} {
  187. if {"$currentNetworkDesc" != ""} {
  188. putlog "yo: $currentNetworkSig $currentNetworkDesc"
  189. lappend mnet_networks "$currentNetworkSig" "$currentNetworkDesc"
  190. set startNetworkInstance 1
  191. }
  192. }
  193. }
  194. }
  195. # getting botinfos...
  196. if {"$startNetworkInstance" == "1"} {
  197. if {"$tagname" == "bot"} {
  198. set currentBot ""
  199. set currentOwner ""
  200. foreach {key value} $attlist {
  201. if {"$key" == "nick"} {
  202. set currentBot [::cims::proper_botnick $value]
  203. }
  204. if {"$key" == "owner"} {
  205. set currentOwner $value
  206. }
  207. }
  208. if {"$currentBot" != ""} {
  209. if {"$currentOwner" != ""} {
  210. # currentbot auf 9 zeichen runter.. lowercase
  211. # botnick ebenso
  212. lappend mnet_bots($currentNetworkSig) "$currentBot"
  213. putlog "::cims:: cfg: reached botcfg for: $currentBot - and my name is: $proper_botnick"
  214. if {"$currentBot" == "$proper_botnick"} {
  215. putlog "::cims:: Oh yes, I, ${botnet-nick}, am listed for the network $currentNetworkSig by using '$currentBot'!"
  216. set startBotInstance 1
  217. }
  218. }
  219. }
  220. }
  221. # end of $tagname == "bot"
  222. if {"$startBotInstance" == "1"} {
  223. if {"$tagname" == "ch"} {
  224. foreach {key value} $attlist {
  225. if {"$key" == "name"} {
  226. putlog "test: ${value}"
  227. lappend mnet_channels($currentNetworkSig,$currentBot) [::cims::proper_channelname ${value}]
  228. putlog "::cims:: cfg: my channels for $currentNetworkSig $mnet_channels($currentNetworkSig,$currentBot)"
  229. }
  230. }
  231. }
  232. # end of $tagname == "ch"
  233. }
  234. # end of a bot...
  235. if {"$tagname" == "frequency"} {
  236. set currentFreqName ""
  237. set currentFreqPrefix ""
  238. set currentFreqHelpmsg ""
  239. set currentFreqDesc ""
  240. set currentDelay "120"
  241. foreach {key value} $attlist {
  242. if {"$key" == "name"} {
  243. set currentFreqName $value
  244. }
  245. if {"$key" == "prefix"} {
  246. set currentFreqPrefix $value
  247. }
  248. if {"$key" == "helpmsg"} {
  249. set currentFreqHelpmsg $value
  250. }
  251. if {"$key" == "desc"} {
  252. set currentFreqDesc $value
  253. }
  254. if {"$key" == "delay"} {
  255. set currentDelay $value
  256. }
  257. }
  258. if {"$currentFreqName" != ""} {
  259. if {"$currentFreqPrefix" != ""} {
  260. if {"$currentFreqHelpmsg" != ""} {
  261. if {"$currentFreqDesc" != ""} {
  262. putlog "::cims:: i, ${botnet-nick}, serve the frequency: $currentFreqName ($currentFreqPrefix) of $currentNetworkSig!"
  263. set startFrequencyInstance 1
  264. lappend mnet_freqs($currentNetworkSig) "$currentNetworkSig" "$currentFreqName" "$currentFreqPrefix" "$currentFreqHelpmsg" "$currentFreqDesc" "$currentDelay"
  265. }
  266. }
  267. }
  268. }
  269. # end of: alle attribs vorhanden fr eine valide frequenz
  270. }
  271. # end of $tagname == "frequency"
  272. # in element Frequency, there are bans as children.
  273. if {"$startFrequencyInstance" == "1"} {
  274. if {"$tagname" == "ban"} {
  275. set currentBanHostmask ""
  276. set currentBanReason ""
  277. foreach {key value} $attlist {
  278. if {"$key" == "hostmask"} {
  279. set currentBanHostmask $value
  280. }
  281. if {"$key" == "reason"} {
  282. set currentBanReason $value
  283. }
  284. }
  285. if {"$currentBanHostmask" != ""} {
  286. if {"$currentBanReason" != ""} {
  287. putlog "::cims:: adding ban to frequency: $currentNetworkSig, $currentFreqPrefix, $currentBanHostmask, reason: $currentBanReason"
  288. lappend mnet_bans($currentNetworkSig,$currentFreqPrefix) "$currentBanHostmask" "$currentBanReason"
  289. }
  290. }
  291. }
  292. }
  293. if {"$tagname" == "color"} {
  294. set currentColor ""
  295. set currentColorOptional ""
  296. foreach {key value} $attlist {
  297. if {"$key" == "id"} {
  298. set currentColor $value
  299. putlog "::cims:: gone into colors! $value"
  300. }
  301. if {"$key" == "optional"} {
  302. set currentColorOptional $value
  303. putlog "::cims:: color optional? $value"
  304. }
  305. }
  306. if {"$currentColor" != ""} {
  307. set mnet_colors($currentNetworkSig) $currentColor
  308. putlog "::cims:: the color-id for $currentNetworkSig is: $currentColor"
  309. } else {
  310. # make NO color if no color-id or tag has been given:
  311. set mnet_colors($currentNetworkSig) none
  312. }
  313. if {"$currentColorOptional" != ""} {
  314. set mnet_colors_optional($currentNetworkSig) $currentColorOptional
  315. putlog "::cims:: the color is optional: $currentColorOptional"
  316. } else {
  317. set mnet_colors_optional($currentNetworkSig) 0
  318. putlog "::cims:: the color is NOT optional and will be forced."
  319. }
  320. }
  321. }
  322. }
  323. proc ::cims::xmlElementEnd {tagname} {
  324. variable mnet_networks
  325. variable mnet_bots
  326. variable mnet_channels
  327. variable currentNetworkDesc
  328. variable currentNetworkSig
  329. variable startNetworkInstance
  330. variable startBotInstance
  331. variable startFrequencyInstance
  332. variable currentBot
  333. variable currentOwner
  334. if {"$tagname" == "mnet"} {
  335. set startNetworkInstance 0
  336. set currentNetworkDesc ""
  337. set currentNetworkSig ""
  338. }
  339. if {"$tagname" == "bot"} {
  340. set startBotInstance 0
  341. }
  342. if {"$tagname" == "frequency"} {
  343. set startFrequencyInstance 0
  344. }
  345. }
  346. proc ::cims::xmlCdata {data} {
  347. # putlog "::cims:: data found: $data"
  348. return 1
  349. }
  350. proc ::cims::xmlHandleError {errorcode errormsg} {
  351. putlog "::cims:: some error occured: errcode $errorcode: $errormsg"
  352. }
  353. proc ::cims::xmlDefaultCmd {data} {
  354. putlog "::cims:: i don't know this element: $data"
  355. return 1
  356. }
  357. ##################################################################
  358. namespace eval ::cims {
  359. ## some internal variables for messaging and stuff :) not your business after all.
  360. set mnet(version) "cims!1.014unique-users"
  361. set mnet_max_history 8
  362. set mnet(reached) "0"
  363. set mnet(reached_channels) "0"
  364. set mnet_reached_users(dummy) "0"
  365. set mnet_reached_chans(dummy) "0"
  366. ##
  367. }
  368. proc ::cims::get_data {n} {
  369. if {[string range $n 0 0] == "\{"} {
  370. set n "\\$n"
  371. }
  372. return $n
  373. }
  374. proc ::cims::proper_botnick {botnick} {
  375. set temp [string tolower $botnick]
  376. # abfrage ob zu lang, dann fixen
  377. # putlog "::cims:: $botnick lowercase: $temp"
  378. if {[string length $botnick] > 9} {
  379. set temp [string range $temp 0 8 ]
  380. # putlog "::cims:: botnickname $botnick too long: capping to: $temp"
  381. }
  382. return $temp
  383. }
  384. proc ::cims::proper_channelname {channelname} {
  385. # channel names ARE NOT and SHOULD NOT BE CASE SENSITIVE!
  386. set temp [string tolower $channelname]
  387. set temp [::cims::umlauts $temp]
  388. # putlog "::cims:: $channelname lowercase: $temp"
  389. return $temp
  390. }
  391. proc ::cims::put_local_netmessage {network name chan rec_nickname rec_channel rec_text} {
  392. variable mnet_colors
  393. variable mnet_colors_optional
  394. variable mnet_colors_onoff
  395. # validating incoming vars..
  396. set chan [::cims::proper_channelname $chan]
  397. if {$mnet_colors($network) == "none"} {
  398. # if no color id was found...
  399. set color_string ""
  400. } else {
  401. # if there was a color id...
  402. if {$mnet_colors_optional($network) == 1} {
  403. # check for the channel if activated..
  404. if {$mnet_colors_onoff($name,$chan) == 1} {
  405. set color_string "\003$mnet_colors($network)"
  406. set bold_string "\002"
  407. set bold_string_end "\002"
  408. } else {
  409. set color_string ""
  410. set bold_string ""
  411. set bold_string_end ""
  412. }
  413. } else {
  414. # if not optional then force colors.. whatever :)
  415. set color_string "\003$mnet_colors($network)"
  416. set bold_string "\002"
  417. set bold_string_end "\002"
  418. }
  419. }
  420. putserv "PRIVMSG $chan :${bold_string}${name}${bold_string_end} ${color_string}${rec_nickname} - ${rec_channel} : ${rec_text}"
  421. putlog "::cims:: + local message from $rec_nickname @ $rec_channel to $chan : $rec_text"
  422. }
  423. proc get_httpfile {host file port} {
  424. global botnet-nick
  425. putlog "get_httpfile::debug host: $host file: $file port: $port"
  426. if {[catch {set sock [socket $host $port] } sockerror]} {
  427. putlog "get_httpfile:: sockerror: $sockerror"
  428. return 0
  429. } else {
  430. puts $sock "GET $file HTTP/1.0"
  431. puts $sock "User-Agent: Mozilla/5.0 (compatible; ${botnet-nick}; mnet.tcl)"
  432. puts $sock "Host: $host"
  433. puts $sock "Connection: close"
  434. puts $sock ""
  435. flush $sock
  436. set header_done 0
  437. set data ""
  438. # get some strings out of the http stream.. better write them in a var.. normally.
  439. while {[eof $sock] != 1} {
  440. set bl [gets $sock]
  441. # putlog "test: $bl"
  442. if {[string match "" $bl]} {
  443. set header_done 1
  444. }
  445. if {$header_done == 1} {
  446. set data "$data\n$bl"
  447. }
  448. }
  449. close $sock
  450. return $data
  451. }
  452. }
  453. proc get_localfile {file} {
  454. set data ""
  455. if {[file isfile "$file"] == 1} {
  456. set fh [open "$file" "r"]
  457. while {![eof $fh]} {
  458. set line [gets $fh]
  459. append data $line
  460. }
  461. return $data
  462. } else {
  463. putlog "::cims:: Local configfile $file not found. Ignoring."
  464. }
  465. }
  466. #::cims::write_arraytofile "scripts/cims/$botnick.cims" "mnet_freqs_onoff"
  467. proc ::cims::write_arraytofile {file arrayname} {
  468. variable $arrayname
  469. set array_string [array get $arrayname]
  470. #putlog "array_string: $array_string"
  471. set fh [open "$file" "w"]
  472. puts $fh $array_string
  473. close $fh
  474. }
  475. #::cims::read_arrayfromfile "scripts/cims/$botnick.cims" "mnet_freqs_onoff"
  476. proc ::cims::read_arrayfromfile {file arrayname} {
  477. variable $arrayname
  478. set fh [open "$file" "r"]
  479. array set $arrayname [gets $fh]
  480. close $fh
  481. }
  482. proc ::cims::dummy {} {
  483. return 0
  484. }
  485. #not used..
  486. proc ::cims::kill_delaytimer {prefix chan} {
  487. variable mnet_delay
  488. set mnet_delay($prefix,$chan) 0
  489. putlog "Sending in $chan allowed again, since the timer expired"
  490. }
  491. proc ::cims::put_bot {botnetnick data} {
  492. set testlink [islinked $botnetnick]
  493. if {$testlink == 1} {
  494. putlog "::cims:: put_bot: + a message delivered to $botnetnick."
  495. if {[string length $data] > 335} {
  496. ::putils::filelog "scripts/cims/cims.log" "alldata put_bot to $botnetnick: $data"
  497. ::putils::put_bot $botnetnick $data
  498. } else {
  499. putbot $botnetnick $data
  500. }
  501. } else {
  502. putlog "::cims:: put_bot: + a message couldn't be delivered. Bot $botnetnick is not linked"
  503. }
  504. }
  505. proc ::cims::chanstat_users {chan users} {
  506. set ch_users [chanlist $chan]
  507. set ch_users_count [llength $ch_users]
  508. set users [expr $users + $ch_users_count]
  509. return $users
  510. }
  511. # this proc makes ::cims::chanstat_users obsolete! hmm.. but we leave it for some time. compatibility stuff!
  512. proc ::cims::chanstat_userlist {chan unique_userlist} {
  513. set ch_userlist [chanlist ${chan}]
  514. set unique_userlist [lsort -unique [join [concat [split $unique_userlist] [split $ch_userlist]]]]
  515. #putlog "uulist: $unique_userlist"
  516. return $unique_userlist
  517. }
  518. proc ::cims::history_queue {bucket nickname channel rec_text} {
  519. #::cims::history_queue $network,$name "$timestamp $rec_nickname $rec_channel $rec_text"
  520. variable mnet_histories
  521. variable mnet_max_history
  522. #putlog "::cims:: HISTORY $nickname $channel $rec_text"
  523. set timestamp [clock format [clock seconds] -format "%H:%M:%S"]
  524. #putlog "tstamp: $timestamp"
  525. # no clean:text needed .. already done sometime before.
  526. set input_history "$timestamp / $nickname - $channel : $rec_text"
  527. # putlog "tst: $timestamp nick: $nickname chan: $channel txt: $rec_text"
  528. set list_length 0
  529. if {[info exists mnet_histories($bucket)] == 1} {
  530. set list_length [llength $mnet_histories($bucket)]
  531. putlog "listlength of mnet_histories($bucket) before: $list_length"
  532. }
  533. if {$list_length >= $mnet_max_history} {
  534. # $mnet_max_history 10 - 9
  535. # 5= 1 4
  536. #10= 1 9
  537. # wenn man von 10 auf 5 wechselt: 6 9
  538. set mnet_histories($bucket) [lrange $mnet_histories($bucket) [expr $list_length - $mnet_max_history + 1] [expr $list_length - 1]]
  539. # putlog "grosser oder gleich 10: str: $input"
  540. }
  541. lappend mnet_histories($bucket) $input_history
  542. #set list_length [llength $mnet_histories($bucket)]
  543. #putlog "listlength of mnet_histories($bucket) now: $list_length"
  544. # define your own logging
  545. if {[info procs ::cims::history_logger] == "::cims::history_logger"} {
  546. ::cims::history_logger $bucket $nickname $channel $rec_text
  547. } else {
  548. putlog "::cims:: procedure ::cims::history_logger hasn't been defined. Do it, if you want logging."
  549. }
  550. }
  551. ## procs which are executed remotely follow:
  552. proc ::cims::receive_message {rec_botnick cmd rec_data} {
  553. variable mnet
  554. variable mnet_channels
  555. variable mnet_freqs_onoff
  556. variable mnet_bots
  557. variable mnet_histories
  558. global botnet-nick
  559. #putlog "rec_data string: '$rec_data'"
  560. # escape it to be a list FIRST.. and for output later, let the list JOIN together to a string again!
  561. set rec_data [split $rec_data]
  562. #putlog "rec_data list: '$rec_data'"
  563. set tmp_network [join [lindex $rec_data 0]]
  564. set tmp_name [join [lindex $rec_data 1]]
  565. #putlog "tmp_net: '$tmp_network'"
  566. #putlog "tmp_nam: '$tmp_name'"
  567. set network [::putils::kill_spaces $tmp_network]
  568. #putlog "network: '$network'"
  569. set name [::putils::kill_spaces $tmp_name]
  570. #putlog "name: '$name'"
  571. set rec_nickname [::putils::kill_spaces [join [lindex $rec_data 2]]]
  572. #set rec_nickname [::putils::kill_spaces clean_txt [join [lrange [split $rec_data] 4 end]]]
  573. set rec_channel [::putils::kill_spaces [join [lindex $rec_data 3]]]
  574. #set rec_text [::cims::clean_txt [join [lrange [split $rec_data] 4 end]]]
  575. set rec_text [join [lrange $rec_data 4 end]]
  576. #putlog "rec_text: '$rec_text'"
  577. set allowed [::cims::allowed_netbot $network ${rec_botnick}]
  578. if {$allowed == 0} {
  579. return 0
  580. }
  581. # now the bot is declared as a known bot.
  582. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  583. putlog "::cims:: + Relaying Message from: $rec_nickname @ $rec_botnick @ $rec_channel: $rec_text"
  584. set unique_userlist ""
  585. set users 0
  586. set count_channels 0
  587. ::cims::history_queue $network,$name $rec_nickname $rec_channel $rec_text
  588. foreach {chan} $mnet_channels($network,$proper_botnick) {
  589. if {[::cims::botonchannel $chan] == "1"} {
  590. # erster disable/enable check
  591. if {$mnet_freqs_onoff($name,$chan) == 1} {
  592. incr count_channels
  593. set users [::cims::chanstat_users $chan $users]
  594. set unique_userlist [::cims::chanstat_userlist $chan $unique_userlist]
  595. putlog "::cims:: * Remote counting, $chan "
  596. ::cims::put_local_netmessage $network $name $chan $rec_nickname $rec_channel $rec_text
  597. } else {
  598. putlog "::cims:: + $chan has disabled $name output"
  599. }
  600. } else {
  601. putlog "::cims:: + Can't relay a message to '$chan' since I am not there."
  602. }
  603. }
  604. # building a fake userlist of 500 people for debugging:
  605. # for {set x 0} {$x < 500} {incr x} {
  606. # set unique_userlist [lsort -unique [concat $unique_userlist [::putils::randomRangeString 6]]]
  607. # }
  608. putlog "::cims:: unique_userlist is [llength $unique_userlist] long"
  609. # it is an eggdrop limitation/bug that it is not allowed to send large botnet messages via putbot.
  610. # it truncates our payload after 388 chars. in fact the payload is 401 chars.
  611. #
  612. # we need a workaround! fixme! tcldcc.c:
  613. #
  614. #static int tcl_putbot STDVAR
  615. #{
  616. #int i;
  617. #char msg[401];
  618. #...
  619. #}
  620. # ANSWER! SEND!
  621. ::cims::put_bot $rec_botnick "mnet_answer $network $name $rec_channel $users $count_channels $unique_userlist"
  622. return 0
  623. }
  624. proc ::cims::receive_answer {rec_botnick cmd rec_data} {
  625. variable mnet
  626. variable mnet_reached_users
  627. variable mnet_reached_userlist
  628. variable mnet_reached_chans
  629. global botnet-nick
  630. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  631. set long [string length $rec_data]
  632. putlog "::cims:: * on receive_answer from $rec_botnick length: $long"
  633. # we receive a STRING in rec_data! convert into list!
  634. set rec_data [split $rec_data]
  635. # work with list (lindex, lrange) and put together to a STRING again with JOIN.
  636. set network [join [lindex $rec_data 0]]
  637. set name [join [lindex $rec_data 1]]
  638. set chan [join [lindex $rec_data 2]]
  639. # rec_usercount is obsolete
  640. set rec_usercount [join [lindex $rec_data 3]]
  641. set rec_chancount [join [lindex $rec_data 4]]
  642. set rec_userlist [join [lrange $rec_data 5 end]]
  643. # override obsolete rec_usercount from above.
  644. set rec_usercount [llength $rec_userlist]
  645. # get this bots results to our namespaced variable:
  646. # FIXME: make simple userstats more accurate by sending nicknames back..
  647. # done: userstats should be counted in a correct variablename .. according to network and frequency
  648. # if you don't pay attention to it, a conflict between two broadcasts counting users may appear.
  649. # $mnet_reached_chans($name,$chan) <- $mnet_reached_users($name,$chan)
  650. set mnet_reached_users($name,$chan) [expr $mnet_reached_users($name,$chan) + $rec_usercount]
  651. set mnet_reached_chans($name,$chan) [expr $mnet_reached_chans($name,$chan) + $rec_chancount]
  652. set mnet_reached_userlist($name,$chan) [lsort -unique [join [concat [split $mnet_reached_userlist($name,$chan)] [split $rec_userlist]]]]
  653. putlog "::cims:: * near end receive_answer from $rec_botnick"
  654. putlog "::cims:: * $rec_botnick told me he sent its message to $rec_usercount unique people in $rec_chancount channels for $network frequency $name!"
  655. #putlog "::cims:: * Users reached since $name call: $mnet_reached_users($name,$chan) in channels: $mnet_reached_chans($name,$chan)"
  656. }
  657. proc ::cims::clean_txt {text} {
  658. # putlog "filter_A: ${text}"
  659. # regsub -all "\\" $text "\\\\" text
  660. # fixes many whitespace between words down to one space between words
  661. regsub -all "\\s+" $text " " text
  662. # filtering out all colorcodes (works well)
  663. regsub -all "\003\[0-9\]\{1,2\},\[0-9\]\{1,2\}" $text "" text
  664. regsub -all "\003\[0-9\]\{1,2\}" $text "" text
  665. regsub -all "\003" $text "" text
  666. # filtering out BOLD text
  667. regsub -all "\002" $text "" text
  668. # underline gets filtered too. (since +c on quakenet would suppress it ...)
  669. regsub -all "\037" $text "" text
  670. # replacing like !!!!!!!!!!!!! with !!!!! (5 letters)
  671. # s/(.?)\1{4,}/\1\1\1\1\1/g;
  672. # - max 5 same chars in a row
  673. regsub -all -nocase -expanded {(.)\1\1\1\1+} $text {\1\1\1\1\1} text
  674. # putlog "test: $text"
  675. set text [string trim $text]
  676. # putlog "filter_B: ${text}"
  677. return $text
  678. }
  679. proc ::cims::umlauts {text} {
  680. # A REAL STRANGE BUG WORKAROUND WITH UMLAUTS
  681. regsub -all "Ä" ${text} "Ä" text
  682. regsub -all "Ü" ${text} "Ü" text
  683. regsub -all "Ö" ${text} "Ö" text
  684. regsub -all "ä" ${text} "ä" text
  685. regsub -all "ü" ${text} "ü" text
  686. regsub -all "ö" ${text} "ö" text
  687. return ${text}
  688. }
  689. proc ::cims::botonchannel {chan} {
  690. # safe "bot-knows-the-channel-and-is-in-there"-function
  691. if {[validchan $chan] == 1 && [botonchan $chan] == 1} {
  692. return 1
  693. } else {
  694. return 0
  695. }
  696. }
  697. # allowcheck for public commands..
  698. proc ::cims::allowed_channel {network name prefix nick mask chan} {
  699. variable mnet
  700. variable mnet_channels
  701. variable mnet_bots
  702. variable mnet_freqs
  703. variable mnet_networks_by_prefix
  704. variable mnet_colors
  705. global botnick botnet-nick lastbind
  706. # validating incoming vars
  707. set chan [::cims::proper_channelname $chan]
  708. putlog "::cims:: = a command for ($prefix) triggered by $nick ($mask) in $chan"
  709. # the command must be from a channel of "ownchannels"
  710. putlog "::cims:: = trying if $chan is allowed in network: $network"
  711. set found 0
  712. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  713. foreach _trigger_chan $mnet_channels($network,$proper_botnick) {
  714. #putlog "::cims:: = test: $_trigger_chan vs $chan"
  715. if {[string compare -nocase $_trigger_chan $chan] == 0} {
  716. putlog "::cims:: = channel: $_trigger_chan is allowed in network: $network"
  717. set found 1
  718. } else {
  719. # putlog "::cims:: = not in list: $chan"
  720. }
  721. }
  722. if {$found == 0} {
  723. putlog "::cims:: = triggering $prefix of network $network in $chan is not allowed (in my point of view)"
  724. return 0
  725. }
  726. set snd_nick [::cims::get_data $nick]
  727. if {![isop $nick $chan]} {
  728. putlog "::cims:: = allowed in $chan, but $nick is no op"
  729. ::putils::put_nick $nick "Sorry $nick, you need to be op."
  730. return 0
  731. }
  732. if {$nick == $botnick} {
  733. # no recursion :)
  734. return 0
  735. }
  736. # got through all checks .. so it's an OK channel
  737. return 1
  738. }
  739. # checks if the user is allowed, returns 1 if OK.
  740. # checks against the xml config of the banned ones.
  741. proc ::cims::allowed_user {network name prefix nick mask chan} {
  742. variable mnet
  743. variable mnet_channels
  744. variable mnet_bots
  745. variable mnet_bans
  746. variable mnet_networks_by_prefix
  747. variable mnet_colors
  748. global botnick botnet-nick lastbind
  749. # validating incoming vars
  750. set chan [::cims::proper_channelname $chan]
  751. # does mnet_bans even exist? it's optional after all.
  752. if {[info exists mnet_bans($network,$prefix)] == 1} {
  753. putlog "::cims:: = a command for ($prefix) triggered by $nick ($mask) in $chan"
  754. # the command must be from a channel of "ownchannels"
  755. putlog "::cims:: = trying if $nick ($mask) banned from frequency: $network,$prefix"
  756. set found 0
  757. foreach {hostmask reason} $mnet_bans($network,$prefix) {
  758. if {[string match [string tolower $hostmask] [string tolower $mask]]} {
  759. putlog "::cims:: = allowed_user: $nick ($mask) is not allowed in network ($network) by hostmask ($hostmask) for reason: $reason"
  760. set found 1
  761. } else {
  762. # putlog "::cims:: = not in list: $chan"
  763. }
  764. }
  765. if {$found == 1} {
  766. # tell the user his ban. :)
  767. ::putils::put_nick $nick "::cims:: Not allowed to send messages. Reason: $reason"
  768. return 0
  769. }
  770. }
  771. # got through all checks .. so user is not in banlist
  772. return 1
  773. }
  774. # allowcheck for bots sending netmessages
  775. proc ::cims::allowed_netbot {network netbot} {
  776. variable mnet
  777. variable mnet_bots
  778. global botnick botnet-nick lastbind
  779. putlog "::cims:: = a netbot $netbot sending message... allowed?"
  780. # the command must be from a channel of "ownchannels"
  781. set found 0
  782. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  783. set proper_netbot [::cims::proper_botnick $netbot]
  784. foreach {_netbotnick} $mnet_bots($network) {
  785. if {$_netbotnick == $proper_netbot} {
  786. putlog "::cims:: = netbot: $proper_netbot is allowed in network: $network"
  787. set found 1
  788. } else {
  789. # putlog "::cims:: = netbot: ...."
  790. }
  791. }
  792. if {$found == 0} {
  793. putlog "::cims:: = $proper_netbot of network $network sent me something - but it is not allowed in my network: $network"
  794. return 0
  795. }
  796. if {$netbot == ${botnet-nick}} {
  797. # if myself (for some reason) then don't do anything... no recursion :)
  798. return 0
  799. }
  800. # got through all checks .. so it's an OK netbot for a given network
  801. return 1
  802. }
  803. proc ::cims::reply_timeout {name chan} {
  804. variable mnet
  805. variable mnet_reached_users
  806. variable mnet_reached_userlist
  807. variable mnet_reached_chans
  808. global botnet-nick
  809. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  810. # clean $name and $chan
  811. set name [::putils::kill_spaces $name]
  812. set chan [::putils::kill_spaces $chan]
  813. # easify variables
  814. set userlist $mnet_reached_userlist($name,$chan)
  815. set user_cnt [llength $userlist]
  816. set mnet_reached_users($name,$chan) $user_cnt
  817. set chan_cnt $mnet_reached_chans($name,$chan)
  818. # inzwischen drfte auch die antwort gekommen sein.. also jetzt mnet_reached_* auswerten nach timeout..
  819. # make stats channel-dependent
  820. #putlog "::cims:: * userlist is finally: $userlist "
  821. putlog "::cims:: * After ALL: Count_Users: $user_cnt Count_Channels: $mnet_reached_chans($name,$chan)"
  822. # Give me some reply.
  823. ::putils::filelog "scripts/cims/${proper_botnick}.log" "$name from $chan reached $user_cnt users: $rec_userlist"
  824. ::putils::put_local_msg ${chan} "::cims:: Message sent to $chan_cnt channels, reaching $user_cnt unique users."
  825. }
  826. ### public reachable commands
  827. proc ::cims::history_frequency {nick mask hand chan text} {
  828. # proc triggered by specified $prefix_history
  829. variable mnet
  830. variable mnet_channels
  831. variable mnet_bots
  832. variable mnet_networks_by_prefix
  833. variable mnet_colors
  834. variable mnet_freqs
  835. variable mnet_freqs_onoff
  836. variable mnet_histories
  837. variable mnet_max_history
  838. global botnick lastbind
  839. set com $lastbind
  840. # validating incoming vars
  841. set chan [::cims::proper_channelname $chan]
  842. # cutting .prefix_history down to .prefix
  843. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  844. #putlog "::cims:: the prefix is $prefix"
  845. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  846. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  847. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  848. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  849. putlog "network: $network"
  850. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  851. if {$allowed == 0} {
  852. return 0
  853. }
  854. # now generally allowed.
  855. putlog "mnet_history: $com triggered by $nick in $chan"
  856. if {[info exists mnet_histories($network,$name)] == 1} {
  857. set list_length [llength $mnet_histories($network,$name)]
  858. if {$list_length >= 1} {
  859. set msg "::cims:: last $mnet_max_history messages of $network $name:"
  860. ::putils::put_nick $nick $msg
  861. set x_count 0
  862. foreach {msg} $mnet_histories($network,$name) {
  863. incr x_count
  864. set backwards_x [expr $list_length - $x_count]
  865. set msg [lindex $mnet_histories($network,$name) $backwards_x]
  866. ::putils::put_nick $nick "($x_count/$list_length) $msg"
  867. }
  868. }
  869. } else {
  870. ::putils::put_nick $nick "::cims:: no accumulated history for $network $name"
  871. }
  872. }
  873. proc ::cims::enable_frequency {nick mask hand chan text} {
  874. # proc triggered by specified $prefix_enable
  875. variable mnet
  876. variable mnet_channels
  877. variable mnet_bots
  878. variable mnet_networks_by_prefix
  879. variable mnet_colors
  880. variable mnet_freqs
  881. variable mnet_freqs_onoff
  882. global botnick lastbind botnet-nick
  883. set com $lastbind
  884. # validating incoming vars
  885. set chan [::cims::proper_channelname $chan]
  886. # cutting .prefix_enable down to .prefix
  887. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  888. #putlog "::cims:: the prefix is $prefix"
  889. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  890. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  891. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  892. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  893. #putlog "network: $network"
  894. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  895. if {$allowed == 0} {
  896. return 0
  897. }
  898. # now generally allowed.
  899. putlog "mnet_freq_status: before $mnet_freqs_onoff($name,$chan)"
  900. set mnet_freqs_onoff($name,$chan) 1
  901. putlog "mnet_freq_status: now $mnet_freqs_onoff($name,$chan)"
  902. ::putils::put_local_msg $chan "::cims:: $name _output_ enabled for $chan"
  903. }
  904. proc ::cims::disable_frequency {nick mask hand chan text} {
  905. # proc triggered by specified $prefix_enable
  906. variable mnet
  907. variable mnet_channels
  908. variable mnet_bots
  909. variable mnet_networks_by_prefix
  910. variable mnet_colors
  911. variable mnet_freqs
  912. variable mnet_freqs_onoff
  913. global botnick lastbind
  914. set com $lastbind
  915. # validating incoming vars
  916. set chan [::cims::proper_channelname $chan]
  917. # cutting .prefix_enable down to .prefix
  918. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  919. putlog "::cims:: the prefix is $prefix"
  920. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  921. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  922. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  923. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  924. #putlog "network: $network"
  925. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  926. if {$allowed == 0} {
  927. return 0
  928. }
  929. # now generally allowed.
  930. putlog "mnet_freq_status: before $mnet_freqs_onoff($name,$chan)"
  931. set mnet_freqs_onoff($name,$chan) 0
  932. putlog "mnet_freq_status: now $mnet_freqs_onoff($name,$chan)"
  933. ::putils::put_local_msg $chan "::cims:: $name _output_ disabled for $chan"
  934. }
  935. proc ::cims::toggle_frequency {nick mask hand chan text} {
  936. # proc triggered by specified $prefix_toggle
  937. variable mnet
  938. variable mnet_channels
  939. variable mnet_bots
  940. variable mnet_networks_by_prefix
  941. variable mnet_colors
  942. variable mnet_freqs
  943. variable mnet_freqs_onoff
  944. global botnick lastbind
  945. set com $lastbind
  946. # validating incoming vars
  947. set chan [::cims::proper_channelname $chan]
  948. # cutting .prefix_enable down to .prefix
  949. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  950. # putlog "::cims:: the prefix is $prefix"
  951. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  952. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  953. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  954. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  955. # putlog "network: $network"
  956. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  957. if {$allowed == 0} {
  958. return 0
  959. }
  960. # now generally allowed.
  961. putlog "mnet_freq_status: toggle $mnet_freqs_onoff($name,$chan)"
  962. if {$mnet_freqs_onoff($name,$chan) == 1} {
  963. set mnet_freqs_onoff($name,$chan) 0
  964. ::putils::put_local_msg $chan "::cims:: toggling ${name} _output_, now disabled for ${chan}"
  965. } else {
  966. set mnet_freqs_onoff($name,$chan) 1
  967. ::putils::put_local_msg $chan "::cims:: toggling ${name} _output_, now enabled for ${chan}"
  968. }
  969. putlog "mnet_freq_status: toggle now $mnet_freqs_onoff($name,$chan)"
  970. # frequency toggled .. :)
  971. }
  972. #::cims::enable_color
  973. proc ::cims::enable_color {nick mask hand chan text} {
  974. # proc triggered by specified $prefix_enable
  975. variable mnet
  976. variable mnet_channels
  977. variable mnet_bots
  978. variable mnet_networks_by_prefix
  979. variable mnet_colors_optional
  980. variable mnet_colors_onoff
  981. global botnick lastbind
  982. set com $lastbind
  983. # validating incoming vars
  984. set chan [::cims::proper_channelname $chan]
  985. # cutting .prefix_enable down to .prefix
  986. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  987. #putlog "::cims:: the prefix is $prefix"
  988. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  989. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  990. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  991. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  992. #putlog "network: $network"
  993. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  994. if {$allowed == 0} {
  995. return 0
  996. }
  997. # now generally allowed.
  998. if {$mnet_colors_optional($network) != 1} {
  999. return 0
  1000. }
  1001. putlog "mnet_color_status: before $mnet_colors_onoff($name,$chan)"
  1002. set mnet_colors_onoff($name,$chan) 1
  1003. putlog "mnet_color_status: now $mnet_colors_onoff($name,$chan)"
  1004. ::putils::put_local_msg $chan "::cims:: $name _coloroutput_ enabled for $chan"
  1005. }
  1006. #::cims::disable_color
  1007. proc ::cims::disable_color {nick mask hand chan text} {
  1008. # proc triggered by specified $prefix_enable
  1009. variable mnet
  1010. variable mnet_channels
  1011. variable mnet_bots
  1012. variable mnet_networks_by_prefix
  1013. variable mnet_colors_optional
  1014. variable mnet_colors_onoff
  1015. global botnick lastbind
  1016. set com $lastbind
  1017. # validating incoming vars
  1018. set chan [::cims::proper_channelname $chan]
  1019. # cutting .prefix_enable down to .prefix
  1020. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  1021. #putlog "::cims:: the prefix is $prefix"
  1022. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  1023. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  1024. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  1025. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  1026. #putlog "network: $network"
  1027. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  1028. if {$allowed == 0} {
  1029. return 0
  1030. }
  1031. # now generally allowed.
  1032. if {$mnet_colors_optional($network) != 1} {
  1033. return 0
  1034. }
  1035. putlog "mnet_color_status: before $mnet_colors_onoff($name,$chan)"
  1036. set mnet_colors_onoff($name,$chan) 0
  1037. putlog "mnet_color_status: now $mnet_colors_onoff($name,$chan)"
  1038. ::putils::put_local_msg $chan "::cims:: $name _coloroutput_ disabled for $chan"
  1039. }
  1040. #::cims::toggle_color
  1041. proc ::cims::toggle_color {nick mask hand chan text} {
  1042. # proc triggered by specified $prefix_toggle
  1043. variable mnet
  1044. variable mnet_channels
  1045. variable mnet_bots
  1046. variable mnet_networks_by_prefix
  1047. variable mnet_colors_optional
  1048. variable mnet_colors_onoff
  1049. global botnick lastbind
  1050. set com $lastbind
  1051. # validating incoming vars
  1052. set chan [::cims::proper_channelname $chan]
  1053. # cutting .prefix_enable down to .prefix
  1054. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  1055. # putlog "::cims:: the prefix is $prefix"
  1056. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  1057. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  1058. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  1059. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  1060. # putlog "network: $network"
  1061. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  1062. if {$allowed == 0} {
  1063. return 0
  1064. }
  1065. # now generally allowed.
  1066. if {$mnet_colors_optional($network) != 1} {
  1067. return 0
  1068. }
  1069. putlog "mnet_color_status: toggle $mnet_colors_onoff($name,$chan)"
  1070. if {$mnet_colors_onoff($name,$chan) == 1} {
  1071. set mnet_colors_onoff($name,$chan) 0
  1072. ::putils::put_local_msg $chan "::cims:: toggling ${name} _coloroutput_, now disabled for ${chan}"
  1073. } else {
  1074. set mnet_colors_onoff($name,$chan) 1
  1075. ::putils::put_local_msg $chan "::cims:: toggling ${name} _coloroutput_, now enabled for ${chan}"
  1076. }
  1077. putlog "mnet_color_status: toggle now $mnet_colors_onoff($name,$chan)"
  1078. # color for a channel toggled .. :)
  1079. }
  1080. #::cims::toggle_color
  1081. proc ::cims::status_frequency {nick mask hand chan text} {
  1082. # proc triggered by specified $prefix_toggle
  1083. variable mnet
  1084. variable mnet_channels
  1085. variable mnet_bots
  1086. variable mnet_networks_by_prefix
  1087. variable mnet_colors_optional
  1088. variable mnet_colors_onoff
  1089. variable mnet_freqs_onoff
  1090. variable mnet_reached_users
  1091. variable mnet_reached_chans
  1092. global botnick lastbind
  1093. set com $lastbind
  1094. # validating incoming vars
  1095. set chan [::cims::proper_channelname $chan]
  1096. # cutting .prefix_enable down to .prefix
  1097. set prefix [string range $com 0 [expr [string first _ $com] -1]]
  1098. # putlog "::cims:: the prefix is $prefix"
  1099. set network [join [lindex $mnet_networks_by_prefix($prefix) 0]]
  1100. set name [join [lindex $mnet_networks_by_prefix($prefix) 1]]
  1101. set help [join [lindex $mnet_networks_by_prefix($prefix) 2]]
  1102. set desc [join [lindex $mnet_networks_by_prefix($prefix) 3]]
  1103. # putlog "network: $network"
  1104. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  1105. if {$allowed == 0} {
  1106. return 0
  1107. }
  1108. # now generally allowed.
  1109. ## get status for this frequency.
  1110. set status_message "$chan $name: "
  1111. if {$mnet_freqs_onoff($name,$chan) == 1} {
  1112. append status_message "frequency output is on. "
  1113. } else {
  1114. append status_message "frequency output is off. "
  1115. }
  1116. if {$mnet_colors_optional($network) == 1} {
  1117. if {$mnet_colors_onoff($name,$chan) == 1} {
  1118. append status_message "colors are turned on. "
  1119. } else {
  1120. append status_message "colors are turned off. "
  1121. }
  1122. } else {
  1123. append status_message "message-colors are forced. "
  1124. }
  1125. if {[info exists mnet_reached_users($name,$chan)]} {
  1126. if {[info exists mnet_reached_chans($name,$chan)]} {
  1127. append status_message "Your last message reached about $mnet_reached_users($name,$chan) users and $mnet_reached_chans($name,$chan) channels. "
  1128. }
  1129. }
  1130. ::putils::put_local_msg $chan "$status_message"
  1131. }
  1132. # checks timers of channels ... if not found, then ok with "0" secs left.. if found then return $secsleft
  1133. proc ::cims::check_spam_protection {prefix chan} {
  1134. variable mnet_delay
  1135. # validating incoming vars
  1136. set chan [::cims::proper_channelname $chan]
  1137. # check for pending timers.. spam-protection
  1138. set timerlist [utimers]
  1139. set secondsLeft 0
  1140. if {![info exists mnet_delay($prefix,$chan)]} {
  1141. set mnet_delay($prefix,$chan) 0
  1142. } else {
  1143. foreach {timerinfo} $timerlist {
  1144. set secsleft [join [lindex $timerinfo 0]]
  1145. set timer_id [join [lindex $timerinfo 2]]
  1146. # putlog "sec: $secsleft tid: $timer_id"
  1147. # putlog "checking timer ($timer_id) against mdelay-id: $mnet_delay($prefix,$chan)"
  1148. if {$timer_id == $mnet_delay($prefix,$chan)} {
  1149. return $secsleft
  1150. }
  1151. }
  1152. }
  1153. return 0
  1154. }
  1155. proc ::cims::messaging_public {nick mask hand chan text} {
  1156. # proc triggered by specified $prefix and calling the following (internal) one
  1157. variable mnet
  1158. variable mnet_channels
  1159. variable mnet_bots
  1160. variable mnet_networks_by_prefix
  1161. variable mnet_colors
  1162. variable mnet_freqs
  1163. variable mnet_freqs_onoff
  1164. variable mnet_delay
  1165. variable mnet_histories
  1166. variable mnet_reached_users
  1167. variable mnet_reached_userlist
  1168. variable mnet_reached_chans
  1169. global botnet-nick lastbind
  1170. # validating incoming vars
  1171. set chan [::cims::proper_channelname $chan]
  1172. set prefix $lastbind
  1173. # $mnet_networks_by_prefix($prefix) is already a list! Don't split it! But work with lindex and lrange on it! later: JOIN it to a string.
  1174. set my_network_entry $mnet_networks_by_prefix($prefix)
  1175. set network [join [lindex $my_network_entry 0]]
  1176. set name [join [lindex $my_network_entry 1]]
  1177. set help [join [lindex $my_network_entry 2]]
  1178. set desc [join [lindex $my_network_entry 3]]
  1179. set delay [join [lindex $my_network_entry 4]]
  1180. set allowed [::cims::allowed_channel $network $name $prefix $nick $mask $chan]
  1181. if {$allowed == 0} {
  1182. return 0
  1183. }
  1184. # now generally allowed.
  1185. # is user banned from sending??
  1186. set user_allowed [::cims::allowed_user $network $name $prefix $nick $mask $chan]
  1187. if {$user_allowed == 0} {
  1188. return 0
  1189. }
  1190. # user has not been matched against banlist. So he's allowed here.
  1191. # cleaning given strings...
  1192. set text [::cims::clean_txt $text]
  1193. # check here if we have some text or not.. :)
  1194. if {$text == "" || $text == "{}"} {
  1195. # output some help:
  1196. putlog "::cims:: = gave him some help"
  1197. ::putils::put_local_msg $chan $help
  1198. } else {
  1199. # ok, now send! (if timer allows it)
  1200. ### spamprotection
  1201. # returns the seconds that are left for this channel .. or 0 if no seconds left.
  1202. set secsleft [::cims::check_spam_protection $prefix $chan]
  1203. if {$secsleft > 0} {
  1204. putlog "::cims:: The timer of $prefix for $chan is still running. The guy has to wait ($secsleft/$delay) more seconds. I tell him."
  1205. ::putils::put_nick $nick "Sorry $nick, usage of $prefix only every $delay seconds. $secsleft seconds for $chan remaining."
  1206. # now BREAK "messaging_public".
  1207. # NO break.. for debug :)) arf
  1208. return 0
  1209. }
  1210. # if we went through the timer check ... so it doesn't exist anymore then we set a new one:
  1211. # set a timer-id to check for again, if it's allowed or not .. delay
  1212. set mnet_delay($prefix,$chan) [utimer $delay ::cims::dummy]
  1213. ### end spamprotection
  1214. ## HE IS SENDING NOW!
  1215. set mnet(reached) "0"
  1216. set mnet(reached_channels) "0"
  1217. # reset stats .. because he is just freshly sending
  1218. set mnet_reached_users($name,$chan) "0"
  1219. set mnet_reached_userlist($name,$chan) ""
  1220. set mnet_reached_chans($name,$chan) "0"
  1221. ::cims::history_queue $network,$name $nick $chan $text
  1222. # send them first, we await a reply!
  1223. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  1224. # putlog "{botnet-nick} huar: $proper_botnick"
  1225. # single sends TO ALL REMOTE BOTS with their remote CHANNELS...
  1226. # let's see which mode for sending is used...
  1227. foreach _botnick $mnet_bots($network) {
  1228. if {$_botnick == $proper_botnick} {
  1229. } else { # if not myself!
  1230. putlog "::cims:: send: + I am about to send a message to $_botnick for $network: $name: $text"
  1231. # safe put_bot function..
  1232. ::cims::put_bot $_botnick "mnet_receive $network $name $nick $chan $text"
  1233. }
  1234. }
  1235. # SEND TO ALL OWN CHANNELS...
  1236. # send to all own channels, except to the channel the message originating from:
  1237. foreach _ownchan $mnet_channels($network,$proper_botnick) {
  1238. if {$_ownchan == $chan} {
  1239. } else { # if not own channel!
  1240. if {[::cims::botonchannel $_ownchan] == "1"} {
  1241. putlog "::cims:: = i am on $_ownchan"
  1242. # second enable/disable check
  1243. if {$mnet_freqs_onoff($name,$_ownchan) == 1} {
  1244. # userstats local
  1245. set mnet_reached_users($name,$chan) [::cims::chanstat_users $_ownchan $mnet_reached_users($name,$chan)]
  1246. set mnet_reached_userlist($name,$chan) [::cims::chanstat_userlist $_ownchan $mnet_reached_userlist($name,$chan)]
  1247. putlog "::cims:: * Localcounting up $_ownchan users: $mnet_reached_users($name,$chan)"
  1248. # channel count local
  1249. incr mnet_reached_chans($name,$chan)
  1250. ::cims::put_local_netmessage $network $name $_ownchan $nick $chan $text
  1251. } else {
  1252. putlog "::cims:: + $_ownchan has disabled $name output"
  1253. }
  1254. } else {
  1255. putlog "::cims:: + Can't send a local message to '$_ownchan' since I am not there."
  1256. }
  1257. }
  1258. }
  1259. # wait here some more! (10 seconds with utimer)
  1260. utimer 5 "::cims::reply_timeout $name $chan"
  1261. # notice: better would be to go into the "reply" when all known bots already answered..
  1262. # then, if a bot is missing, but it was a known bot, just use a timeout with about 5-10 seconds..
  1263. # if the bot still didn't answer, then just go into "reply".
  1264. }
  1265. }
  1266. ### END public reachable commands
  1267. proc ::cims::binds {} {
  1268. variable currentNetworkDesc
  1269. variable currentNetworkSig
  1270. variable startNetworkInstance
  1271. variable currentBot
  1272. variable currentOwner
  1273. variable startBotInstance
  1274. variable mnet_networks
  1275. variable mnet_freqs
  1276. variable mnet_networks_by_prefix
  1277. variable mnet_channels
  1278. variable mnet_freqs_onoff
  1279. variable mnet_colors_onoff
  1280. global botnet-nick
  1281. set proper_botnick [::cims::proper_botnick ${botnet-nick}]
  1282. # binding of irc commands ... only works well if a config was loaded..
  1283. foreach {network desc} $mnet_networks {
  1284. # putlog "test: network is $network"
  1285. # putlog "test: networkdesc is $desc"
  1286. foreach {network name prefix help desc msgdelay} $mnet_freqs($network) {
  1287. putlog "network: $network"
  1288. putlog "name: $name"
  1289. putlog "prefix: $prefix"
  1290. putlog "help: $help"
  1291. putlog "desc: $desc"
  1292. putlog "msgdelay: $msgdelay"
  1293. set mnet_networks_by_prefix($prefix) "{$network} {$name} {$help} {$desc} {$msgdelay}"
  1294. # setting variables on init.
  1295. foreach {channel} $mnet_channels($network,$proper_botnick) {
  1296. # catch like php isset() - but catch returns 0 if OK!
  1297. set channel [::cims::proper_channelname $channel]
  1298. if {[catch {set mnet_freqs_onoff($name,$channel)}] == 1} {
  1299. set mnet_freqs_onoff($name,$channel) 1
  1300. putlog "::cims:: initially enabling $name for channel: $channel"
  1301. } else {
  1302. putlog "::cims:: $name for $channel is already on output: $mnet_freqs_onoff($name,$channel)"
  1303. }
  1304. # catch like php isset() - but catch returns 0 if OK!
  1305. if {[catch {set mnet_colors_onoff($name,$channel)}] == 1} {
  1306. set mnet_colors_onoff($name,$channel) 0
  1307. putlog "::cims:: initially disabling colors for $name for channel (if not forced): $channel"
  1308. } else {
  1309. putlog "::cims:: coloroutput of $name for $channel is already on (if not forced): $mnet_colors_onoff($name,$channel)"
  1310. }
  1311. }
  1312. bind pub - ${prefix} ::cims::messaging_public
  1313. # $network $name $prefix $help $desc
  1314. bind pub - ${prefix}_enable ::cims::enable_frequency
  1315. bind pub - ${prefix}_disable ::cims::disable_frequency
  1316. bind pub - ${prefix}_toggle ::cims::toggle_frequency
  1317. bind pub - ${prefix}_enable_colors ::cims::enable_color
  1318. bind pub - ${prefix}_disable_colors ::cims::disable_color
  1319. bind pub - ${prefix}_toggle_colors ::cims::toggle_color
  1320. # with typos..
  1321. bind pub - ${prefix}_enable_color ::cims::enable_color
  1322. bind pub - ${prefix}_disable_color ::cims::disable_color
  1323. bind pub - ${prefix}_toggle_color ::cims::toggle_color
  1324. bind pub - ${prefix}_history ::cims::history_frequency
  1325. bind pub - ${prefix}_status ::cims::status_frequency
  1326. bind pub - .mnet_status ::cims::dummy
  1327. # FIXME: make some more commands available to have control over the messages
  1328. # - "_stats"?
  1329. }
  1330. }
  1331. }
  1332. bind bot - mnet_receive ::cims::receive_message
  1333. bind bot - mnet_answer ::cims::receive_answer
  1334. bind dcc m cims_reload ::cims::dcc_configload
  1335. proc ::cims::conf_parsing {} {
  1336. variable mnet_config
  1337. variable mnet_num_configs
  1338. set parser [xml::parser \
  1339. -elementstartcommand ::cims::xmlElementStart \
  1340. -elementendcommand ::cims::xmlElementEnd \
  1341. -defaultcommand ::cims::xmlDefaultCmd \
  1342. -characterdatacommand ::cims::xmlCdata \
  1343. ]
  1344. # -errorcommand xmlHandleError
  1345. for {set y 1} {$y <= $mnet_num_configs} {incr y} {
  1346. set configfile $mnet_config($y)
  1347. # putlog "HRM?: $configfile"
  1348. $parser parse "$configfile"
  1349. }
  1350. }
  1351. proc ::cims::dcc_configload {dummy1 dummy2 dummy3} {
  1352. # wrapper command for ::cims::netconfigload
  1353. putlog "::cims:: = reloading configs..."
  1354. ::cims::clean_netconfig
  1355. ::cims::netconfigload
  1356. }
  1357. proc ::cims::netconfigload {} {
  1358. variable mnet_http_config
  1359. variable mnet_local_config
  1360. variable mnet_config
  1361. variable mnet_num_configs
  1362. putlog "debug:: $mnet_http_config"
  1363. # possible methods to load a config are: get_httpfile and get_localfile
  1364. # get_httpfile
  1365. # get_localfile RELATIVE_PATH_AND_FILE
  1366. set x 0
  1367. if {[info exists mnet_http_config]} {
  1368. foreach {host httpfile port} $mnet_http_config {
  1369. # FIXME config validating.. is the file received correctly? or do
  1370. # we only have a 404 error message there? ... bad thing still.
  1371. incr x
  1372. set mnet_config($x) [get_httpfile $host $httpfile $port]
  1373. }
  1374. }
  1375. if {[info exists mnet_local_config]} {
  1376. foreach {localfile} $mnet_local_config {
  1377. # FIXME config validating.. could we open the file?
  1378. # is the file a correct config?
  1379. incr x
  1380. set mnet_config($x) [get_localfile $localfile]
  1381. }
  1382. }
  1383. # $x is now at its highest count.
  1384. set mnet_num_configs $x
  1385. if {$x > 0} {
  1386. utimer 5 ::cims::conf_parsing
  1387. utimer 10 ::cims::binds
  1388. } else {
  1389. putlog "::cims:: no configurations loaded."
  1390. }
  1391. }
  1392. # looping this one
  1393. proc ::cims::interval_conf {} {
  1394. variable mnet_cfg_reload_interval
  1395. variable mnet_configtimer_set
  1396. variable mnet_netconfig_timer
  1397. # putlog "::cims:: interval conf! $mnet_configtimer_set huh?"
  1398. if {$mnet_configtimer_set == 1} {
  1399. putlog "::cims:: starting intervalled configload ($mnet_cfg_reload_interval minutes)"
  1400. ::cims::clean_netconfig
  1401. ::cims::netconfigload
  1402. set mnet_netconfig_timer [timer $mnet_cfg_reload_interval ::cims::interval_conf]
  1403. } else {
  1404. putlog "::cims:: was about to make a automatic configload again, but it has been disabled recently."
  1405. }
  1406. }
  1407. namespace eval ::cims {
  1408. ############# CONFIG SECTION ##############
  1409. ### the real config section is supposed to be here...
  1410. if {[info exists configs_file]} {
  1411. if {[file isfile "$configs_file"] == 1} {
  1412. source $configs_file
  1413. putlog "::cims:: configs file '$configs_file' loaded."
  1414. }
  1415. }
  1416. ############# CONFIG SHOULD NOW BE LOADED ONTO THE MANY VARIABLES ##############
  1417. # pls hands away again: starting up just defined configs ...
  1418. putlog "::cims:: = Messaging Network ($mnet(version)) loaded"
  1419. putlog "::cims:: = as master, reload current net-configs with .cims_reload"
  1420. putlog "::cims:: = do .rehash and .cims_reload if you defined a new net-config."
  1421. if {$mnet_cfg_reload_interval >= 90} {
  1422. if {![info exists mnet_netconfig_timer]} {
  1423. set mnet_configtimer_set 1
  1424. ::cims::interval_conf
  1425. # this one loops
  1426. }
  1427. } else {
  1428. if {[info exists mnet_netconfig_timer]} {
  1429. set mnet_configtimer_set 0
  1430. killtimer $mnet_netconfig_timer
  1431. unset mnet_netconfig_timer
  1432. }
  1433. set mnet_cfg_reload_interval 0
  1434. ::cims::clean_netconfig
  1435. ::cims::netconfigload
  1436. }
  1437. }