botbrain.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. # now implemented as a module
  55. #def _weather(self, channel, zipcode):
  56. # now implemented as a module
  57. # def _getyoutubetitle(self, line, channel):
  58. def _ctof(self, channel, c_temp):
  59. c = float(c_temp)
  60. f = (c * 1.8)+32
  61. self.say(channel, str(f) + u"° F")
  62. def _ftoc(self, channel, f_temp):
  63. f = float(f_temp)
  64. c = (f - 32)*(.5555)
  65. self.say(channel, str(c) + u"° C")
  66. def _speak(self, user, target, message):
  67. if target.startswith("#"):
  68. self.say(target, message)
  69. else:
  70. target = "#" + target
  71. self.say(target, message)
  72. def _onstat(self, channel):
  73. self.say(channel, "Yep, I'm on. Idiot.")
  74. def _help(self, user):
  75. self.microphone('PRIVMSG ' + user + ' :' + ".uptime,\n")
  76. self.microphone('PRIVMSG ' + user + ' :' + ".imgs,\n")
  77. self.microphone('PRIVMSG ' + user + ' :' + ".ctof [celsius],\n")
  78. self.microphone('PRIVMSG ' + user + ' :' + ".ftoc [fahrenheit],\n")
  79. def _join(self, usr, message):
  80. if self._isAdmin(usr):
  81. if len(message.split()) is 3:
  82. channel = message.split()[1]
  83. extraArg = message.split()[-1]
  84. self.__bareSay("JOIN " + channel + " " + extraArg)
  85. else:
  86. channel = message.split()[-1] # second word (join #channel password)
  87. self.__bareSay("JOIN " + channel)
  88. def __quit(self, usr):
  89. if self._isAdmin(usr):
  90. self.__bareSay("QUIT :quitting")
  91. print "quitting as per " + usr
  92. sys.exit()
  93. def respond(self, usr, channel, message):
  94. # this bit is not a command
  95. # TODO (pull this out into a module)
  96. if any(k in message for k in (".png",".gif",".jpg",".jpeg", ".gifv")) and ("http:" in message or "https:" in message) \
  97. or ("imgur.com" in message and "gallery" in message) or ("https" in message and "gfycat.com" in message):
  98. url = re.search("(?P<url>https?://[^\s]+)", message).group("url")
  99. if url:
  100. self._insertImg(usr, url, channel)
  101. # this bit is
  102. if message.startswith(".join"):
  103. self._join(usr, message)
  104. if message.strip() == ".quit":
  105. self.__quit(usr)
  106. if message.startswith(".imgs"):
  107. self.ww._generate(self.db.getImgs())
  108. # hackish TODO
  109. if os.getenv('USER') == 'pybot':
  110. self.say(channel, "http://pybot.zero9f9.com/img/")
  111. else:
  112. self.say(channel, "http://zero9f9.com/~"+os.getenv('USER')+"/img/")
  113. #if message.startswith(".seen"):
  114. # self._seen(message.split()[-1], channel)
  115. if message.startswith(".ctof"):
  116. last = message.split()
  117. if last[-1] != "":
  118. self._ctof(channel, last[-1])
  119. if message.startswith(".ftoc"):
  120. last = message.split()
  121. if last[-1] != "":
  122. self._ftoc(channel, last[-1])
  123. if message.startswith(".help"):
  124. self._help(usr)
  125. if message.startswith(".onstat"):
  126. self._onstat(channel)
  127. if message.startswith(".speak"):
  128. tmp = message.split(" ",2)
  129. chnl = tmp[1]
  130. msg = tmp[2]
  131. self._speak(usr, chnl, msg)