summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Arnold2025-02-20 09:18:03 -0500
committerMatt Arnold2025-02-20 09:18:03 -0500
commitd67d4b25cbf24f2ac3190b6bf03e78c10334ca0d (patch)
tree46ad4fb3f3041235358b1bbb9801bd6034082e5f
parent6d185593b351a9c229af10d8d35e413039a516a7 (diff)
new year new bot
-rw-r--r--IRC.py53
-rw-r--r--client.py65
2 files changed, 67 insertions, 51 deletions
diff --git a/IRC.py b/IRC.py
index 90ef1fa..9361982 100644
--- a/IRC.py
+++ b/IRC.py
@@ -4,26 +4,31 @@ import socket
import sys
import irctokens
import time
+
+
class IRCBadMessage(Exception):
pass
+
+
class IRCError(Exception):
pass
+
def printred(s):
t = f"\033[1;31m {s} \033[0m\n"
print(t)
+
def parsemsg(s):
- """Breaks a message from an IRC server into its prefix, command, and arguments.
- """
- prefix = ''
+ """Breaks a message from an IRC server into its prefix, command, and arguments."""
+ prefix = ""
trailing = []
if not s:
- raise IRCBadMessage("Empty line.")
- if s[0] == ':':
- prefix, s = s[1:].split(' ', 1)
- if s.find(' :') != -1:
- s, trailing = s.split(' :', 1)
+ raise IRCBadMessage("Empty line.")
+ if s[0] == ":":
+ prefix, s = s[1:].split(" ", 1)
+ if s.find(" :") != -1:
+ s, trailing = s.split(" :", 1)
args = s.split()
args.append(trailing)
else:
@@ -32,10 +37,10 @@ def parsemsg(s):
return prefix, command, args
-LINEEND = '\r\n'
+LINEEND = "\r\n"
-class IRCBot:
+class IRCBot:
irc = None
def __init__(self, sock, config=None):
@@ -45,10 +50,10 @@ class IRCBot:
def send_cmd(self, line):
"""Send an IRC Command, takes an IRC command string without the CRLF
- Returns encoded msg on success raises IRCError on failure """
+ Returns encoded msg on success raises IRCError on failure"""
if not self.connected:
raise IRCError("Not Connected")
- encmsg = bytes(line.format() + LINEEND, 'UTF-8' )
+ encmsg = bytes(line.format() + LINEEND, "UTF-8")
expected = len(encmsg)
if self.irc.send(encmsg) == expected:
return str(encmsg)
@@ -56,16 +61,15 @@ class IRCBot:
raise IRCError("Unexpected Send Length")
def on_welcome(self, *args, **kwargs):
- authmsg = irctokens.build("NICKSERV", ['IDENTIFY', self.config['nickpass']])
+ authmsg = irctokens.build("NICKSERV", ["IDENTIFY", self.config["nickpass"]])
self.send_cmd(authmsg)
- joinmsg = irctokens.build("JOIN", [self.config['channel']])
+ joinmsg = irctokens.build("JOIN", [self.config["channel"]])
self.send_cmd(joinmsg)
-
def send_privmsg(self, dst, msg):
- msg = irctokens.build("PRIVMSG",[dst, msg])
+ msg = irctokens.build("PRIVMSG", [dst, msg])
self.send_cmd(msg)
-
+
def send_quit(self, quitmsg):
msg = irctokens.build("QUIT", [quitmsg])
print(msg)
@@ -87,12 +91,16 @@ class IRCBot:
self.connected = True
# Perform user registration
- usermsg = irctokens.build("USER", [botnick, "0","*", "RabbitEars Bot"]).format()
+ password_msg = irctokens.build("PASS", [self.config["nickpass"]])
+ self.send_cmd(password_msg)
+ usermsg = irctokens.build(
+ "USER", [botnick, "0", "*", "Nyx Kermitsdotter"]
+ ).format()
print(usermsg)
self.send_cmd(usermsg)
nickmsg = irctokens.build("NICK", [botnick])
self.send_cmd(nickmsg)
-
+
def get_response(self):
# Get the response
resp = self.irc.recv(4096).decode("UTF-8")
@@ -103,9 +111,8 @@ class IRCBot:
self.on_welcome(nwmsg.params)
if nwmsg.command == "ERROR":
raise IRCError(str(nwmsg.params[0]))
- if nwmsg.command == 'PING':
- print('Sending pong')
- self.irc.send(
- bytes('PONG ' + LINEEND, "UTF-8"))
+ if nwmsg.command == "PING":
+ print("Sending pong")
+ self.irc.send(bytes("PONG " + LINEEND, "UTF-8"))
return msg
diff --git a/client.py b/client.py
index a3ce217..d1e7474 100644
--- a/client.py
+++ b/client.py
@@ -1,6 +1,6 @@
# Part of rabbitears See LICENSE for permissions
# Copyright (C) 2022 Matt Arnold
-from IRC import *
+from IRC import IRCBot, IRCError, printred
import os
import random
import ssl
@@ -8,53 +8,64 @@ import socket
import sys
import irctokens
import json
+import re
-LINEEND = '\r\n'
+horse_counter = 0
+trigger_regex = re.compile(".*frog.*")
+LINEEND = "\r\n"
# IRC Config
config = None
-with open('config.json') as f:
+with open("config.json") as f:
jld = f.read()
config = json.loads(jld)
-
-# Need to pass the IRCBot class a socket the reason it doesn't do this itself is
+# Need to pass the IRCBot class a socket the reason it doesn't do this itself is
# so you can set up TLS or not as you need it
# These provide good defaults. But your milage may vary
oursock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.SSLContext()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
-oursock = context.wrap_socket(oursock, server_hostname=config['hostname'])
+oursock = context.wrap_socket(oursock, server_hostname=config["hostname"])
irc = IRCBot(oursock)
-irc.connect(config['hostname'],
- config['port'],
- config['channel'],
- config['nick'],
- config['nickpass'])
+irc.connect(
+ config["hostname"],
+ config["port"],
+ config["channel"],
+ config["nick"],
+ config["nickpass"],
+)
+
def generate_response(person, message):
msg = message.strip(LINEEND)
- if 'cool.person' in person and msg.lower() == "hello botley":
- return "Greetings Master"
- elif msg.lower() == "hello":
- return "Greetings Human!"
- elif "Ground Control to Major Tom" in msg:
- return "Put your helmet on!"
- elif "Ziggy Stardust" in msg:
- return "Looks good in leather pants"
- else:
- return None
+ if "horse" in person:
+ global horse_counter
+ horse_counter += 1
+ if horse_counter % 50 == 0:
+ return "Ribbit, Frog on a horse, nobody knows where they'll go"
+ return "Ribbit, Frog on the floor where did she come from "
+ if trigger_regex.match(msg.lower()):
+ return "ribbit ribbit "
+ if "dozens" in person:
+ dice = random.randint(1, 20)
+ if dice <= 7:
+ return "watch it horse you almost clobbered me"
+ if "acdw" in person:
+ dice = random.randint(1, 12)
+ if dice == 1 or dice == 12:
+ return "I've been chilling in the basement for a minute"
+
while True:
try:
-
text = irc.get_response()
- print(text[0],text[1],text[2])
- if text[1] == 'PRIVMSG' and text[2][0] == config['channel']:
- r = generate_response(text[0],text[2][1])
+ print(text[0], text[1], text[2])
+ if text[1] == "PRIVMSG" and text[2][0] == config["channel"]:
+ r = generate_response(text[0], text[2][1])
if r is not None:
- irc.send_privmsg(config['channel'],r)
+ irc.send_privmsg(config["channel"], r)
except KeyboardInterrupt:
irc.send_quit("Ctrl-C Pressed")
msg = oursock.recv(4096)
@@ -63,5 +74,3 @@ while True:
except IRCError as e:
printred(e)
sys.exit(1)
-
-