A Python robot that edits Wikipedia and interacts with people over IRC https://en.wikipedia.org/wiki/User:EarwigBot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
9.3 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2009-2012 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from earwigbot.commands import Command
  23. class Notes(Command):
  24. """A mini IRC-based wiki for storing notes, tips, and reminders."""
  25. name = "notes"
  26. def process(self, data):
  27. pass
  28. class OldCommand(object):
  29. def parse(self):
  30. if command == "notes" or command == "note" or command == "about" or command == "data" or command == "database":
  31. try:
  32. action = line2[4]
  33. except BaseException:
  34. reply("What do you want me to do? Type \"!notes help\" for more information.", chan, nick)
  35. return
  36. import MySQLdb
  37. db = MySQLdb.connect(db="u_earwig_ircbot", host="sql", read_default_file="/home/earwig/.my.cnf")
  38. specify = ' '.join(line2[5:])
  39. if action == "help" or action == "manual":
  40. shortCommandList = "read, write, change, undo, delete, move, author, category, list, report, developer"
  41. if specify == "read":
  42. say("To read an entry, type \"!notes read <entry>\".", chan)
  43. elif specify == "write":
  44. say("To write a new entry, type \"!notes write <entry> <content>\". This will create a new entry only if one does not exist, see the below command...", chan)
  45. elif specify == "change":
  46. say("To change an entry, type \"!notes change <entry> <new content>\". The old entry will be stored in the database, so it can be undone later.", chan)
  47. elif specify == "undo":
  48. say("To undo a change, type \"!notes undo <entry>\".", chan)
  49. elif specify == "delete":
  50. say("To delete an entry, type \"!notes delete <entry>\". For security reasons, only bot admins can do this.", chan)
  51. elif specify == "move":
  52. say("To move an entry, type \"!notes move <old_title> <new_title>\".", chan)
  53. elif specify == "author":
  54. say("To return the author of an entry, type \"!notes author <entry>\".", chan)
  55. elif specify == "category" or specify == "cat":
  56. say("To change an entry's category, type \"!notes category <entry> <category>\".", chan)
  57. elif specify == "list":
  58. say("To list all categories in the database, type \"!notes list\". Type \"!notes list <category>\" to get all entries in a certain category.", chan)
  59. elif specify == "report":
  60. say("To give some statistics about the mini-wiki, including some debugging information, type \"!notes report\" in a PM.", chan)
  61. elif specify == "developer":
  62. say("To do developer work, such as writing to the database directly, type \"!notes developer <command>\". This can only be done by the bot owner.", chan)
  63. else:
  64. db.query("SELECT * FROM version;")
  65. r = db.use_result()
  66. data = r.fetch_row(0)
  67. version = data[0]
  68. reply("The Earwig Mini-Wiki: running v%s." % version, chan, nick)
  69. reply("The full list of commands, for reference, are: %s." % shortCommandList, chan, nick)
  70. reply("For an explaination of a certain command, type \"!notes help <command>\".", chan, nick)
  71. reply("You can also access the database from the Toolserver: http://toolserver.org/~earwig/cgi-bin/irc_database.py", chan, nick)
  72. time.sleep(0.4)
  73. return
  74. elif action == "read":
  75. specify = string.lower(specify)
  76. if " " in specify: specify = string.split(specify, " ")[0]
  77. if not specify or "\"" in specify:
  78. reply("Please include the name of the entry you would like to read after the command, e.g. !notes read earwig", chan, nick)
  79. return
  80. try:
  81. db.query("SELECT entry_content FROM entries WHERE entry_title = \"%s\";" % specify)
  82. r = db.use_result()
  83. data = r.fetch_row(0)
  84. entry = data[0][0]
  85. say("Entry \"\x02%s\x0F\": %s" % (specify, entry), chan)
  86. except Exception:
  87. reply("There is no entry titled \"\x02%s\x0F\"." % specify, chan, nick)
  88. return
  89. elif action == "delete" or action == "remove":
  90. specify = string.lower(specify)
  91. if " " in specify: specify = string.split(specify, " ")[0]
  92. if not specify or "\"" in specify:
  93. reply("Please include the name of the entry you would like to delete after the command, e.g. !notes delete earwig", chan, nick)
  94. return
  95. if authy == "owner" or authy == "admin":
  96. try:
  97. db.query("DELETE from entries where entry_title = \"%s\";" % specify)
  98. r = db.use_result()
  99. db.commit()
  100. reply("The entry on \"\x02%s\x0F\" has been removed." % specify, chan, nick)
  101. except Exception:
  102. phenny.reply("Unable to remove the entry on \"\x02%s\x0F\", because it doesn't exist." % specify, chan, nick)
  103. else:
  104. reply("Only bot admins can remove entries.", chan, nick)
  105. return
  106. elif action == "developer":
  107. if authy == "owner":
  108. db.query(specify)
  109. r = db.use_result()
  110. try:
  111. print r.fetch_row(0)
  112. except Exception:
  113. pass
  114. db.commit()
  115. reply("Done.", chan, nick)
  116. else:
  117. reply("Only the bot owner can modify the raw database.", chan, nick)
  118. return
  119. elif action == "write":
  120. try:
  121. write = line2[5]
  122. content = ' '.join(line2[6:])
  123. except Exception:
  124. reply("Please include some content in your entry.", chan, nick)
  125. return
  126. db.query("SELECT * from entries WHERE entry_title = \"%s\";" % write)
  127. r = db.use_result()
  128. data = r.fetch_row(0)
  129. if data:
  130. reply("An entry on %s already exists; please use \"!notes change %s %s\"." % (write, write, content), chan, nick)
  131. return
  132. content2 = content.replace('"', '\\' + '"')
  133. db.query("INSERT INTO entries (entry_title, entry_author, entry_category, entry_content, entry_content_old) VALUES (\"%s\", \"%s\", \"uncategorized\", \"%s\", NULL);" % (write, nick, content2))
  134. db.commit()
  135. reply("You have written an entry titled \"\x02%s\x0F\", with the following content: \"%s\"" % (write, content), chan, nick)
  136. return
  137. elif action == "change":
  138. reply("NotImplementedError", chan, nick)
  139. elif action == "undo":
  140. reply("NotImplementedError", chan, nick)
  141. elif action == "move":
  142. reply("NotImplementedError", chan, nick)
  143. elif action == "author":
  144. try:
  145. entry = line2[5]
  146. except Exception:
  147. reply("Please include the name of the entry you would like to get information for after the command, e.g. !notes author earwig", chan, nick)
  148. return
  149. db.query("SELECT entry_author from entries WHERE entry_title = \"%s\";" % entry)
  150. r = db.use_result()
  151. data = r.fetch_row(0)
  152. if data:
  153. say("The author of \"\x02%s\x0F\" is \x02%s\x0F." % (entry, data[0][0]), chan)
  154. return
  155. reply("There is no entry titled \"\x02%s\x0F\"." % entry, chan, nick)
  156. return
  157. elif action == "cat" or action == "category":
  158. reply("NotImplementedError", chan, nick)
  159. elif action == "list":
  160. reply("NotImplementedError", chan, nick)
  161. elif action == "report":
  162. reply("NotImplementedError", chan, nick)