API change for group and private callbacks

No longer responding to own name
Added a timeout to ignore messages that arrived in the first n-seconds
after a join
Added last_active command
master
Stefan Krulj 14 years ago
parent 88be4a1053
commit 988a2acbee
  1. 39
      brathuhn.py
  2. 3
      kraken.py
  3. 4
      logger.py
  4. 1
      main.py
  5. 3
      tantrum.py

@ -4,8 +4,8 @@ import threading
from config import *
VERSION = "0.2"
MIN_CONNECT_INTERVAL=300
MIN_CONNECT_INTERVAL = 300
IGNORE_FIRST = 3
last_connect = 0
commands = dict()
@ -15,6 +15,7 @@ private_listeners = []
status_listeners = []
call_ats = dict()
call_ats_id = 0
join_time = time.time()+60*60*24*365 #1 year in the future
users = dict()
@ -126,32 +127,29 @@ def __help(msg,args):
def __messageCB(conn,msg):
if msg.getType() == "groupchat":
#print unicode(msg.getFrom()) +": "+ unicode(msg.getBody()) +" " +unicode(msg.getChildren())
body = unicode(msg.getBody())
mfrom = unicode(msg.getFrom())
if NAME in body and not room_nick in mfrom:
sendMsg(ROOM,NAME+" --(brathuhn bot framework (v"+VERSION+"), type !help)",'groupchat')
mfrom = msg.getFrom()
for cmd in commands:
if body.startswith("!"+cmd) and not room_nick in mfrom:
if body.startswith("!"+cmd) and not room_nick in unicode(mfrom):
args = body.split()
commands[cmd][1](msg,args)
for listener in group_listeners:
listener( unicode(msg.getFrom()), unicode(msg.getBody()))
listener( mfrom , msg )
if msg.getType() == "chat":
if msg.getBody() != None:
body = unicode(msg.getBody())
mfrom = unicode(msg.getFrom())
mfrom = msg.getFrom()
for cmd in commands_private:
if body.startswith("!"+cmd) and not room_nick in mfrom:
if body.startswith("!"+cmd) and not room_nick in unicode(mfrom):
args = body.split()
commands_private[cmd][1](msg,args)
for listener in private_listeners:
listener( mfrom, body)
listener( mfrom, msg )
raise xmpp.NodeProcessed
def __presenceCB(conn,msg):
#print "Status",msg.getFrom(), msg.getStatus(), msg.getStatusCode(), msg.getType()
if msg.getType()=='unavailable':
nick=msg.getNick()
if nick==None:
@ -159,7 +157,7 @@ def __presenceCB(conn,msg):
#Disconnect
if not msg.getFrom() in users:
for listener in status_listeners:
listener(msg.getFrom(),msg,STATUS_OFFLINE)
listener(msg.getFrom(),msg,STATUS_OFFLINE)
else:
del users[msg.getFrom()]
#Rename
@ -195,6 +193,15 @@ def __presenceCB(conn,msg):
for listener in status_listeners:
listener(msg.getFrom(),msg.getStatus(),status)
def __ignore_first(conn,msg):
global join_time
dt = time.time()-join_time
if dt>IGNORE_FIRST:
cl.RegisterHandler('message',__messageCB)
cl.UnregisterHandler('message',__ignore_first)
__messageCB(conn,msg)
jid=xmpp.protocol.JID(node=NAME,domain=SRV,resource='bot')
cl=xmpp.Client(jid.getDomain(),debug=[])
@ -203,6 +210,8 @@ def connect_and_join():
global room_nick
global room_to
global last_connect
global cl
global join_time
last_connect=time.time()
#connect to server
con=cl.connect()
@ -211,7 +220,7 @@ def connect_and_join():
raise Exception('could not connect!')
print 'connected'
cl.RegisterHandler('message',__messageCB)
cl.RegisterHandler('message',__ignore_first)
cl.RegisterHandler('presence',__presenceCB)
#authenticate
@ -233,6 +242,7 @@ def connect_and_join():
#join the room
cl.send(msg)
join_time = time.time()
#set status
cl.send(xmpp.Presence(to=room_to,status=NAME+" --(brathuhn bot framework (v"+VERSION+"), type !help)"))
@ -261,7 +271,6 @@ def loop():
except:
print "-------ERROR OCCURED-------"
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_tb(exc_traceback, file=sys.stdout)
traceback.print_exc(file=sys.stdout)
if not cl.connected:
s=MIN_CONNECT_INTERVAL-(time.time()-last_connect)

@ -13,7 +13,8 @@ kraken_img = """ _,.---. ,''-._
def kraken(mfrom,msg):
if "kraken" in msg.lower() and not brathuhn.room_nick in mfrom:
body = unicode(msg.getBody())
if "kraken" in body.lower() and not brathuhn.room_nick in unicode(mfrom):
for line in kraken_img.split('\n'):
brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat')
brathuhn.addGroupListener(kraken)

@ -41,8 +41,9 @@ def start_log(msg,args):
user_logs[msg.getFrom()]=open(tempfile.mkstemp(brathuhn.NAME)[1],"w+")
def message_logger(mfrom,msg):
body = unicode(msg.getBody())
for key in user_logs:
user_logs[key].write(unicode(mfrom)+": "+msg+"\n")
user_logs[key].write(unicode(mfrom)+": "+body+"\n")
def status_logger(mfrom,msg,status):
for key in user_logs:
@ -50,7 +51,6 @@ def status_logger(mfrom,msg,status):
user_logs[key].write(unicode(mfrom)+" joined\n")
if status==brathuhn.STATUS_ONLINE:
user_logs[key].write(unicode(mfrom)+" is now online\n")
pass
if status==brathuhn.STATUS_OFFLINE:
user_logs[key].write(unicode(mfrom)+" left\n")
if status==brathuhn.STATUS_RENAME:

@ -10,6 +10,7 @@ import cowsay
import choose
import tantrum
import tell
import last_active
brathuhn.connect_and_join()
brathuhn.loop()

@ -13,7 +13,8 @@ tantrum_img = """ ###
,' TANTRUM `."""
def tantrum(mfrom,msg):
if "tantrum" in msg.lower() and not brathuhn.room_nick in mfrom:
body = unicode(msg.getBody())
if "tantrum" in body.lower() and not brathuhn.room_nick in unicode(mfrom):
for line in tantrum_img.split('\n'):
brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat')
brathuhn.addGroupListener(tantrum)

Loading…
Cancel
Save