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