Port from xmpppy to sleekxmpp
This commit is contained in:
parent
002fd865c4
commit
2c85e34daf
134
hybridbot.py
134
hybridbot.py
@ -4,7 +4,7 @@
|
|||||||
import sys
|
import sys
|
||||||
sys.path.append('lib')
|
sys.path.append('lib')
|
||||||
import time
|
import time
|
||||||
import xmpp
|
import sleekxmpp
|
||||||
from ircbot import SingleServerIRCBot
|
from ircbot import SingleServerIRCBot
|
||||||
from ircbot import Channel
|
from ircbot import Channel
|
||||||
from irclib import nm_to_n
|
from irclib import nm_to_n
|
||||||
@ -68,130 +68,102 @@ class IRCBot(SingleServerIRCBot):
|
|||||||
|
|
||||||
else:
|
else:
|
||||||
message = '[' + from_nick + '] ' + ''.join(e.arguments())
|
message = '[' + from_nick + '] ' + ''.join(e.arguments())
|
||||||
m = xmpp.protocol.Message(to=muc, body=message, typ='groupchat')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
inter.xmpp(m)
|
inter.xmpp(message)
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class XMPPBot:
|
class XMPPBot(sleekxmpp.ClientXMPP):
|
||||||
def __init__(self, jabber, remotejid, chanmuc):
|
def __init__(self, remotejid, chanmuc):
|
||||||
self.jabber = jabber
|
if sys.version_info < (3, 0):
|
||||||
|
sleekxmpp.util.misc_ops.setdefaultencoding('utf8')
|
||||||
|
|
||||||
|
self.jabber = sleekxmpp.ClientXMPP(jid, password)
|
||||||
self.remotejid = remotejid
|
self.remotejid = remotejid
|
||||||
self.chanmuc = chanmuc
|
self.chanmuc = chanmuc
|
||||||
|
|
||||||
def register_handlers(self):
|
def register_handlers(self):
|
||||||
self.jabber.RegisterHandler('message', self.xmpp_message)
|
self.jabber.add_event_handler('session_start', self.session_start)
|
||||||
self.jabber.RegisterHandler('presence', self.xmpp_presence)
|
self.jabber.add_event_handler('groupchat_message', self.xmpp_message)
|
||||||
|
self.jabber.add_event_handler('muc::%s::presence' % self.remotejid,
|
||||||
|
self.xmpp_presence)
|
||||||
|
|
||||||
def xmpp_message(self, con, event):
|
def session_start(self, event):
|
||||||
|
self.jabber.get_roster()
|
||||||
|
self.jabber.send_presence()
|
||||||
|
self.jabber.plugin['xep_0045'].joinMUC(self.remotejid,
|
||||||
|
m_nick,
|
||||||
|
wait=True)
|
||||||
|
|
||||||
|
def xmpp_message(self, event):
|
||||||
try:
|
try:
|
||||||
type = event.getType()
|
text = event['body']
|
||||||
text = event.getBody()
|
fromjid = event['from'].bare
|
||||||
fromjid = event.getFrom().getStripped()
|
nick = event['mucnick']
|
||||||
|
|
||||||
if type in ['message', 'groupchat', None] and \
|
text = text.replace('\r', '').replace('\n', '')
|
||||||
fromjid == self.remotejid:
|
|
||||||
|
|
||||||
m = text.replace('\r', '')
|
if text and nick != m_nick:
|
||||||
m = m.replace('\n', '')
|
if text == '.users':
|
||||||
n = event.getFrom().getResource()
|
|
||||||
|
|
||||||
if text and n != m_nick:
|
|
||||||
if m == '.users':
|
|
||||||
users = self.chanmuc.chan_users()
|
users = self.chanmuc.chan_users()
|
||||||
users = ', '.join(users)
|
users = ', '.join(users)
|
||||||
|
|
||||||
if users:
|
if users:
|
||||||
users = '[ IRC Users ] ' + users
|
users = '[ IRC Users ] ' + users
|
||||||
|
|
||||||
m = xmpp.protocol.Message(to=muc, body=users,
|
self.jabber.send_message(mto=muc, mbody=users,
|
||||||
typ='groupchat')
|
mtype='groupchat')
|
||||||
inter.xmpp(m)
|
|
||||||
|
|
||||||
elif m == '.help':
|
elif text == '.help':
|
||||||
message = 'The only command I have is \'.users\''+ \
|
message = 'The only command I have is \'.users\''+ \
|
||||||
'. Also, my owner is ' + owner + '.'
|
'. Also, my owner is ' + owner + '.'
|
||||||
|
|
||||||
m = xmpp.protocol.Message(to=muc, body=message,
|
self.jabber.send_message(mto=muc, mbody=message,
|
||||||
typ='groupchat')
|
mtype='groupchat')
|
||||||
inter.xmpp(m)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
m = '[' + n + '] ' + m
|
text = '[' + nick + '] ' + text
|
||||||
|
|
||||||
buffer = 460
|
buffer = 460
|
||||||
for i in range(0, len(m), buffer):
|
for i in range(0, len(text), buffer):
|
||||||
inter.irc(m[i:i + buffer].encode('latin-1',
|
inter.irc(text[i:i + buffer].encode('latin-1',
|
||||||
'replace'))
|
'replace'))
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print e
|
print e
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def xmpp_presence(self, con, event):
|
def xmpp_presence(self, event):
|
||||||
try:
|
try:
|
||||||
type = event.getType()
|
type = event['muc']['type']
|
||||||
n = event.getFrom().getResource()
|
nick = event['muc']['nick']
|
||||||
|
|
||||||
if type == 'unavailable':
|
if type == 'unavailable':
|
||||||
if n in self.chanmuc.muc_users():
|
if nick in self.chanmuc.muc_users():
|
||||||
self.chanmuc.muc_remove_user(n)
|
self.chanmuc.muc_remove_user(n)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
if n not in self.chanmuc.muc_users():
|
if nick not in self.chanmuc.muc_users():
|
||||||
self.chanmuc.muc_add_user(n.decode())
|
self.chanmuc.muc_add_user(nick.decode())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print e
|
print e
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def stdio_message(self, message):
|
def start(self):
|
||||||
m = xmpp.protocol.Message(to=self.remotejid, body=message, typ='chat')
|
self.jabber.register_plugin('xep_0045') # XMPP MUC.
|
||||||
self.jabber.send(m)
|
self.jabber.register_plugin('xep_0199') # XMPP Ping.
|
||||||
pass
|
if self.jabber.connect():
|
||||||
|
|
||||||
def xmpp_connect(self):
|
|
||||||
con = self.jabber.connect()
|
|
||||||
|
|
||||||
if not con:
|
|
||||||
sys.stderr.write('could not connect!\n')
|
|
||||||
return False
|
|
||||||
|
|
||||||
# sys.stderr.write('connected with %s\n'%con)
|
# sys.stderr.write('connected with %s\n'%con)
|
||||||
auth = self.jabber.auth(jid.getNode(), password,
|
|
||||||
resource=jid.getResource())
|
|
||||||
|
|
||||||
if not auth:
|
|
||||||
sys.stderr.write('could not authenticate!\n')
|
|
||||||
return False
|
|
||||||
print 'Connected to XMPP'
|
print 'Connected to XMPP'
|
||||||
# sys.stderr.write('authenticated using %s\n'%auth)
|
|
||||||
self.register_handlers()
|
self.register_handlers()
|
||||||
|
self.jabber.process(block=True)
|
||||||
return con
|
else:
|
||||||
|
# sys.stderr.write('could not connect!\n')
|
||||||
def DisconnectHandler(self):
|
|
||||||
reconnectAndReauth(self)
|
|
||||||
|
|
||||||
def start(self, cl):
|
|
||||||
if not self.xmpp_connect():
|
|
||||||
sys.stderr.write('Could not connect to server, or password '
|
sys.stderr.write('Could not connect to server, or password '
|
||||||
'mismatch!\n')
|
'mismatch!\n')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
p = xmpp.Presence(to='%s/%s' % (muc, m_nick))
|
|
||||||
p.setTag('x', namespace=xmpp.NS_MUC).addChild('history', {'maxchars': '0','maxstanzas': '0'})
|
|
||||||
|
|
||||||
cl.send(p)
|
|
||||||
|
|
||||||
while cl.Process(1):
|
|
||||||
pass
|
|
||||||
|
|
||||||
cl.disconnect()
|
|
||||||
|
|
||||||
|
|
||||||
class ChannelAndMuc:
|
class ChannelAndMuc:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.m_users = []
|
self.m_users = []
|
||||||
@ -219,7 +191,7 @@ class ChannelAndMuc:
|
|||||||
class Intermedia:
|
class Intermedia:
|
||||||
def xmpp(self, m):
|
def xmpp(self, m):
|
||||||
try:
|
try:
|
||||||
xmppbot.jabber.send(m)
|
xmppbot.jabber.send_message(mto=muc, mbody=m, mtype='groupchat')
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -242,8 +214,6 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
jid = parser.get('XMPP', 'jid')
|
jid = parser.get('XMPP', 'jid')
|
||||||
password = parser.get('XMPP', 'password')
|
password = parser.get('XMPP', 'password')
|
||||||
jid = xmpp.protocol.JID(jid)
|
|
||||||
cl = xmpp.Client(jid.getDomain(), debug=[])
|
|
||||||
muc = parser.get('XMPP', 'muc')
|
muc = parser.get('XMPP', 'muc')
|
||||||
m_nick = parser.get('XMPP', 'nick')
|
m_nick = parser.get('XMPP', 'nick')
|
||||||
|
|
||||||
@ -257,14 +227,16 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
ircb0t = IRCBot(channel, i_nick, server, port, chanmuc)
|
ircb0t = IRCBot(channel, i_nick, server, port, chanmuc)
|
||||||
xmppbot = XMPPBot(cl, muc, chanmuc)
|
xmppbot = XMPPBot(muc, chanmuc)
|
||||||
inter = Intermedia()
|
inter = Intermedia()
|
||||||
z = Thread(target=ircb0t.start, args=())
|
z = Thread(target=ircb0t.start, args=())
|
||||||
w = Thread(target=xmppbot.start, args=(cl,))
|
w = Thread(target=xmppbot.start, args=())
|
||||||
|
z.daemon = True
|
||||||
|
w.daemon = True
|
||||||
z.start()
|
z.start()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
w.start()
|
w.start()
|
||||||
z.join()
|
z.join()
|
||||||
w.join()
|
w.join()
|
||||||
except KeyboardInterrupt:
|
except (KeyboardInterrupt, SystemExit):
|
||||||
print 'Exit'
|
print 'Exit'
|
||||||
|
@ -1 +1 @@
|
|||||||
xmpppy >= 0.5
|
sleekxmpp>=1.2.0
|
||||||
|
Loading…
Reference in New Issue
Block a user