def rconCommand(self, event, commandstr): if not config.getboolean("rcon", "enable"): return argv = commandstr.split() if event.isByOp() and len(config.get("rcon", "password")) > 0: msg = commandstr[len(argv[0]) + 1:] password = config.get("rcon", "password") elif len(argv) < 3: event.respond("Usage: rcon <password> <msg>") return else: msg = commandstr[len(argv[0]) + len(argv[1]) + 1:] password = argv[1] self.lastevent = event payload = openttd.structz.pack('zz', password, msg) self.client.sendMsg_TCP(const.PACKET_CLIENT_RCON, payload)
def isCommand(self): """ Check if the message could be a command @rtype: boolean @return: True if the msg could be a command(starts with the command prefix) """ return self.msg.startswith(config.get("main", "commandprefix"))
def readTimeFile(self, forcenew=False): timestr = '' try: f = open(config.get('timewarning', 'starttimefile'), 'r') timestr = f.read() f.close() except: LOG.error("error while reading starttimefile") if timestr != '' and not forcenew: self.server_start = int(timestr.strip()) else: f = open(config.get('timewarning', 'starttimefile'), 'wb') f.write(str(int(time.time()))) f.close() self.server_start = int(time.time()) self.server_runtime = config.getint('timewarning', 'time_running')
def clearStats(self): if not config.getboolean('stats', 'enable'): return fn = config.get("stats", "cachefilename") try: os.remove(fn) LOG.debug("stats cleared") except: pass
def updateStats(self): self.last = time.time() if not config.getboolean('stats', 'enable'): return LOG.debug("updating stats...") tstart = time.time() fn = config.get("stats", "cachefilename") obj = [] firstSave = False try: f = open(fn, 'rb') obj = pickle.load(f) f.close() except IOError: firstSave = True value = [ self.client.getGameInfo(encode_grfs=True, short=not firstSave), self.client.getCompanyInfo().companies, tstart ] obj.append(value) try: f = open(fn, 'wb') #if you use python < 2.3 use this line: #pickle.dump(obj, f) pickle.dump(obj, f, 1) f.close() except IOError: LOG.error("error while saving stats cache file!") tdiff = time.time() - tstart fs = float(os.path.getsize(fn)) / float(1024) LOG.debug( "stats updated in %0.5f seconds. File is %.2fKB big (%d lines)" % (tdiff, fs, len(obj)))
def updateStats(self): self.last = time.time() if not config.getboolean('stats', 'enable'): return LOG.debug("updating stats...") tstart = time.time() fn = config.get("stats", "cachefilename") obj = [] firstSave = False try: f = open(fn, 'rb') obj = pickle.load(f) f.close() except IOError: firstSave=True value = [ self.client.getGameInfo(encode_grfs=True, short=not firstSave), self.client.getCompanyInfo().companies, tstart ] obj.append(value) try: f = open(fn, 'wb') #if you use python < 2.3 use this line: #pickle.dump(obj, f) pickle.dump(obj, f, 1) f.close() except IOError: LOG.error("error while saving stats cache file!") tdiff = time.time() - tstart fs = float(os.path.getsize(fn)) / float(1024) LOG.debug("stats updated in %0.5f seconds. File is %.2fKB big (%d lines)"%(tdiff, fs, len(obj)))
def isCommand(self): return self.msg.startswith(config.get("main", "commandprefix")) and not self._isAction()
def do_GET(self): #print self.path self.path = urllib.quote (urllib.unquote (self.path)) basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'web') #print self.path if self.path == "/": cls = self.server._callbackclass content = "running on %s:%d<p/>" % (cls.ip, cls.port) playerlist = "no information availabe, please check back later<p/>" if not cls is None: playerlist="<ul>" for clientid in cls.playerlist.keys(): playerlist+= "<li>Client #%d: %s, playing in company %d</li>" % (clientid, cls.playerlist[clientid]['name'], cls.playerlist[clientid]['company']) playerlist+="</ul>" content += playerlist template = self.loadTemplate("index.html") if not template is None: output = template % { 'content':content, 'server_ip': cls.ip, 'server_port': cls.port, } else: self.send_error(404) return encoding, data = self.compress(output) self.send_response(200, "OK") self.send_header('Content-type', 'text/html') if not encoding is None: self.send_header('Content-Encoding', encoding) self.end_headers() self.wfile.write(data) elif self.path == "/data/companies": try: f = open(config.get("stats", "cachefilename"), 'rb') except IOError: obj = [] else: obj = pickle.load(f) f.close() cls = self.server._callbackclass jsonoutput = json.dumps(obj, sort_keys=True, indent=4, cls=DataStorageJSONEncoder) encoding, data = self.compress(jsonoutput) self.send_response(200, "OK") self.send_header('Content-type', 'application/json') if not encoding is None: self.send_header('Content-Encoding', encoding) self.send_header ("Content-Length", len(data)) self.end_headers() self.wfile.write(data) elif self.path == "/data/clients": cls = self.server._callbackclass response = json.dumps(cls.playerlist, indent=4, sort_keys=True) encoding, data = self.compress(response) self.send_response(200, "OK") self.send_header('Content-type', 'application/json') if not encoding is None: self.send_header('Content-Encoding', encoding) self.send_header ("Content-Length", len(data)) self.end_headers() self.wfile.write(data) else: path = urllib.unquote(self.path) if path.find("?") >= 0: path = path[:path.find("?")] fn = os.path.normpath(os.path.join(basedir, path.lstrip('/'))) #print path, fn if os.path.isdir(fn): newindex = os.path.join(fn,'index.html') if os.path.isfile(newindex): fn = newindex else: self.send_error(500, "couldn't find template") return if os.path.exists(fn): f = open(fn, "rb") content = f.read() f.close() encoding, data = self.compress(content) self.send_response (200) self.send_header ("Content-type", content_type(fn)) if not encoding is None: self.send_header("Content-Encoding", encoding) self.send_header ("Content-Length", len(data)) self.end_headers() self.wfile.write(data) else: self.send_error(404) return
def isCommand(self): return self.msg.startswith(config.get( "main", "commandprefix")) and not self._isAction()
def do_GET(self): #print self.path self.path = urllib.quote(urllib.unquote(self.path)) basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'web') #print self.path if self.path == "/": cls = self.server._callbackclass content = "running on %s:%d<p/>" % (cls.ip, cls.port) playerlist = "no information availabe, please check back later<p/>" if not cls is None: playerlist = "<ul>" for clientid in cls.playerlist.keys(): playerlist += "<li>Client #%d: %s, playing in company %d</li>" % ( clientid, cls.playerlist[clientid]['name'], cls.playerlist[clientid]['company']) playerlist += "</ul>" content += playerlist template = self.loadTemplate("index.html") if not template is None: output = template % { 'content': content, 'server_ip': cls.ip, 'server_port': cls.port, } else: self.send_error(404) return encoding, data = self.compress(output) self.send_response(200, "OK") self.send_header('Content-type', 'text/html') if not encoding is None: self.send_header('Content-Encoding', encoding) self.end_headers() self.wfile.write(data) elif self.path == "/data/companies": try: f = open(config.get("stats", "cachefilename"), 'rb') except IOError: obj = [] else: obj = pickle.load(f) f.close() cls = self.server._callbackclass jsonoutput = json.dumps(obj, sort_keys=True, indent=4, cls=DataStorageJSONEncoder) encoding, data = self.compress(jsonoutput) self.send_response(200, "OK") self.send_header('Content-type', 'application/json') if not encoding is None: self.send_header('Content-Encoding', encoding) self.send_header("Content-Length", len(data)) self.end_headers() self.wfile.write(data) elif self.path == "/data/clients": cls = self.server._callbackclass response = json.dumps(cls.playerlist, indent=4, sort_keys=True) encoding, data = self.compress(response) self.send_response(200, "OK") self.send_header('Content-type', 'application/json') if not encoding is None: self.send_header('Content-Encoding', encoding) self.send_header("Content-Length", len(data)) self.end_headers() self.wfile.write(data) else: path = urllib.unquote(self.path) if path.find("?") >= 0: path = path[:path.find("?")] fn = os.path.normpath(os.path.join(basedir, path.lstrip('/'))) #print path, fn if os.path.isdir(fn): newindex = os.path.join(fn, 'index.html') if os.path.isfile(newindex): fn = newindex else: self.send_error(500, "couldn't find template") return if os.path.exists(fn): f = open(fn, "rb") content = f.read() f.close() encoding, data = self.compress(content) self.send_response(200) self.send_header("Content-type", content_type(fn)) if not encoding is None: self.send_header("Content-Encoding", encoding) self.send_header("Content-Length", len(data)) self.end_headers() self.wfile.write(data) else: self.send_error(404) return