# Twisted/Klein imports from twisted.logger import Logger from twisted.words.protocols import irc from twisted.internet import protocol, reactor, ssl # Project imports from settings import settings class IRCBot(irc.IRCClient): def __init__(self, log): self.log = log self.nickname = settings.IRC.Nick self.password = settings.IRC.Pass self.realname = self.nickname self.username = self.nickname self.userinfo = None self.fingerReply = None self.versionName = None self.sourceURL = None self.prefix = "!" self.admins = (settings.IRC.Admins).split("\n") self.highlight = (settings.IRC.Highlight).split("\n") def parse(self, user, host, channel, msg): spl = msg.split() # nick = user.split("!")[0] cmd = spl[0] # help command # if cmd == "help": # self.msg(channel, "Hey it's me") # setprefix command if cmd == "setprefix" and host in self.admins: if len(spl) == 2: if len(spl[1]) == 1: self.prefix = spl[1] self.msg(channel, "Prefix set to '%s'." % self.prefix) else: self.msg(channel, "Incorrect usage") # addmod command elif cmd == "addmod" and host in self.admins: if len(spl) == 2: if not spl[1] in self.admins: self.admins.append(spl[1]) self.msg(channel, "%s added to admins." % spl[1]) else: self.msg(channel, "%s is already an admin!" % spl[1]) else: self.msg(channel, "Incorrect usage") # delmod command elif cmd == "delmod" and host in self.admins: if len(spl) == 2: if spl[1] in self.admins: self.admins.remove(spl[1]) self.msg(channel, "%s removed from admins." % spl[1]) else: self.msg(channel, "%s is not an admin!" % spl[1]) else: self.msg(channel, "Incorrect usage") # join command elif cmd == "join" and host in self.admins: if len(spl) == 2: self.join(spl[1]) else: self.msg(channel, "Incorrect usage") # part command elif cmd == "part" and host in self.admins: if len(spl) == 2: self.part(spl[1]) else: self.part(channel) elif cmd == "raw" and host in self.admins: self.sendLine(" ".join(spl[1:])) def stopcall(self, call): call.stop() def signedOn(self): self.log.info("Signed on as %s" % (self.nickname)) self.join(settings.IRC.Channel) def joined(self, channel): self.log.info("Joined channel %s" % (channel)) def privmsg(self, user, channel, msg): nick = user.split("!")[0] if channel == self.nickname: channel = nick host = user.split("!")[1] host = host.split("@")[1] ident = user.split("!")[1] ident = ident.split("@")[0] self.log.info("(%s) %s: %s" % (channel, user, msg)) if msg[0] == self.prefix: if len(msg) > 1: if msg.split()[0] != "!": self.parse(user, host, channel, msg[1:]) def noticed(self, user, channel, message): nick = user.split("!")[0] if channel == self.nickname: channel = nick # self.log.info("[%s] %s: %s" % (channel, user, message)) class IRCBotFactory(protocol.ClientFactory): def __init__(self): self.log = Logger("irc") def buildProtocol(self, addr): prcol = IRCBot(self.log) self.client = prcol return prcol def clientConnectionLost(self, connector, reason): self.log.error("Lost connection (%s), reconnecting" % (reason)) connector.connect() def clientConnectionFailed(self, connector, reason): self.log.error("Could not connect: %s" % (reason)) def bot(): context = ssl.DefaultOpenSSLContextFactory(settings.IRC.Cert, settings.IRC.Cert) factory = IRCBotFactory() reactor.connectSSL(settings.IRC.Host, int(settings.IRC.Port), factory, context) return factory