# 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:
def watchdog(self):
if time.time() > int(self.parent.cfg.get('watchdog', 'maxtime', default=300))+self.lastreceived:
self.parse("ERROR :Fake-error from watchdog timer.")
+ return
if self.conn.registered():
self.conn.send("PING :%s" % (time.time()))
self._checknick()
self.conn.send("NICK %s" % (self.permnick))
def parse(self, line):
- if self.parent.cfg.getboolean('debug', 'io'):
- self.log('I', line)
pieces = line.split()
if pieces[0][0] == ":":
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):
if self.state == 0:
+ pss = self.parent.parent.cfg.get('erebus', 'pass')
+ if pss:
+ self.send("PASS %s" % (pss))
self.send("NICK %s" % (self.parent.nick))
self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname))
self.state = 1
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):
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)