cims_admin.tcl 12 KB

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