Add optional aliases for IRCServers, mostly to make logging prettier.

This commit is contained in:
jesopo 2018-09-19 01:13:54 +01:00
parent 2abb3c6787
commit b7dda02951
3 changed files with 14 additions and 10 deletions

View file

@ -5,22 +5,22 @@ class Table(object):
self.database = database
class Servers(Table):
def add(self, hostname, port, password, ipv4, tls, nickname,
def add(self, alias, hostname, port, password, ipv4, tls, nickname,
username=None, realname=None):
username = username or nickname
realname = realname or nickname
self.database.execute(
"""INSERT INTO servers (hostname, port, password, ipv4,
"""INSERT INTO servers (alias, hostname, port, password, ipv4,
tls, nickname, username, realname) VALUES (
?, ?, ?, ?, ?, ?, ?, ?)""",
[hostname, port, password, ipv4, tls, nickname, username, realname])
def get_all(self):
return self.database.execute_fetchall(
"""SELECT server_id, hostname, port, password, ipv4,
"""SELECT server_id, alias, hostname, port, password, ipv4,
tls, nickname, username, realname FROM servers""")
def get(self, id):
return self.database.execute_fetchone(
"""SELECT server_id, hostname, port, password, ipv4,
"""SELECT server_id, alias, hostname, port, password, ipv4,
tls, nickname, username, realname FROM servers WHERE
server_id=?""",
[id])
@ -306,7 +306,7 @@ class Database(object):
def make_servers_table(self):
if not self.has_table("servers"):
self.execute("""CREATE TABLE servers
(server_id INTEGER PRIMARY KEY, hostname TEXT,
(server_id INTEGER PRIMARY KEY, alias TEXT, hostname TEXT,
port INTEGER,password TEXT,ipv4 BOOLEAN, tls BOOLEAN,
nickname TEXT, username TEXT, realname TEXT)""")
def make_channels_table(self):

View file

@ -24,10 +24,11 @@ class Bot(object):
self._events.on("timer").on("reconnect").hook(self.reconnect)
self._events.on("boot").on("done").hook(self.setup_timers)
def add_server(self, id, hostname, port, password, ipv4, tls,
def add_server(self, id, alias, hostname, port, password, ipv4, tls,
nickname, username, realname, connect=False):
new_server = IRCServer.Server(self, self._events, id, hostname,
port, password, ipv4, tls, nickname, username, realname)
new_server = IRCServer.Server(self, self._events, id, alias,
hostname, port, password, ipv4, tls, nickname, username,
realname)
if not new_server.get_setting("connect", True):
return
self._events.on("new").on("server").call(server=new_server)

View file

@ -11,12 +11,13 @@ if hasattr(ssl, "PROTOCOL_TLS"):
OUR_TLS_PROTOCOL = ssl.PROTOCOL_TLS
class Server(object):
def __init__(self, bot, events, id, hostname, port, password, ipv4,
tls, nickname, username, realname):
def __init__(self, bot, events, id, alias, hostname, port, password,
ipv4, tls, nickname, username, realname):
self.connected = False
self.bot = bot
self.events = events
self.id = id
self.alias = alias
self.target_hostname = hostname
self.port = port
self.tls = tls
@ -74,6 +75,8 @@ class Server(object):
def __repr__(self):
return "IRCServer.Server(%s)" % self.__str__()
def __str__(self):
if self.alias:
return self.alias
return "%s:%s%s" % (self.target_hostname, "+" if self.tls else "",
self.port)
def fileno(self):