forked from m13253/ircbindxmpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmppbindxmpp.py
executable file
·91 lines (79 loc) · 2.63 KB
/
xmppbindxmpp.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
#!/usr/bin/env python
import os
import re
import sleekxmpp
import socket
import sys
import time
import config
def FilterBadChars(s):
s = re.sub('\x03[0-9]{1,2}(,[0-9]{1,2})?|[\x02\x03\x0f\x16\x1d\x1f]', '', s)
return re.sub('[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f]', '\ufffd', s)
class XMPPBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler("message", self.message)
def start(self, event):
self.send_presence()
self.get_roster()
def message(self, msg):
try:
if msg['type'] not in ('chat', 'normal'):
return
if " (IRC): " in msg["body"]:
return
from_jid = msg['from'].bare
for i in config.XMPP['forward']:
if i[0] == from_jid:
for l in msg['body'].splitlines():
sys.stderr.write('< %s\n' % l)
xmpp.send_message(mto=i[1], mbody=msg, mtype='chat')
except UnicodeEncodeError:
pass
except socket.error:
try:
self.disconnect(wait=True)
except:
pass
time.sleep(10)
sys.stderr.write("Restarting...\n")
try:
os.execlp("python3", "python3", __file__)
except:
os.execlp("python", "python", __file__)
except Exception as e:
sys.stderr.write('Exception: %s\n' % e)
if __name__ == '__main__':
try:
xmpp = XMPPBot(config.XMPP['JID'], config.XMPP['password'])
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
if xmpp.connect((config.XMPP['server'], config.XMPP['port'])):
xmpp.process(block=False)
else:
exit()
else:
raise socket.error
except KeyboardInterrupt:
xmpp.disconnect(wait=True)
except UnicodeEncodeError:
pass
except SystemExit:
raise
except socket.error:
try:
xmpp.disconnect(wait=True)
except:
pass
time.sleep(10)
sys.stderr.write("Restarting...\n")
try:
os.execlp("python3", "python3", __file__)
except:
os.execlp("python", "python", __file__)
except Exception as e:
sys.stderr.write('Exception: %s\n' % e)
# vim: et ft=python sts=4 sw=4 ts=4