Christian Damken
745c313fd5
- added passwd_request() and passwd_reply() to class comm - updated authentication process to fit protocol - fixed version numbers for each *.py-file
177 lines
5 KiB
Python
177 lines
5 KiB
Python
# includes/comm.py
|
|
#
|
|
# module version: 0.0.20130625
|
|
# for protocol version 0.2.20130625
|
|
#
|
|
#
|
|
# contains:
|
|
# init()
|
|
# header()
|
|
# encode_message()
|
|
# decode_message()
|
|
# motd()
|
|
# passwd_request()
|
|
# passwd_reply()
|
|
# command()
|
|
#
|
|
|
|
|
|
from .statuscodes import statuscodes
|
|
|
|
|
|
class comm:
|
|
|
|
# just some settings
|
|
aliases = dict()
|
|
server_version = ""
|
|
protocol_version = ""
|
|
verbosity = 0
|
|
|
|
|
|
# initializes global settings
|
|
def init(ServerVersion, ProtocolVersion, Verbosity, Aliases, Password):
|
|
"""
|
|
makes settings aviable in this class and initialize auth
|
|
"""
|
|
global aliases, server_version, protocol_version, verbosity
|
|
aliases = Aliases
|
|
server_version = ServerVersion
|
|
protocol_version = ProtocolVersion
|
|
verbosity = Verbosity
|
|
|
|
|
|
# builds an header
|
|
def header(CodeList, AdditionalHeaderLines = "", Newlines = True):
|
|
"""
|
|
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)] + " " +\
|
|
statuscodes.description['s' + CodeList[int(i)]]
|
|
if AdditionalHeaderLines:
|
|
ret += "\n" + AdditionalHeaderLines
|
|
if Newlines: ret += "\n\n"
|
|
return ret
|
|
|
|
|
|
# formats a massage
|
|
def encode_message(text):
|
|
"""
|
|
formats a massage, replaces some things
|
|
"""
|
|
return text.replace('{', '\{').replace('}', '\}')
|
|
|
|
|
|
# "deformats" a message
|
|
def decode_message(text):
|
|
"""
|
|
removes replacements in a message
|
|
"""
|
|
return text.replace('\{', '{').replace('\}', '}')
|
|
|
|
|
|
# returns the motd
|
|
def motd(motd):
|
|
"""
|
|
builds and returns a motd package
|
|
"""
|
|
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"
|
|
|
|
|
|
# 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)
|
|
|
|
hdr = comm.header(['202','002'])
|
|
ret = "ServerVersion:" + server_version + "\n"\
|
|
"ProtocolVersion:" + protocol_version + "\n"
|
|
|
|
# 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..."
|
|
|
|
# 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
|
|
hdr = comm.header(['202'])
|
|
|
|
# send the list of aliases
|
|
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
|
|
hdr = comm.header(['201'])
|
|
|
|
# ohmagawd! a debug message!!1! (sry...)
|
|
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
|
|
hdr = comm.header(['203'])
|
|
|
|
if verbosity >= 2: print("Got invalid command from",
|
|
str(client_address), ": ", data)
|
|
|
|
ret = comm.encode_message(ret) + "{END}\n"
|
|
|
|
return hdr + ret
|