summary refs log tree commit diff
path: root/client.py
blob: f0cd359a66c4882a28ba0faa97f345bd30b63255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Part of rabbitears See LICENSE for permissions
# Copyright (C) 2022 Matt Arnold
from IRC import IRCBot, IRCError, printred
import random
import ssl
import socket
import sys
import json
import re

horse_counter = 0
trigger_regex = re.compile(".*frog.*")
LINEEND = "\r\n"
# IRC Config
config = None
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
# so you can set up TLS or not as you need it
# These provide good defaults. But your mileage may vary
oursock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
context.minimum_version = ssl.TLSVersion.TLSv1_2
context.maximum_version = ssl.TLSVersion.TLSv1_3
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
oursock = context.wrap_socket(oursock, server_hostname=config["hostname"])
irc = IRCBot(oursock)
irc.connect(
    config["hostname"],
    config["port"],
    config["channel"],
    config["nick"],
    config["nickpass"],
)


def generate_response(person, message):
    msg = message.strip(LINEEND)
    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])
            if r is not None:
                irc.send_privmsg(config["channel"], r)
    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)