botbrain.py 4.3 KB

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