botbrain.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import os
  4. from collections import defaultdict
  5. import webwriter
  6. import time
  7. import logger
  8. import datetime
  9. if sys.version_info > (3,0,0):
  10. import urllib.request, urllib.error, urllib.parse
  11. from urllib.parse import urlparse, parse_qsl
  12. else:
  13. import urllib2
  14. from urlparse import urlparse, parse_qsl
  15. import json
  16. import re
  17. from xml.dom.minidom import parseString
  18. from datetime import datetime, timedelta
  19. import lite
  20. class BotBrain:
  21. BRAINDEBUG = False
  22. def __init__(self, microphone, bot=None):
  23. self.microphone = microphone
  24. self.bot = bot
  25. yth = dict()
  26. self.db = self.bot.db
  27. self.ww = webwriter.WebWriter()
  28. def _isAdmin(self, username):
  29. if self.bot.conf.getOwner(self.bot.network) == username:
  30. return True
  31. if self.db.isAdmin(username):
  32. return True
  33. return False
  34. def getMicrophone(self):
  35. return self.microphone
  36. def _updateSeen(self, user, statement, event):
  37. self.db.updateSeen(user, statement, event)
  38. def _insertImg(self, user, url, channel):
  39. self.db.insertImg(user, url, channel)
  40. def __bareSay(self, thing):
  41. self.microphone(thing + '\n')
  42. def say(self, channel, thing):
  43. try:
  44. s = thing.encode('utf-8', 'ignore')
  45. except UnicodeEncodeError as e:
  46. print(e)
  47. print(thing)
  48. return None
  49. except UnicodeDecodeError as d:
  50. print(d)
  51. print(thing)
  52. return None
  53. outstring = 'PRIVMSG ' + channel + ' :' + s.decode('utf-8','ignore') + '\n'
  54. self.microphone(outstring)
  55. def notice(self, channel, thing):
  56. self.microphone('NOTICE ' + channel + ' :' + str(thing) + '\n')
  57. def _speak(self, user, target, message):
  58. if target.startswith("#"):
  59. self.say(target, message)
  60. else:
  61. target = "#" + target
  62. self.say(target, message)
  63. def _onstat(self, channel):
  64. self.say(channel, "Yep, I'm on. Idiot.")
  65. def _join(self, usr, message):
  66. if self._isAdmin(usr):
  67. if len(message.split()) is 3:
  68. channel = message.split()[1]
  69. extraArg = message.split()[-1]
  70. self.__bareSay("JOIN " + channel + " " + extraArg)
  71. else:
  72. channel = message.split()[-1] # second word (join #channel password)
  73. self.__bareSay("JOIN " + channel)
  74. def __quit(self, usr):
  75. if self._isAdmin(usr):
  76. self.__bareSay("QUIT :quitting")
  77. print(("quitting as per " + usr))
  78. sys.exit()
  79. def respond(self, usr, channel, message):
  80. # this bit is not a command
  81. # TODO (pull this out into a module)
  82. if any(k in message for k in (".png",".gif",".jpg",".jpeg", ".gifv")) and ("http:" in message or "https:" in message) \
  83. or ("imgur.com" in message and "gallery" in message) or ("https" in message and "gfycat.com" in message):
  84. url = re.search("(?P<url>https?://[^\s]+)", message).group("url")
  85. if url:
  86. self._insertImg(usr, url, channel)
  87. # this bit is
  88. if message.startswith(".join"):
  89. self._join(usr, message)
  90. if message.strip() == ".quit":
  91. self.__quit(usr)
  92. if message.startswith(".imgs"):
  93. self.ww._generate(self.db.getImgs())
  94. # hackish TODO
  95. if os.getenv('USER') == 'pybot':
  96. self.say(channel, "http://pybot.zero9f9.com/img/")
  97. else:
  98. self.say(channel, "http://zero9f9.com/~"+os.getenv('USER')+"/img/")
  99. if message.startswith(".onstat"):
  100. self._onstat(channel)
  101. if message.startswith(".speak"):
  102. tmp = message.split(" ",2)
  103. chnl = tmp[1]
  104. msg = tmp[2]
  105. self._speak(usr, chnl, msg)