Pārlūkot izejas kodu

Implementing something to split up long IRC messages (#34)

tags/v0.1^2
Ben Kurtovic pirms 12 gadiem
vecāks
revīzija
8116c465e0
2 mainītis faili ar 23 papildinājumiem un 5 dzēšanām
  1. +22
    -4
      earwigbot/irc/connection.py
  2. +1
    -1
      earwigbot/irc/watcher.py

+ 22
- 4
earwigbot/irc/connection.py Parādīt failu

@@ -95,6 +95,22 @@ class IRCConnection(object):
if not hidelog:
self.logger.debug(msg)

def _split(msgs, maxlen, maxsplits=3):
"""Split a large message into multiple messages smaller than maxlen."""
words = msgs.split(" ")
splits = 0
while words and splits < maxsplits:
splits += 1
if len(words[0]) > maxlen:
word = words.pop(0)
yield word[:maxlen]
words.insert(0, word[maxlen:])
else:
msg = []
while words and len(" ".join(msg + [words[0]])) <= maxlen:
msg.append(words.pop(0))
yield " ".join(msg)

def _quit(self, msg=None):
"""Issue a quit message to the server. Doesn't close the connection."""
if msg:
@@ -142,8 +158,9 @@ class IRCConnection(object):

def say(self, target, msg, hidelog=False):
"""Send a private message to a target on the server."""
msg = "PRIVMSG {0} :{1}".format(target, msg)
self._send(msg, hidelog)
for msg in self._split(msg, 500 - len(target)):
msg = "PRIVMSG {0} :{1}".format(target, msg)
self._send(msg, hidelog)

def reply(self, data, msg, hidelog=False):
"""Send a private message as a reply to a user on the server."""
@@ -160,8 +177,9 @@ class IRCConnection(object):

def notice(self, target, msg, hidelog=False):
"""Send a notice to a target on the server."""
msg = "NOTICE {0} :{1}".format(target, msg)
self._send(msg, hidelog)
for msg in self._split(msg, 500 - len(target)):
msg = "NOTICE {0} :{1}".format(target, msg)
self._send(msg, hidelog)

def join(self, chan, hidelog=False):
"""Join a channel on the server."""


+ 1
- 1
earwigbot/irc/watcher.py Parādīt failu

@@ -120,4 +120,4 @@ class Watcher(IRCConnection):
if chans and frontend and not frontend.is_stopped():
pretty = rc.prettify()
for chan in chans:
frontend.say(chan, pretty)
frontend.say(chan, pretty[:500 - len(chan)])

Notiek ielāde…
Atcelt
Saglabāt