]> jfr.im git - erebus.git/blobdiff - bot.py
admin_config - add !getconfig, remove some unused functions
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index 8b58d39b487fac0e5f0318582eb1a3139fceabbb..fd9292295aa3d2051d729d8e8e6a6e1b5b73b8a2 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:
@@ -119,6 +119,7 @@ class Bot(object):
                        '354': self._got354, #WHO
                        '396': self._gotHiddenHost, # hidden host has been set
                        '433': self._got433, #nick in use
+                       '437': self._got433, #nick protected
                        'JOIN': self._gotjoin,
                        'PART': self._gotpart,
                        'KICK': self._gotkick,
@@ -246,6 +247,10 @@ class Bot(object):
                        for u in chan.users:
                                if u.nick != self.nick:
                                        self._clientLeft(u.nick, chan)
+                       if chan.deleting:
+                               chan.bot.chans.remove(chan)
+                               del self.parent.chans[chan.name.lower()]
+                               del chan
                else:
                        user = self.parent.user(nick)
                        gone = user.part(chan)
@@ -553,6 +558,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 +572,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):
@@ -584,7 +592,7 @@ class BotConnection(object):
                                self.parent.log('X', line)
 
        def _write(self, line):
-               self.socket.sendall(line.encode('utf-8', 'backslashreplace')+b"\r\n")
+               self.socket.sendall(line.encode('utf-8', 'surrogateescape')+b"\r\n")
 
        def _getsockerr(self):
                try: # SO_ERROR might not exist on all platforms
@@ -606,5 +614,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)