argument processing changed

master
Stefan Krulj 14 years ago
parent 0b3b5686ba
commit 532b704847
  1. 9
      modules/active/cowsay.py
  2. 14
      modules/active/tell.py
  3. 4
      sys/brathuhn.py
  4. 43
      sys/util.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:

@ -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

@ -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 )

@ -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)

Loading…
Cancel
Save