cims_admin.tcl 13 KB

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