Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion
multicraft.py
| @@ -7,6 +7,7 @@ | |||
| 7 | 7 | # http://opensource.org/licenses/GPL-3.0 | |
| 8 | 8 | ||
| 9 | 9 | # Updated to use hashlib instead of calling an external website for md5 hashing | |
| 10 | + | # For a working example of this, join irc.esper.net #xD and ask blha303 for a demonstration of multicraft.py | |
| 10 | 11 | ||
| 11 | 12 | from util import hook, http | |
| 12 | 13 | from urllib import urlencode | |
Steven Smith revised this gist . Go to revision
1 file changed, 4 insertions, 2 deletions
multicraft.py
| @@ -6,9 +6,12 @@ | |||
| 6 | 6 | # GPLv3 license (because CloudBot is GPL) | |
| 7 | 7 | # http://opensource.org/licenses/GPL-3.0 | |
| 8 | 8 | ||
| 9 | + | # Updated to use hashlib instead of calling an external website for md5 hashing | |
| 10 | + | ||
| 9 | 11 | from util import hook, http | |
| 10 | 12 | from urllib import urlencode | |
| 11 | 13 | import json | |
| 14 | + | import hashlib | |
| 12 | 15 | ||
| 13 | 16 | url = "MULTICRAFTURL/api.php" | |
| 14 | 17 | user = "USER" | |
| @@ -22,8 +25,7 @@ def multicraft(inp, reply=''): | |||
| 22 | 25 | values = [] | |
| 23 | 26 | for k, v in data.iteritems(): | |
| 24 | 27 | values.append(v) | |
| 25 | - | key = http.get("http://blha303.com.au/md5.php?q=%s%s%s%s" | |
| 26 | - | % (key, method, user, "".join(values))) | |
| 28 | + | key = hashlib.md5("%s%s%s%s" % (key, method, user, "".join(values))).hexdigest() | |
| 27 | 29 | string = urlencode(dict(_MulticraftAPIMethod=method, | |
| 28 | 30 | _MulticraftAPIUser=user, | |
| 29 | 31 | _MulticraftAPIKey=key)) | |
Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
multicraft.py
| @@ -2,7 +2,7 @@ | |||
| 2 | 2 | # ('import multicraft; data = multicraft.multicraft("METHOD PARAM=VALUE")') | |
| 3 | 3 | # Full API documentation coming soon (better than Multicraft's PHP-specific documentation, I'm hoping) | |
| 4 | 4 | ||
| 5 | - | # This file (multicraft.py) Copyright 2013 Steven Smith (blha303). All Rights Reserved. | |
| 5 | + | # This file (multicraft.py) created by Steven Smith (blha303) 2013 | |
| 6 | 6 | # GPLv3 license (because CloudBot is GPL) | |
| 7 | 7 | # http://opensource.org/licenses/GPL-3.0 | |
| 8 | 8 | ||
Steven Smith revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
multicraft.py
| @@ -94,7 +94,7 @@ def mcrestart(inp): | |||
| 94 | 94 | ||
| 95 | 95 | ||
| 96 | 96 | @hook.command(adminonly=True, autohelp=False) | |
| 97 | - | def mclist(): | |
| 97 | + | def mclist(inp): | |
| 98 | 98 | """mclist - List servers.""" | |
| 99 | 99 | out = "" | |
| 100 | 100 | data = multicraft("listServers") | |
Steven Smith revised this gist . Go to revision
1 file changed, 18 insertions, 14 deletions
multicraft.py
| @@ -37,17 +37,15 @@ def multicraft(inp, reply=''): | |||
| 37 | 37 | if not "=" in i: | |
| 38 | 38 | return "Invalid data at '%s'." % i | |
| 39 | 39 | moredata[i.split("=")[0]] = i.split("=")[1] | |
| 40 | - | data = http.get_json(url, post_data=getqstring(method, user, apikey, data=moredata)) | |
| 40 | + | data = http.get_json(url, post_data=getqstring(method, user, apikey, | |
| 41 | + | data=moredata)) | |
| 41 | 42 | if not data['success']: | |
| 42 | 43 | return "Failure: %s" % ", ".join(data['errors']) | |
| 43 | 44 | else: | |
| 44 | 45 | return json.dumps(data['data']) | |
| 45 | 46 | ||
| 47 | + | ||
| 46 | 48 | def getCommand(inp, method): | |
| 47 | - | """ for methods with only an 'id' parameter, like getUser | |
| 48 | - | example usage: getCommand("1", "getUser") will return | |
| 49 | - | {"User": {"lang": "", "email": "...", "global_role": "...", | |
| 50 | - | "id": "1", "name": "admin"}} as a JSON string """ | |
| 51 | 49 | try: | |
| 52 | 50 | id = int(inp.split(" ")[0]) | |
| 53 | 51 | except: | |
| @@ -58,14 +56,13 @@ def getCommand(inp, method): | |||
| 58 | 56 | else: | |
| 59 | 57 | return None | |
| 60 | 58 | ||
| 59 | + | ||
| 61 | 60 | def updateCommand(inp, method): | |
| 62 | - | """ For methods that take field>value parameters. | |
| 63 | - | inp should be a string, "field=value field=value ..." | |
| 64 | - | Example: updateCommand("name=admin email=admin@localhost.local", "findUsers") | |
| 65 | - | returns {"Users": {"1": "admin"}} as a JSON string """ | |
| 66 | 61 | inp = inp.split(" ") | |
| 62 | + | ||
| 67 | 63 | def fix(text): | |
| 68 | 64 | return str(text).replace('"', "'").replace("u'", "'") | |
| 65 | + | ||
| 69 | 66 | dats = {} | |
| 70 | 67 | for i in inp: | |
| 71 | 68 | if not "=" in i: | |
| @@ -76,20 +73,25 @@ def updateCommand(inp, method): | |||
| 76 | 73 | for k in dats: | |
| 77 | 74 | fields.append(k) | |
| 78 | 75 | values.append(dats[k]) | |
| 79 | - | data = multicraft("%s field=%s value=%s" % (method, fix(fields), fix(values))) | |
| 76 | + | data = multicraft("%s field=%s value=%s" | |
| 77 | + | % (method, fix(fields), fix(values))) | |
| 80 | 78 | if not "Failure: " in data: | |
| 81 | 79 | return data | |
| 82 | 80 | else: | |
| 83 | 81 | return None | |
| 84 | 82 | ||
| 83 | + | ||
| 85 | 84 | @hook.command(adminonly=True) | |
| 86 | 85 | def mcrestart(inp): | |
| 87 | - | """mcrestart <id> - Restart specified server. Admin only. id must be an integer.""" | |
| 86 | + | """mcrestart <id> - Restart specified server. | |
| 87 | + | Admin only. id must be an integer.""" | |
| 88 | 88 | data = getCommand(inp, "restartServer") | |
| 89 | 89 | if data == "[]": | |
| 90 | 90 | return "Success restarting server %s" % inp.split(" ")[0] | |
| 91 | 91 | else: | |
| 92 | - | return "Error restarting server %s. Invalid server ID?" % inp.split(" ")[0] | |
| 92 | + | return "Error restarting server %s." \ | |
| 93 | + | "Invalid server ID?" % inp.split(" ")[0] | |
| 94 | + | ||
| 93 | 95 | ||
| 94 | 96 | @hook.command(adminonly=True, autohelp=False) | |
| 95 | 97 | def mclist(): | |
| @@ -108,9 +110,11 @@ def mclist(): | |||
| 108 | 110 | else: | |
| 109 | 111 | return data | |
| 110 | 112 | ||
| 113 | + | ||
| 111 | 114 | @hook.command(adminonly=True) | |
| 112 | 115 | def mcfindusers(inp): | |
| 113 | - | """mcfindusers [name=value] [email=value] ... - Find user matching name, email, role, etc""" | |
| 116 | + | """mcfindusers [name=value] [email=value] ... | |
| 117 | + | - Find user matching name, email, role, etc""" | |
| 114 | 118 | data = updateCommand(inp, "findUsers") | |
| 115 | 119 | if data: | |
| 116 | 120 | data = json.loads(data) | |
| @@ -125,4 +129,4 @@ def mcfindusers(inp): | |||
| 125 | 129 | out = out + ", %s (%s)" % (data["Users"][i], i) | |
| 126 | 130 | return "Users: %s" % out | |
| 127 | 131 | else: | |
| 128 | - | return "Error getting data!" | |
| 132 | + | return "Error getting data!" | |
Steven Smith revised this gist . Go to revision
1 file changed, 128 insertions
multicraft.py(file created)
| @@ -0,0 +1,128 @@ | |||
| 1 | + | # For use with CloudBot (http://git.io/cbgit), although it'll also work as a Python module | |
| 2 | + | # ('import multicraft; data = multicraft.multicraft("METHOD PARAM=VALUE")') | |
| 3 | + | # Full API documentation coming soon (better than Multicraft's PHP-specific documentation, I'm hoping) | |
| 4 | + | ||
| 5 | + | # This file (multicraft.py) Copyright 2013 Steven Smith (blha303). All Rights Reserved. | |
| 6 | + | # GPLv3 license (because CloudBot is GPL) | |
| 7 | + | # http://opensource.org/licenses/GPL-3.0 | |
| 8 | + | ||
| 9 | + | from util import hook, http | |
| 10 | + | from urllib import urlencode | |
| 11 | + | import json | |
| 12 | + | ||
| 13 | + | url = "MULTICRAFTURL/api.php" | |
| 14 | + | user = "USER" | |
| 15 | + | apikey = 'APIKEY' | |
| 16 | + | ||
| 17 | + | ||
| 18 | + | @hook.command(adminonly=True) | |
| 19 | + | def multicraft(inp, reply=''): | |
| 20 | + | """multicraft <method> [option=value]... - Call multicraft server""" | |
| 21 | + | def getqstring(method, user, key, data={}): | |
| 22 | + | values = [] | |
| 23 | + | for k, v in data.iteritems(): | |
| 24 | + | values.append(v) | |
| 25 | + | key = http.get("http://blha303.com.au/md5.php?q=%s%s%s%s" | |
| 26 | + | % (key, method, user, "".join(values))) | |
| 27 | + | string = urlencode(dict(_MulticraftAPIMethod=method, | |
| 28 | + | _MulticraftAPIUser=user, | |
| 29 | + | _MulticraftAPIKey=key)) | |
| 30 | + | string += "&" + urlencode(data) | |
| 31 | + | return string | |
| 32 | + | ||
| 33 | + | method = inp.split(" ")[0] | |
| 34 | + | moredata = {} | |
| 35 | + | if len(inp.split(" ")) > 1: | |
| 36 | + | for i in inp.split(" ")[1:]: | |
| 37 | + | if not "=" in i: | |
| 38 | + | return "Invalid data at '%s'." % i | |
| 39 | + | moredata[i.split("=")[0]] = i.split("=")[1] | |
| 40 | + | data = http.get_json(url, post_data=getqstring(method, user, apikey, data=moredata)) | |
| 41 | + | if not data['success']: | |
| 42 | + | return "Failure: %s" % ", ".join(data['errors']) | |
| 43 | + | else: | |
| 44 | + | return json.dumps(data['data']) | |
| 45 | + | ||
| 46 | + | def getCommand(inp, method): | |
| 47 | + | """ for methods with only an 'id' parameter, like getUser | |
| 48 | + | example usage: getCommand("1", "getUser") will return | |
| 49 | + | {"User": {"lang": "", "email": "...", "global_role": "...", | |
| 50 | + | "id": "1", "name": "admin"}} as a JSON string """ | |
| 51 | + | try: | |
| 52 | + | id = int(inp.split(" ")[0]) | |
| 53 | + | except: | |
| 54 | + | return "id must be an integer." | |
| 55 | + | data = multicraft("%s id=%s" % (method, id)) | |
| 56 | + | if not "Failure: " in data: | |
| 57 | + | return data | |
| 58 | + | else: | |
| 59 | + | return None | |
| 60 | + | ||
| 61 | + | def updateCommand(inp, method): | |
| 62 | + | """ For methods that take field>value parameters. | |
| 63 | + | inp should be a string, "field=value field=value ..." | |
| 64 | + | Example: updateCommand("name=admin email=admin@localhost.local", "findUsers") | |
| 65 | + | returns {"Users": {"1": "admin"}} as a JSON string """ | |
| 66 | + | inp = inp.split(" ") | |
| 67 | + | def fix(text): | |
| 68 | + | return str(text).replace('"', "'").replace("u'", "'") | |
| 69 | + | dats = {} | |
| 70 | + | for i in inp: | |
| 71 | + | if not "=" in i: | |
| 72 | + | return "Invalid data at %s" % i | |
| 73 | + | dats[i.split("=")[0]] = i.split("=")[1] | |
| 74 | + | fields = [] | |
| 75 | + | values = [] | |
| 76 | + | for k in dats: | |
| 77 | + | fields.append(k) | |
| 78 | + | values.append(dats[k]) | |
| 79 | + | data = multicraft("%s field=%s value=%s" % (method, fix(fields), fix(values))) | |
| 80 | + | if not "Failure: " in data: | |
| 81 | + | return data | |
| 82 | + | else: | |
| 83 | + | return None | |
| 84 | + | ||
| 85 | + | @hook.command(adminonly=True) | |
| 86 | + | def mcrestart(inp): | |
| 87 | + | """mcrestart <id> - Restart specified server. Admin only. id must be an integer.""" | |
| 88 | + | data = getCommand(inp, "restartServer") | |
| 89 | + | if data == "[]": | |
| 90 | + | return "Success restarting server %s" % inp.split(" ")[0] | |
| 91 | + | else: | |
| 92 | + | return "Error restarting server %s. Invalid server ID?" % inp.split(" ")[0] | |
| 93 | + | ||
| 94 | + | @hook.command(adminonly=True, autohelp=False) | |
| 95 | + | def mclist(): | |
| 96 | + | """mclist - List servers.""" | |
| 97 | + | out = "" | |
| 98 | + | data = multicraft("listServers") | |
| 99 | + | if not "Failure " in data: | |
| 100 | + | data = json.loads(data)["Servers"] | |
| 101 | + | for k in data: | |
| 102 | + | v = data[k] | |
| 103 | + | if out == "": | |
| 104 | + | out = "%s (%s)" % (v, k) | |
| 105 | + | else: | |
| 106 | + | out = out + ", " + "%s (%s)" % (v, k) | |
| 107 | + | return "Servers: " + out | |
| 108 | + | else: | |
| 109 | + | return data | |
| 110 | + | ||
| 111 | + | @hook.command(adminonly=True) | |
| 112 | + | def mcfindusers(inp): | |
| 113 | + | """mcfindusers [name=value] [email=value] ... - Find user matching name, email, role, etc""" | |
| 114 | + | data = updateCommand(inp, "findUsers") | |
| 115 | + | if data: | |
| 116 | + | data = json.loads(data) | |
| 117 | + | if data["Users"] == []: | |
| 118 | + | return "No results." | |
| 119 | + | else: | |
| 120 | + | out = "" | |
| 121 | + | for i in data["Users"]: | |
| 122 | + | if not out: | |
| 123 | + | out = "%s (%s)" % (data["Users"][i], i) | |
| 124 | + | else: | |
| 125 | + | out = out + ", %s (%s)" % (data["Users"][i], i) | |
| 126 | + | return "Users: %s" % out | |
| 127 | + | else: | |
| 128 | + | return "Error getting data!" | |