cims_admin.tcl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 {nick mask hand chan text} {
  147. variable admin_channels
  148. global botnet-nick
  149. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  150. set botslist [userlist b]
  151. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  152. if {$allowed == 0} {
  153. return 0
  154. }
  155. foreach _try_bot $botslist {
  156. if {[islinked ${_try_bot}] == 0} {
  157. set _try [link ${_try_bot}]
  158. if {$_try == 1} {
  159. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" tries to link to \"${_try_bot}\""
  160. utimer 5 "::cims::admin::trylink_test ${_try_bot} ${nick}"
  161. }
  162. } else {
  163. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00303already linked\003 to \"${_try_bot}\" - good."
  164. }
  165. }
  166. return 0
  167. }
  168. proc ::cims::admin::trylink_test {_try_bot nick} {
  169. variable admin_channels
  170. global botnet-nick
  171. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  172. if {[islinked ${_try_bot}] == 1} {
  173. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00309succeeded\003 to link to \"${_try_bot}\"! :)"
  174. } else {
  175. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" \00304FAILED\003 to link to \"${_try_bot}\"! :("
  176. }
  177. }
  178. proc ::cims::admin::savesettings {nick mask hand chan text} {
  179. variable admin_channels
  180. global botnet-nick botnick
  181. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  182. if {$allowed == 0} {
  183. return 0
  184. }
  185. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  186. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" saves settings."
  187. namespace eval ::cims {
  188. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  189. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_freqs" [array get mnet_freqs_onoff]
  190. ::putils::write_f_array "scripts/cims/save/${proper_botnick}_colors" [array get mnet_colors_onoff]
  191. }
  192. }
  193. proc ::cims::admin::loadsettings {nick mask hand chan text} {
  194. variable admin_channels
  195. global botnet-nick botnick
  196. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  197. if {$allowed == 0} {
  198. return 0
  199. }
  200. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  201. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" restores settings."
  202. namespace eval ::cims {
  203. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  204. array set mnet_freqs_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_freqs"]
  205. array set mnet_colors_onoff [::putils::read_f_array "scripts/cims/save/${proper_botnick}_colors"]
  206. }
  207. }
  208. proc ::cims::admin::reload {nick mask hand chan text} {
  209. variable admin_channels
  210. global botnet-nick
  211. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  212. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  213. if {$allowed == 0} {
  214. return 0
  215. }
  216. set cmd "dummy"
  217. # reload config... be sure this script works or the bot will DIE!
  218. putlog "mnet! = reloading configs by admin"
  219. ::cims::dcc_configload ${cmd} ${cmd} ${cmd}
  220. ::putils::put_nick ${nick} "Mnet! \"${proper_botnick}\" Configs reload triggered."
  221. }
  222. proc ::cims::admin::main {nick mask hand chan text} {
  223. variable admin_channels
  224. global botnet-nick
  225. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  226. set allowed [::cims::admin::allowed_channel $nick $mask $hand $chan $admin_channels]
  227. if {$allowed == 0} {
  228. return 0
  229. }
  230. set tmp_cmd [lindex ${text} 0]
  231. set tmp_value [lindex ${text} 1]
  232. set cmd [string tolower [::putils::kill_spaces ${tmp_cmd}]]
  233. set value [string tolower [::putils::kill_spaces ${tmp_value}]]
  234. if {${cmd} == "add"} {
  235. # check $value ... if there
  236. if {${value} != ""} {
  237. # get channelrecord: ${value} if we don't have the record already:
  238. if {[::cims::admin::has_channelrecord ${value}] == 0} {
  239. channel add ${value}
  240. # save it
  241. savechannels
  242. # did it work?
  243. utimer 10 "::cims::admin::check_addchannel ${nick} ${value}"
  244. } else {
  245. ::putils::put_nick ${nick} "Mnet! Can't add again - I already have the channel ${value}"
  246. putlog "Mnet! Can't add again - I already have the channel ${value}"
  247. }
  248. } else {
  249. ::putils::put_nick ${nick} "Mnet! Can't add: no channel given"
  250. }
  251. return 0
  252. }
  253. if {${cmd} == "remove"} {
  254. # check $value ... if there
  255. if {${value} != ""} {
  256. # remove ${value} from bot, if we have a record for it.
  257. if {[::cims::admin::has_channelrecord ${value}] == 1} {
  258. channel remove ${value}
  259. savechannels
  260. ::putils::put_nick ${nick} "Mnet! I removed the channelrecord: ${value}"
  261. putlog "Mnet! I removed the channelrecord: ${value}"
  262. } else {
  263. ::putils::put_nick ${nick} "Mnet! Can't remove - I don't have that channelrecord: ${value}"
  264. putlog "Mnet! Can't remove - I don't have that channelrecord: ${value}"
  265. }
  266. } else {
  267. ::putils::put_nick ${nick} "Mnet! Can't remove: no channel given"
  268. }
  269. return 0
  270. }
  271. if {${cmd} == "reload"} {
  272. # reload network configs, pointing to procedure
  273. ::cims::admin::reload $nick $mask $hand $chan $text
  274. return 0
  275. }
  276. if {${cmd} == "channels"} {
  277. # tell channel infos, pointing to procedure
  278. ::cims::admin::channelinfo $nick $mask $hand $chan $text
  279. return 0
  280. }
  281. if {${cmd} == "rehash"} {
  282. # rehash
  283. ::cims::admin::rehashme $nick $mask $hand $chan $text
  284. return 0
  285. }
  286. if {${cmd} == "savesettings"} {
  287. # savesettings
  288. ::cims::admin::savesettings $nick $mask $hand $chan $text
  289. return 0
  290. }
  291. if {${cmd} == "loadsettings"} {
  292. # loadsettings
  293. ::cims::admin::loadsettings $nick $mask $hand $chan $text
  294. return 0
  295. }
  296. if {${cmd} == "version"} {
  297. # tells version
  298. ::cims::admin::version $nick $mask $hand $chan $text
  299. return 0
  300. }
  301. # none of the above worked .. so we are here:
  302. ::putils::put_nick ${nick} "mnet! possible commands are: add remove reload channels rehash version savesettings loadsettings"
  303. ::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."
  304. ::putils::put_nick ${nick} "add #channelname || bot joins a channel"
  305. ::putils::put_nick ${nick} "remove #channelname || bot parts a channel"
  306. ::putils::put_nick ${nick} "reload || reloads the mnet_configs"
  307. ::putils::put_nick ${nick} "channels || tells some channel-infos"
  308. ::putils::put_nick ${nick} "rehash || rehashes the bot, reloads all botscripts"
  309. ::putils::put_nick ${nick} "version || tells version"
  310. ::putils::put_nick ${nick} "savesettings || saves channel output settings to file"
  311. ::putils::put_nick ${nick} "loadsettings || restores channel output settings from file"
  312. }
  313. proc ::cims::admin::bindings {} {
  314. variable mnet_admin_version
  315. global botnet-nick
  316. set proper_botnick [::putils::proper_botnick ${botnet-nick}]
  317. bind pub - !mnet_${proper_botnick} ::cims::admin::main
  318. bind pub - !mnet_reload ::cims::admin::reload
  319. bind pub - !mnet_channels ::cims::admin::channelinfo
  320. bind pub - !mnet_rehash ::cims::admin::rehashme
  321. bind pub - !mnet_version ::cims::admin::version
  322. bind pub - !mnet_trylink ::cims::admin::trylink
  323. bind pub - !mnet_savesettings ::cims::admin::savesettings
  324. bind pub - !mnet_loadsettings ::cims::admin::loadsettings
  325. putlog "mnet! = mnet adminscript loaded: $mnet_admin_version"
  326. }
  327. namespace eval ::cims::admin {
  328. # timer weil $botnet-nick nicht sofort von eggdrop gesetzt wird
  329. utimer 3 "::cims::admin::bindings"
  330. }