-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.py
240 lines (197 loc) · 7.67 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import functions
import re
class EventError(Exception): pass
class EventListener:
regex = re.compile("^on(?P<command>[a-zA-Z0-9]*$)")
def __init__(self, dispatcher):
self.dispatcher = dispatcher
self.register_mapping()
def register_mapping(self):
mapping = self.find_event_listeners()
for command, method in mapping.items():
self.dispatcher.listen( method, command )
def find_event_listeners(self):
mapping = {}
matches = [self.regex.match(prop)
for prop in dir(self) if self.regex.match(prop)]
for match in matches:
method = getattr(self, match.group())
if callable(method):
mapping[ match.group('command').lower() ] = method
return mapping
class RelevantEventListener(EventListener):
def register_mapping(self):
self._event_mapping = self.find_event_listeners()
def __call__(self, event):
if event.command in self._event_mapping:
self._event_mapping[ event.command ]( event )
class EventDispatcher:
def __init__(self):
self.listeners = {}
self.global_listeners = []
self.handles = {}
self.register_mapping( mapping )
def dispatch(self, irc, server, prefix, command, arguments):
if command.isdigit() and command in functions.numeric_events:
command = functions.numeric_events[ command ]
if command in self.handles:
klass = self.handles[command]
else:
klass = Event
event = klass(irc, server, prefix, command, arguments)
listeners = []
listeners.extend(self.global_listeners)
if command in self.listeners:
listeners.extend( self.listeners[command] )
event.handle( listeners )
def listen(self, callable, command=None):
if command:
if not command in self.listeners:
self.listeners[ command ] = []
self.listeners[ command ].append( callable )
else:
self.global_listeners.append( callable )
def handle(self, command, klass):
self.handles[command] = klass
def register_mapping(self, mapping):
for command in mapping:
self.handle( command, mapping[ command ] )
class Event:
def __init__(self, irc, server, prefix, command, arguments ):
self.irc = irc
self.server = server
self.prefix = prefix
self.command = command
self.arguments = arguments
self.init()
def handle(self, listeners):
for listener in listeners:
listener(self)
def init(self):
if self.irc.debug:
print self.prefix, self.command, self.arguments
class EntityEvent(Event):
def __init__(self, *arguments ):
self.entities = []
Event.__init__( self, *arguments )
def handle(self, listeners):
listeners.extend( self.entities )
Event.handle(self, listeners)
class EventPing(Event):
def init(self):
self.server.pong( *self.arguments )
class EventMessage(EntityEvent):
def init(self):
self.source = self.server.entity(self.prefix)
self.target = self.server.entity(self.arguments[0])
self.entities = [ self.source, self.target ]
action = False
message = functions.ctcp_dequote(self.arguments[1])[0]
if (type(message)==tuple):
message = message[1]
action = True
self.message = message
if self.target.type=='channel':
if action:
self.command = "pubaction"
else:
self.command = 'pubmsg'
elif action:
self.command = 'privaction'
def handle(self, listeners):
if not self.command == 'privmsg':
listeners = []
listeners.extend(self.irc.dispatcher.global_listeners)
if self.command in self.irc.dispatcher.listeners:
listeners.extend( self.irc.dispatcher.listeners[ self.command ] )
if 'message' in self.irc.dispatcher.listeners:
listeners.extend( self.irc.dispatcher.listeners['message'] )
EntityEvent.handle(self, listeners)
class EventNotice(EntityEvent):
def init(self):
self.source = self.server.entity(self.prefix)
self.target = self.server.entity(self.arguments[0])
self.entities = [self.source, self.target]
self.message = self.arguments[1]
if self.target.type=='channel':
self.command = 'pubnotice'
else:
self.command = 'privnotice'
def handle(self, listeners):
listeners = []
listeners.extend(self.irc.dispatcher.global_listeners)
if self.command in self.irc.dispatcher.listeners:
listeners.extend( self.irc.dispatcher.listeners[ command ] )
if 'notice' in self.irc.dispatcher.listeners:
listeners.extend( self.irc.dispatcher.listeners['message'] )
EntityEvent.handle(self, listeners)
class EventJoin(EntityEvent):
def init(self):
self.source = self.server.entity(self.prefix)
self.target = self.server.entity(self.arguments[0])
self.entities = [self.source, self.target]
class EventPart(EntityEvent):
def init(self):
self.source = self.server.entity( self.prefix )
self.target = self.server.entity( self.arguments[0] )
self.entities = [self.source, self.target]
class EventMotd(Event):
def init(self):
self.message = self.arguments[1]
class EventCurrentTopic(EntityEvent):
def init(self):
self.target = self.server.entity( self.arguments[1] )
self.message = self.arguments[2]
self.entities = [self.target]
class EventNamReply(EntityEvent):
regex = re.compile('^(?P<mode>[+%@!&~])?(?P<nickname>.*)')
def init(self):
self.source = self.server.entity( self.arguments[2] )
self.entities = [self.source]
self.arguments = self.arguments[3].split()
self.users = {}
for mode, nickname in [self.regex.match(name).groups() for name in self.arguments]:
self.users[nickname] = mode
class EventEndOf(EntityEvent):
def init(self):
self.source = self.server.entity( self.arguments[1] )
self.entities = [self.source]
class EventWhoReply(EntityEvent):
def init(self):
#arguments
# self channel username hostname server nickname mode? 0-realname
self.source = self.server.entity( self.arguments[1] )
self.nickname = self.arguments[5]
self.username = self.arguments[2]
self.hostname = self.arguments[3]
self.target = self.server.entity( "%s!%s@%s" % (self.nickname, self.username, self.hostname ))
self.entities = [self.source, self.target]
class EventQuit(EntityEvent):
def init(self):
self.source = self.server.entity( self.prefix )
self.entities = [self.source]
if self.source.type=='user' and self.source.channels:
self.entities.extend(self.source.channels)
self.message = self.arguments[0]
class EventNick(EntityEvent):
def init(self):
self.source = self.server.entity( self.prefix )
self.entities = [ self.source ]
self.target = self.arguments[0]
mapping = {
'ping' : EventPing,
'privmsg' : EventMessage,
'join' : EventJoin,
'nick' : EventNick,
'part' : EventPart,
'quit' : EventQuit,
'notice' : EventNotice,
'motd' : EventMotd,
'motdstart' : EventMotd,
'endofmotd' : EventEndOf,
'currenttopic' : EventCurrentTopic,
'namreply' : EventNamReply,
'endofnames': EventEndOf,
'endofwho' : EventEndOf,
'whoreply' : EventWhoReply,
}