2013-04-26 12:55:37 +02:00
|
|
|
# includes/comm.py
|
2013-04-24 20:25:50 +02:00
|
|
|
#
|
2013-04-26 12:55:37 +02:00
|
|
|
# module version: 0.0.20130426
|
2013-04-24 20:25:50 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
|
2013-04-26 12:55:37 +02:00
|
|
|
import statuscodes
|
|
|
|
|
|
|
|
|
|
|
|
class comm:
|
2013-04-24 20:25:50 +02:00
|
|
|
|
2013-04-26 12:55:37 +02:00
|
|
|
aliases = dict()
|
|
|
|
server_version = ""
|
|
|
|
protocol_version = ""
|
|
|
|
verbosity = 0
|
|
|
|
|
|
|
|
# initializes global settings
|
|
|
|
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases):
|
2013-04-24 20:25:50 +02:00
|
|
|
|
2013-04-26 12:55:37 +02:00
|
|
|
aliases = Aliases
|
|
|
|
server_version = ServerVersion
|
|
|
|
protocol_version = ProtocolVersion
|
|
|
|
verbosity = Verbosity
|
|
|
|
|
|
|
|
def header(statuscode, AdditionalHeaderLines = ""):
|
|
|
|
"""
|
|
|
|
returns the header
|
|
|
|
"""
|
|
|
|
|
|
|
|
ret =\
|
2013-04-24 20:25:50 +02:00
|
|
|
"{BEGIN}\n"\
|
2013-04-26 12:55:37 +02:00
|
|
|
"asrcp" + protocol_version + "\n"\
|
|
|
|
statuscode + " " + statuscodes.description[statuscodes] + "\n"\
|
|
|
|
"ServerVersion:" + server_version + "\n"
|
|
|
|
if AdditionalHeaderLines != "": ret += AdditionalHeaderLines + "\n"
|
|
|
|
ret += "\n\n"
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# returns the motd
|
|
|
|
def motd(motd):
|
|
|
|
"""
|
|
|
|
builds and returns a motd package
|
|
|
|
"""
|
|
|
|
|
|
|
|
ret = motd
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
# handles the content
|
|
|
|
def command(client_address, data):
|
|
|
|
"""
|
|
|
|
processes a command
|
|
|
|
"""
|
|
|
|
|
|
|
|
ret = ""
|
2013-04-24 20:25:50 +02:00
|
|
|
|
|
|
|
# Look if the received message is an
|
|
|
|
# valid alias or a predefined command
|
|
|
|
|
|
|
|
# if it's 'version', return the server and protocol version
|
|
|
|
if data == "version":
|
|
|
|
|
|
|
|
if verbosity >= 2: print("Got valid service command from"
|
|
|
|
+ str(client_address) + ": ", data)
|
|
|
|
|
|
|
|
ret = ret +\
|
|
|
|
"202 Valid Service Command\n"\
|
|
|
|
"002 Version\n"\
|
|
|
|
"ServerVersion:" + server_version + "\n"\
|
|
|
|
"ProtocolVersion:" + protocol_version + "\n"
|
|
|
|
|
|
|
|
# if it's 'help', give a little help
|
|
|
|
elif data == 'help':
|
|
|
|
|
|
|
|
if verbosity >= 2: print("Got valid command from"
|
|
|
|
+ str(client_address) + ": ", data)
|
|
|
|
|
|
|
|
# send status code
|
|
|
|
ret = ret + "202 Valid Service Command\n\n"
|
|
|
|
|
|
|
|
# send the list of aliases
|
|
|
|
ret = ret + "Aviable aliases:\n"
|
|
|
|
for i in aliases.keys():
|
|
|
|
ret = ret + str(i) + "\n"
|
|
|
|
|
|
|
|
# if it's a valid userdefined command
|
|
|
|
elif data in aliases:
|
|
|
|
|
|
|
|
# send status code
|
|
|
|
ret = ret + "201 Valid Command\n\n"
|
|
|
|
|
|
|
|
# ohmagawd! a debug message!!1!
|
|
|
|
if verbosity >= 2: print("Got valid command from"
|
|
|
|
+ str(client_address) + ": ", data)
|
|
|
|
|
|
|
|
# execute the aliased command
|
|
|
|
g_dict, l_dict = {}, {}
|
|
|
|
exec(str(aliases[data]), g_dict, l_dict)
|
|
|
|
|
|
|
|
# send may contain data to send to the client
|
|
|
|
if l_dict["send"]:
|
|
|
|
content = str(l_dict["send"]).replace('{', '\{')
|
|
|
|
content = content.replace('}', '\}')
|
|
|
|
|
|
|
|
ret = ret + content + "\n"
|
|
|
|
|
|
|
|
# ALL IS LOST!!1! this has to be invalid!
|
|
|
|
else:
|
|
|
|
|
|
|
|
# send status code
|
|
|
|
ret = ret + "203 Invalid Command\n"
|
|
|
|
|
|
|
|
if verbosity >= 2: print("Got invalid command from",
|
|
|
|
str(client_address), ": ", data)
|
|
|
|
|
|
|
|
ret = ret + "{END}\n"
|
|
|
|
|
|
|
|
return ret
|