]> jfr.im git - irc/rizon/acid.git/blame - pyva/pyva/src/main/python/erepublik/cmd_admin.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / erepublik / cmd_admin.py
CommitLineData
685e346e
A
1from pseudoclient import sys_base
2
3from datetime import datetime
4from utils import *
5
6#---------------------------------------------------------------------#
7
8from pseudoclient import cmd_admin
9from pseudoclient.cmd_admin import \
10 admin_unregistered, \
11 admin_log, \
12 admin_msg, \
13 admin_opt, \
14 admin_db
15
16#---------------------------------------------------------------------#
17
18def admin_chan(self, source, target, pieces):
19 def subcmd_news(self, action, channel, source, target, pieces):
20 if channel in self.channels:
21 cur_state = self.channels[channel].news
22 self.channels.set(channel, 'news', not cur_state)
23 self.msg(target, 'Toggled news for channel @b%s@b. Current state: %s.' % (channel, 'disabled' if cur_state else 'enabled'))
24 else:
25 self.msg(target, 'Channel @b%s@b is not in the database.' % channel)
26
27 def subcmd_mass(self, action, channel, source, target, pieces):
28 if channel in self.channels:
29 cur_state = self.channels[channel].mass
30 self.channels.set(channel, 'mass', not cur_state)
31 self.msg(target, 'Toggled mass for channel @b%s@b. Current state: %s.' % (channel, 'disabled' if cur_state else 'enabled'))
32 else:
33 self.msg(target, 'Channel @b%s@b is not in the database.' % channel)
34
35 return cmd_admin.admin_chan(self, source, target, pieces, meta={
36 'cmds':{
37 'n': subcmd_news,
38 'news': subcmd_news,
39 'm': subcmd_mass,
40 'mass': subcmd_mass,
41 },
42 'extra_info': lambda chan: ' News: @b%s@b. Mass: @b%s@b.' \
43 % ('enabled' if chan.news else 'disabled', 'enabled' if chan.mass else 'disabled')
44 })
45
46#---------------------------------------------------------------------#
47
48def admin_user(self, source, target, pieces):
49 def extra_info(user):
50 # i am too tired to think about how to write this as a lambda
51 m = ''
52 if user.citizen != None:
53 m += ' Citizen: @b%s@b.' % user.citizen
54 if user.company != None:
55 m += ' Company: @b%s@b.' % user.company
56 return m + ' Mass: ' + ('enabled.' if user.mass else 'disabled.')
57
58 def subcmd_mass(self, action, username, source, target, pieces):
59 if username in self.users:
60 cur_state = self.users[username].mass
61 self.users.set(username, 'mass', not cur_state)
62 self.msg(target, 'Toggled mass for user @b%s@b. Current state: %s.' % (username, 'disabled' if cur_state else 'enabled'))
63 else:
64 self.msg(target, 'User @b%s@b is not in the database.' % channel)
65
66 return cmd_admin.admin_user(self, source, target, pieces, meta={
67 'cmds':{
68 'm': subcmd_mass,
69 'mass': subcmd_mass,
70 },
71 'extra_info': extra_info
72 })
73
74#---------------------------------------------------------------------#
75
76def admin_sys(self, source, target, pieces):
77 if len(pieces) < 2:
78 return False
79
80 starget = pieces[0]
81 operation = pieces[1]
82 names = []
83 subsystems = []
84
85 if 'o' in starget:
86 names.append('options')
87 subsystems.append(self.options)
88
89 if 'u' in starget:
90 names.append('users')
91 subsystems.append(self.users)
92
93 if 'c' in starget:
94 names.append('channels')
95 subsystems.append(self.channels)
96
97 if 'n' in starget:
98 names.append('news')
99 subsystems.append(self.news)
100
101 if 'a' in starget:
102 names.append('auth')
103 subsystems.append(self.auth)
104
105 if 'f' in starget:
106 names.append('antiflood')
107 subsystems.append(self.antiflood)
108
109 if len(names) == 0:
110 return False
111
112 if operation in ['u', 'update']:
113 for subsystem in subsystems:
114 subsystem.force()
115
116 self.msg(target, 'Forced update for @b%s@b.' % '@b, @b'.join(names))
117 elif operation in ['r', 'reload']:
118 for subsystem in subsystems:
119 subsystem.reload()
120
121 self.msg(target, 'Forced reload for @b%s@b.' % '@b, @b'.join(names))
122 elif operation in ['d', 'delay']:
123 if len(pieces) == 2:
124 for subsystem in subsystems:
125 self.msg(target, 'Auto-update delay for @b%s@b is %d seconds.' % (subsystem.name, subsystem.delay))
126 else:
127 try:
128 seconds = int(pieces[2])
129 except:
130 return False
131
132 if seconds < 10:
133 self.msg(target, 'Auto-update delay must be greater than 10 seconds.')
134 return True
135
136 for subsystem in subsystems:
137 subsystem.set_option('update_period', seconds)
138 subsystem.reload()
139
140 self.msg(target, 'Auto-update delay for @b%s@b set to @b%d@b seconds.' % ('@b, @b'.join(names), seconds))
141 else:
142 return False
143
144 return True
145
146def admin_status(self, source, target, pieces):
147 if len(pieces) < 1:
148 pass
149 elif pieces[0] == 'on':
150 self.online = True
151 self.offline_msg = None
152 elif pieces[0] == 'off':
153 self.online = False
154 self.offline_msg = ' '.join(pieces[1:]) if len(pieces) > 1 else None
155 else:
156 return False
157
158 self.msg(target, 'Module status: @b%s@b' % ('online' if self.online else 'offline'))
159 return True
160
161def admin_stats(self, source, target, pieces):
162 self.msg(target, 'Registered users: @b%d@b.' % len(self.users.list_all()))
163 self.msg(target, 'Registered channels: @b%d@b.' % len(self.channels.list_all()))
164 return True
165
166def get_commands():
167 return {
168 'chan' : (admin_chan, '<ban|unban|info|add|remove|list|blist|news|mass> <channel> [reason]'),
169 'unreg' : (admin_unregistered, '<check|list|part> - remove unregistered channels'),
170 'user' : (admin_user, '<ban|unban|info|add|remove|list|blist|mass> <user> [reason]'),
171 'stats' : (admin_stats, 'counts registered users and channels'),
172 'db' : (admin_db, '[on|off] - enables/disables auto commits to db'),
173 'opt' : (admin_opt, '[get|set|clear] [option] [value] - manipulates options (list all if no arguments)'),
174 'sys' : (admin_sys, '<subsystem> <operation> [value] - (subsystems: options (o), users (u), channels (c), news (n), auth (a), antiflood (f)) (operations: update (u), reload (r), delay (d))'),
175 'log' : (admin_log, '[level] - gets or sets the log level (0-7).'),
176 'msg' : (admin_msg, '<message> - sends a message to all channels'),
177 'status' : (admin_status, '<on/off> - disables/enables API commands (use when API is down)'),
178 }
179