Fixes for multiline messages

This commit is contained in:
Alexei Sorokin 2016-08-11 19:31:40 +03:00
parent 641950aaea
commit b5ea9cf6d2

View File

@ -85,11 +85,21 @@ class IRCBot:
self.nick = self.nick + '_'
conn.nick(self.nick)
def send_message(self, msg):
buffer = 460
def send_message(self, msg, lead_prefix='', prefix=''):
buf = 460
result = []
try:
for i in range(0, len(msg), buffer):
self.conn.privmsg(self.chan, msg[i:i + buffer])
for line in msg:
for i in range(0, len(line), buf):
result.append(line[i:i + buf])
self.conn.privmsg(self.chan, lead_prefix + result[0])
for r in result[1:]:
time.sleep(0.5)
self.conn.privmsg(self.chan, prefix + r)
except Exception as e:
sys.stderr.write(str(e) + '\n')
@ -168,10 +178,13 @@ class XMPPBot:
except Exception as e:
sys.stderr.write(str(e) + '\n')
def send_message(self, msg):
def send_message(self, msg, prefix=''):
try:
self.client.send_message(mto=self.muc, mbody=msg,
msg[0] = prefix + msg[0]
result = '\n'.join(msg)
self.client.send_message(mto=self.muc, mbody=result,
mtype='groupchat')
except Exception as e:
sys.stderr.write(str(e) + '\n')
@ -205,13 +218,13 @@ class Intermedia:
self.ircbot = ircbot
self.xmppbot = xmppbot
def to_irc(self, msg):
def to_irc(self, msg, lead_prefix='', prefix=''):
if self.ircbot:
self.ircbot.send_message(msg)
self.ircbot.send_message(msg, lead_prefix, prefix)
def to_xmpp(self, msg):
def to_xmpp(self, msg, prefix=''):
if self.xmppbot:
self.xmppbot.send_message(msg)
self.xmppbot.send_message(msg, prefix)
def relay_message(self, from_net, nick, body):
if not self.ircbot or not self.xmppbot:
@ -242,38 +255,36 @@ class Intermedia:
if from_net == 'irc':
for answer in [xmpp_users]:
self.to_irc(answer)
self.to_irc([answer])
elif from_net == 'xmpp':
for answer in [irc_users]:
self.to_xmpp(answer)
self.to_xmpp([answer])
elif len(msg) == 1 and msg[0] == self.prefix + 'help':
answer = 'The only command I have is \'' + self.prefix + \
'users\'. Also, my owner is ' + self.owner + '.'
if from_net == 'irc':
self.to_irc(answer)
self.to_irc([answer])
elif from_net == 'xmpp':
self.to_xmpp(answer)
self.to_xmpp([answer])
else:
nick_prefix = '[' + nick + '] '
nick_prefix_me = '***' + nick + ' '
if (not re.match('^/me .+$', msg[0])):
msg[0] = nick_prefix + msg[0]
nick_prefix_lead = nick_prefix
else:
msg[0] = nick_prefix_me + re.split('^/me ', msg[0])[1]
msg[0] = re.split('^/me ', msg[0])[1]
nick_prefix_lead = nick_prefix_me
if from_net == 'irc':
self.to_xmpp('\n'.join(msg))
self.to_xmpp(msg, prefix=nick_prefix_lead)
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(nick_prefix + m)
self.to_irc(msg,
lead_prefix=nick_prefix_lead,
prefix=nick_prefix)
except Exception as e:
sys.stderr.write(str(e) + '\n')