]> jfr.im git - irc/rizon/acid.git/blob - pyva/pyva/src/main/python/pseudoclient/inviteable.py
pyva: De-duplicate join/part code across modules
[irc/rizon/acid.git] / pyva / pyva / src / main / python / pseudoclient / inviteable.py
1 #
2 # inviteable.py
3 # Copyright (c) 2014 Kristina Brooks
4 #
5 #
6 #
7
8 from pseudoclient.cmd_manager import *
9 import mythreading as threading
10 from datetime import datetime
11 from utils import strip_ascii_irc
12 from core import anope_major
13
14 import pyva_net_rizon_acid_core_User as User
15 import pyva_net_rizon_acid_core_Channel as Channel
16
17 #---------------------------------------------------------------------#
18 # this is terrible
19
20 def private_channel_request(self, manager, opts, arg, channel, sender, userinfo):
21 if self.channels.is_valid(arg):
22 self.notice(channel, "I'm already in @b%s@b." % arg)
23 elif self.channels.is_banned(arg):
24 chan = self.channels[arg]
25 self.elog.request('Request for banned channel @b%s@b from @b%s@b.' % (arg, sender))
26 message = 'Request failed, channel @b%s@b was banned by @b%s@b.' % (arg, chan.ban_source)
27
28 if chan.ban_reason != None:
29 message += ' Reason: @b%s@b.' % chan.ban_reason
30
31 if chan.ban_expiry != None:
32 message += ' Expires: @b%s@b.' % datetime.fromtimestamp(chan.ban_expiry)
33
34 self.notice(channel, message)
35 # self.notice(channel, 'Please email @c3????@rizon.net@o to appeal.')
36 else:
37 self.auth.request(sender, arg, 'request')
38
39 def private_channel_remove(self, manager, opts, arg, channel, sender, userinfo):
40 if not self.channels.is_valid(arg):
41 self.notice(channel, "I'm not in @b%s@b." % arg)
42 else:
43 self.auth.request(sender, arg, 'remove')
44
45 def private_help(self, manager, opts, arg, channel, sender, userinfo):
46 command = arg.lower()
47
48 if command == '':
49 message = manager.get_help()
50 else:
51 message = manager.get_help(command)
52
53 if message == None:
54 message = ['%s is not a valid command.' % arg]
55
56 for line in message:
57 self.notice(channel, line)
58
59 class PrivateCommandManager(CommandManager):
60 def get_prefix(self):
61 return ''
62
63 def get_invalid(self):
64 return 'Invalid message. Say help for a list of valid messages.'
65
66 def get_commands(self):
67 return {
68 'request': (private_channel_request, ARG_YES|ARG_OFFLINE, 'requests a channel (must be founder)', [], '#channel'),
69 'remove': (private_channel_remove, ARG_YES|ARG_OFFLINE, 'removes a channel (must be founder)', [], '#channel'),
70 'hi': 'help',
71 'hello': 'help',
72 'help': (private_help, ARG_OPT, 'displays help text', []),
73 }
74
75 #---------------------------------------------------------------------#
76
77 class InviteablePseudoclient(object):
78 def __init__(self):
79 pass
80
81 def start(self):
82 self.commands_private = PrivateCommandManager()
83
84 # override this if you want to add additional crap
85 # (ie check if the chan is eligible or not)
86 def do_accept(self, nick, channel):
87 self.auth.accept(nick)
88
89 def join(self, channel):
90 me = User.findUser(self.nick)
91 me.joinChan(channel)
92
93 def part(self, channel):
94 me = User.findUser(self.nick)
95 chan = Channel.findChannel(channel)
96 if chan:
97 me.partChan(chan)
98
99 def onNotice(self, source, target, message):
100 if not self.initialized:
101 return
102
103 me = User.findUser(self.nick)
104 user = User.findUser(target)
105 userinfo = User.findUser(source)
106
107 if me != user or (userinfo != None and userinfo['nick'] != 'ChanServ'):
108 return
109
110 try:
111 msg = message.strip()
112 except:
113 return
114
115 if msg.startswith('[#'): #It's a channel welcome message. Let's ignore it.
116 return
117
118 self.elog.chanserv('%s' % msg)
119 sp = msg.split(' ')
120
121 if userinfo == None:
122 if 'tried to kick you from' in msg:
123 nick = strip_ascii_irc(sp[1])
124 channel = strip_ascii_irc(sp[7])
125 self.notice(nick, 'To remove this bot (must be channel founder): @b/msg %s remove %s@b' % (self.nick, channel))
126
127 return
128
129 if anope_major == 2 and msg == 'Password authentication required for that command.':
130 self.elog.error('%s is not authed with services (maybe a wrong NickServ password was configured?).' % target)
131
132 # common to both anope1 and anope2
133 if "isn't registered" in msg:
134 self.auth.reject_not_registered(strip_ascii_irc(sp[1]))
135 return
136
137 if len(sp) < 6:
138 return
139
140 if 'inviting' in sp[2]: #It's an invite notice. Let's ignore it.
141 return
142
143 nick = strip_ascii_irc(sp[0])
144 channel = sp[5][:-1]
145
146 if anope_major == 1:
147 should_accept = 'Founder' in sp[2]
148 elif anope_major == 2:
149 channel = strip_ascii_irc(channel)
150 should_accept = ('founder' == sp[3])
151
152 if should_accept:
153 self.do_accept(nick, channel)
154 else:
155 self.auth.reject_not_founder(nick, channel)
156
157 def onPrivmsg(self, source, target, message):
158 if not self.initialized:
159 return False
160
161 userinfo = User.findUser(source)
162 myself = User.findUser(self.nick)
163
164 sender = userinfo['nick']
165 channel = target
166 msg = message.strip()
167 index = msg.find(' ')
168
169 if index == -1:
170 command = msg
171 argument = ''
172 else:
173 command = msg[:index]
174 argument = msg[index + 1:]
175
176 # do we have a deferal command handler?
177 if hasattr(self, 'execute'):
178 if channel == myself['nick'] and command.startswith(self.commands_private.prefix): # XXX uid
179 self.elog.debug('Deferred parsing of private message (sender: @b%s@b, command: @b%s@b, argument: @b%s@b)' % (sender, command, argument))
180 threading.deferToThread(self.execute, self.commands_private, command, argument, sender, sender, userinfo)
181 elif self.channels.is_valid(channel) and command.startswith(self.commands_user.prefix):
182 self.elog.debug('Deferred parsing of channel message (sender: @b%s@b, channel: @b%s@b, command: @b%s@b, argument: @b%s@b)' % (sender, channel, command, argument))
183 threading.deferToThread(self.execute, self.commands_user, command, argument, channel, sender, userinfo)
184 elif target == self.nick:
185 cmd = self.commands_private.get_command(command)
186 if cmd == None:
187 # not found, forward through
188 return True
189 # (self, manager, opts, arg, channel, sender, userinfo):
190 cmd[0](self, self.commands_private, command, argument, sender, sender, userinfo)
191 else:
192 # forward anyway
193 return True