2018-02-23 22:05:40 +00:00
|
|
|
from twisted.internet.protocol import ReconnectingClientFactory
|
|
|
|
from twisted.words.protocols.irc import IRCClient
|
|
|
|
from twisted.internet.defer import Deferred
|
2018-07-28 20:30:50 +00:00
|
|
|
from twisted.internet.task import LoopingCall
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-07-29 12:04:47 +00:00
|
|
|
from random import randint
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
import modules.keyword as keyword
|
|
|
|
import modules.userinfo as userinfo
|
2018-02-24 12:42:27 +00:00
|
|
|
import modules.counters as count
|
2018-07-27 21:58:37 +00:00
|
|
|
import modules.monitor as monitor
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
import main
|
2018-02-23 22:05:40 +00:00
|
|
|
from utils.logging.log import *
|
|
|
|
from utils.logging.send import *
|
|
|
|
|
|
|
|
class IRCBot(IRCClient):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.connected = False
|
|
|
|
self.channels = []
|
2018-03-14 20:13:40 +00:00
|
|
|
self.buffer = ""
|
2018-02-23 22:05:40 +00:00
|
|
|
self.name = name
|
2018-03-14 20:13:40 +00:00
|
|
|
instance = main.pool[name]
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
self.nickname = instance["nickname"]
|
|
|
|
self.realname = instance["realname"]
|
|
|
|
self.username = instance["username"]
|
|
|
|
self.userinfo = instance["userinfo"]
|
|
|
|
self.fingerReply = instance["finger"]
|
|
|
|
self.versionName = instance["version"]
|
|
|
|
self.versionNum = None
|
|
|
|
self.versionEnv = None
|
|
|
|
self.sourceURL = instance["source"]
|
|
|
|
self.autojoin = instance["autojoin"]
|
|
|
|
|
|
|
|
self._who = {}
|
|
|
|
self._getWho = {}
|
|
|
|
|
|
|
|
self.authtype = instance["authtype"]
|
|
|
|
if self.authtype == "ns":
|
|
|
|
self.authpass = instance["password"]
|
|
|
|
self.authentity = instance["authentity"]
|
|
|
|
else:
|
|
|
|
self.password = instance["password"]
|
|
|
|
|
|
|
|
def refresh(self):
|
2018-03-14 20:13:40 +00:00
|
|
|
instance = main.pool[self.name]
|
2018-02-23 22:05:40 +00:00
|
|
|
if not instance["nickname"] == self.nickname:
|
|
|
|
self.nickname = instance["nickname"]
|
|
|
|
self.setNick(self.nickname)
|
|
|
|
|
|
|
|
self.userinfo = instance["userinfo"]
|
|
|
|
self.fingerReply = instance["finger"]
|
|
|
|
self.versionName = instance["version"]
|
|
|
|
self.versionNum = None
|
|
|
|
self.versionEnv = None
|
|
|
|
self.sourceURL = instance["source"]
|
|
|
|
|
|
|
|
def parsen(self, user):
|
|
|
|
step = user.split("!")
|
|
|
|
nick = step[0]
|
|
|
|
if len(step) == 2:
|
|
|
|
step2 = step[1].split("@")
|
|
|
|
ident, host = step2
|
|
|
|
else:
|
|
|
|
ident = nick
|
|
|
|
host = nick
|
|
|
|
|
|
|
|
return [nick, ident, host]
|
|
|
|
|
|
|
|
def privmsg(self, user, channel, msg):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "privmsg")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, channel, msg, self.nickname, "MSG", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "msg", "exact": user, "nick": nick, "ident": ident, "host": host, "message": msg})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def noticed(self, user, channel, msg):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "notice")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, channel, msg, self.nickname, "NOTICE", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "notice", "exact": user, "nick": nick, "ident": ident, "host": host, "message": msg})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def action(self, user, channel, msg):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "action")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, channel, msg, self.nickname, "ACTION", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "action", "exact": user, "nick": nick, "ident": ident, "host": host, "message": msg})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def get(self, var):
|
|
|
|
try:
|
|
|
|
result = getattr(self, var)
|
|
|
|
except AttributeError:
|
|
|
|
result = None
|
|
|
|
return result
|
|
|
|
|
|
|
|
def setNick(self, nickname):
|
|
|
|
self._attemptedNick = nickname
|
|
|
|
self.sendLine("NICK %s" % nickname)
|
|
|
|
self.nickname = nickname
|
|
|
|
|
|
|
|
def alterCollidedNick(self, nickname):
|
|
|
|
newnick = nickname + "_"
|
|
|
|
return newnick
|
|
|
|
|
2018-03-10 14:01:43 +00:00
|
|
|
def nickChanged(self, nick):
|
|
|
|
self.nickname = nick
|
|
|
|
count.event(self.name, "selfnick")
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def irc_ERR_NICKNAMEINUSE(self, prefix, params):
|
|
|
|
self._attemptedNick = self.alterCollidedNick(self._attemptedNick)
|
|
|
|
self.setNick(self._attemptedNick)
|
|
|
|
|
|
|
|
def irc_ERR_PASSWDMISMATCH(self, prefix, params):
|
|
|
|
log("%s: password mismatch" % self.name)
|
|
|
|
sendAll("%s: password mismatch" % self.name)
|
|
|
|
|
|
|
|
def who(self, channel):
|
|
|
|
channel = channel
|
|
|
|
d = Deferred()
|
|
|
|
if channel not in self._who:
|
|
|
|
self._who[channel] = ([], {})
|
|
|
|
self._who[channel][0].append(d)
|
|
|
|
self.sendLine("WHO %s" % channel)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def irc_RPL_WHOREPLY(self, prefix, params):
|
|
|
|
channel = params[1]
|
|
|
|
user = params[2]
|
|
|
|
host = params[3]
|
|
|
|
server = params[4]
|
|
|
|
nick = params[5]
|
|
|
|
status = params[6]
|
|
|
|
realname = params[7]
|
|
|
|
if channel not in self._who:
|
|
|
|
return
|
|
|
|
n = self._who[channel][1]
|
|
|
|
n[nick] = [nick, user, host, server, status, realname]
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "whoreply")
|
2018-07-28 20:30:50 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "who", "exact": nick+"!"+user+"@"+host, "nick": nick, "ident": user, "host": host, "realname": realname, "server": server, "status": status})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def irc_RPL_ENDOFWHO(self, prefix, params):
|
|
|
|
channel = params[1]
|
|
|
|
if channel not in self._who:
|
|
|
|
return
|
|
|
|
callbacks, info = self._who[channel]
|
|
|
|
for cb in callbacks:
|
|
|
|
cb.callback((channel, info))
|
|
|
|
del self._who[channel]
|
|
|
|
|
|
|
|
def got_who(self, whoinfo):
|
|
|
|
userinfo.setWho(self.name, whoinfo[1])
|
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
#twisted sucks so i have to do this to actually get the user info
|
|
|
|
def irc_JOIN(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user joins a channel.
|
|
|
|
"""
|
|
|
|
nick = prefix.split('!')[0]
|
|
|
|
channel = params[-1]
|
|
|
|
if nick == self.nickname:
|
|
|
|
self.joined(channel)
|
|
|
|
else:
|
|
|
|
self.userJoined(prefix, channel)
|
|
|
|
|
|
|
|
def irc_PART(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user leaves a channel.
|
|
|
|
"""
|
|
|
|
nick = prefix.split('!')[0]
|
|
|
|
channel = params[0]
|
|
|
|
if len(params) >= 2:
|
|
|
|
message = params[1]
|
|
|
|
else:
|
|
|
|
message = None
|
|
|
|
if nick == self.nickname:
|
|
|
|
self.left(channel, message)
|
|
|
|
else:
|
|
|
|
self.userLeft(prefix, channel, message)
|
|
|
|
|
|
|
|
def irc_QUIT(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user has quit.
|
|
|
|
"""
|
|
|
|
#nick = prefix.split('!')[0]
|
|
|
|
self.userQuit(prefix, params[0])
|
|
|
|
|
|
|
|
def irc_NICK(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user changes their nickname.
|
|
|
|
"""
|
|
|
|
nick = prefix.split('!', 1)[0]
|
|
|
|
if nick == self.nickname:
|
|
|
|
self.nickChanged(params[0])
|
|
|
|
else:
|
|
|
|
self.userRenamed(prefix, params[0])
|
|
|
|
|
|
|
|
def irc_KICK(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user is kicked from a channel.
|
|
|
|
"""
|
|
|
|
#kicker = prefix.split('!')[0]
|
|
|
|
channel = params[0]
|
|
|
|
kicked = params[1]
|
|
|
|
message = params[-1]
|
|
|
|
if kicked.lower() == self.nickname.lower():
|
|
|
|
# Yikes!
|
|
|
|
self.kickedFrom(channel, prefix, message)
|
|
|
|
else:
|
|
|
|
self.userKicked(kicked, channel, prefix, message)
|
|
|
|
|
|
|
|
def irc_TOPIC(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Someone in the channel set the topic.
|
|
|
|
"""
|
|
|
|
#user = prefix.split('!')[0]
|
|
|
|
channel = params[0]
|
|
|
|
newtopic = params[1]
|
|
|
|
self.topicUpdated(prefix, channel, newtopic)
|
|
|
|
#END hacks
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def signedOn(self):
|
|
|
|
self.connected = True
|
|
|
|
log("signed on: %s" % self.name)
|
2018-05-07 18:58:19 +00:00
|
|
|
if main.config["Notifications"]["Connection"]:
|
2018-02-23 22:05:40 +00:00
|
|
|
keyword.sendMaster("SIGNON: %s" % self.name)
|
|
|
|
if self.authtype == "ns":
|
|
|
|
self.msg(self.authentity, "IDENTIFY %s" % self.nspass)
|
|
|
|
for i in self.autojoin:
|
|
|
|
self.join(i)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "signedon")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-07-28 20:30:50 +00:00
|
|
|
def getWho(self, channel):
|
|
|
|
self.who(channel).addCallback(self.got_who)
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def joined(self, channel):
|
|
|
|
if not channel in self.channels:
|
|
|
|
self.channels.append(channel)
|
|
|
|
self.who(channel).addCallback(self.got_who)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "selfjoin")
|
2018-03-14 20:13:40 +00:00
|
|
|
if self.name == main.config["Master"][0] and channel == main.config["Master"][1]:
|
|
|
|
for i in range(len(main.masterbuf)):
|
|
|
|
self.msg(channel, main.masterbuf.pop(0))
|
|
|
|
main.saveConf("masterbuf")
|
2018-07-28 20:30:50 +00:00
|
|
|
lc = LoopingCall(self.getWho, channel)
|
|
|
|
self._getWho[channel] = lc
|
2018-07-29 12:04:47 +00:00
|
|
|
intrange = main.config["Tweaks"]["Delays"]["WhoRange"]
|
|
|
|
minint = main.config["Tweaks"]["Delays"]["WhoLoop"]
|
|
|
|
interval = randint(minint, minint+intrange)
|
|
|
|
lc.start(interval)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
def left(self, channel, message):
|
2018-02-23 22:05:40 +00:00
|
|
|
if channel in self.channels:
|
|
|
|
self.channels.remove(channel)
|
2018-07-27 21:58:37 +00:00
|
|
|
keyword.actKeyword(self.nickname, channel, message, self.nickname, "SELFPART", self.name)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "selfpart")
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "part", "message": message})
|
2018-07-28 20:30:50 +00:00
|
|
|
lc = self._getWho[channel]
|
|
|
|
lc.stop()
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def kickedFrom(self, channel, kicker, message):
|
2018-07-27 21:58:37 +00:00
|
|
|
nick, ident, host = self.parsen(kicker)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-23 22:05:40 +00:00
|
|
|
if channel in self.channels:
|
|
|
|
self.channels.remove(channel)
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.sendMaster("KICK %s: (%s/%s) %s" % (self.name, kicker, channel, message))
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "selfkick")
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "kick", "exact": kicker, "nick": nick, "ident": ident, "host": host, "message": message})
|
2018-07-29 12:04:47 +00:00
|
|
|
lc = self._getWho[channel]
|
|
|
|
lc.stop()
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userJoined(self, user, channel):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "join")
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "join", "exact": user, "nick": nick, "ident": ident, "host": host})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
def userLeft(self, user, channel, message):
|
2018-02-23 22:05:40 +00:00
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, channel, message, self.nickname, "PART", self.name)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "part")
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "part", "exact": user, "nick": nick, "ident": ident, "host": host, "message": message})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userQuit(self, user, quitMessage):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "quit")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, None, quitMessage, self.nickname, "QUIT", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, None, {"type": "quit", "exact": user, "nick": nick, "ident": ident, "host": host, "message": quitMessage})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userKicked(self, kickee, channel, kicker, message):
|
|
|
|
nick, ident, host = self.parsen(kicker)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "kick")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(kicker, channel, message, self.nickname, "KICK", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "kick", "exact": kicker, "nick": nick, "ident": ident, "host": host, "message": message, "user": kickee})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userRenamed(self, oldname, newname):
|
|
|
|
nick, ident, host = self.parsen(oldname)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
|
|
|
userinfo.setWhoSingle(self.name, newname, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "nick")
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, None, {"type": "nick", "exact": oldname, "nick": nick, "ident": ident, "host": host, "user": newname})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def topicUpdated(self, user, channel, newTopic):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "topic")
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
keyword.actKeyword(user, channel, newTopic, self.nickname, "TOPIC", self.name)
|
2018-07-27 21:58:37 +00:00
|
|
|
monitor.event(self.name, channel, {"type": "topic", "exact": user, "nick": nick, "ident": ident, "host": host, "message": newTopic})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def modeChanged(self, user, channel, toset, modes, args):
|
|
|
|
nick, ident, host = self.parsen(user)
|
|
|
|
userinfo.setWhoSingle(self.name, nick, ident, host)
|
2018-02-24 12:42:27 +00:00
|
|
|
count.event(self.name, "mode")
|
2018-07-28 20:30:50 +00:00
|
|
|
argList = list(args)
|
|
|
|
modeList = [i for i in modes]
|
|
|
|
for a, m in zip(argList, modeList):
|
|
|
|
monitor.event(self.name, channel, {"type": "mode", "exact": user, "nick": nick, "ident": ident, "host": host, "modes": m, "modeargs": a})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
class IRCBotFactory(ReconnectingClientFactory):
|
|
|
|
def __init__(self, name):
|
2018-03-14 20:13:40 +00:00
|
|
|
self.instance = main.pool[name]
|
2018-02-23 22:05:40 +00:00
|
|
|
self.name = name
|
|
|
|
self.client = None
|
|
|
|
self.maxDelay = self.instance["maxdelay"]
|
|
|
|
self.initialDelay = self.instance["initialdelay"]
|
|
|
|
self.factor = self.instance["factor"]
|
|
|
|
self.jitter = self.instance["jitter"]
|
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
|
|
|
entry = IRCBot(self.name)
|
2018-03-14 20:13:40 +00:00
|
|
|
main.IRCPool[self.name] = entry
|
2018-02-23 22:05:40 +00:00
|
|
|
self.client = entry
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
|
|
|
if not self.client == None:
|
|
|
|
self.client.connected = False
|
|
|
|
self.client.channels = []
|
|
|
|
error = reason.getErrorMessage()
|
|
|
|
log("%s: connection lost: %s" % (self.name, error))
|
|
|
|
sendAll("%s: connection lost: %s" % (self.name, error))
|
2018-05-07 18:58:19 +00:00
|
|
|
if main.config["Notifications"]["Connection"]:
|
2018-02-23 22:05:40 +00:00
|
|
|
keyword.sendMaster("CONNLOST %s: %s" % (self.name, error))
|
|
|
|
self.retry(connector)
|
|
|
|
#ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
|
|
|
|
|
|
|
def clientConnectionFailed(self, connector, reason):
|
|
|
|
if not self.client == None:
|
|
|
|
self.client.connected = False
|
|
|
|
self.client.channels = []
|
|
|
|
error = reason.getErrorMessage()
|
|
|
|
log("%s: connection failed: %s" % (self.name, error))
|
|
|
|
sendAll("%s: connection failed: %s" % (self.name, error))
|
2018-05-07 18:58:19 +00:00
|
|
|
if main.config["Notifications"]["Connection"]:
|
2018-02-23 22:05:40 +00:00
|
|
|
keyword.sendMaster("CONNFAIL %s: %s" % (self.name, error))
|
|
|
|
self.retry(connector)
|
|
|
|
#ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
|
|
|
|