cims_admin.tcl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. # CIMS: Community IRC Messaging Service
  2. # CIMS formerly known as MNET (Message Network)
  3. # Administration 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::admin {
  21. variable admin_channels {"#messagenet" "#quad.dev" "#qwnet"}
  22. variable mnet_admin_version "cims!A!1.14namespaced+savesettings"
  23. # include my/our little eggdrop utilities and helper library. ;-) Putils!
  24. if {[file isfile "scripts/cims/putils.tcl"] == 1} {
  25. source "scripts/cims/putils.tcl"
  26. }
  27. }
  28. # allowcheck for admin commands..
  29. proc ::cims::admin::allowed_channel {nick mask hand chan admin_channels} {
  30. global botnick botnet-nick lastbind
  31. putlog "mnet! = an admin command triggered triggered by $nick ($mask) in $chan"
  32. # the command must be from a channel of "ownchannels"
  33. putlog "mnet! = trying if $chan is an admin-channel"
  34. set found 0
  35. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  36. foreach _try_chan $admin_channels {
  37. if {[string compare -nocase ${chan} ${_try_chan}] == 0} {
  38. putlog "mnet! = channel: ${chan} is allowed to administrate cims on this bot"
  39. set found 1
  40. } else {
  41. # putlog "mnet: = not in list: $chan"
  42. }
  43. }
  44. if {$found == 0} {
  45. putlog "mnet! = administration on ${chan} on this bot is not allowed"
  46. return 0
  47. }
  48. if {![isop ${nick} ${chan}]} {
  49. putlog "mnet! = allowed in ${chan}, but $nick is no op"
  50. ::putils::put_nick ${nick} "Sorry $nick, you are not an administrator here."
  51. return 0
  52. }
  53. if {${nick} == ${botnick}} {
  54. # no recursion :)
  55. return 0
  56. }
  57. # got through all checks .. so it's an OK channel
  58. return 1
  59. }
  60. proc ::cims::admin::check_addchannel {nick channel} {
  61. if {[botonchan ${channel}] == 1} {
  62. ::putils::put_nick ${nick} "Mnet! I added myself to the channel ${channel}"
  63. putlog "Mnet! I added myself to the channel ${channel}"
  64. } else {
  65. ::putils::put_nick ${nick} "Mnet! I tried to add myself to the channel ${value} - but failed! Pls check."
  66. putlog "Mnet! I tried to add myself to the channel ${value} - but failed! Pls check."
  67. }
  68. }
  69. proc ::cims::admin::rehashme {nick mask hand chan text} {
  70. variable admin_channels
  71. global botnet-nick
  72. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  73. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  74. if {$allowed == 0} {
  75. return 0
  76. }
  77. putlog "test"
  78. # rehash the bot... be sure the scripts are coded correctly!! or the bot will DIE!
  79. rehash
  80. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" has rehashed and thus reloaded the mnet.tcl"
  81. }
  82. proc ::cims::admin::version {nick mask hand chan text} {
  83. variable admin_channels
  84. variable mnet_admin_version
  85. global botnet-nick
  86. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  87. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  88. if {$allowed == 0} {
  89. return 0
  90. }
  91. # tells version
  92. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" Admin: $mnet_admin_version and Mnet is: $::cims::mnet(version)"
  93. }
  94. proc ::cims::admin::channelinfo {nick mask hand chan text} {
  95. variable admin_channels
  96. global botnet-nick
  97. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  98. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  99. if {$allowed == 0} {
  100. return 0
  101. }
  102. # tell channel infos
  103. set chan_list [channels]
  104. set chan_count [llength $chan_list]
  105. set output ""
  106. foreach _try_chan ${chan_list} {
  107. set q_missing_s ""
  108. set q_missing_e ""
  109. set userlist [chanlist "${_try_chan}"]
  110. set usercount [llength ${userlist}]
  111. #::putils::put_nick ${nick} "${_try_chan}: ${userlist} is -${usercount}- in length"
  112. if {[lsearch -exact ${userlist} Q] < 0} {
  113. #Q missing
  114. set q_missing_s ",5\002"
  115. set q_missing_e "\002"
  116. }
  117. if {${usercount} == 0} {
  118. # no users! someone banned us!
  119. set output "${output}\00313${_try_chan} ${usercount}\003, "
  120. } elseif {${usercount} <= 3} {
  121. # toooo few users .. channel dead?
  122. set output "${output}\00304${q_missing_s}${_try_chan} ${usercount}\003${q_missing_e}, "
  123. } elseif {${usercount} <=5} {
  124. # puh .. a bit more..
  125. set output "${output}\00308${q_missing_s}${_try_chan} ${usercount}\003${q_missing_e}, "
  126. } else {
  127. # a healthy channel .. > 5 users
  128. set output "${output}\00303${q_missing_s}${_try_chan} ${usercount}\003${q_missing_e}, "
  129. }
  130. }
  131. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" has \002${chan_count}\002 channels: ${output}"
  132. }
  133. proc ::cims::admin::has_channelrecord {chan} {
  134. set channel_records [channels]
  135. foreach _try_chan $channel_records {
  136. if {${chan} == ${_try_chan}} {
  137. # found the channel in the records, go for it and return 1
  138. return 1
  139. } else {
  140. # putlog "mnet: = not in list: $chan"
  141. }
  142. }
  143. # not found till now? then return 0
  144. return 0
  145. }
  146. proc ::cims::admin::trylink_admin {} {
  147. global botnet-nick
  148. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  149. set botslist [userlist b]
  150. foreach _try_bot $botslist {
  151. if {[islinked ${_try_bot}] == 0} {
  152. link ${_try_bot}
  153. }
  154. }
  155. return 0
  156. }
  157. proc ::cims::admin::trylink {nick mask hand chan text} {
  158. variable admin_channels
  159. global botnet-nick
  160. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  161. set botslist [userlist b]
  162. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  163. if {$allowed == 0} {
  164. return 0
  165. }
  166. foreach _try_bot $botslist {
  167. if {[islinked ${_try_bot}] == 0} {
  168. set _try [link ${_try_bot}]
  169. if {$_try == 1} {
  170. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" tries to link to \"${_try_bot}\""
  171. utimer 5 "::cims::admin::trylink_test ${_try_bot} ${nick}"
  172. }
  173. } else {
  174. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00303already linked\003 to \"${_try_bot}\" - good."
  175. }
  176. }
  177. return 0
  178. }
  179. proc ::cims::admin::trylink_test {_try_bot nick} {
  180. variable admin_channels
  181. global botnet-nick
  182. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  183. if {[islinked ${_try_bot}] == 1} {
  184. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00309succeeded\003 to link to \"${_try_bot}\"! :)"
  185. } else {
  186. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00304FAILED\003 to link to \"${_try_bot}\"! :("
  187. }
  188. }
  189. proc ::cims::admin::savesettings_admin {} {
  190. global botnet-nick botnick
  191. namespace eval ::cims {
  192. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  193. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_freqs" [array get mnet_freqs_onoff]
  194. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_colors" [array get mnet_colors_onoff]
  195. }
  196. }
  197. proc ::cims::admin::savesettings {nick mask hand chan text} {
  198. variable admin_channels
  199. global botnet-nick botnick
  200. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  201. if {$allowed == 0} {
  202. return 0
  203. }
  204. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  205. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" saves settings."
  206. namespace eval ::cims {
  207. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  208. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_freqs" [array get mnet_freqs_onoff]
  209. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_colors" [array get mnet_colors_onoff]
  210. }
  211. }
  212. proc ::cims::admin::loadsettings_admin {} {
  213. global botnet-nick botnick
  214. namespace eval ::cims {
  215. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  216. array set mnet_freqs_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_freqs"]
  217. array set mnet_colors_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_colors"]
  218. }
  219. }
  220. proc ::cims::admin::loadsettings {nick mask hand chan text} {
  221. variable admin_channels
  222. global botnet-nick botnick
  223. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  224. if {$allowed == 0} {
  225. return 0
  226. }
  227. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  228. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" restores settings."
  229. namespace eval ::cims {
  230. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  231. array set mnet_freqs_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_freqs"]
  232. array set mnet_colors_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_colors"]
  233. }
  234. }
  235. proc ::cims::admin::reload {nick mask hand chan text} {
  236. variable admin_channels
  237. global botnet-nick
  238. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  239. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  240. if {$allowed == 0} {
  241. return 0
  242. }
  243. set cmd "dummy"
  244. # reload config... be sure this script works or the bot will DIE!
  245. putlog "mnet! = reloading configs by admin"
  246. ::cims::dcc_configload ${cmd} ${cmd} ${cmd}
  247. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" Configs reload triggered."
  248. }
  249. proc ::cims::admin::main {nick mask hand chan text} {
  250. variable admin_channels
  251. global botnet-nick
  252. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  253. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  254. if {$allowed == 0} {
  255. return 0
  256. }
  257. set tmp_cmd [lindex ${text} 0]
  258. set tmp_value [lindex ${text} 1]
  259. set cmd [string tolower [::putils::kill_spaces ${tmp_cmd}]]
  260. set value [string tolower [::putils::kill_spaces ${tmp_value}]]
  261. if {${cmd} == "add"} {
  262. # check $value ... if there
  263. if {${value} != ""} {
  264. # get channelrecord: ${value} if we don't have the record already:
  265. if {[::cims::admin::has_channelrecord ${value}] == 0} {
  266. channel add ${value}
  267. # save it
  268. savechannels
  269. # did it work?
  270. utimer 10 "::cims::admin::check_addchannel ${nick} ${value}"
  271. } else {
  272. ::putils::put_nick ${nick} "Mnet! Can't add again - I already have the channel ${value}"
  273. putlog "Mnet! Can't add again - I already have the channel ${value}"
  274. }
  275. } else {
  276. ::putils::put_nick ${nick} "Mnet! Can't add: no channel given"
  277. }
  278. return 0
  279. }
  280. if {${cmd} == "remove"} {
  281. # check $value ... if there
  282. if {${value} != ""} {
  283. # remove ${value} from bot, if we have a record for it.
  284. if {[::cims::admin::has_channelrecord ${value}] == 1} {
  285. channel remove ${value}
  286. savechannels
  287. ::putils::put_nick ${nick} "Mnet! I removed the channelrecord: ${value}"
  288. putlog "Mnet! I removed the channelrecord: ${value}"
  289. } else {
  290. ::putils::put_nick ${nick} "Mnet! Can't remove - I don't have that channelrecord: ${value}"
  291. putlog "Mnet! Can't remove - I don't have that channelrecord: ${value}"
  292. }
  293. } else {
  294. ::putils::put_nick ${nick} "Mnet! Can't remove: no channel given"
  295. }
  296. return 0
  297. }
  298. if {${cmd} == "reload"} {
  299. # reload network configs, pointing to procedure
  300. ::cims::admin::reload $nick $mask $hand $chan $text
  301. return 0
  302. }
  303. if {${cmd} == "channels"} {
  304. # tell channel infos, pointing to procedure
  305. ::cims::admin::channelinfo $nick $mask $hand $chan $text
  306. return 0
  307. }
  308. if {${cmd} == "rehash"} {
  309. # rehash
  310. ::cims::admin::rehashme $nick $mask $hand $chan $text
  311. return 0
  312. }
  313. if {${cmd} == "savesettings"} {
  314. # savesettings
  315. ::cims::admin::savesettings $nick $mask $hand $chan $text
  316. return 0
  317. }
  318. if {${cmd} == "loadsettings"} {
  319. # loadsettings
  320. ::cims::admin::loadsettings $nick $mask $hand $chan $text
  321. return 0
  322. }
  323. if {${cmd} == "version"} {
  324. # tells version
  325. ::cims::admin::version $nick $mask $hand $chan $text
  326. return 0
  327. }
  328. # none of the above worked .. so we are here:
  329. ::putils::put_nick ${nick} "mnet! possible commands are: add remove reload channels rehash version savesettings loadsettings"
  330. ::putils::put_nick ${nick} "mnet! use either \002!mnet_botnetnickname <command>\002 to admin a single bot or just \002!mnet_<command>\002 to admin ALL bots that listen to your words here."
  331. ::putils::put_nick ${nick} "add #channelname || bot joins a channel"
  332. ::putils::put_nick ${nick} "remove #channelname || bot parts a channel"
  333. ::putils::put_nick ${nick} "reload || reloads the mnet_configs"
  334. ::putils::put_nick ${nick} "channels || tells some channel-infos"
  335. ::putils::put_nick ${nick} "rehash || rehashes the bot, reloads all botscripts"
  336. ::putils::put_nick ${nick} "version || tells version"
  337. ::putils::put_nick ${nick} "savesettings || saves channel output settings to file"
  338. ::putils::put_nick ${nick} "loadsettings || restores channel output settings from file"
  339. }
  340. proc ::cims::admin::startup {} {
  341. variable mnet_admin_version
  342. global botnet-nick
  343. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  344. bind pub - !mnet_${proper_botnick} ::cims::admin::main
  345. bind pub - !mnet_reload ::cims::admin::reload
  346. bind pub - !mnet_channels ::cims::admin::channelinfo
  347. bind pub - !mnet_rehash ::cims::admin::rehashme
  348. bind pub - !mnet_version ::cims::admin::version
  349. bind pub - !mnet_trylink ::cims::admin::trylink
  350. bind pub - !mnet_savesettings ::cims::admin::savesettings
  351. bind pub - !mnet_loadsettings ::cims::admin::loadsettings
  352. ::cims::admin::loadsettings_admin
  353. utimer 10 "::cims::admin::trylink_admin"
  354. timer 1405 "::cims::admin::trylink_admin" 0
  355. timer 1205 "::cims::admin::savesettings_admin" 0
  356. putlog "mnet! = mnet adminscript loaded: $mnet_admin_version"
  357. }
  358. namespace eval ::cims::admin {
  359. # timer weil $botnet-nick nicht sofort von eggdrop gesetzt wird
  360. utimer 3 "::cims::admin::startup"
  361. }