Put XMPP and IRC msg handling logic in one place

This commit is contained in:
Alexei Sorokin 2016-08-01 17:42:03 +03:00
parent c108f2ded9
commit a74381ae52

View File

@ -21,6 +21,7 @@ class IRCBot(SingleServerIRCBot):
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.owner = opts['owner']
self.inter = inter self.inter = inter
def _on_join(self, c, e): def _on_join(self, c, e):
@ -50,39 +51,15 @@ class IRCBot(SingleServerIRCBot):
def on_pubmsg(self, c, e): def on_pubmsg(self, c, e):
nick = e.source.split('!')[0] nick = e.source.split('!')[0]
body = ''.join(e.arguments)
if str(e.arguments[0]) == '.users': self.inter.relay_message('irc', nick, body)
users = ', '.join(self.inter.get_xmpp_users())
m = '[ XMPP Users ] ' + users
buffer = 460
for i in range(0, len(m), buffer):
self.inter.to_irc(m[i:i + buffer])
elif str(e.arguments[0]) == '.help':
m = 'The only command I know is \'.users\'. Also, my owner is ' \
+ self.opts['owner'] + '.'
buffer = 460
for i in range(0, len(m), buffer):
self.inter.to_irc(m[i:i + buffer])
else:
message = '[' + nick + '] ' + ''.join(e.arguments)
try:
self.inter.to_xmpp(message)
except:
pass
def on_action(self, c, e): def on_action(self, c, e):
nick = e.source.split('!')[0] nick = e.source.split('!')[0]
body = '/me ' + ''.join(e.arguments)
if (nick != self.nick): self.inter.relay_message('irc', nick, body)
message = '***' + nick + ' ' + ''.join(e.arguments)
try:
self.inter.to_xmpp(message)
except:
pass
class XMPPBot(sleekxmpp.ClientXMPP): class XMPPBot(sleekxmpp.ClientXMPP):
@ -93,6 +70,7 @@ class XMPPBot(sleekxmpp.ClientXMPP):
self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd']) self.jabber = sleekxmpp.ClientXMPP(opts['jid'], opts['passwd'])
self.opts = opts self.opts = opts
self.nick = opts['nick'] self.nick = opts['nick']
self.owner = opts['owner']
self.inter = inter self.inter = inter
def register_handlers(self): def register_handlers(self):
@ -109,52 +87,10 @@ class XMPPBot(sleekxmpp.ClientXMPP):
wait=True) wait=True)
def xmpp_message(self, event): def xmpp_message(self, event):
try: body = event['body']
text = event['body']
fromjid = event['from'].bare
nick = event['mucnick'] nick = event['mucnick']
if text: self.inter.relay_message('xmpp', nick, body)
text = text.replace('\r\n', '\n').replace('\r', '\n').split('\n')
if text and len(text) > 0 and nick != self.nick:
if len(text) == 1 and text[0] == '.users':
users = ', '.join(self.inter.get_irc_users())
if users:
users = '[ IRC Users ] ' + users
self.jabber.send_message(mto=self.opts['muc'], mbody=users,
mtype='groupchat')
elif len(text) == 1 and text[0] == '.help':
message = 'The only command I have is \'.users\''+ \
'. Also, my owner is ' + self.opts['owner'] + '.'
self.jabber.send_message(mto=self.opts['muc'], mbody=message,
mtype='groupchat')
else:
isme = False
if (re.match('^/me .+$', text[0])):
text[0] = re.split('^/me ', text[0])[1]
isme = True
for i in range(0, len(text)):
if not isme:
m = '[' + nick + '] ' + text[i]
else:
m = '***' + nick + ' ' + text[i]
# Slow down a bit.
if i > 0:
time.sleep(0.5)
buffer = 460
for j in range(0, len(m), buffer):
self.inter.to_irc(m[j:j + buffer])
except Exception as e:
print(e)
def xmpp_presence(self, event): def xmpp_presence(self, event):
try: try:
@ -199,6 +135,16 @@ class Intermedia:
self.ircbot = ircbot self.ircbot = ircbot
self.xmppbot = xmppbot self.xmppbot = xmppbot
def to_irc(self, msg):
buffer = 460
if self.ircbot:
try:
for i in range(0, len(msg), buffer):
self.ircbot.connection.privmsg(self.irc_chan,
msg[i:i + buffer])
except:
pass
def to_xmpp(self, msg): def to_xmpp(self, msg):
if self.xmppbot: if self.xmppbot:
try: try:
@ -207,12 +153,75 @@ class Intermedia:
except: except:
pass pass
def to_irc(self, msg): def relay_message(self, from_net, nick, body):
if self.ircbot: if not self.ircbot or not self.xmppbot:
return
if from_net != 'irc' and from_net != 'xmpp':
return
if from_net == 'irc' and nick == self.ircbot.nick or \
from_net == 'xmpp' and nick == self.xmppbot.nick:
return
if not body or len(body) <= 0:
return
try: try:
self.ircbot.connection.privmsg(self.irc_chan, msg) msg = body.replace('\r\n', '\n').replace('\r', '\n').split('\n')
except:
pass if from_net == 'irc':
owner = self.ircbot.owner
elif from_net == 'xmpp':
owner = self.xmppbot.owner
if msg and len(msg) > 0:
if len(msg) == 1 and msg[0] == '.users':
irc_users = ', '.join(self.get_irc_users())
xmpp_users = ', '.join(self.get_xmpp_users())
if irc_users:
irc_users = '[ IRC Users ] ' + irc_users
if xmpp_users:
xmpp_users = '[ XMPP Users ] ' + xmpp_users
if from_net == 'irc':
for answer in [xmpp_users]:
self.to_irc(answer)
elif from_net == 'xmpp':
for answer in [irc_users]:
self.to_xmpp(answer)
elif len(msg) == 1 and msg[0] == '.help':
answer = 'The only command I have is \'.users\''+ \
'. Also, my owner is ' + owner + '.'
if from_net == 'irc':
self.to_irc(answer)
elif from_net == 'xmpp':
self.to_xmpp(answer)
else:
prefix = '[' + nick + '] '
prefix_me = '***' + nick + ' '
if (not re.match('^/me .+$', msg[0])):
msg[0] = prefix + msg[0]
else:
msg[0] = prefix_me + re.split('^/me ', msg[0])[1]
if from_net == 'irc':
self.to_xmpp('\n'.join(msg))
elif from_net == 'xmpp':
self.to_irc(msg[0])
# Separately as with a standard prefix, always.
for m in msg[1:]:
time.sleep(0.5)
self.to_irc(prefix + m)
except Exception as e:
print(e)
def get_irc_users(self): def get_irc_users(self):
return self.irc_users return self.irc_users