import string,cgi,time,os,socket from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer from permissionsparser import parse, writeYml #Set up some global variables PERMISSIONS_FILE = '' ESSENTIALS_FILE = '' MCSTATS_JSON_FILE = '' MINECRAFT_LCK_FILE = '' TELNET_HOST = '' TELNET_PORT = TELNET_USER = '' TELNET_PASS = '' PASSWORD = '' #this is for http form authentication #and also very important... (these are for THIS WEBSERVER SCRIPT)!!! MINECRAFT_SERVER_NAME = 'survival.minecraft.co.nz' MINECRAFT_SERVER_PORT = 8101 class MyHandler(BaseHTTPRequestHandler): def do_GET(self): try: if self.path.endswith("users"): f = open(PERMISSIONS_FILE, 'r') self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() can_start = False st_mtime = os.stat(PERMISSIONS_FILE).st_mtime send_data = '{"epochTime":"%s","users":{' % st_mtime for i in f.readlines(): if can_start == True and not ' - ' in i and not 'permissions:' in i: if 'group:' in i: send_data += '"%s",' % i[15:].strip() else: send_data += '"%s":' % i.strip()[:-1] if 'users:' in i: can_start = True f.close() send_data = send_data[:-1] + '}}' self.wfile.write(send_data) return if self.path.endswith("motd"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() f = open(ESSENTIALS_FILE, 'r') w = f.readlines() f.close() ready = False motd = [] for i in w: if ready == True: if i[:4] == ' - ': motd += [i[5:-2]] else: ready = False if i == 'motd:\n': ready = True self.wfile.write("

Message of the Day editor

\

This message appears when players join or type '/motd'.

\

Colour codes:

\
\
Password:
\ (takes about 8 seconds)
") return # BEGIN +============================================================================= if self.path.endswith("permissions"): self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() ymldata, users = parse(PERMISSIONS_FILE) u = [] for i in users: u += [[i, users[i]['group']]] u = sorted(u) self.wfile.write("

Permissions Editor (alpha)

\
\ Select a user or add a new one \

New group \



Password: \
") return # END +============================================================================= else: f = open(MCSTATS_JSON_FILE) self.send_response(200) self.send_header('Content-type', 'text/html') self.end_headers() can_start = False status = os.path.isfile(MINECRAFT_LCK_FILE) send_data = '{"serverStatus":"%s",' % status usersonline = [] for i in f.read().split('playersOnline')[1].split('playerName":'): if i[1] != ':': usersonline += [i.split('"')[1]] playercount = len(usersonline) if status == False: playercount = 0 if usersonline == ['No One']: playercount = 0 send_data += '"playerCount":%s,"playersOnline":"' % playercount if status == False: send_data += ',' else: for i in usersonline: send_data += '%s,' % i send_data = send_data[:-1] + '"}' self.wfile.write(send_data) f.close() return return except IOError: self.send_error(404,'File Not Found: %s' % self.path) def do_POST(self): global rootnode try: if self.path.endswith("motd"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': query=cgi.parse_multipart(self.rfile, pdict) self.send_response(301) self.end_headers() response = query.get('motd')[0].split('\r\n') pwd = query.get('pwd')[0] if pwd == PASSWORD: self.wfile.write('

Success!

') f = open(ESSENTIALS_FILE, 'r') w = f.readlines() f.close() ready = False config = [] for i in w: if ready == True: if i[:4] != ' - ': for j in response: config += [" - '%s'\n" % j] config += i ready = False else: config += i if i == 'motd:\n': ready = True f = open(ESSENTIALS_FILE, 'w') f.write(''.join(config)) f.close() self.telnetCommand('reload') else: self.wfile.write('

Error!

Invalid password.') self.wfile.write("
Go back\ " % (MINECRAFT_SERVER_NAME, MINECRAFT_SERVER_PORT)) if self.path.endswith("permissions-confirm"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': query=cgi.parse_multipart(self.rfile, pdict) self.send_response(301) self.end_headers() userselect = query.get('userselect')[0] usertext = query.get('usertext')[0] pwd = query.get('pwd')[0] group = query.get('newgroup')[0] user = userselect if usertext: user = usertext if pwd == PASSWORD: self.wfile.write('

Please confirm:

You want to change %s to group %s\

Is this correct?' % (user, group)) self.wfile.write("


\n\ (takes about 8 seconds)\n\ \n\ \n\
" % (MINECRAFT_SERVER_NAME, MINECRAFT_SERVER_PORT, user, group)) else: self.wfile.write('

Error!

Invalid password.') if self.path.endswith("permissions-proceed"): ctype, pdict = cgi.parse_header(self.headers.getheader('content-type')) if ctype == 'multipart/form-data': query=cgi.parse_multipart(self.rfile, pdict) self.send_response(301) self.end_headers() #get our info user = query.get('user')[0] group = query.get('group')[0] ymldata, users = parse(PERMISSIONS_FILE) if user not in users: users[user] = {} users[user]['group'] = group #apply changes writeYml(ymldata, users, PERMISSIONS_FILE) self.telnetCommand('say User %s has been moved to %s' % (user, group)) result = self.telnetCommand('permissions -reload all') self.wfile.write('

Success!

Console output:
%s

' % '
'.join(result)) self.wfile.write("Go back" % (MINECRAFT_SERVER_NAME, MINECRAFT_SERVER_PORT)) except : print '--- An internal error occurred while processing the POST data' def telnetCommand(self, cmd): try: s = socket.socket() s.connect((TELNET_HOST, TELNET_PORT)) s.send('%s\n' % TELNET_USER) time.sleep(2) s.send('%s\n' % TELNET_PASS) time.sleep(2) s.send('%s\n' % cmd) time.sleep(2) r = s.recv(1024) s.close() j = [] for i in r.split('\r\n'): if len(i) > 1 and i[0] == ':': j += [i[1:]] return j except: pass def main(): try: server = HTTPServer(('', MINECRAFT_SERVER_PORT), MyHandler) print 'started httpserver...' server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down server' server.socket.close() if __name__ == '__main__': main()