multicraft.py
· 4.0 KiB · Python
Raw
# For use with CloudBot (http://git.io/cbgit), although it'll also work as a Python module
# ('import multicraft; data = multicraft.multicraft("METHOD PARAM=VALUE")')
# Full API documentation coming soon (better than Multicraft's PHP-specific documentation, I'm hoping)
# This file (multicraft.py) created by Steven Smith (blha303) 2013
# GPLv3 license (because CloudBot is GPL)
# http://opensource.org/licenses/GPL-3.0
from util import hook, http
from urllib import urlencode
import json
url = "MULTICRAFTURL/api.php"
user = "USER"
apikey = 'APIKEY'
@hook.command(adminonly=True)
def multicraft(inp, reply=''):
"""multicraft <method> [option=value]... - Call multicraft server"""
def getqstring(method, user, key, data={}):
values = []
for k, v in data.iteritems():
values.append(v)
key = http.get("http://blha303.com.au/md5.php?q=%s%s%s%s"
% (key, method, user, "".join(values)))
string = urlencode(dict(_MulticraftAPIMethod=method,
_MulticraftAPIUser=user,
_MulticraftAPIKey=key))
string += "&" + urlencode(data)
return string
method = inp.split(" ")[0]
moredata = {}
if len(inp.split(" ")) > 1:
for i in inp.split(" ")[1:]:
if not "=" in i:
return "Invalid data at '%s'." % i
moredata[i.split("=")[0]] = i.split("=")[1]
data = http.get_json(url, post_data=getqstring(method, user, apikey,
data=moredata))
if not data['success']:
return "Failure: %s" % ", ".join(data['errors'])
else:
return json.dumps(data['data'])
def getCommand(inp, method):
try:
id = int(inp.split(" ")[0])
except:
return "id must be an integer."
data = multicraft("%s id=%s" % (method, id))
if not "Failure: " in data:
return data
else:
return None
def updateCommand(inp, method):
inp = inp.split(" ")
def fix(text):
return str(text).replace('"', "'").replace("u'", "'")
dats = {}
for i in inp:
if not "=" in i:
return "Invalid data at %s" % i
dats[i.split("=")[0]] = i.split("=")[1]
fields = []
values = []
for k in dats:
fields.append(k)
values.append(dats[k])
data = multicraft("%s field=%s value=%s"
% (method, fix(fields), fix(values)))
if not "Failure: " in data:
return data
else:
return None
@hook.command(adminonly=True)
def mcrestart(inp):
"""mcrestart <id> - Restart specified server.
Admin only. id must be an integer."""
data = getCommand(inp, "restartServer")
if data == "[]":
return "Success restarting server %s" % inp.split(" ")[0]
else:
return "Error restarting server %s." \
"Invalid server ID?" % inp.split(" ")[0]
@hook.command(adminonly=True, autohelp=False)
def mclist(inp):
"""mclist - List servers."""
out = ""
data = multicraft("listServers")
if not "Failure " in data:
data = json.loads(data)["Servers"]
for k in data:
v = data[k]
if out == "":
out = "%s (%s)" % (v, k)
else:
out = out + ", " + "%s (%s)" % (v, k)
return "Servers: " + out
else:
return data
@hook.command(adminonly=True)
def mcfindusers(inp):
"""mcfindusers [name=value] [email=value] ...
- Find user matching name, email, role, etc"""
data = updateCommand(inp, "findUsers")
if data:
data = json.loads(data)
if data["Users"] == []:
return "No results."
else:
out = ""
for i in data["Users"]:
if not out:
out = "%s (%s)" % (data["Users"][i], i)
else:
out = out + ", %s (%s)" % (data["Users"][i], i)
return "Users: %s" % out
else:
return "Error getting data!"
| 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) created by Steven Smith (blha303) 2013 |
| 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, |
| 41 | data=moredata)) |
| 42 | if not data['success']: |
| 43 | return "Failure: %s" % ", ".join(data['errors']) |
| 44 | else: |
| 45 | return json.dumps(data['data']) |
| 46 | |
| 47 | |
| 48 | def getCommand(inp, method): |
| 49 | try: |
| 50 | id = int(inp.split(" ")[0]) |
| 51 | except: |
| 52 | return "id must be an integer." |
| 53 | data = multicraft("%s id=%s" % (method, id)) |
| 54 | if not "Failure: " in data: |
| 55 | return data |
| 56 | else: |
| 57 | return None |
| 58 | |
| 59 | |
| 60 | def updateCommand(inp, method): |
| 61 | inp = inp.split(" ") |
| 62 | |
| 63 | def fix(text): |
| 64 | return str(text).replace('"', "'").replace("u'", "'") |
| 65 | |
| 66 | dats = {} |
| 67 | for i in inp: |
| 68 | if not "=" in i: |
| 69 | return "Invalid data at %s" % i |
| 70 | dats[i.split("=")[0]] = i.split("=")[1] |
| 71 | fields = [] |
| 72 | values = [] |
| 73 | for k in dats: |
| 74 | fields.append(k) |
| 75 | values.append(dats[k]) |
| 76 | data = multicraft("%s field=%s value=%s" |
| 77 | % (method, fix(fields), fix(values))) |
| 78 | if not "Failure: " in data: |
| 79 | return data |
| 80 | else: |
| 81 | return None |
| 82 | |
| 83 | |
| 84 | @hook.command(adminonly=True) |
| 85 | def mcrestart(inp): |
| 86 | """mcrestart <id> - Restart specified server. |
| 87 | 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." \ |
| 93 | "Invalid server ID?" % inp.split(" ")[0] |
| 94 | |
| 95 | |
| 96 | @hook.command(adminonly=True, autohelp=False) |
| 97 | def mclist(inp): |
| 98 | """mclist - List servers.""" |
| 99 | out = "" |
| 100 | data = multicraft("listServers") |
| 101 | if not "Failure " in data: |
| 102 | data = json.loads(data)["Servers"] |
| 103 | for k in data: |
| 104 | v = data[k] |
| 105 | if out == "": |
| 106 | out = "%s (%s)" % (v, k) |
| 107 | else: |
| 108 | out = out + ", " + "%s (%s)" % (v, k) |
| 109 | return "Servers: " + out |
| 110 | else: |
| 111 | return data |
| 112 | |
| 113 | |
| 114 | @hook.command(adminonly=True) |
| 115 | def mcfindusers(inp): |
| 116 | """mcfindusers [name=value] [email=value] ... |
| 117 | - Find user matching name, email, role, etc""" |
| 118 | data = updateCommand(inp, "findUsers") |
| 119 | if data: |
| 120 | data = json.loads(data) |
| 121 | if data["Users"] == []: |
| 122 | return "No results." |
| 123 | else: |
| 124 | out = "" |
| 125 | for i in data["Users"]: |
| 126 | if not out: |
| 127 | out = "%s (%s)" % (data["Users"][i], i) |
| 128 | else: |
| 129 | out = out + ", %s (%s)" % (data["Users"][i], i) |
| 130 | return "Users: %s" % out |
| 131 | else: |
| 132 | return "Error getting data!" |
| 133 |