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.

91 line
3.7 KiB

  1. # -*- coding: utf-8 -*-
  2. import string, re, subprocess
  3. from config import *
  4. s, send, say, action, notice, join = None, None, None, None, None, None
  5. def check_triggers(cmds, act, nick, ident, host, chan, msg = None):
  6. global s, send, say, action, notice, join # set commands as globals so we can use them in other functions
  7. s, send, say, action, notice, join = cmds # unpack commands
  8. if act == "join":
  9. pass
  10. if act == "msg_private":
  11. pass
  12. if act == "msg_public":
  13. pass
  14. if act == "msg":
  15. if msg == "!test":
  16. cmd_test(nick, chan)
  17. if msg.startswith("!git"):
  18. cmd_git(nick, host, chan, msg)
  19. def get_args(msg): # get command arguments
  20. args = msg.strip().split(' ') # strip out extra whitespace and split the message into a list
  21. while '' in args: # remove any empty arguments
  22. args.remove('')
  23. return args[1:] # remove the command itself
  24. def cmd_test(nick, chan): # bot test
  25. say(chan, "'sup %s?" % nick)
  26. def cmd_git(nick, host, chan, msg): # commands to interface with the bot's git repository
  27. if host not in ADMINS:
  28. say(chan, "%s: you must be a bot admin to use this command." % nick)
  29. return
  30. args = get_args(msg)
  31. if not args:
  32. say(chan, "%s: no arguments provided." % nick)
  33. return
  34. if args[0] == "help": # display all commands
  35. cmds = ["branch (show current branch)", "branches (show all branches)", "checkout (switch branches)", "pull (update current branch)"]
  36. cmds = ', '.join(cmds)
  37. say(chan, "%s: sub-commands are: %s" % (nick, cmds))
  38. elif args[0] == "branch": # get our current branch
  39. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  40. branch = branch[:-1] # strip newline
  41. say(chan, "%s: currently on branch '%s'." % (nick, branch))
  42. elif args[0] == "branches": # get list of branches
  43. branches = subprocess.check_output(['git', 'branch'], stderr=subprocess.STDOUT) # git branch
  44. branches = branches[:-1] # strip newline
  45. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  46. branches = branches.replace('* ', ' ')
  47. branches = branches.replace('\n ', ', ')
  48. branches = branches.strip()
  49. say(chan, "%s: branches: %s." % (nick, branches))
  50. elif args[0] == "checkout": # switch branches
  51. try:
  52. branch = args[1]
  53. except IndexError: # no branch name provided
  54. say(chan, "%s: switch to which branch?" % nick)
  55. return
  56. try:
  57. result = subprocess.check_output(['git', 'checkout', branch], stderr=subprocess.STDOUT) # git checkout our_branch
  58. result = result[:-1] # strip newline
  59. result = string.lower(result[0] + result[1:]) # lowercase first word
  60. say(chan, "%s: %s." % (nick, result))
  61. except subprocess.CalledProcessError: # git couldn't switch branches
  62. say(chan, "%s: branch '%s' does not exist!" % (nick, branch))
  63. elif args[0] == "pull": # pull from remote repository
  64. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  65. branch = branch[:-1] # strip newline
  66. say(chan, "%s: pulling branch '%s' from remote..." % (nick, branch))
  67. result = subprocess.check_output(['git', 'pull', 'origin', branch]) # pull from remote
  68. if "Already up-to-date." in result:
  69. say(chan, "%s: done; no new changes." % nick)
  70. else:
  71. say(chan, "%s: done; new changes merged." % nick)
  72. else:
  73. say(chan, "%s: unknown argument: '%s'." % (nick, arg[0]))