client.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Part of rabbitears See LICENSE for permissions
  2. # Copyright (C) 2022 Matt Arnold
  3. from IRC import *
  4. import os
  5. import random
  6. import ssl
  7. import socket
  8. import sys
  9. import irctokens
  10. import json
  11. import sqlite3
  12. import logging
  13. from daemonize import Daemonize
  14. logging.basicConfig(filename='bot.log', encoding='utf-8', level=logging.DEBUG)
  15. class NullDevice:
  16. def write(self,s):
  17. pass
  18. LINEEND = '\r\n'
  19. # IRC Config
  20. config = None
  21. with open('config.json') as f:
  22. jld = f.read()
  23. config = json.loads(jld)
  24. # Need to pass the IRCBot class a socket the reason it doesn't do this itself is
  25. # so you can set up TLS or not as you need it
  26. # These provide good defaults. But your milage may vary
  27. def do_connect():
  28. oursock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. context = ssl.SSLContext()
  30. context.check_hostname = False
  31. context.verify_mode = ssl.CERT_NONE
  32. oursock = context.wrap_socket(oursock, server_hostname=config['hostname'])
  33. irc = IRCBot(oursock, isBad=config["isBad"])
  34. irc.connect(config['hostname'],
  35. config['port'],
  36. config['channel'],
  37. config['nick'],
  38. config['nickpass'])
  39. return irc
  40. def hup_handle(sig, fr):
  41. sys.exit()
  42. def do_mean():
  43. con = sqlite3.connect(config['db'])
  44. cur = con.cursor()
  45. qr = cur.execute("SELECT count(*) from mean").fetchone()
  46. maxrows = int(qr[0])
  47. slrow = str(random.randint(1, maxrows))
  48. qr = cur.execute("SELECT data from mean where rowid=(?)", (slrow,)).fetchone()
  49. result = str(qr[0])
  50. return result
  51. def generate_response(person, message):
  52. msg = message.strip(LINEEND)
  53. if 'cool.person' in person and msg.lower() == "hello botley":
  54. return "Greetings Master"
  55. elif msg.lower() == "hello":
  56. return "Greetings Human!"
  57. elif "Ground Control to Major Tom" in msg:
  58. return "Put your helmet on!"
  59. elif "Ziggy Stardust" in msg:
  60. return "Looks good in leather pants"
  61. elif msg.lower() == "!mean":
  62. return do_mean()
  63. else:
  64. return None
  65. def do_main_loop():
  66. irc = do_connect()
  67. while True:
  68. try:
  69. text = irc.get_response()
  70. logging.debug(text[0],text[1],text[2])
  71. if text[1] == "MODE": # in leiu of RPL_WELCOME
  72. botmode = irctokens.build("MODE", [config['nick'], '+B'])
  73. irc.send_cmd(botmode)
  74. if text[1] == 'PRIVMSG' and text[2][0] == config['channel']:
  75. r = generate_response(text[0],text[2][1])
  76. if r is not None:
  77. irc.send_privmsg(config['channel'],r)
  78. except IRCError as e:
  79. logging.error(e)
  80. sys.exit(1)
  81. pid = "bot.pid"
  82. daemon = Daemonize(app="theodebot", pid=pid, action=do_main_loop)
  83. daemon.start()