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
|
2019-01-26 01:57:24 +00:00
|
|
|
from twisted.internet.task import LoopingCall, deferLater
|
|
|
|
from twisted.internet import reactor
|
2018-08-27 19:42:49 +00:00
|
|
|
|
2018-08-26 18:08:27 +00:00
|
|
|
from string import digits
|
2018-07-29 12:04:47 +00:00
|
|
|
from random import randint
|
2019-08-11 19:52:10 +00:00
|
|
|
from copy import deepcopy
|
2018-07-29 12:04:47 +00:00
|
|
|
|
2019-08-05 21:51:16 +00:00
|
|
|
from modules import userinfo
|
2019-08-15 20:20:49 +00:00
|
|
|
from modules import counters
|
2019-08-05 21:51:16 +00:00
|
|
|
from modules import monitor
|
2019-10-08 17:15:23 +00:00
|
|
|
from modules import chankeep
|
2019-08-05 21:51:16 +00:00
|
|
|
|
|
|
|
from core.relay import sendRelayNotification
|
2019-08-15 20:20:49 +00:00
|
|
|
from utils.dedup import dedup
|
2019-09-29 21:45:16 +00:00
|
|
|
from utils.get import getRelay
|
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 *
|
|
|
|
|
2019-01-26 01:57:24 +00:00
|
|
|
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
def deliverRelayCommands(num, relayCommands, user=None, stage2=None):
|
2019-08-11 20:58:14 +00:00
|
|
|
# where relay is a dictionary extracted from the Network object
|
2019-01-26 01:57:24 +00:00
|
|
|
keyFN = main.certPath+main.config["Key"]
|
|
|
|
certFN = main.certPath+main.config["Certificate"]
|
|
|
|
contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"),
|
|
|
|
certFN.encode("utf-8", "replace"))
|
2019-08-25 20:29:11 +00:00
|
|
|
bot = IRCBotFactory(net=None, num=num, relayCommands=relayCommands, user=user, stage2=stage2)
|
|
|
|
host, port = getRelay(num)
|
|
|
|
rct = reactor.connectSSL(host,
|
|
|
|
port,
|
2019-01-26 01:57:24 +00:00
|
|
|
bot, contextFactory)
|
|
|
|
|
|
|
|
class IRCRelay(IRCClient):
|
2019-08-25 20:29:11 +00:00
|
|
|
def __init__(self, num, relayCommands, user, stage2):
|
2019-01-26 01:57:24 +00:00
|
|
|
self.connected = False
|
|
|
|
self.buffer = ""
|
|
|
|
if user == None:
|
2019-08-25 20:29:11 +00:00
|
|
|
self.user = main.config["Relay"]["User"]
|
2019-01-26 01:57:24 +00:00
|
|
|
else:
|
|
|
|
self.user = user
|
2019-08-25 20:29:11 +00:00
|
|
|
password = main.config["Relay"]["Password"]
|
|
|
|
self.nickname = "relay"
|
|
|
|
self.realname = "relay"
|
2019-01-26 01:57:24 +00:00
|
|
|
self.username = self.user
|
|
|
|
self.password = self.user+":"+password
|
|
|
|
|
|
|
|
self.relayCommands = relayCommands
|
2019-08-25 20:29:11 +00:00
|
|
|
self.num = num
|
2019-01-26 01:57:24 +00:00
|
|
|
self.stage2 = stage2
|
|
|
|
|
|
|
|
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)
|
|
|
|
if nick[0] == main.config["Tweaks"]["ZNC"]["Prefix"]:
|
|
|
|
nick = nick[1:]
|
|
|
|
if nick in self.relayCommands.keys():
|
2019-08-25 20:29:11 +00:00
|
|
|
sendAll("[%s] %s -> %s" % (self.num, nick, msg))
|
2019-01-26 01:57:24 +00:00
|
|
|
|
|
|
|
def irc_ERR_PASSWDMISMATCH(self, prefix, params):
|
2019-08-25 20:29:11 +00:00
|
|
|
log("%s: relay password mismatch" % self.num)
|
|
|
|
sendAll("%s: relay password mismatch" % self.num)
|
2019-01-26 01:57:24 +00:00
|
|
|
|
|
|
|
def signedOn(self):
|
|
|
|
self.connected = True
|
2019-08-25 20:29:11 +00:00
|
|
|
log("signed on as a relay: %s" % self.num)
|
2019-08-11 19:52:10 +00:00
|
|
|
#sendRelayNotification("Relay", {"type": "conn", "status": "connected"}) nobody actually cares
|
2019-01-26 01:57:24 +00:00
|
|
|
for i in self.relayCommands.keys():
|
|
|
|
for x in self.relayCommands[i]:
|
|
|
|
self.msg(main.config["Tweaks"]["ZNC"]["Prefix"]+i, x)
|
|
|
|
if not self.stage2 == None: # [["user", {"sasl": ["message1", "message2"]}], []]
|
|
|
|
if not len(self.stage2) == 0:
|
|
|
|
user = self.stage2[0].pop(0)
|
|
|
|
commands = self.stage2[0].pop(0)
|
|
|
|
del self.stage2[0]
|
2019-08-25 20:29:11 +00:00
|
|
|
deliverRelayCommands(self.num, commands, user, self.stage2)
|
2019-01-26 01:57:24 +00:00
|
|
|
deferLater(reactor, 1, self.transport.loseConnection)
|
|
|
|
return
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
class IRCBot(IRCClient):
|
2019-08-25 20:29:11 +00:00
|
|
|
def __init__(self, net, num):
|
2018-02-23 22:05:40 +00:00
|
|
|
self.connected = False
|
|
|
|
self.channels = []
|
2019-08-25 20:29:11 +00:00
|
|
|
self.net = net
|
|
|
|
self.num = num
|
2018-03-14 20:13:40 +00:00
|
|
|
self.buffer = ""
|
2019-08-25 20:29:11 +00:00
|
|
|
self.name = net + str(num)
|
2019-09-29 21:45:16 +00:00
|
|
|
alias = main.alias[num]
|
2019-08-25 20:29:11 +00:00
|
|
|
relay = main.network[self.net].relays[num]
|
2019-01-26 18:58:21 +00:00
|
|
|
self.nickname = alias["nick"]
|
|
|
|
self.realname = alias["realname"]
|
2019-08-11 20:58:14 +00:00
|
|
|
self.username = alias["nick"]+"/"+relay["net"]
|
2019-08-25 20:29:11 +00:00
|
|
|
self.password = main.config["Relay"]["Password"]
|
2019-01-26 18:58:21 +00:00
|
|
|
self.userinfo = None
|
|
|
|
self.fingerReply = None
|
|
|
|
self.versionName = None
|
2018-02-23 22:05:40 +00:00
|
|
|
self.versionNum = None
|
|
|
|
self.versionEnv = None
|
2019-01-26 18:58:21 +00:00
|
|
|
self.sourceURL = None
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-10-06 20:10:44 +00:00
|
|
|
self._getWho = {} # LoopingCall objects -- needed to be able to stop them
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-10-06 20:10:44 +00:00
|
|
|
self._tempWho = {} # Temporary storage for gathering WHO info
|
|
|
|
self._tempNames = {} # Temporary storage for gathering NAMES info
|
|
|
|
self._tempList = ([], []) # Temporary storage for gathering LIST info
|
|
|
|
self.listOngoing = False
|
2018-08-27 19:42:49 +00:00
|
|
|
|
2019-10-08 17:15:23 +00:00
|
|
|
self.chanLimit = 0
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def parsen(self, user):
|
|
|
|
step = user.split("!")
|
|
|
|
nick = step[0]
|
|
|
|
if len(step) == 2:
|
|
|
|
step2 = step[1].split("@")
|
2019-02-01 23:26:01 +00:00
|
|
|
if len(step2) == 2:
|
|
|
|
ident, host = step2
|
|
|
|
else:
|
|
|
|
ident = nick
|
|
|
|
host = nick
|
2018-02-23 22:05:40 +00:00
|
|
|
else:
|
|
|
|
ident = nick
|
|
|
|
host = nick
|
|
|
|
|
|
|
|
return [nick, ident, host]
|
|
|
|
|
2019-08-11 19:52:10 +00:00
|
|
|
def event(self, **cast):
|
|
|
|
for i in list(cast.keys()): # Make a copy of the .keys() as Python 3 cannot handle iterating over
|
|
|
|
if cast[i] == "": # a dictionary that changes length with each iteration
|
|
|
|
del cast[i]
|
|
|
|
if "muser" in cast.keys():
|
|
|
|
cast["nick"], cast["ident"], cast["host"] = self.parsen(cast["muser"])
|
2019-08-12 20:03:47 +00:00
|
|
|
#if not cast["type"] in ["nick", "kick", "quit", "part", "join"]:
|
|
|
|
# del cast["muser"]
|
2019-10-03 17:02:00 +00:00
|
|
|
if set(["nick", "ident", "host", "msg"]).issubset(set(cast)):
|
|
|
|
if "msg" in cast.keys():
|
2019-08-11 19:52:10 +00:00
|
|
|
if cast["ident"] == "znc" and cast["host"] == "znc.in":
|
|
|
|
cast["type"] = "znc"
|
2019-10-05 17:13:04 +00:00
|
|
|
cast["num"] = self.num
|
2019-08-11 19:52:10 +00:00
|
|
|
del cast["nick"]
|
|
|
|
del cast["ident"]
|
|
|
|
del cast["host"]
|
2019-10-04 23:51:00 +00:00
|
|
|
del cast["channel"]
|
2019-08-11 19:52:10 +00:00
|
|
|
if not cast["type"] in ["query", "self", "highlight", "znc", "who"]:
|
2019-10-04 23:51:00 +00:00
|
|
|
if "channel" in cast.keys() and not cast["type"] == "mode": # don't handle modes here
|
|
|
|
if cast["channel"].lower() == self.nickname.lower(): # as they are channel == nickname
|
2019-08-15 20:20:49 +00:00
|
|
|
#castDup = deepcopy(cast) # however modes are not queries!
|
2019-08-11 19:52:10 +00:00
|
|
|
cast["mtype"] = cast["type"]
|
|
|
|
cast["type"] = "query"
|
2019-10-05 17:13:04 +00:00
|
|
|
cast["num"] = self.num
|
2019-08-11 19:52:10 +00:00
|
|
|
#self.event(**castDup)
|
|
|
|
# Don't call self.event for this one because queries are not events on a
|
|
|
|
# channel, but we still want to see them
|
|
|
|
if "user" in cast.keys():
|
2019-08-11 19:53:26 +00:00
|
|
|
if cast["user"].lower() == self.nickname.lower():
|
2019-08-11 19:52:10 +00:00
|
|
|
castDup = deepcopy(cast)
|
|
|
|
castDup["mtype"] = cast["type"]
|
|
|
|
castDup["type"] = "self"
|
2019-10-05 17:13:04 +00:00
|
|
|
cast["num"] = self.num
|
2019-08-11 19:52:10 +00:00
|
|
|
self.event(**castDup)
|
|
|
|
if "nick" in cast.keys():
|
2019-08-11 19:53:26 +00:00
|
|
|
if cast["nick"].lower() == self.nickname.lower():
|
2019-08-11 19:52:10 +00:00
|
|
|
castDup = deepcopy(cast)
|
|
|
|
castDup["mtype"] = cast["type"]
|
|
|
|
castDup["type"] = "self"
|
2019-10-05 17:13:04 +00:00
|
|
|
cast["num"] = self.num
|
2019-10-04 23:51:00 +00:00
|
|
|
if not cast["channel"].lower() == self.nickname.lower(): # modes has been set on us directly
|
2019-08-15 21:14:45 +00:00
|
|
|
self.event(**castDup) # don't tell anyone else
|
2019-10-03 17:02:00 +00:00
|
|
|
if "msg" in cast.keys() and not cast["type"] == "query": # Don't highlight queries
|
|
|
|
if not cast["msg"] == None:
|
|
|
|
if self.nickname.lower() in cast["msg"].lower():
|
2019-08-15 21:14:45 +00:00
|
|
|
castDup = deepcopy(cast)
|
|
|
|
castDup["mtype"] = cast["type"]
|
|
|
|
castDup["type"] = "highlight"
|
2019-10-05 17:13:04 +00:00
|
|
|
cast["num"] = self.num
|
2019-08-15 21:14:45 +00:00
|
|
|
self.event(**castDup)
|
2019-08-11 19:52:10 +00:00
|
|
|
|
2019-10-05 17:13:04 +00:00
|
|
|
if not "net" in cast.keys():
|
|
|
|
cast["net"] = self.net
|
2019-08-15 20:20:49 +00:00
|
|
|
counters.event(self.net, cast["type"])
|
2019-10-05 17:13:04 +00:00
|
|
|
monitor.event(self.net, cast)
|
2019-08-11 19:52:10 +00:00
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def privmsg(self, user, channel, msg):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="msg", muser=user, channel=channel, msg=msg)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def noticed(self, user, channel, msg):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="notice", muser=user, channel=channel, msg=msg)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def action(self, user, channel, msg):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="action", muser=user, channel=channel, msg=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-08-27 19:42:49 +00:00
|
|
|
def nickChanged(self, olduser, newnick):
|
|
|
|
self.nickname = newnick
|
2019-08-12 20:03:47 +00:00
|
|
|
self.event(type="self", mtype="nick", muser=olduser, user=newnick)
|
2018-03-10 14:01:43 +00:00
|
|
|
|
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):
|
2019-10-02 20:25:15 +00:00
|
|
|
print(locals())
|
|
|
|
print(globals())
|
2019-10-05 17:13:04 +00:00
|
|
|
log("%s - %i: password mismatch" % (self.net, self.num))
|
|
|
|
sendAll("%s - %i: password mismatch" % (self.net, self.num))
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-09-28 18:46:10 +00:00
|
|
|
def _who(self, channel):
|
2018-02-23 22:05:40 +00:00
|
|
|
d = Deferred()
|
2019-09-28 18:46:10 +00:00
|
|
|
if channel not in self._tempWho:
|
|
|
|
self._tempWho[channel] = ([], [])
|
|
|
|
self._tempWho[channel][0].append(d)
|
2018-02-23 22:05:40 +00:00
|
|
|
self.sendLine("WHO %s" % channel)
|
|
|
|
return d
|
|
|
|
|
2019-09-28 18:46:10 +00:00
|
|
|
def who(self, channel):
|
|
|
|
self._who(channel).addCallback(self.got_who)
|
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
def irc_RPL_WHOREPLY(self, prefix, params):
|
|
|
|
channel = params[1]
|
2019-08-11 19:52:10 +00:00
|
|
|
ident = params[2]
|
2018-02-23 22:05:40 +00:00
|
|
|
host = params[3]
|
|
|
|
server = params[4]
|
|
|
|
nick = params[5]
|
|
|
|
status = params[6]
|
|
|
|
realname = params[7]
|
2019-09-28 18:46:10 +00:00
|
|
|
if channel not in self._tempWho:
|
2018-02-23 22:05:40 +00:00
|
|
|
return
|
2019-09-28 18:46:10 +00:00
|
|
|
n = self._tempWho[channel][1]
|
2019-08-11 19:52:10 +00:00
|
|
|
n.append([nick, nick, host, server, status, realname])
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="who", nick=nick, ident=ident, host=host, realname=realname, channel=channel, server=server, status=status)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def irc_RPL_ENDOFWHO(self, prefix, params):
|
|
|
|
channel = params[1]
|
2019-09-28 18:46:10 +00:00
|
|
|
if channel not in self._tempWho:
|
2018-02-23 22:05:40 +00:00
|
|
|
return
|
2019-09-28 18:46:10 +00:00
|
|
|
callbacks, info = self._tempWho[channel]
|
2018-02-23 22:05:40 +00:00
|
|
|
for cb in callbacks:
|
|
|
|
cb.callback((channel, info))
|
2019-09-28 18:46:10 +00:00
|
|
|
del self._tempWho[channel]
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def got_who(self, whoinfo):
|
2018-08-27 19:42:49 +00:00
|
|
|
userinfo.initialUsers(self.net, whoinfo[0], whoinfo[1])
|
|
|
|
|
|
|
|
def sanit(self, data):
|
|
|
|
if len(data) >= 1:
|
|
|
|
if data[0] in ["!", "~", "&", "@", "%", "+"]:
|
|
|
|
data = data[1:]
|
|
|
|
return data
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def names(self, channel):
|
|
|
|
d = Deferred()
|
2019-10-06 20:10:44 +00:00
|
|
|
if channel not in self._tempNames:
|
|
|
|
self._tempNames[channel] = ([], [])
|
|
|
|
self._tempNames[channel][0].append(d)
|
2018-08-27 19:42:49 +00:00
|
|
|
self.sendLine("NAMES %s" % channel)
|
|
|
|
return d
|
|
|
|
|
|
|
|
def irc_RPL_NAMREPLY(self, prefix, params):
|
|
|
|
channel = params[2]
|
|
|
|
nicklist = params[3].split(' ')
|
2019-10-06 20:10:44 +00:00
|
|
|
if channel not in self._tempNames:
|
2018-08-27 19:42:49 +00:00
|
|
|
return
|
2019-10-06 20:10:44 +00:00
|
|
|
n = self._tempNames[channel][1]
|
2018-08-27 19:42:49 +00:00
|
|
|
n.append(nicklist)
|
|
|
|
|
|
|
|
def irc_RPL_ENDOFNAMES(self, prefix, params):
|
|
|
|
channel = params[1]
|
2019-10-06 20:10:44 +00:00
|
|
|
if channel not in self._tempNames:
|
2018-08-27 19:42:49 +00:00
|
|
|
return
|
2019-10-06 20:10:44 +00:00
|
|
|
callbacks, namelist = self._tempNames[channel]
|
2018-08-27 19:42:49 +00:00
|
|
|
for cb in callbacks:
|
|
|
|
cb.callback((channel, namelist))
|
2019-10-06 20:10:44 +00:00
|
|
|
del self._tempNames[channel]
|
2018-08-27 19:42:49 +00:00
|
|
|
|
|
|
|
def got_names(self, nicklist):
|
|
|
|
newNicklist = []
|
|
|
|
for i in nicklist[1]:
|
|
|
|
for x in i:
|
2019-08-11 19:52:10 +00:00
|
|
|
f = self.sanit(x) # need to store this as well, or potentially just do not remove it...
|
2018-08-27 19:42:49 +00:00
|
|
|
if f:
|
|
|
|
newNicklist.append(f)
|
|
|
|
userinfo.initialNames(self.net, nicklist[0], newNicklist)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-10-06 20:10:44 +00:00
|
|
|
def _list(self):
|
|
|
|
d = Deferred()
|
|
|
|
self._tempList = ([], [])
|
|
|
|
self._tempList[0].append(d)
|
2019-10-08 17:15:23 +00:00
|
|
|
self.sendLine("LIST >0")
|
2019-10-06 20:10:44 +00:00
|
|
|
return d
|
|
|
|
|
|
|
|
def list(self):
|
|
|
|
if not self.listOngoing:
|
|
|
|
self._list().addCallback(self.got_list)
|
2019-10-08 17:15:23 +00:00
|
|
|
|
|
|
|
def irc_RPL_LISTSTART(self, prefix, params):
|
|
|
|
self.listOngoing = True
|
2019-10-06 20:10:44 +00:00
|
|
|
|
|
|
|
def irc_RPL_LIST(self, prefix, params):
|
|
|
|
channel = params[1]
|
|
|
|
users = params[2]
|
|
|
|
topic = params[3]
|
|
|
|
self._tempList[1].append([channel, users, topic])
|
|
|
|
|
|
|
|
def irc_RPL_LISTEND(self, prefix, params):
|
|
|
|
callbacks, info = self._tempList
|
|
|
|
for cb in callbacks:
|
|
|
|
cb.callback((info))
|
|
|
|
self.listOngoing = False
|
|
|
|
self._tempList[0].clear()
|
|
|
|
self._tempList[1].clear()
|
|
|
|
|
|
|
|
def got_list(self, listinfo):
|
2019-10-08 17:15:23 +00:00
|
|
|
chankeep.initialList(self.net, self.num, listinfo, self.chanLimit)
|
|
|
|
|
|
|
|
def isupport(self, options):
|
|
|
|
for i in options:
|
|
|
|
if i.startswith("CHANLIMIT"):
|
|
|
|
if "#" in i:
|
|
|
|
split = i.split("#")
|
|
|
|
if len(split) >= 2:
|
|
|
|
chanLimit = split[-1].replace(":", "")
|
|
|
|
try:
|
|
|
|
self.chanLimit = int(chanLimit)
|
|
|
|
return
|
|
|
|
except TypeError:
|
|
|
|
error("Invalid CHANLIMIT: %s" % i)
|
2019-10-06 20:10:44 +00:00
|
|
|
|
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:
|
2019-08-05 21:51:16 +00:00
|
|
|
self.left(prefix, channel, message)
|
2018-03-14 20:13:40 +00:00
|
|
|
else:
|
|
|
|
self.userLeft(prefix, channel, message)
|
|
|
|
|
|
|
|
def irc_QUIT(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user has quit.
|
|
|
|
"""
|
2019-08-05 21:51:16 +00:00
|
|
|
nick = prefix.split('!')[0]
|
2019-08-11 20:52:46 +00:00
|
|
|
#if nick == self.nickname:
|
|
|
|
#self.botQuit(prefix, params[0])
|
|
|
|
#else:
|
|
|
|
self.userQuit(prefix, params[0])
|
2018-03-14 20:13:40 +00:00
|
|
|
|
|
|
|
def irc_NICK(self, prefix, params):
|
|
|
|
"""
|
|
|
|
Called when a user changes their nickname.
|
|
|
|
"""
|
|
|
|
nick = prefix.split('!', 1)[0]
|
|
|
|
if nick == self.nickname:
|
2018-08-27 19:42:49 +00:00
|
|
|
self.nickChanged(prefix, params[0])
|
2018-03-14 20:13:40 +00:00
|
|
|
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]
|
2019-08-11 19:52:10 +00:00
|
|
|
# Checks on whether it was us that was kicked are done in userKicked
|
|
|
|
self.userKicked(kicked, channel, prefix, message)
|
2018-03-14 20:13:40 +00:00
|
|
|
|
|
|
|
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
|
2019-10-05 17:13:04 +00:00
|
|
|
log("signed on: %s - %i" % (self.net, self.num))
|
2019-10-03 17:02:00 +00:00
|
|
|
#self.event(type="conn", status="connected")
|
2019-10-05 17:13:04 +00:00
|
|
|
sendRelayNotification({"type": "conn", "net": self.net, "num": self.num, "status": "signedon"})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def joined(self, channel):
|
|
|
|
if not channel in self.channels:
|
|
|
|
self.channels.append(channel)
|
2018-08-27 19:42:49 +00:00
|
|
|
self.names(channel).addCallback(self.got_names)
|
2019-08-11 19:52:10 +00:00
|
|
|
if main.config["Toggles"]["Who"]:
|
2019-09-28 18:46:10 +00:00
|
|
|
#self.who(channel).addCallback(self.got_who)
|
|
|
|
#self.who(channel)
|
2019-08-11 19:52:10 +00:00
|
|
|
lc = LoopingCall(self.who, channel)
|
|
|
|
self._getWho[channel] = lc
|
|
|
|
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-08-27 19:42:49 +00:00
|
|
|
def botLeft(self, channel):
|
2018-02-23 22:05:40 +00:00
|
|
|
if channel in self.channels:
|
|
|
|
self.channels.remove(channel)
|
2018-08-27 19:42:49 +00:00
|
|
|
if channel in self._getWho.keys():
|
|
|
|
lc = self._getWho[channel]
|
|
|
|
lc.stop()
|
|
|
|
del self._getWho[channel]
|
2019-08-12 20:03:47 +00:00
|
|
|
userinfo.delChannel(self.net, channel) # < we do not need to deduplicate this
|
2019-08-19 19:12:42 +00:00
|
|
|
#log("Can no longer cover %s, removing records" % channel)# as it will only be matched once --
|
2019-08-12 20:03:47 +00:00
|
|
|
# other bots have different nicknames so
|
|
|
|
def left(self, user, channel, message): # even if they saw it, they wouldn't react
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="part", muser=user, channel=channel, message=message)
|
2018-08-27 19:42:49 +00:00
|
|
|
self.botLeft(channel)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userJoined(self, user, channel):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="join", muser=user, channel=channel)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2018-03-14 20:13:40 +00:00
|
|
|
def userLeft(self, user, channel, message):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="part", muser=user, channel=channel, message=message)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userQuit(self, user, quitMessage):
|
2019-08-15 20:20:49 +00:00
|
|
|
self.chanlessEvent({"type": "quit", "muser": user, "message": quitMessage})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userKicked(self, kickee, channel, kicker, message):
|
2019-08-12 20:03:47 +00:00
|
|
|
if kickee.lower() == self.nickname.lower():
|
2019-08-11 19:52:10 +00:00
|
|
|
self.botLeft(channel)
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="kick", muser=kicker, channel=channel, message=message, user=kickee)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
2019-08-15 20:20:49 +00:00
|
|
|
def chanlessEvent(self, cast):
|
|
|
|
cast["nick"], cast["ident"], cast["host"] = self.parsen(cast["muser"])
|
2019-10-05 17:13:04 +00:00
|
|
|
if dedup(self.name, cast): # Needs to be kept self.name until the dedup
|
|
|
|
# function is converted to the new net, num
|
|
|
|
# format
|
2019-08-15 20:20:49 +00:00
|
|
|
return # stop right there sir!
|
|
|
|
chans = userinfo.getChanList(self.net, cast["nick"])
|
2019-08-11 20:52:46 +00:00
|
|
|
if chans == None:
|
2019-08-15 20:20:49 +00:00
|
|
|
error("No channels returned for chanless event: %s" % cast)
|
|
|
|
# self.event(**cast) -- no, should NEVER happen
|
2019-08-11 20:52:46 +00:00
|
|
|
return
|
2019-08-12 20:03:47 +00:00
|
|
|
# getChansSingle returns all channels of the user, we only want to use
|
|
|
|
# ones we have common with them
|
|
|
|
realChans = set(chans).intersection(set(self.channels))
|
2019-08-15 20:20:49 +00:00
|
|
|
for i in realChans:
|
2019-10-04 23:51:00 +00:00
|
|
|
cast["channel"] = i
|
2019-08-11 20:52:46 +00:00
|
|
|
self.event(**cast)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def userRenamed(self, oldname, newname):
|
2019-08-15 20:20:49 +00:00
|
|
|
self.chanlessEvent({"type": "nick", "muser": oldname, "user": newname})
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def topicUpdated(self, user, channel, newTopic):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="topic", muser=user, channel=channel, message= newTopic)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def modeChanged(self, user, channel, toset, modes, args):
|
2018-07-28 20:30:50 +00:00
|
|
|
argList = list(args)
|
|
|
|
modeList = [i for i in modes]
|
|
|
|
for a, m in zip(argList, modeList):
|
2019-10-04 23:51:00 +00:00
|
|
|
self.event(type="mode", muser=user, channel=channel, modes=m, status=toset, modeargs=a)
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
class IRCBotFactory(ReconnectingClientFactory):
|
2019-08-25 20:29:11 +00:00
|
|
|
def __init__(self, net, num=None, relayCommands=None, user=None, stage2=None):
|
|
|
|
if net == None:
|
|
|
|
self.num = num
|
2019-10-08 17:15:23 +00:00
|
|
|
self.net = None
|
2019-10-05 17:13:04 +00:00
|
|
|
self.name = "relay - %i" % num
|
2019-08-25 20:29:11 +00:00
|
|
|
self.relay = True
|
2019-01-26 01:57:24 +00:00
|
|
|
else:
|
2019-10-05 17:13:04 +00:00
|
|
|
self.name = net+str(num)
|
2019-08-25 20:29:11 +00:00
|
|
|
self.num = num
|
|
|
|
self.net = net
|
|
|
|
self.relay = False
|
2018-02-23 22:05:40 +00:00
|
|
|
self.client = None
|
2019-01-26 01:57:24 +00:00
|
|
|
self.maxDelay = main.config["Tweaks"]["Delays"]["MaxDelay"]
|
|
|
|
self.initialDelay = main.config["Tweaks"]["Delays"]["InitialDelay"]
|
|
|
|
self.factor = main.config["Tweaks"]["Delays"]["Factor"]
|
|
|
|
self.jitter = main.config["Tweaks"]["Delays"]["Jitter"]
|
|
|
|
|
2019-08-25 20:29:11 +00:00
|
|
|
self.relayCommands, self.user, self.stage2 = relayCommands, user, stage2
|
2018-02-23 22:05:40 +00:00
|
|
|
|
|
|
|
def buildProtocol(self, addr):
|
2019-09-29 13:57:36 +00:00
|
|
|
if self.relay == False:
|
2019-08-25 20:29:11 +00:00
|
|
|
entry = IRCBot(self.net, self.num)
|
|
|
|
main.IRCPool[self.name] = entry
|
2019-09-29 13:57:36 +00:00
|
|
|
else:
|
|
|
|
entry = IRCRelay(self.num, self.relayCommands, self.user, self.stage2)
|
2019-01-26 01:57:24 +00:00
|
|
|
|
2018-02-23 22:05:40 +00:00
|
|
|
self.client = entry
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def clientConnectionLost(self, connector, reason):
|
2019-01-26 01:57:24 +00:00
|
|
|
if not self.relay:
|
|
|
|
userinfo.delNetwork(self.net, self.client.channels)
|
2018-02-23 22:05:40 +00:00
|
|
|
if not self.client == None:
|
|
|
|
self.client.connected = False
|
|
|
|
self.client.channels = []
|
|
|
|
error = reason.getErrorMessage()
|
2019-10-05 17:13:04 +00:00
|
|
|
log("%s - %i: connection lost: %s" % (self.net, self.num, error))
|
2019-01-26 01:57:24 +00:00
|
|
|
if not self.relay:
|
2019-10-05 17:13:04 +00:00
|
|
|
sendAll("%s - %i: connection lost: %s" % (self.net, self.num, error))
|
|
|
|
sendRelayNotification({"type": "conn", "net": self.net, "num": self.num, "status": "lost", "message": error})
|
2019-01-26 01:57:24 +00:00
|
|
|
self.retry(connector)
|
2018-02-23 22:05:40 +00:00
|
|
|
#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()
|
2019-10-05 17:13:04 +00:00
|
|
|
log("%s - %i: connection failed: %s" % (self.net, self.num, error))
|
2019-08-05 21:51:16 +00:00
|
|
|
if not self.relay:
|
2019-10-05 17:13:04 +00:00
|
|
|
sendAll("%s -%s: connection failed: %s" % (self.net, self.num, error))
|
|
|
|
sendRelayNotification({"type": "conn", "net": self.net, "num": self.num, "status": "failed", "message": error})
|
2019-01-26 01:57:24 +00:00
|
|
|
self.retry(connector)
|
2018-02-23 22:05:40 +00:00
|
|
|
#ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
|
|
|
|
|