Skip to content

Commit

Permalink
take care of command lentgh to split messages. see #191
Browse files Browse the repository at this point in the history
  • Loading branch information
gawel committed May 14, 2021
1 parent 2d7d65c commit 53b110f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
26 changes: 19 additions & 7 deletions irc3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,20 @@ def _send(self, data):
def privmsg(self, target, message, nowait=False):
"""send a privmsg to target"""
if message:
messages = utils.split_message(message, self.config.max_length)
if isinstance(target, DCCChat):
is_dcc = isinstance(target, DCCChat)
prefix = '' if is_dcc else 'PRIVMSG %s :' % target
messages = utils.split_message(
message,
self.config.max_length,
prefix=prefix,
)
if is_dcc:
for message in messages:
target.send_line(message)
elif target:
f = None
for message in messages:
f = self.send_line('PRIVMSG %s :%s' % (target, message),
f = self.send_line(prefix + message,
nowait=nowait)
return f

Expand All @@ -243,14 +249,20 @@ def action(self, target, message, nowait=False):
def notice(self, target, message, nowait=False):
"""send a notice to target"""
if message:
messages = utils.split_message(message, self.config.max_length)
if isinstance(target, DCCChat):
is_dcc = isinstance(target, DCCChat)
prefix = '' if is_dcc else 'NOTICE %s :' % target
messages = utils.split_message(
message,
self.config.max_length,
prefix=prefix,
)
if is_dcc:
for message in messages:
target.action(message)
target.send_line(message)
elif target:
f = None
for message in messages:
f = self.send_line('NOTICE %s :%s' % (target, message),
f = self.send_line(prefix + message,
nowait=nowait)
return f

Expand Down
3 changes: 2 additions & 1 deletion irc3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ def tagdict(self):
STRIPPED_CHARS = '\t '


def split_message(message, max_length):
def split_message(message, max_length, prefix=''):
"""Split long messages"""
max_length = max_length - len(prefix)
if len(message) > max_length:
for message in textwrap.wrap(message, max_length):
yield message
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_message(self):
self.assertSent(['PRIVMSG gawel :\x01ACTION does a cool action\x01'])

def test_long_message(self):
bot = self.callFTU(max_length=8)
bot = self.callFTU(max_length=23)
message = 'How you doing?'
bot.privmsg('gawel', message)
self.assertSent([
Expand Down

0 comments on commit 53b110f

Please sign in to comment.