From 532b70484736563f3f65c55c2c9c760b7daf1c3d Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Tue, 4 Dec 2012 13:56:08 +0100 Subject: [PATCH] argument processing changed --- modules/active/cowsay.py | 9 ++++----- modules/active/tell.py | 14 ++++++------- sys/brathuhn.py | 4 ++-- sys/util.py | 43 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/modules/active/cowsay.py b/modules/active/cowsay.py index d48d6e4..985db5e 100644 --- a/modules/active/cowsay.py +++ b/modules/active/cowsay.py @@ -5,12 +5,11 @@ def cowsay(msg,args): text="" if( len(args)==2 ): text=args[1] - elif( len(args)>=3 ): - cowfile=args[1] - for i in range(2,len(args)): - text+=args[i]+" " + elif( len(args)==4 and args[1]=="-f"): + cowfile=args[2] + text = args[3] else: - brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [cowfile] text",'groupchat') + brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [-l] [-f cowfile] text",'groupchat') return try: diff --git a/modules/active/tell.py b/modules/active/tell.py index 625ca66..0f5acc5 100644 --- a/modules/active/tell.py +++ b/modules/active/tell.py @@ -13,7 +13,7 @@ import config # HOLDBACK ... how long will brathuhn wait # to tell someone DELAY = 60*60*24 -HOLDBACK = 60*60*24*7 # TODO make brathuhn tell if it deletes messages +HOLDBACK = 60*60*24*7 # TODO brathuhn should tell if it deletes messages ############################################## container = PersistentContainer(__name__+'_data',config.PCACHE) @@ -24,7 +24,7 @@ def tell(msg, args): global container store = container["store"] - if( len(args) <= 2): + if( len(args) == 2): brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" nick text",'groupchat') return @@ -34,14 +34,12 @@ def tell(msg, args): brathuhn.sendMsg(brathuhn.ROOM,"User "+args[1]+" is already here, tell them yourself!",'groupchat') return - body = unicode(msg.getBody()) - spl = body.split(' ', 2) - message = spl[2] - nick = spl[1] - origin = msg.getFrom().getResource() + message = args[2] + nick = args[1] + origin = msg.getFrom().getResource() #TODO ugly store.append( (nick, message, origin, datetime.today()) ) container["store"] = store - brathuhn.sendMsg(brathuhn.ROOM,origin+": will do!",'groupchat') + brathuhn.sendMsg(brathuhn.ROOM,origin+': will send "'+message+'" to "'+nick+'"!','groupchat') def tellCB(nick, msg, status): global container diff --git a/sys/brathuhn.py b/sys/brathuhn.py index be1f06b..9850842 100644 --- a/sys/brathuhn.py +++ b/sys/brathuhn.py @@ -141,7 +141,7 @@ def __messageCB(conn,msg): for cmd in commands: if body.startswith("!"+cmd) and not room_nick in unicode(mfrom): - args = body.split() + args = util.split_args(body) commands[cmd][1](msg,args) for listener in group_listeners: listener( mfrom , msg ) @@ -152,7 +152,7 @@ def __messageCB(conn,msg): mfrom = msg.getFrom() for cmd in commands_private: if body.startswith("!"+cmd) and not room_nick in unicode(mfrom): - args = body.split() + args = util.split_args(body) commands_private[cmd][1](msg,args) for listener in private_listeners: listener( mfrom, msg ) diff --git a/sys/util.py b/sys/util.py index d254fe9..b2e711c 100644 --- a/sys/util.py +++ b/sys/util.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import select import socket import threading @@ -8,6 +9,48 @@ MAGIC_PACKET = "" for i in xrange(50): MAGIC_PACKET += chr(random.randrange(ord('a'),ord('z'))) + + +def split_args(txt): + SA_S0, SA_QUOT = range(2) + split_args_state = SA_S0 + # FSM: + # + # else + # ,--. + # | V " + # `-(S0)----->(QUOT) + # ^ | + # `---------ยด + # " + # + args = [] + current = u"" + for i in xrange(len(txt)): + c = txt[i] + if split_args_state==SA_S0: + if c=='"': + split_args_state = SA_QUOT + args.append(current) + current = u"" + elif c in " \t\n": + if len(current)!=0: + args.append(current) + current = u"" + else: + current += c + elif split_args_state==SA_QUOT: + if c=='"': + split_args_state = SA_S0 + else: + current += c + else: + assert False + if len(current)!=0: + args.append(current) + return args + + def get_local_ip(): def udp_listening_server(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)