2013-04-26 12:55:37 +02:00
|
|
|
# includes/comm.py
|
2013-04-24 20:25:50 +02:00
|
|
|
#
|
2013-06-25 14:03:28 +02:00
|
|
|
# module version: 0.0.20130625
|
|
|
|
# for protocol version 0.2.20130625
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# contains:
|
|
|
|
# init()
|
|
|
|
# header()
|
|
|
|
# encode_message()
|
|
|
|
# decode_message()
|
|
|
|
# motd()
|
|
|
|
# passwd_request()
|
|
|
|
# passwd_reply()
|
|
|
|
# command()
|
2013-04-24 20:25:50 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
from .statuscodes import statuscodes
|
2013-04-26 12:55:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
class comm:
|
2013-04-29 21:51:20 +02:00
|
|
|
|
2013-06-25 14:03:28 +02:00
|
|
|
# just some settings
|
2013-04-29 21:51:20 +02:00
|
|
|
aliases = dict()
|
|
|
|
server_version = ""
|
|
|
|
protocol_version = ""
|
|
|
|
verbosity = 0
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# initializes global settings
|
2013-06-19 16:25:43 +02:00
|
|
|
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases, Password):
|
2013-04-29 21:51:20 +02:00
|
|
|
"""
|
2013-06-19 16:25:43 +02:00
|
|
|
makes settings aviable in this class and initialize auth
|
2013-04-29 21:51:20 +02:00
|
|
|
"""
|
|
|
|
global aliases, server_version, protocol_version, verbosity
|
|
|
|
aliases = Aliases
|
|
|
|
server_version = ServerVersion
|
|
|
|
protocol_version = ProtocolVersion
|
|
|
|
verbosity = Verbosity
|
2013-06-25 14:03:28 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
|
|
|
|
# builds an header
|
2013-06-25 14:03:28 +02:00
|
|
|
def header(CodeList, AdditionalHeaderLines = "", Newlines = True):
|
2013-04-29 21:51:20 +02:00
|
|
|
"""
|
|
|
|
returns the header with one ore mode given status codes
|
|
|
|
and optional additional header lines
|
|
|
|
"""
|
|
|
|
ret =\
|
|
|
|
"{BEGIN}\n"\
|
|
|
|
"asrcp" + protocol_version + "\n"
|
|
|
|
for i in range(0, len(CodeList)):
|
|
|
|
ret += CodeList[int(i)] + " " +\
|
2013-06-25 14:03:28 +02:00
|
|
|
statuscodes.description['s' + CodeList[int(i)]]
|
|
|
|
if AdditionalHeaderLines:
|
|
|
|
ret += "\n" + AdditionalHeaderLines
|
|
|
|
if Newlines: ret += "\n\n"
|
2013-04-29 21:51:20 +02:00
|
|
|
return ret
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# formats a massage
|
|
|
|
def encode_message(text):
|
|
|
|
"""
|
|
|
|
formats a massage, replaces some things
|
|
|
|
"""
|
|
|
|
return text.replace('{', '\{').replace('}', '\}')
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# "deformats" a message
|
|
|
|
def decode_message(text):
|
|
|
|
"""
|
|
|
|
removes replacements in a message
|
|
|
|
"""
|
|
|
|
return text.replace('\{', '{').replace('\}', '}')
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# returns the motd
|
|
|
|
def motd(motd):
|
|
|
|
"""
|
|
|
|
builds and returns a motd package
|
|
|
|
"""
|
2013-06-25 14:03:28 +02:00
|
|
|
return comm.header(['202', '003'], "", False) + comm.encode_message(motd) + "\n{END}\n"
|
|
|
|
|
|
|
|
|
|
|
|
def passwd_request():
|
|
|
|
"""
|
|
|
|
sends a password request
|
|
|
|
"""
|
|
|
|
return comm.header(['101'], "", False) + "\n{END}\n"
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def passwd_reply(valid):
|
|
|
|
"""
|
|
|
|
replies to a password
|
|
|
|
"""
|
|
|
|
if valid:
|
|
|
|
return comm.header(['102'], "", False) + "\n{END}\n"
|
|
|
|
else:
|
|
|
|
return comm.header(['103'], "", False) + "\n{END}\n"
|
2013-06-19 16:25:43 +02:00
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
|
|
|
|
# handles the content
|
|
|
|
def command(client_address, data):
|
|
|
|
"""
|
|
|
|
processes a command
|
|
|
|
"""
|
|
|
|
ret = ""
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
hdr = comm.header(['202','002'])
|
|
|
|
ret = "ServerVersion:" + server_version + "\n"\
|
2013-04-29 21:51:20 +02:00
|
|
|
"ProtocolVersion:" + protocol_version + "\n"
|
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
# if it's 'exit', then send .. nothing!
|
|
|
|
elif data == 'exit':
|
|
|
|
if verbosity >= 2: print("Got valid service command from"
|
|
|
|
+ str(client_address) + ": ", data)
|
|
|
|
|
|
|
|
hdr = comm.header(['202','004'])
|
|
|
|
ret = "closing connection..."
|
|
|
|
|
2013-04-29 21:51:20 +02:00
|
|
|
# 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
|
2013-06-19 16:25:43 +02:00
|
|
|
hdr = comm.header(['202'])
|
2013-04-29 21:51:20 +02:00
|
|
|
|
|
|
|
# send the list of aliases
|
2013-06-19 16:25:43 +02:00
|
|
|
ret = "Aviable aliases:\n"
|
2013-04-29 21:51:20 +02:00
|
|
|
for i in aliases.keys():
|
|
|
|
ret = ret + str(i) + "\n"
|
|
|
|
|
|
|
|
# if it's a valid userdefined command
|
|
|
|
elif data in aliases:
|
|
|
|
|
|
|
|
# send status code
|
2013-06-19 16:25:43 +02:00
|
|
|
hdr = comm.header(['201'])
|
2013-04-29 21:51:20 +02:00
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
# ohmagawd! a debug message!!1! (sry...)
|
2013-04-29 21:51:20 +02:00
|
|
|
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
|
2013-06-19 16:25:43 +02:00
|
|
|
hdr = comm.header(['203'])
|
2013-04-29 21:51:20 +02:00
|
|
|
|
|
|
|
if verbosity >= 2: print("Got invalid command from",
|
|
|
|
str(client_address), ": ", data)
|
|
|
|
|
2013-06-25 14:03:28 +02:00
|
|
|
ret = comm.encode_message(ret) + "{END}\n"
|
2013-04-29 21:51:20 +02:00
|
|
|
|
2013-06-19 16:25:43 +02:00
|
|
|
return hdr + ret
|