Import the main module properly and fix some oddities in Twisted to prevent it from discarding some data
This commit is contained in:
113
core/bot.py
113
core/bot.py
@@ -6,7 +6,7 @@ import modules.keyword as keyword
|
||||
import modules.userinfo as userinfo
|
||||
import modules.counters as count
|
||||
|
||||
from core.main import *
|
||||
import main
|
||||
from utils.logging.log import *
|
||||
from utils.logging.send import *
|
||||
|
||||
@@ -14,9 +14,9 @@ class IRCBot(IRCClient):
|
||||
def __init__(self, name):
|
||||
self.connected = False
|
||||
self.channels = []
|
||||
|
||||
self.buffer = ""
|
||||
self.name = name
|
||||
instance = pool[name]
|
||||
instance = main.pool[name]
|
||||
|
||||
self.nickname = instance["nickname"]
|
||||
self.realname = instance["realname"]
|
||||
@@ -40,7 +40,7 @@ class IRCBot(IRCClient):
|
||||
self.password = instance["password"]
|
||||
|
||||
def refresh(self):
|
||||
instance = pool[self.name]
|
||||
instance = main.pool[self.name]
|
||||
if not instance["nickname"] == self.nickname:
|
||||
self.nickname = instance["nickname"]
|
||||
self.setNick(self.nickname)
|
||||
@@ -69,21 +69,21 @@ class IRCBot(IRCClient):
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "privmsg")
|
||||
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "PRV", self.name)
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "MSG", self.name)
|
||||
|
||||
def noticed(self, user, channel, msg):
|
||||
nick, ident, host = self.parsen(user)
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "notice")
|
||||
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "NOT", self.name)
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "NOTICE", self.name)
|
||||
|
||||
def action(self, user, channel, msg):
|
||||
nick, ident, host = self.parsen(user)
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "action")
|
||||
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "ACT", self.name)
|
||||
keyword.actKeyword(user, channel, msg, self.nickname, "ACTION", self.name)
|
||||
|
||||
def get(self, var):
|
||||
try:
|
||||
@@ -148,10 +148,79 @@ class IRCBot(IRCClient):
|
||||
def got_who(self, whoinfo):
|
||||
userinfo.setWho(self.name, whoinfo[1])
|
||||
|
||||
|
||||
#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
|
||||
|
||||
def signedOn(self):
|
||||
self.connected = True
|
||||
log("signed on: %s" % self.name)
|
||||
if config["ConnectionNotifications"]:
|
||||
if main.config["ConnectionNotifications"]:
|
||||
keyword.sendMaster("SIGNON: %s" % self.name)
|
||||
if self.authtype == "ns":
|
||||
self.msg(self.authentity, "IDENTIFY %s" % self.nspass)
|
||||
@@ -164,20 +233,21 @@ class IRCBot(IRCClient):
|
||||
self.channels.append(channel)
|
||||
self.who(channel).addCallback(self.got_who)
|
||||
count.event(self.name, "selfjoin")
|
||||
if self.name == config["Master"][0] and channel == config["Master"][1]:
|
||||
for i in range(len(masterbuf)):
|
||||
self.msg(channel, masterbuf.pop(0))
|
||||
saveConf("masterbuf")
|
||||
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")
|
||||
|
||||
def left(self, channel):
|
||||
def left(self, channel, message):
|
||||
if channel in self.channels:
|
||||
self.channels.remove(channel)
|
||||
keyword.actKeyword(user, channel, message, self.nickname, "SELFPART", self.name)
|
||||
count.event(self.name, "selfpart")
|
||||
|
||||
def kickedFrom(self, channel, kicker, message):
|
||||
if channel in self.channels:
|
||||
self.channels.remove(channel)
|
||||
keyword.sendMaster("KICK %s: (%s/%s) %s" % (self.name, kicker, channel, message))
|
||||
keyword.sendMaster("KICK %s: (%s/%s) %s" % (self.name, kicker, channel, message))
|
||||
count.event(self.name, "selfkick")
|
||||
|
||||
def userJoined(self, user, channel):
|
||||
@@ -185,9 +255,10 @@ class IRCBot(IRCClient):
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "join")
|
||||
|
||||
def userLeft(self, user, channel):
|
||||
def userLeft(self, user, channel, message):
|
||||
nick, ident, host = self.parsen(user)
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
keyword.actKeyword(user, channel, message, self.nickname, "PART", self.name)
|
||||
count.event(self.name, "part")
|
||||
|
||||
def userQuit(self, user, quitMessage):
|
||||
@@ -195,14 +266,14 @@ class IRCBot(IRCClient):
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "quit")
|
||||
|
||||
keyword.actKeyword(user, None, quitMessage, self.nickname, "QUT", self.name)
|
||||
keyword.actKeyword(user, None, quitMessage, self.nickname, "QUIT", self.name)
|
||||
|
||||
def userKicked(self, kickee, channel, kicker, message):
|
||||
nick, ident, host = self.parsen(kicker)
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "kick")
|
||||
|
||||
keyword.actKeyword(kicker, channel, message, self.nickname, "KCK", self.name)
|
||||
keyword.actKeyword(kicker, channel, message, self.nickname, "KICK", self.name)
|
||||
|
||||
def userRenamed(self, oldname, newname):
|
||||
nick, ident, host = self.parsen(oldname)
|
||||
@@ -215,7 +286,7 @@ class IRCBot(IRCClient):
|
||||
userinfo.setWhoSingle(self.name, nick, ident, host)
|
||||
count.event(self.name, "topic")
|
||||
|
||||
keyword.actKeyword(user, channel, newTopic, self.nickname, "TOP", self.name)
|
||||
keyword.actKeyword(user, channel, newTopic, self.nickname, "TOPIC", self.name)
|
||||
|
||||
def modeChanged(self, user, channel, toset, modes, args):
|
||||
nick, ident, host = self.parsen(user)
|
||||
@@ -224,7 +295,7 @@ class IRCBot(IRCClient):
|
||||
|
||||
class IRCBotFactory(ReconnectingClientFactory):
|
||||
def __init__(self, name):
|
||||
self.instance = pool[name]
|
||||
self.instance = main.pool[name]
|
||||
self.name = name
|
||||
self.client = None
|
||||
self.maxDelay = self.instance["maxdelay"]
|
||||
@@ -234,7 +305,7 @@ class IRCBotFactory(ReconnectingClientFactory):
|
||||
|
||||
def buildProtocol(self, addr):
|
||||
entry = IRCBot(self.name)
|
||||
IRCPool[self.name] = entry
|
||||
main.IRCPool[self.name] = entry
|
||||
self.client = entry
|
||||
return entry
|
||||
|
||||
@@ -245,7 +316,7 @@ class IRCBotFactory(ReconnectingClientFactory):
|
||||
error = reason.getErrorMessage()
|
||||
log("%s: connection lost: %s" % (self.name, error))
|
||||
sendAll("%s: connection lost: %s" % (self.name, error))
|
||||
if config["ConnectionNotifications"]:
|
||||
if main.config["ConnectionNotifications"]:
|
||||
keyword.sendMaster("CONNLOST %s: %s" % (self.name, error))
|
||||
self.retry(connector)
|
||||
#ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
|
||||
|
||||
@@ -2,43 +2,43 @@ from twisted.internet import reactor
|
||||
from twisted.internet.ssl import DefaultOpenSSLContextFactory
|
||||
|
||||
from core.bot import IRCBot, IRCBotFactory
|
||||
from core.main import *
|
||||
import main
|
||||
from utils.logging.log import *
|
||||
|
||||
def addBot(name):
|
||||
instance = pool[name]
|
||||
instance = main.pool[name]
|
||||
log("Started bot %s to %s:%s protocol %s nickname %s" % (name, instance["host"], instance["port"], instance["protocol"], instance["nickname"]))
|
||||
if instance["protocol"] == "plain":
|
||||
if instance["bind"] == None:
|
||||
bot = IRCBotFactory(name)
|
||||
rct = reactor.connectTCP(instance["host"], instance["port"], bot, timeout=int(instance["timeout"]))
|
||||
|
||||
ReactorPool[name] = rct
|
||||
FactoryPool[name] = bot
|
||||
main.ReactorPool[name] = rct
|
||||
main.FactoryPool[name] = bot
|
||||
return
|
||||
else:
|
||||
bot = IRCBotFactory(name)
|
||||
rct = reactor.connectTCP(instance["host"], instance["port"], bot, timeout=int(instance["timeout"]), bindAddress=instance["bind"])
|
||||
|
||||
ReactorPool[name] = rct
|
||||
FactoryPool[name] = bot
|
||||
main.ReactorPool[name] = rct
|
||||
main.FactoryPool[name] = bot
|
||||
return
|
||||
elif instance["protocol"] == "ssl":
|
||||
keyFN = certPath+instance["key"]
|
||||
certFN = certPath+instance["certificate"]
|
||||
keyFN = main.certPath+instance["key"]
|
||||
certFN = main.certPath+instance["certificate"]
|
||||
contextFactory = DefaultOpenSSLContextFactory(keyFN.encode("utf-8", "replace"), certFN.encode("utf-8", "replace"))
|
||||
if instance["bind"] == None:
|
||||
bot = IRCBotFactory(name)
|
||||
rct = reactor.connectSSL(instance["host"], int(instance["port"]), bot, contextFactory)
|
||||
|
||||
ReactorPool[name] = rct
|
||||
FactoryPool[name] = bot
|
||||
main.ReactorPool[name] = rct
|
||||
main.FactoryPool[name] = bot
|
||||
return
|
||||
else:
|
||||
|
||||
bot = IRCBotFactory(name)
|
||||
rct = reactor.connectSSL(instance["host"], int(instance["port"]), bot, contextFactory, bindAddress=instance["bind"])
|
||||
|
||||
ReactorPool[name] = rct
|
||||
FactoryPool[name] = bot
|
||||
main.ReactorPool[name] = rct
|
||||
main.FactoryPool[name] = bot
|
||||
return
|
||||
|
||||
50
core/main.py
50
core/main.py
@@ -1,50 +0,0 @@
|
||||
from json import load, dump, loads
|
||||
|
||||
from utils.loaders.command_loader import loadCommands
|
||||
from utils.logging.log import *
|
||||
|
||||
configPath = "conf/"
|
||||
certPath = "cert/"
|
||||
|
||||
filemap = {
|
||||
"config": ["config.json", "configuration"],
|
||||
"keyconf": ["keyword.json", "keyword lists"],
|
||||
"pool": ["pool.json", "pool"],
|
||||
"help": ["help.json", "command help"],
|
||||
"wholist": ["wholist.json", "WHO lists"],
|
||||
"counters": ["counters.json", "counters file"],
|
||||
"masterbuf": ["masterbuf.json", "master buffer"],
|
||||
}
|
||||
|
||||
connections = {}
|
||||
IRCPool = {}
|
||||
ReactorPool = {}
|
||||
FactoryPool = {}
|
||||
|
||||
MonitorPool = []
|
||||
|
||||
CommandMap = {}
|
||||
|
||||
def register(command, function):
|
||||
if not command in CommandMap:
|
||||
CommandMap[command] = function
|
||||
debug("Registered command: %s" % command)
|
||||
else:
|
||||
error("Duplicate command: %s" % (command))
|
||||
|
||||
def saveConf(var):
|
||||
with open(configPath+filemap[var][0], "w") as f:
|
||||
dump(globals()[var], f, indent=4)
|
||||
return
|
||||
|
||||
def loadConf(var):
|
||||
with open(configPath+filemap[var][0], "r") as f:
|
||||
globals()[var] = load(f)
|
||||
|
||||
def initConf():
|
||||
for i in filemap.keys():
|
||||
loadConf(i)
|
||||
|
||||
def initMain():
|
||||
initConf()
|
||||
loadCommands(register)
|
||||
@@ -1,12 +1,12 @@
|
||||
from core.main import *
|
||||
import main
|
||||
from utils.logging.log import *
|
||||
from utils.logging.send import *
|
||||
|
||||
def parseCommand(addr, authed, data):
|
||||
#call command modules with: (addr, authed, data, spl, success, failure, info, incUsage, length)
|
||||
spl = data.split()
|
||||
if addr in connections.keys():
|
||||
obj = connections[addr]
|
||||
if addr in main.connections.keys():
|
||||
obj = main.connections[addr]
|
||||
else:
|
||||
warn("Got connection object with no instance in the address pool")
|
||||
return
|
||||
@@ -22,9 +22,9 @@ def parseCommand(addr, authed, data):
|
||||
else:
|
||||
failure("No text was sent")
|
||||
return
|
||||
for i in CommandMap.keys():
|
||||
for i in main.CommandMap.keys():
|
||||
if spl[0] == i:
|
||||
CommandMap[i](addr, authed, data, obj, spl, success, failure, info, incUsage, length)
|
||||
main.CommandMap[i](addr, authed, data, obj, spl, success, failure, info, incUsage, length)
|
||||
return
|
||||
incUsage(None)
|
||||
return
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from twisted.internet.protocol import Protocol, Factory, ClientFactory
|
||||
from core.main import *
|
||||
import main
|
||||
from utils.logging.log import *
|
||||
|
||||
from core.parser import parseCommand
|
||||
@@ -8,7 +8,7 @@ class Server(Protocol):
|
||||
def __init__(self, addr):
|
||||
self.addr = addr
|
||||
self.authed = False
|
||||
if config["UsePassword"] == False:
|
||||
if main.config["UsePassword"] == False:
|
||||
self.authed = True
|
||||
|
||||
def send(self, data):
|
||||
@@ -34,22 +34,22 @@ class Server(Protocol):
|
||||
def connectionLost(self, reason):
|
||||
self.authed = False
|
||||
log("Connection lost from %s:%s -- %s" % (self.addr.host, self.addr.port, reason.getErrorMessage()))
|
||||
if self.addr in connections.keys():
|
||||
del connections[self.addr]
|
||||
if self.addr in main.connections.keys():
|
||||
del main.connections[self.addr]
|
||||
else:
|
||||
warn("Tried to remove a non-existant connection.")
|
||||
if self.addr in MonitorPool:
|
||||
MonitorPool.remove(self.addr)
|
||||
if self.addr in main.MonitorPool:
|
||||
main.MonitorPool.remove(self.addr)
|
||||
|
||||
class ServerFactory(Factory):
|
||||
def buildProtocol(self, addr):
|
||||
entry = Server(addr)
|
||||
connections[addr] = entry
|
||||
main.connections[addr] = entry
|
||||
return entry
|
||||
|
||||
def send(self, addr, data):
|
||||
if addr in connections.keys():
|
||||
connection = connections[addr]
|
||||
if addr in main.connections.keys():
|
||||
connection = main.connections[addr]
|
||||
connection.send(data)
|
||||
else:
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user