summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Yu <andrew@andrewyu.org>2022-08-11 20:26:26 +0800
committerAndrew Yu <andrew@andrewyu.org>2022-08-11 20:26:26 +0800
commitcbd0046251a3725e6eeede650730930e4448e50f (patch)
tree2885351ca43d9a0af7f5e588583f394cab19bc66
parentfed93dcd209b4c147a87dbb59770024767e3da99 (diff)
downloadirc-mod-bot-cbd0046251a3725e6eeede650730930e4448e50f.tar.gz
irc-mod-bot-cbd0046251a3725e6eeede650730930e4448e50f.zip
Implement chatcommand `OP'
-rw-r--r--bot.py33
1 files changed, 32 insertions, 1 deletions
diff --git a/bot.py b/bot.py
index 84b1775..e5d5761 100644
--- a/bot.py
+++ b/bot.py
@@ -36,7 +36,7 @@ import sys
import base64
import re
-from config import SERVER, PORT, NICK, IDENT, GECOS, CHANNELS, PREFIX, LOGIN, PASSWORD
+from config import SERVER, PORT, NICK, IDENT, GECOS, CHANNELS, PREFIX, LOGIN, PASSWORD, ADMINS
car = lambda l: l[0]
cdr = lambda l: l[1:]
@@ -103,6 +103,13 @@ def match_hostmask(hostmask: bytes, pattern: bytes) -> bool:
regexp += b'$'
return bool(re.compile(regexp, re.I).match(hostmask))
+def is_admin(user: User) -> Optional[bool]:
+ if not (user.nick and user.ident and user.host):
+ return None
+ for a in ADMINS:
+ if match_hostmask((user.nick + b"!" + user.ident + b"@" + user.host), a):
+ return True
+ return False
def parse_nih(nih: bytes) -> tuple[Optional[bytes], Optional[bytes], Optional[bytes]]:
@@ -432,12 +439,15 @@ def handle_privmsg(
target = msg.args[0]
text = msg.args[1]
+ channel: Optional[Channel]
if target.startswith(b"#"):
+ channel = channels[target]
reply_to = target
if not text.startswith(PREFIX):
return
text = text[len(PREFIX) :]
elif target == me.nick:
+ channel = None
reply_to = msg.source.nick
if text.startswith(PREFIX):
text = text[len(PREFIX) :]
@@ -451,6 +461,27 @@ def handle_privmsg(
if chat_command[0] == b"DUMP":
for send_buf in break_up(repr(channels).encode("utf-8"), 400):
send(s, b"PRIVMSG", reply_to, send_buf)
+ return
+ elif chat_command[0] == b"OP":
+ if not channel:
+ send(s, b"PRIVMSG", reply_to, b"%s: You may only do this in a channel." % msg.source.nick)
+ return
+ elif msg.source not in channel.users:
+ send(s, b"PRIVMSG", reply_to, b"%s: You are not on the channel, and you're haxsending messages to it. This isn't nice." % msg.source.nick)
+ return
+ priv = is_admin(msg.source)
+ if priv is False:
+ send(s, b"PRIVMSG", reply_to, b"%s: Access denied." % msg.source.nick)
+ return
+ elif priv is None:
+ send(s, b"PRIVMSG", reply_to, b"%s: I don't know your ident or host, so I can't check your permissions. Something is wrong." % msg.source.nick)
+ return
+ elif msg.source in channel.ops:
+ send(s, b"PRIVMSG", reply_to, b"%s: You are already a channel operator." % msg.source.nick)
+ return
+ else:
+ send(s, b"MODE", reply_to, b"+o", msg.source.nick)
+ send(s, b"PRIVMSG", reply_to, b"%s: I tried to make you a channel operator." % msg.source.nick)
else:
send(
s,