]> jfr.im git - irc/rizon/acid.git/blob - pyva/pyva/src/main/python/limitserv/limitserv.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / limitserv / limitserv.py
1 #!/usr/bin/python pseudoserver.py
2 #psm-limitserv.py
3 # module for pypseudoserver
4 # written by celestin - martin <martin@rizon.net>
5
6 import sys
7 import types
8 from istring import istring
9 from pseudoclient import sys_log, sys_options, sys_channels, inviteable
10 from utils import *
11
12 from pyva import *
13 import logging
14 from core import *
15 from plugin import *
16
17 import cmd_admin, sys_auth, limitmanager
18
19 class limitserv(
20 AcidPlugin,
21 inviteable.InviteablePseudoclient
22 ):
23 initialized = False
24
25 def start_threads(self):
26 self.options.start()
27 self.channels.start()
28 self.auth.start()
29 self.limit_monitor.start()
30
31 def bind_function(self, function):
32 func = types.MethodType(function, self, limitserv)
33 setattr(limitserv, function.__name__, func)
34 return func
35
36 def bind_admin_commands(self):
37 list = cmd_admin.get_commands()
38 self.commands_admin = []
39
40 for command in list:
41 self.commands_admin.append((command, {'permission': 'j', 'callback': self.bind_function(list[command][0]),
42 'usage': list[command][1]}))
43
44 def __init__(self):
45 AcidPlugin.__init__(self)
46
47 self.name = "limitserv"
48 self.log = logging.getLogger(__name__)
49
50 try:
51 self.nick = istring(self.config.get('limitserv', 'nick'))
52 except Exception, err:
53 self.log.exception("Error reading 'limitserv:nick' configuration option: %s" % err)
54 raise
55
56 try:
57 self.chan = istring(self.config.get('limitserv', 'channel'))
58 except Exception, err:
59 self.log.exception("Error reading 'limitserv:channel' configuration option: %s" % err)
60 raise
61
62 self.bind_admin_commands()
63
64 def start(self):
65 try:
66 self.dbp.execute("CREATE TABLE IF NOT EXISTS limitserv_chans (item SMALLINT(5) NOT NULL AUTO_INCREMENT, channel VARCHAR(35), \
67 PRIMARY KEY (item), UNIQUE KEY (channel));")
68 except Exception, err:
69 self.log.exception("Error creating table for limitserv module (%s)" % err)
70 raise
71
72 try:
73 AcidPlugin.start(self)
74 inviteable.InviteablePseudoclient.start(self)
75
76 self.options = sys_options.OptionManager(self)
77 self.elog = sys_log.LogManager(self)
78 self.auth = sys_auth.LimitServAuthManager(self)
79 self.channels = sys_channels.ChannelManager(self)
80 self.limit_monitor = limitmanager.LimitManager(self)
81 except Exception, err:
82 self.log.exception('Error initializing subsystems for limitserv module (%s)' % err)
83 raise
84
85 for channel in self.channels.list_valid():
86 self.join(channel.name)
87
88 self.elog.debug('Joined channels.')
89
90 try:
91 self.start_threads()
92 except Exception, err:
93 self.log.exception('Error starting threads for limitserv module (%s)' % err)
94 raise
95
96 self.initialized = True
97 self.elog.debug('Started threads.')
98 return True
99
100 def onSync(self):
101 for channel in self.channels.list_valid():
102 self.join(channel.name)
103
104 self.log.debug('Joined channels.')
105
106 def stop(self):
107 if hasattr(self, 'auth'):
108 self.auth.stop()
109
110 if hasattr(self, 'channels'):
111 if self.initialized:
112 self.channels.force()
113
114 self.channels.stop()
115 self.channels.db_close()
116
117 if hasattr(self, 'options'):
118 if self.initialized:
119 self.options.force()
120
121 self.options.stop()
122 self.options.db_close()
123
124 if hasattr(self, 'limit_monitor'):
125 self.limit_monitor.stop()
126
127 def join(self, channel):
128 me = self.inter.findUser(self.nick)
129 me.joinChan(channel)
130 self.msg('ChanServ', 'OP %s' % channel) # and the channel is not empty. For now, use /cs op
131
132 def part(self, channel):
133 me = self.inter.findUser(self.nick)
134 me.partChan(channel)
135
136 def msg(self, target, message):
137 if message != '':
138 self.inter.privmsg(self.nick, target, format_ascii_irc(message))
139
140 def multimsg(self, target, count, intro, separator, pieces, outro = ''):
141 cur = 0
142
143 while cur < len(pieces):
144 self.msg(target, intro + separator.join(pieces[cur:cur + count]) + outro)
145 cur += count
146
147 def notice(self, target, message):
148 if message != '':
149 self.inter.notice(self.nick, target, format_ascii_irc(message))
150
151 def onPrivmsg(self, source, target, message):
152 if inviteable.InviteablePseudoclient.onPrivmsg(self, source, target, message) and \
153 target == self.nick:
154 # if inviteable fell through, we don't have anything else to do
155 self.msg(source, 'Invalid message. Say help for a list of valid messages.')
156
157 def do_accept(self, nick, channel):
158 chan = self.inter.findChannel(channel)
159 if chan.size() < self.options.get('required_users', int, 20):
160 self.auth.reject_not_enough_users(nick, channel)
161 else:
162 # super call
163 inviteable.InviteablePseudoclient.do_accept(self, nick, channel)
164
165 def onChanModes(self, prefix, channel, modes):
166 if not self.initialized:
167 return
168
169 if not modes == '-z':
170 return
171
172 if channel in self.channels:
173 self.channels.remove(channel)
174 self.elog.request('Channel @b%s@b was dropped. Deleting it.' % channel)
175
176 def onJoin(self, user, channel):
177 if self.channels.is_valid(channel) and channel not in self.limit_monitor:
178 self.limit_monitor.insert(channel)
179
180 def onPart(self, user, channel):
181 self.onJoin(None, channel)
182
183 def onKick(self, kicker, victim, channel, reason):
184 self.onJoin(None, channel)
185
186 def getCommands(self):
187 return self.commands_admin