]> jfr.im git - erebus.git/commitdiff
add oidentd support
authorJohn Runyon <redacted>
Mon, 11 Mar 2024 17:09:50 +0000 (11:09 -0600)
committerJohn Runyon <redacted>
Mon, 11 Mar 2024 17:09:50 +0000 (11:09 -0600)
bot.config.example
bot.py

index 933b34b37db1c642173d23960a73cbc4efd27315..09b1ac20d9193a16ce29e9dd815df1fa714d322b 100644 (file)
@@ -21,6 +21,10 @@ pidfile = pidfile
 ; Wait for numeric 396 (hidden host) before joining channels?
 wait_for_hidden_host = 0
 
+; path to write bot's ident config (~/.oidentd.conf format) into
+; empty or not supplied to disable
+;oidentd_path = /home/erebus/.oidentd.conf
+
 [autoloads]
 control = 1
 eval = 0
diff --git a/bot.py b/bot.py
index 8b58d39b487fac0e5f0318582eb1a3139fceabbb..138b2fc001d4384943117c3d9720738d11fee726 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -4,7 +4,7 @@
 # Erebus IRC bot - Author: John Runyon
 # "Bot" and "BotConnection" classes (handling a specific "arm")
 
-import os, random, socket, struct, sys, threading, time, traceback
+import os, random, socket, struct, sys, threading, time, traceback, fcntl
 from collections import deque
 
 if sys.version_info.major < 3:
@@ -553,6 +553,7 @@ class BotConnection(object):
                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 0, 0))
                self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
                self.socket.bind((self.bind, 0))
+               self._write_oidentd()
                self.socket.connect((self.server, self.port))
                return True
        def register(self):
@@ -566,7 +567,9 @@ class BotConnection(object):
                return True
 
        def registered(self, done=False):
-               if done: self.state = 2
+               if done:
+                       self.state = 2
+                       self._unwrite_oidentd()
                return self.state == 2
 
        def send(self, line):
@@ -606,5 +609,33 @@ class BotConnection(object):
 
                return lines
 
+       def _format_oidentd(self):
+               ident = self.parent.user
+               fport = self.parent.port
+               from_ = self.bind
+               lport = self.socket.getsockname()[1]
+               if from_:
+                       return 'fport %s from %s lport %s { reply "%s" }\n' % (fport, from_, lport, ident)
+               else:
+                       return 'fport %s lport %s { reply "%s" }\n' % (fport, lport, ident)
+       def _write_oidentd(self):
+               path = self.parent.parent.cfg.get('erebus', 'oidentd_path')
+               if path is not None:
+                       with open(path, 'a') as fh:
+                               fcntl.lockf(fh, fcntl.LOCK_EX)
+                               fh.write(self._format_oidentd())
+                               fcntl.lockf(fh, fcntl.LOCK_UN)
+       def _unwrite_oidentd(self):
+               path = self.parent.parent.cfg.get('erebus', 'oidentd_path')
+               if path is not None:
+                       with open(path, 'r+') as fh:
+                               fcntl.lockf(fh, fcntl.LOCK_EX)
+                               data = fh.read()
+                               newdata = data.replace(self._format_oidentd(), '')
+                               fh.seek(0)
+                               fh.write(newdata)
+                               fh.truncate()
+                               fcntl.lockf(fh, fcntl.LOCK_UN)
+
        def __str__(self): return self.parent.nick
        def __repr__(self): return "<BotConnection %r (%r)>" % (self.socket.fileno(), self.parent.nick)