2020-11-15 15:53:28 +01:00
|
|
|
#!/bin/env python3
|
2020-11-17 18:15:06 +01:00
|
|
|
import asyncio, configparser
|
2020-11-15 15:53:28 +01:00
|
|
|
from irctokens import build, Line
|
|
|
|
from ircrobots import Bot as BaseBot
|
|
|
|
from ircrobots import Server as BaseServer
|
|
|
|
from ircrobots import ConnectionParams
|
|
|
|
|
2020-11-17 18:15:06 +01:00
|
|
|
servers = []
|
|
|
|
match_players = {}
|
2020-11-24 19:29:56 +01:00
|
|
|
connections = {}
|
2020-11-15 15:53:28 +01:00
|
|
|
|
2020-11-16 02:35:34 +01:00
|
|
|
def update_file(name):
|
|
|
|
file = open(f"playerlist-{name}.txt", "w")
|
2020-11-15 15:53:28 +01:00
|
|
|
file.seek(0)
|
|
|
|
file.truncate()
|
2020-11-16 02:35:34 +01:00
|
|
|
for player in match_players[name]:
|
2020-11-15 15:53:28 +01:00
|
|
|
file.write(player + '\n')
|
|
|
|
|
2020-11-17 18:15:06 +01:00
|
|
|
def read_file(name):
|
|
|
|
file = open(f"playerlist-{name}.txt", "r")
|
|
|
|
for player in file:
|
|
|
|
match_players[name].add(player[:-1]) #Remove newline
|
|
|
|
file.close()
|
2020-11-15 15:53:28 +01:00
|
|
|
|
|
|
|
class Server(BaseServer):
|
|
|
|
async def line_read(self, line: Line):
|
|
|
|
print(f"{self.name} < {line.format()}")
|
|
|
|
|
2020-11-17 18:15:06 +01:00
|
|
|
if "PRIVMSG" in line.command and any(channel in line.params[0] for channel in self.params.autojoin):
|
2020-11-16 00:39:54 +01:00
|
|
|
chan = self.channels[line.params[0]].name
|
2020-11-15 15:53:28 +01:00
|
|
|
if line.params[1].startswith(".matchmake"):
|
|
|
|
channel = self.channels[line.params[0]]
|
|
|
|
ping = ""
|
2020-11-17 18:15:06 +01:00
|
|
|
for player in match_players[self.name + '-' + chan]:
|
2020-11-15 15:53:28 +01:00
|
|
|
pfold = self.casefold(player)
|
|
|
|
if pfold in channel.users:
|
|
|
|
ping += f"{channel.users[pfold].nickname} "
|
|
|
|
|
|
|
|
await self.send(build("PRIVMSG", [chan, f"Pinging users: {ping}"]))
|
2020-11-24 19:29:56 +01:00
|
|
|
if connections[self.name]:
|
|
|
|
for connection,address in connections[self.name].items():
|
|
|
|
if connection in line.params[1]:
|
|
|
|
if ':' in address:
|
|
|
|
realaddress = address.split(':')[0]
|
|
|
|
port = address.split(':')[1]
|
|
|
|
self.send(build("PRIVMSG", [chan, f"Connect using nc {realaddress} {port}"]))
|
|
|
|
else:
|
|
|
|
self.send(build("PRIVMSG", [chan, f"Connect using nc {address} 1234"]))
|
2020-11-15 15:53:28 +01:00
|
|
|
|
|
|
|
elif line.params[1].startswith(".matchadd"):
|
2020-11-16 02:35:34 +01:00
|
|
|
user = line.source.split('!')[0]
|
2020-11-17 18:15:06 +01:00
|
|
|
if user in match_players[self.name + '-' + chan]:
|
2020-11-15 15:53:28 +01:00
|
|
|
await self.send(build("PRIVMSG", [chan, "ERROR: player already in list."]))
|
|
|
|
else:
|
2020-11-17 18:15:06 +01:00
|
|
|
match_players[self.name + '-' + chan].add(user)
|
|
|
|
update_file(self.name + '-' + chan)
|
2020-11-15 15:53:28 +01:00
|
|
|
await self.send(build("PRIVMSG", [chan, "Added to the match list."]))
|
|
|
|
|
|
|
|
elif line.params[1].startswith(".matchdel"):
|
|
|
|
user = line.source.split('!')[0]
|
2020-11-17 18:15:06 +01:00
|
|
|
if user not in match_players[self.name + '-' + chan]:
|
2020-11-15 15:53:28 +01:00
|
|
|
await self.send(build("PRIVMSG", [chan, "ERROR: player isn't in list."]))
|
|
|
|
else:
|
2020-11-17 18:15:06 +01:00
|
|
|
match_players[self.name + '-' + chan].remove(user)
|
|
|
|
update_file(self.name + '-' + chan)
|
2020-11-15 15:53:28 +01:00
|
|
|
await self.send(build("PRIVMSG", [chan, "Removed from match list."]))
|
|
|
|
|
|
|
|
elif line.params[1].startswith(".help"):
|
2020-11-19 03:16:43 +01:00
|
|
|
await self.send(build("PRIVMSG", [chan, " .matchadd: Add to list; .matchdel: Remove from list; .matchmake: Ping everyone on list"]))
|
2020-11-15 15:53:28 +01:00
|
|
|
|
|
|
|
async def line_send(self, line: Line):
|
|
|
|
print(f"{self.name} > {line.format()}")
|
|
|
|
|
|
|
|
class Bot(BaseBot):
|
|
|
|
def create_server(self, name: str):
|
|
|
|
return Server(self, name)
|
|
|
|
|
|
|
|
async def main():
|
2020-11-17 18:15:06 +01:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
with open('bot.ini', 'r') as configfile:
|
|
|
|
config.read_file(configfile)
|
|
|
|
for section in config.sections():
|
|
|
|
if not config[section]['server']:
|
|
|
|
print(f"ERROR: The section {section} on your bot.ini doesn't have a 'server' parameter.")
|
|
|
|
exit(1)
|
|
|
|
elif not config[section]['nickname']:
|
|
|
|
print(f"ERROR: The section {section} has no nickname defined.")
|
|
|
|
exit(1)
|
|
|
|
elif not config[section]['channels']:
|
|
|
|
print(f"ERROR: You didn't define any channels in section {section}.")
|
2020-11-24 18:14:29 +01:00
|
|
|
exit(1)
|
2020-11-17 18:15:06 +01:00
|
|
|
server = config[section]['server']
|
|
|
|
nickname = config[section]['nickname']
|
2020-11-24 19:29:56 +01:00
|
|
|
channels = set(config[section]['channels'].split())
|
|
|
|
if config[section]['connections']:
|
|
|
|
if len(config[section]['connections'].split()) % 2 != 0: # If connections isn't even
|
|
|
|
print(f"ERROR: The section {section} has an invalid 'connections' defined.")
|
|
|
|
exit(1)
|
|
|
|
config_connections = config[section]['connections'].split()
|
|
|
|
connections.update({section:{}})
|
|
|
|
for i in range(0, len(config_connections), 2):
|
|
|
|
connections[section].update({config_connections[i]:config_connections[i+1]})
|
|
|
|
|
2020-11-17 18:15:06 +01:00
|
|
|
servers.append({'name':section, 'opts':{'server':server, 'nickname':nickname, 'channels':channels}})
|
|
|
|
for channel in channels:
|
|
|
|
match_players.update([(section + '-' + channel, set())])
|
|
|
|
|
|
|
|
# read_file()
|
2020-11-15 15:53:28 +01:00
|
|
|
bot = Bot()
|
2020-11-17 18:15:06 +01:00
|
|
|
for entry in servers:
|
|
|
|
name = entry['name']
|
|
|
|
server = entry['opts']['server']
|
|
|
|
botnick = entry['opts']['nickname']
|
|
|
|
channels = entry['opts']['channels']
|
|
|
|
|
|
|
|
params = ConnectionParams(botnick, server, 6697, True)
|
|
|
|
for channel in channels:
|
|
|
|
params.autojoin.append(channel)
|
|
|
|
read_file(name + '-' + channel)
|
2020-11-15 15:53:28 +01:00
|
|
|
await bot.add_server(name, params)
|
|
|
|
|
|
|
|
await bot.run()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|