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.

actions.py 3.9 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: utf-8 -*-
  2. import string, re, subprocess
  3. from config.irc_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, "Hey \x02%s\x0F!" % 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, "\x02%s\x0F: 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, "\x02%s\x0F: no arguments provided." % nick)
  33. return
  34. if args[0] == "help": # display all commands
  35. cmds = ["\x0303branch\x0301 (show current branch)", "\x0303branches\x0301 (show all branches)",
  36. "\x0303checkout\x0301 (switch branches)", "\x0303pull\x0301 (update current branch)"]
  37. cmds = ', '.join(cmds)
  38. say(chan, "\x02%s\x0F: sub-commands are: %s" % (nick, cmds))
  39. elif args[0] == "branch": # get our current branch
  40. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  41. branch = branch[:-1] # strip newline
  42. say(chan, "\x02%s\x0F: currently on branch \x0302%s\x0301." % (nick, branch))
  43. elif args[0] == "branches": # get list of branches
  44. branches = subprocess.check_output(['git', 'branch'], stderr=subprocess.STDOUT) # git branch
  45. branches = branches[:-1] # strip newline
  46. branches = branches.replace('\n* ', ', ') # cleanup extraneous characters
  47. branches = branches.replace('* ', ' ')
  48. branches = branches.replace('\n ', ', ')
  49. branches = branches.strip()
  50. say(chan, "\x02%s\x0F: branches: \x0302%s\x0301." % (nick, branches))
  51. elif args[0] == "checkout": # switch branches
  52. try:
  53. branch = args[1]
  54. except IndexError: # no branch name provided
  55. say(chan, "\x02%s\x0F: switch to which branch?" % nick)
  56. return
  57. try:
  58. result = subprocess.check_output(['git', 'checkout', branch], stderr=subprocess.STDOUT) # git checkout our_branch
  59. if "Already on" in result:
  60. say(chan, "\x02%s\x0F: already on \x0302%s\x0301!" % (nick, branch))
  61. else:
  62. say(chan, "\x02%s\x0F: switched to branch \x0302%s\x0301." % (nick, branch))
  63. except subprocess.CalledProcessError: # git couldn't switch branches
  64. say(chan, "\x02%s\x0F: branch \x0302%s\x0301 does not exist!" % (nick, branch))
  65. elif args[0] == "pull": # pull from remote repository
  66. branch = subprocess.check_output(['git', 'name-rev', '--name-only', 'HEAD'], stderr=subprocess.STDOUT) # git name-rev --name-only HEAD
  67. branch = branch[:-1] # strip newline
  68. say(chan, "\x02%s\x0F: pulling from remote (currently on \x0302%s\x0301)..." % (nick, branch))
  69. result = subprocess.check_output(['git', 'pull']) # pull from remote
  70. if "Already up-to-date." in result:
  71. say(chan, "\x02%s\x0F: done; no new changes." % nick)
  72. else:
  73. say(chan, "\x02%s\x0F: done; new changes merged." % nick)
  74. else:
  75. say(chan, "\x02%s\x0F: unknown argument: \x0303%s\x0301." % (nick, args[0]))