diff --git a/.gitignore b/.gitignore index 3f2e438..bd69f72 100755 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,7 @@ +persist +config +*.db +*.log .*.swp *.pyc *.orig -persist -config -pep8.py -.project -.pydevproject -*.db -web -control.sh -bot.log \ No newline at end of file diff --git a/README.md b/README.md index abb56f9..df71b1c 100755 --- a/README.md +++ b/README.md @@ -48,13 +48,13 @@ In addition, for `.whois` to work optimally, you must have `whois` installed. Ag If you are a user of another Linux disto, use your package manager to install the dependencies, or, for other operating systems, use **Google** to locate source packages you can install. -Once you have installed the required dependencies, run the bot with +Once you have installed the required dependencies, run the wrapper -`python bot.py` +`python cloudbot` It will generate a default config for you. Once you have edited the config, run it again with -`python bot.py` +`python cloudbot` and it will connect to any server(s) you have added to the config. (Config docs at the [wiki](http://git.io/cloudbotconfig)) diff --git a/cloudbot b/cloudbot new file mode 100755 index 0000000..2308667 --- /dev/null +++ b/cloudbot @@ -0,0 +1,182 @@ +#!/usr/bin/env python +# Bot Wrapper by neersighted +import sys +import os +import subprocess +import re +import json +try: + config = json.load(open('config')) +except ValueError, e: + print 'error: malformed config', e + +nocol = "\033[1;m" +red = "\033[1;31m" +green = "\033[1;32m" + +usage = "usage: ./cloudbot {start|stop|restart|status}" +quit = nocol + "Goodbye! Thanks for using CloudBot!" + +pwd = os.getcwd() +clearlog = ": >./bot.log && " +command = ":" + +daemoncheck = subprocess.check_output("locate /usr/bin/daemon", shell=True) +daemon = re.match(r'^/usr/bin/daemon$', daemoncheck) +screencheck = subprocess.check_output("locate /usr/bin/screen", shell=True) +screen = re.match(r'^/usr/bin/screen$', screencheck) +backend = config.get("wrapper", {}).get("backend", "daemon") + +try: + runningcheck = subprocess.check_output("ps ax|grep cloudbot|"\ + "grep -v grep|grep -v ./cloudbot", shell=True) + running = re.match(r'^[1-9]+', runningcheck) +except (subprocess.CalledProcessError): + running = False + +error1 = red + "Neither screen nor daemon is installed! "\ + "This program cannot run! {ERROR 1}" + nocol +error2 = red + "Could not find bot.py! Are you in the wrong folder? "\ + "(" + pwd + ") {ERROR 2}" + nocol +error3 = red + "Invalid choice, exiting! {ERROR 3}" + nocol +error4 = red + "Program killed by user! {ERROR 4}" + nocol +error5 = red + "Invalid backend in config! {ERROR 5}" + nocol +error6 = red + "Author error! We be derpin'! {ERROR 6}" + nocol + +start = "echo " + error1 +stop = "echo " + error1 +restart = "echo " + error1 +pid = "echo 'Cannot get pid'" + +if daemon: + if backend == "daemon": + start = "daemon -n cloudbot -O " + pwd + \ + "/bot.log -r python " + pwd + "/bot.py" + stop = clearlog + "daemon -n cloudbot --stop" + restart = stop + " && " + start + pid = "pidof /usr/bin/daemon" + elif backend == "screen": + start = "screen -S cloudbot -dm python" + pwd +\ + "/bot.py >>" + pwd + "/bot.log 2>&1" + stop = clearlog + "kill `pidof /usr/bin/screen`" + restart = stop + " && " + start + pid = "pidof /usr/bin/screen" + else: + print error5 +elif screen: + if backend == "daemon": + start = "daemon -n cloudbot -O " + pwd + \ + "/bot.log -r python " + pwd + "/bot.py" + stop = clearlog + "daemon -n cloudbot --stop" + restart = stop + " && " + start + pid = "pidof /usr/bin/daemon" + elif backend == "screen": + start = "screen -S cloudbot -dm python" + pwd +\ + "/bot.py >>" + pwd + "/bot.log 2>&1" + stop = clearlog + "kill `pidof /usr/bin/screen`" + restart = stop + " && " + start + pid = "pidof /usr/bin/screen" + else: + print error5 +else: + command = command + +print " ______ __ ______ __ __ "\ +" _______ .______ ______ .___________." +print " / || | / __ \ | | | | "\ +"| \ | _ \ / __ \ | |" +print "| ,----'| | | | | | | | | | "\ +"| .--. || |_) | | | | | `---| |----`" +print "| | | | | | | | | | | | "\ +"| | | || _ < | | | | | | " +print "| `----.| `----.| `--' | | `--' | "\ +"| '--' || |_) | | `--' | | | " +print " \______||_______| \______/ \______/ "\ +"|_______/ |______/ \______/ |__| " +print "http://git.io/cloudbot "\ +" by lukeroge" +try: + if running: + if (len(sys.argv) > 1): + if (sys.argv[1] == 'start'): + print "Bot is alread running, cannot start!" + elif (sys.argv[1] == 'stop'): + command = stop + print "Stopping... (" + backend + ")" + elif (sys.argv[1] == 'restart'): + command = restart + print "Restarting... (" + backend + ")" + elif (sys.argv[1] == 'status'): + command = pid + print green + "Bot is running! " + nocol + else: + print usage + exit + else: + print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}" + read = int(raw_input('Please choose a option: ')) + if read == 1: + print "Bot is alread running, cannot start!" + elif read == 2: + command = stop + print "Stopping... (" + backend + ")" + elif read == 3: + command = restart + print "Restarting... (" + backend + ")" + elif read == 4: + command = pid + print green + "Bot is running! " + nocol + elif read == 5: + print quit + else: + print error3 + else: + if (len(sys.argv) > 1): + if (sys.argv[1] == 'start'): + command = start + print "Starting... (" + backend + ")" + elif (sys.argv[1] == 'stop'): + print "Bot is not running, cannot stop!" + elif (sys.argv[1] == 'restart'): + print "Bot is not running, cannot restart!" + elif (sys.argv[1] == 'status'): + print red + "Bot is not running!" + nocol + else: + print usage + exit + else: + print "{1|start} {2|stop} {3|restart} {4|status} {5|exit}" + read = int(raw_input('Please choose a option: ')) + if read == 1: + command = start + print "Starting... (" + backend + ")" + elif read == 2: + print "Bot is not running, cannot stop!" + elif read == 3: + command = restart + elif read == 4: + print red + "Bot is not running, cannot restart!" + nocol + elif read == 5: + print quit + else: + print error3 + exit +except (TypeError, ValueError), e: + print error3 + exit +except (KeyboardInterrupt), e: + print error4 + exit +except (NameError, SyntaxError), e: + print error6 + exit + +if os.path.isfile("./bot.py"): + command = command +else: + print error2 + exit + +subprocess.call(command, shell=True) +print quit +exit diff --git a/core/config.py b/core/config.py index 72f41d6..96f2302 100755 --- a/core/config.py +++ b/core/config.py @@ -41,6 +41,10 @@ if not os.path.exists('config'): "mc_user": "INSERT MINECRAFT USERNAME HERE", "mc_pass": "INSERT MINECRAFT PASSWORD HERE" }, + "wrapper": + { + "backend": "daemon" + }, "censored_strings": [ "mypass",