This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxmpp_component.py
65 lines (51 loc) · 1.75 KB
/
xmpp_component.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
#!/usr/bin/env python
import sys
import logging
import getpass
from optparse import OptionParser
import requests
import json
import ConfigParser
import sleekxmpp
config = ConfigParser.ConfigParser()
config.read('/etc/mxbridge.conf')
MXAPI = config.get("Matrix", "as_api_url")
MXROOM = config.get("Matrix", "room_id")
class BridgeBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, room, nick):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.room = room
self.nick = nick
self.add_event_handler("session_start", self.start)
self.add_event_handler("groupchat_message", self.muc_message)
def start(self, event):
self.get_roster()
self.send_presence()
self.plugin['xep_0045'].joinMUC(self.room,
self.nick,
wait=True)
def muc_message(self, msg):
if(msg['mucnick'] != self.nick):
data = {"from": "test", "to": MXROOM, "body": msg['mucnick'] + ": " + msg['body']}
requests.post(MXAPI + "/mxbridge/send", data=json.dumps(data), headers={"Content-Type": "application/json"})
#if sys.version_info < (3, 0):
#reload(sys)
#sys.setdefaultencoding('utf8')
if(__name__ == '__main__'):
jid = config.get("XMPP", "username")
room = config.get("XMPP", "muc_room")
nick = config.get("XMPP", "nick")
try:
password = config.get("XMPP", "password")
except ConfigParser.NoOptionError:
password = getpass.getpass("Password: ")
xmpp = BridgeBot(jid, password, room, nick)
xmpp.register_plugin('xep_0045')
if xmpp.connect():
try:
xmpp.process(block=True)
except TypeError:
xmpp.process(threaded=False) # Used for older versions of SleekXMPP
print("Done")
else:
print("Unable to connect.")