-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhub.py
97 lines (74 loc) · 2.94 KB
/
hub.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
import ssl
from sleekxmpp import ClientXMPP
import logging
logger = logging.getLogger(__name__)
# SleekXMPP is stupid and forces PROTOCOL_TLSv1, ignoring
# that proper control of the TLS protocol version is done
# by using SSLv23 and then selecting which to use.
#
# Since the OS guards us against using SSLv2/3 or TLS < 1.2
# we monkey patch here.
ssl.PROTOCOL_TLSv1 = ssl.PROTOCOL_SSLv23
class HubBot(ClientXMPP):
HUB = "hub.sotecware.net"
SWITCH = "switch.hub.sotecware.net"
FEED = "feed.hub.sotecware.net"
def __init__(self, localpart, resource, password):
jid = "{0}@{1}".format(localpart, self.HUB)
if resource:
jid += "/" + resource
super().__init__(jid, password)
self.register_plugin("xep_0004") # dataforms
self.register_plugin("xep_0045") # muc
self.register_plugin("xep_0060") # pubsub -- let the fun begin
self.add_event_handler("session_start", self.sessionStart)
self.add_event_handler("session_end", self.sessionEnd)
self.add_event_handler("groupchat_message", self.messageMUC)
self.add_event_handler("message", self.message)
self.muc = None
self.pubsub = None
self.dataforms = self.plugin["xep_0004"]
self._switchHandlers = {}
self._switches = []
def _getSwitchJID(self, switch):
return "{0}@{1}".format(switch, self.SWITCH)
def _joinSwitch(self, switchTuple, wait=False):
logger.debug("joining %s as %s", *switchTuple)
self.muc.joinMUC(*switchTuple, wait=wait)
def recieved_roster(self, roster):
pass
def sessionStart(self, event):
self.send_presence()
roster = self.get_roster(block=True)
self.recieved_roster(roster)
self.muc = self.plugin["xep_0045"]
self.pubsub = self.plugin["xep_0060"]
for switchTuple in self._switches:
self._joinSwitch(switchTuple)
def sessionEnd(self, event):
pass
def addSwitch(self, switch, nick, handler=None):
if handler is not None:
self.addSwitchHandler(switch, handler)
switchTuple = (self._getSwitchJID(switch), nick)
self._switches.append(switchTuple)
if self.muc is not None:
self._joinSwitch(switchTuple)
return switchTuple
def addSwitchHandler(self, room, handler):
self._switchHandlers.setdefault(self._getSwitchJID(room), []).append(handler)
def messageMUC(self, msg):
muc = str(msg["from"].bare)
handlers = self._switchHandlers.get(muc, [])
for handler in handlers:
handler(msg)
def message(self, msg):
pass
def reply(self, msg, body):
if msg["type"] == "groupchat":
self.send_message(mtype="groupchat", mto=msg["from"].bare, mbody=body)
else:
self.send_message(mto=msg["from"], mbody=body, mtype="chat")
def run(self):
self.connect()
self.process(block=True)