-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdispatcher.py
70 lines (59 loc) · 2.46 KB
/
dispatcher.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
# coding: utf8
from __future__ import unicode_literals
import Queue
import message_parsing
import mythread
from IRC import Bot
class Dispatcher(mythread.Thread):
"""
Dispatcher of Message class.
It will take msg from is queue and dispatch them to the correct elems if the Message match the pattern
"""
def __init__(self):
mythread.Thread.__init__(self)
self.queue = Queue.Queue()
self.elems = []
def append(self, queue, message_form):
"""
add of new queue to dispatch a Message
:param queue: queue to store Message if they match message_form
:param message_form: pattern to match to put the Message to the queue
:return: Nothing what did you expect
"""
if isinstance(queue, Bot):
queue = queue.queue
self.elems.append((message_form, queue))
def main(self):
"""
Main loop of the boot once it is start
:return: Nothing what did you expect
"""
message = self.queue.get()
print ("[D] received {}".format(message))
if isinstance(message, message_parsing.Message):
for el in self.elems:
param = el[0]
if param.pseudo is None or param.pseudo == message.pseudo:
if param.user_account is None or param.user_account == message.user_account:
if param.ip is None or param.ip == message.ip:
if param.msg_type is None or param.msg_type == message.msg_type:
if param.content is None or param.content in message.content:
if param.target is None or param.target == message.target:
if param.server is None or param.server == "NOT_IMPLEMENTED_YET" or param.server == message.server:
# print "[D] good Message"
el[1].put(message)
def remove(self, target):
"""
remove a queue from elements
:param target: the pattern to remove
:return: Nothing what did you expect
"""
if isinstance(target , message_parsing.Message):
for el in self.elems:
if target == el[0]:
self.elems.remove(el)
break
else:
for el in self.elems:
if target == el[1]:
self.elems.remove(el)