rabbitears/client.py

68 lines
1.8 KiB
Python
Raw Permalink Normal View History

# Part of rabbitears See LICENSE for permissions
# Copyright (C) 2022 Matt Arnold
2022-02-13 02:25:13 +00:00
from IRC import *
import os
import random
import ssl
import socket
import sys
import irctokens
import json
2022-02-13 02:25:13 +00:00
LINEEND = '\r\n'
# IRC Config
config = None
with open('config.json') as f:
jld = f.read()
config = json.loads(jld)
2022-02-13 02:25:13 +00:00
# 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'])
2022-02-13 02:25:13 +00:00
irc = IRCBot(oursock)
irc.connect(config['hostname'],
config['port'],
config['channel'],
config['nick'],
config['nickpass'])
2022-02-13 02:25:13 +00:00
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"
2022-02-13 02:25:13 +00:00
else:
return None
while True:
try:
text = irc.get_response()
print(text[0],text[1],text[2])
if text[1] == 'PRIVMSG' and text[2][0] == config['channel']:
2022-02-13 02:25:13 +00:00
r = generate_response(text[0],text[2][1])
if r is not None:
irc.send_privmsg(config['channel'],r)
2022-02-13 02:25:13 +00:00
except KeyboardInterrupt:
irc.send_quit("Ctrl-C Pressed")
msg = oursock.recv(4096)
print(msg)
sys.exit(0)
except IRCError as e:
printred(e)
sys.exit(1)
2022-02-13 02:25:13 +00:00