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="" text=""
if( len(args)==2 ): if( len(args)==2 ):
text=args[1] text=args[1]
elif( len(args)>=3 ): elif( len(args)==4 and args[1]=="-f"):
cowfile=args[1] cowfile=args[2]
for i in range(2,len(args)): text = args[3]
text+=args[i]+" "
else: else:
brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [cowfile] text",'groupchat') brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [-l] [-f cowfile] text",'groupchat')
return return
try: try:

@ -13,7 +13,7 @@ import config
# HOLDBACK ... how long will brathuhn wait # HOLDBACK ... how long will brathuhn wait
# to tell someone # to tell someone
DELAY = 60*60*24 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) container = PersistentContainer(__name__+'_data',config.PCACHE)
@ -24,7 +24,7 @@ def tell(msg, args):
global container global container
store = container["store"] store = container["store"]
if( len(args) <= 2): if( len(args) == 2):
brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" nick text",'groupchat') brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" nick text",'groupchat')
return return
@ -34,14 +34,12 @@ def tell(msg, args):
brathuhn.sendMsg(brathuhn.ROOM,"User "+args[1]+" is already here, tell them yourself!",'groupchat') brathuhn.sendMsg(brathuhn.ROOM,"User "+args[1]+" is already here, tell them yourself!",'groupchat')
return return
body = unicode(msg.getBody()) message = args[2]
spl = body.split(' ', 2) nick = args[1]
message = spl[2] origin = msg.getFrom().getResource() #TODO ugly
nick = spl[1]
origin = msg.getFrom().getResource()
store.append( (nick, message, origin, datetime.today()) ) store.append( (nick, message, origin, datetime.today()) )
container["store"] = store 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): def tellCB(nick, msg, status):
global container global container

@ -141,7 +141,7 @@ def __messageCB(conn,msg):
for cmd in commands: for cmd in commands:
if body.startswith("!"+cmd) and not room_nick in unicode(mfrom): if body.startswith("!"+cmd) and not room_nick in unicode(mfrom):
args = body.split() args = util.split_args(body)
commands[cmd][1](msg,args) commands[cmd][1](msg,args)
for listener in group_listeners: for listener in group_listeners:
listener( mfrom , msg ) listener( mfrom , msg )
@ -152,7 +152,7 @@ def __messageCB(conn,msg):
mfrom = msg.getFrom() mfrom = msg.getFrom()
for cmd in commands_private: for cmd in commands_private:
if body.startswith("!"+cmd) and not room_nick in unicode(mfrom): 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) commands_private[cmd][1](msg,args)
for listener in private_listeners: for listener in private_listeners:
listener( mfrom, msg ) listener( mfrom, msg )

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import select import select
import socket import socket
import threading import threading
@ -8,6 +9,48 @@ MAGIC_PACKET = ""
for i in xrange(50): for i in xrange(50):
MAGIC_PACKET += chr(random.randrange(ord('a'),ord('z'))) 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 get_local_ip():
def udp_listening_server(): def udp_listening_server():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

Loading…
Cancel
Save