Add structured logging (somewhat)

This commit is contained in:
hedy 2024-04-06 04:13:40 +00:00
parent 745c6ed116
commit 2f9955e601
4 changed files with 58 additions and 20 deletions

View File

@ -1,19 +1,29 @@
**IRC Relay**
A multi chan irc relay in python
A multi chan irc relay in python. Based on fellow ~xfnw's single chan
[relay](https://tildegit.org/xfnw/relay).
Multi chan part doesn't actually show the chan though, I think
Use at your own risk!
Use at your own risk
It's prone to netsplits (but you can let your service auto-retry when it exits)
In active development
---
Prone to netsplits (but you can let your service auto-retry when it exits)
**Features**
I think that's all.
Please read the config files. Or better, read `main.py`.
It supports formatted logging written to `./log.txt` by default (see config).
Search for `[MANUAL]` prefix for lesser noisy messages and use the per-server
messages for debugging.
You are of course welcome to join the channels referenced in the default config
examples!
---
**Setup**
- Clone repo
- Set up virtualenv
- Install `requirements.txt`
@ -21,4 +31,4 @@ I think that's all.
- Edit the config
- Copy and adapt `relay.service`
- ???
- Profit
- Profit!

View File

@ -18,6 +18,14 @@ SERVERS = {
"chans": ["##hedy-dev"],
"hidechan": True,
},
"teapot": {
"connection": {
"host": "irc.teapot.chat", "port": 6697,
"sasl": SASLUserPass("username", "password"),
},
"chans": ["##hedy-dev"],
"hidechan": True,
},
}

View File

@ -18,6 +18,14 @@ SERVERS = {
"chans": ["##hedy"],
"hidechan": True,
},
"teapot": {
"connection": {
"host": "irc.teapot.chat", "port": 6697,
"sasl": SASLUserPass("username", "password"),
},
"chans": ["##hedy"],
"hidechan": True,
},
}

36
main.py
View File

@ -14,6 +14,16 @@ import ircrobots.server as S
import config as cfg
f_log = open(cfg.log_file, "w+")
def log(msg: str, also_print: bool = False):
f_log.write(f"{msg}\n")
if also_print:
print(msg)
def manual_log(msg: str, *args, **kwargs):
log(f"[MANUAL] {msg}", *args, **kwargs)
class Server(Ir.Server):
# overwrite connect so i can put try except blocks there
@ -35,24 +45,26 @@ class Server(Ir.Server):
self.params = params
await self.handshake()
except:
print("connection with {} failed, disconnecting".format(self.name))
manual_log("connection with {} failed, disconnecting".format(self.name), True)
self.disconnected = True
self.f_log = open(cfg.log_file, "w+")
def auto_log(self, msg, *args, **kwargs):
log(f"[{self.name}] {msg}", *args, **kwargs)
async def line_read(self, line: It.line):
self.f_log.write(f"{self.name} < {line.format()}\n")
self.auto_log(f"< {line.format()}")
if line.command == "001":
print(f"connected to {self.name}")
manual_log(f"connected to {self.name} :D", True)
self.chans = cfg.SERVERS[self.name]["chans"]
self.chans_actual = []
for c in self.chans:
await self.send(It.build("JOIN", [c]))
print(f"joined {self.name} {c} through config")
manual_log(f"joined {self.name} {c} through config", True)
self.chans_actual.append(c)
if line.command == "PRIVMSG" and line.params[0] == self.nickname:
elif line.command == "PRIVMSG" and line.params[0] == self.nickname:
self.auto_log(f"< {line.format()}")
line.params.pop(0)
nick = line.source.split("!")[0]
text = line.params[0]
@ -61,7 +73,7 @@ class Server(Ir.Server):
asyncio.create_task(self.ac(self.name, args))
return
if line.command == "PRIVMSG" and line.params[0] in self.chans_actual:
elif line.command == "PRIVMSG" and line.params[0] in self.chans_actual:
chan = line.params.pop(0)
me = False
if "\1ACTION" in line.params[0]:
@ -122,16 +134,16 @@ class Server(Ir.Server):
if line.command == "INVITE":
await self.send(It.build("JOIN", [line.params[1]]))
print(f"joined {self.name} {line.params[1]} through invite")
manual_log(f"joined {self.name} {line.params[1]} through invite", True)
# TODO: add to relay chans if needed
self.chans.append(line.params[1])
self.chans_actual.append(line.params[1])
async def line_send(self, line: It.Line):
self.f_log.write(f"{self.name} > {line.format()}\n")
self.auto_log(f"> {line.format()}")
async def bc(self, name, chan, nick, msg, hide):
print(f"<RELAY> {self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}")
manual_log(f"<RELAY> {self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}", True)
if self.disconnected or "chans" not in list(dir(self)):
return
if name == self.name and len(cfg.SERVERS[name]["chans"]) == 1:
@ -144,7 +156,7 @@ class Server(Ir.Server):
await self.send(It.build("PRIVMSG", [c, s]))
async def ac(self, name, args):
print(f"<ADMIN_CMD> {self.name}: {args}")
manual_log(f"<ADMIN_CMD> {self.name}: {args}", True)
if self.disconnected or "chans" not in list(dir(self)):
return
nargs = []