botbrain.py 3.7 KB

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