Last active
December 20, 2015 08:49
-
-
Save blha303/6103147 to your computer and use it in GitHub Desktop.
Multicraft Python Cloudbot plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
# Updated to use hashlib instead of calling an external website for md5 hashing | |
# For a working example of this, join irc.esper.net #xD and ask blha303 for a demonstration of multicraft.py | |
from util import hook, http | |
from urllib import urlencode | |
import json | |
import hashlib | |
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 = hashlib.md5("%s%s%s%s" % (key, method, user, "".join(values))).hexdigest() | |
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!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment