cims_admin.tcl 13 KB

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