From c352088f9de7c8c73a4893863df057306e5859c3 Mon Sep 17 00:00:00 2001 From: overflowerror Date: Tue, 27 Jan 2015 02:10:53 +0100 Subject: [PATCH] not testen; i'll do that tomorrow --- config.py | 29 ++++++++++++++++++ main.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 config.py create mode 100644 main.py diff --git a/config.py b/config.py new file mode 100644 index 0000000..1ea6b35 --- /dev/null +++ b/config.py @@ -0,0 +1,29 @@ +CONSUMER_KEY = 'Your Twitter API Consumer Key' +CONSUMER_SECRET = 'Your Consumer Secret Key' +ACCESS_TOKEN_KEY = 'Your Twitter API Access Token Key' +ACCESS_TOKEN_SECRET = 'Your Access Token Secret' + +# add @ to account name +# leave empty for no mention +DESTINATION_ACCOUNT = '' +DESTINATION_ACCOUNT_IMPORTANT = '' + +ALLOW_COMMANDS = false +ALLOW_ONLY_DM_COMMANDS = false + +# empty array for everyone +# else add comma-separated, quote-enclosed twitter names with @ +COMMAND_SOURCE_ACCOUNTS = [] + +# use "w" for overwrite, or "a" for append +LOG_TYPE = "a" +LOG_FILE = "~/minerva.log" + +COMMAND_NAME_SEPERATOR = "\n" + +UPDATE_COMMANDS = [ + ["uptime:", "uptime"], + ["mdstat:", 'cat /proc/mdstat | grep block | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /"'], + ["lxc:", 'lxc-ls -f -F name,state | grep -v "NAME" | grep -v \- | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ /: /" | sed "s/RUNNING/UP/" | sed "s/STOPPED/DOWN/"'], + ["df:", 'df -h --output=source,size,used | grep /dev/ | grep -v tmpfs | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /" | sed "s/ / /"'] +] diff --git a/main.py b/main.py new file mode 100644 index 0000000..0730a2c --- /dev/null +++ b/main.py @@ -0,0 +1,89 @@ +import twitter +import sys +import subprocess +import datetime +import time + +from config import * + +logfile = open(LOG_FILE, LOG_TYPE) + +def log(text): + logfile.write(datetime.datetime.now().isoformat() + ": " + text)) + +def connect(): + api = twitter.Api( + consume_key = CONSUMER_KEY, + consumer_secret = CONSUMER_SECRET, + access_token_key = ACCESS_TOKEN_KEY, + access_token_secret = ACCESS_TOKEN_SECRET + ) + return api + + +if __name__ == "__main__": + + log("starting minerva") + + lastChange = 0 + + lastChange = api.getDirectMessages(since_id = lastChange)[0].GetId() + lastChange = api.getMentions(since_id = lastChange)[0].GetId() + + while true: + if ALLOW_COMMANDS: + dms = api.getDirectMessages(since_id = lastChange) + + commandsToExecute = [] + for dm in dms: + if COMMAND_SOURCE_ACCOUNTS.__len__() == 0: + commandsToExecute.append([ + dm.GetSenderScreenName(), + dm.GetText() + ]) + else + for user in COMMAND_SOURCE_ACCOUNTS: + if dm.GetSenderScreenName() == user: + commandsToExecute.append([ + dm.GetSenderScreenName(), + dm.GetText() + ]) + else + log("unprivileged user @" + dm.GetSenderScreenName() + " tried to execute command (dm) \"" + dm.GetText().replace("\n", "\\n") + "\"\n") + + if ! ALLOW_ONLY_DM_COMMANDS: + mentions = api.GetMentions(since_id = lastChange) + for mention in mentions: + if COMMAND_SOURCE_ACCOUNTS.__len__() == 0: + commandsToExecute.append([ + mention.GetUser().GetScreenName(), + mention.GetText() + ]) + else + for user in COMMAND_SOURCE_ACCOUNTS: + if mention.GetUser().GetScreenName() == user: + commandsToExecute.append([ + mention.GetUser().GetScreenName(), + mention.GetText() + ]) + else + log("unprivileged user @" + mention.GetUser().GetScreenName() + " tried to execute command \"" + mention.GetText().replace("\n", "\\n") + "\"\n") + + + for command in commandsToExecute: + log("executing command (@" + command[0] + ") \"" + command[1].replace("\n", "\\n") + "\"") + output = subprocess.Popen(command[1], shell=True, stdout=PIPE).stdout.read() + log("result: " + output); + if (output + command[0]).len() + 2 > 140: + api.PostUpdate(status = command[0] + "Output of command is too long. I'm sry. : /") + else + api.PostUpdate(status = command[0] + " " + output) + + + for command in UPDATE_COMMANDS: + output = subprocess.Popen(command[1], shell=True, stdout=PIPE).stdout.read() + api.PostUpdate(status = (command[0] + COMMAND_NAME_SEPERATOR + output)) + + + time.sleep(5 * 60) +