Fixes for multiline messages
This commit is contained in:
parent
641950aaea
commit
b5ea9cf6d2
57
hybridbot.py
57
hybridbot.py
@ -85,11 +85,21 @@ class IRCBot:
|
|||||||
self.nick = self.nick + '_'
|
self.nick = self.nick + '_'
|
||||||
conn.nick(self.nick)
|
conn.nick(self.nick)
|
||||||
|
|
||||||
def send_message(self, msg):
|
def send_message(self, msg, lead_prefix='', prefix=''):
|
||||||
buffer = 460
|
buf = 460
|
||||||
|
result = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for i in range(0, len(msg), buffer):
|
for line in msg:
|
||||||
self.conn.privmsg(self.chan, msg[i:i + buffer])
|
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:
|
except Exception as e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
sys.stderr.write(str(e) + '\n')
|
||||||
|
|
||||||
@ -168,10 +178,13 @@ class XMPPBot:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
sys.stderr.write(str(e) + '\n')
|
||||||
|
|
||||||
def send_message(self, msg):
|
def send_message(self, msg, prefix=''):
|
||||||
try:
|
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')
|
mtype='groupchat')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
sys.stderr.write(str(e) + '\n')
|
||||||
|
|
||||||
@ -205,13 +218,13 @@ class Intermedia:
|
|||||||
self.ircbot = ircbot
|
self.ircbot = ircbot
|
||||||
self.xmppbot = xmppbot
|
self.xmppbot = xmppbot
|
||||||
|
|
||||||
def to_irc(self, msg):
|
def to_irc(self, msg, lead_prefix='', prefix=''):
|
||||||
if self.ircbot:
|
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:
|
if self.xmppbot:
|
||||||
self.xmppbot.send_message(msg)
|
self.xmppbot.send_message(msg, prefix)
|
||||||
|
|
||||||
def relay_message(self, from_net, nick, body):
|
def relay_message(self, from_net, nick, body):
|
||||||
if not self.ircbot or not self.xmppbot:
|
if not self.ircbot or not self.xmppbot:
|
||||||
@ -242,38 +255,36 @@ class Intermedia:
|
|||||||
|
|
||||||
if from_net == 'irc':
|
if from_net == 'irc':
|
||||||
for answer in [xmpp_users]:
|
for answer in [xmpp_users]:
|
||||||
self.to_irc(answer)
|
self.to_irc([answer])
|
||||||
elif from_net == 'xmpp':
|
elif from_net == 'xmpp':
|
||||||
for answer in [irc_users]:
|
for answer in [irc_users]:
|
||||||
self.to_xmpp(answer)
|
self.to_xmpp([answer])
|
||||||
|
|
||||||
elif len(msg) == 1 and msg[0] == self.prefix + 'help':
|
elif len(msg) == 1 and msg[0] == self.prefix + 'help':
|
||||||
answer = 'The only command I have is \'' + self.prefix + \
|
answer = 'The only command I have is \'' + self.prefix + \
|
||||||
'users\'. Also, my owner is ' + self.owner + '.'
|
'users\'. Also, my owner is ' + self.owner + '.'
|
||||||
|
|
||||||
if from_net == 'irc':
|
if from_net == 'irc':
|
||||||
self.to_irc(answer)
|
self.to_irc([answer])
|
||||||
elif from_net == 'xmpp':
|
elif from_net == 'xmpp':
|
||||||
self.to_xmpp(answer)
|
self.to_xmpp([answer])
|
||||||
|
|
||||||
else:
|
else:
|
||||||
nick_prefix = '[' + nick + '] '
|
nick_prefix = '[' + nick + '] '
|
||||||
nick_prefix_me = '***' + nick + ' '
|
nick_prefix_me = '***' + nick + ' '
|
||||||
|
|
||||||
if (not re.match('^/me .+$', msg[0])):
|
if (not re.match('^/me .+$', msg[0])):
|
||||||
msg[0] = nick_prefix + msg[0]
|
nick_prefix_lead = nick_prefix
|
||||||
else:
|
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':
|
if from_net == 'irc':
|
||||||
self.to_xmpp('\n'.join(msg))
|
self.to_xmpp(msg, prefix=nick_prefix_lead)
|
||||||
elif from_net == 'xmpp':
|
elif from_net == 'xmpp':
|
||||||
self.to_irc(msg[0])
|
self.to_irc(msg,
|
||||||
|
lead_prefix=nick_prefix_lead,
|
||||||
# Separately as with a standard prefix, always.
|
prefix=nick_prefix)
|
||||||
for m in msg[1:]:
|
|
||||||
time.sleep(0.5)
|
|
||||||
self.to_irc(nick_prefix + m)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
sys.stderr.write(str(e) + '\n')
|
||||||
|
Loading…
Reference in New Issue
Block a user