]> jfr.im git - erebus.git/commitdiff
basic_socket - wrap incoming lines
authorJohn Runyon <redacted>
Thu, 2 Nov 2023 00:20:33 +0000 (18:20 -0600)
committerJohn Runyon <redacted>
Thu, 2 Nov 2023 00:20:33 +0000 (18:20 -0600)
bot.py
modules/basic_socket.py

diff --git a/bot.py b/bot.py
index 3ce39474d39452f673493faacfe35a72450bb3be..51b7a0a33414decb30f7dcf8ceb9a23da4ee42d8 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -27,6 +27,8 @@ else:
 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
 class Bot(object):
        def __init__(self, parent, nick, user, bind, authname, authpass, server, port, realname):
+               self.maxlen = 510
+
                self.parent = parent
                self.nick = nick
                self.permnick = nick
@@ -420,15 +422,9 @@ class Bot(object):
                if self.parent.cfg.getboolean('erebus', 'nofakelag'): append_callback = self.conn.send
 
                cmd = self._formatmsg(target, msg, msgtype)
-               # The max length is much shorter than recvq (510) because of the length the server adds on about the source (us).
+               # The max length is much shorter than conn.maxlen (510) because of the length the server adds on about the source (us).
                # If you know your hostmask, you can of course figure the exact length, but it's very difficult to reliably know your hostmask.
-               maxlen = (
-                       self.conn.recvq
-                       - 63 # max hostname len
-                       - 11 # max ident len
-                       - 3  # the symbols in :nick!user@host
-                       - len(self.nick)
-               )
+               maxlen = self.maxmsglen()
                if len(cmd) > maxlen:
                        if not truncate:
                                return False
@@ -504,6 +500,15 @@ class Bot(object):
        def quit(self, reason="Shutdown"):
                self.conn.send("QUIT :%s" % (reason))
 
+       def maxmsglen(self):
+               return (
+                       self.maxlen
+                       - 63 # max hostname len
+                       - 11 # max ident len
+                       - 3  # the symbols in :nick!user@host
+                       - len(self.nick)
+               )
+
        def __str__(self): return self.nick
        def __repr__(self): return "<Bot %r>" % (self.nick)
 
@@ -520,7 +525,7 @@ class BotConnection(object):
                self.state = 0 # 0=disconnected, 1=registering, 2=connected
 
                self.bytessent = 0
-               self.recvq = 510
+               self.recvq = 510 # How much we can send per period
                self.exceeded = False
                self._nowrite = False
 
index 3d3b7e9ed1243a1935a3b52d1fa59bfde4c7e169..bfd6b72cd12ecca2cef85cb8cf3418d2b7adebc0 100644 (file)
@@ -33,9 +33,11 @@ modstop = lib.modstop
 # XXX error handling? what happens when the other side closes the socket?
 #
 # You can interact with the rest of the bot through `lib.parent`.
+
 @lib.bind_tcp('0.0.0.0', 12543)
 class BasicServer(object):
        def __init__(self, sock):
+               self.chan = lib.parent.cfg.get('basic_socket', 'channel', '#')
                self.buffer = b''
                self.sock = sock
 
@@ -63,7 +65,13 @@ class BasicServer(object):
 
        def parse(self, line):
                peer = self.sock.getpeername()
-               lib.parent.randbot().msg('#', "%s:%d says: %s" % (peer[0], peer[1], line))
+               bot = lib.parent.randbot()
+               maxlen = bot.maxmsglen() - len("PRIVMSG  :") - len(self.chan)
+               bot.msg(self.chan, "%s:%d says:" % peer)
+               while len(line) > maxlen:
+                       bot.msg(self.chan, line[0:maxlen])
+                       line = line[maxlen:]
+               bot.msg(self.chan, line)
 
        def send(self, line):
                self.socket.sendall(line.encode('utf-8', 'backslashreplace')+b"\r\n")