Merge branch 'master' of 89.22.98.13:2012/brathuhn

master
fbr 14 years ago
commit 007774cc5e
  1. 35
      brathuhn.py
  2. 3
      kraken.py
  3. 33
      last_active.py
  4. 4
      logger.py
  5. 1
      main.py
  6. 3
      tantrum.py

@ -4,8 +4,8 @@ import threading
from config import *
VERSION = "0.2"
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:
@ -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)

@ -0,0 +1,33 @@
import brathuhn, datetime, time
last_active = dict()
def active(mfrom,msg):
global last_active
body = unicode(msg.getBody())
res = mfrom.getResource()
date=datetime.datetime.fromtimestamp(time.time())
last_active[res]=(body,date.strftime("%a, %d-%b-%Y %H:%M:%S"))
brathuhn.addGroupListener(active)
def last_active_cmd(msg,args):
if( len(args)==2 ):
res = args[1]
if res in last_active:
if res[-1]!='s':
mymsg=res+"'s"
else:
mymsg=res+"'"
la = last_active[res]
mymsg+= " last message on "+la[1]+' : "'+la[0]+'"'
brathuhn.sendMsg(brathuhn.ROOM,mymsg,'groupchat')
else:
brathuhn.sendMsg(brathuhn.ROOM,"no message in record",'groupchat')
else:
brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" nick",'groupchat')
brathuhn.addCommand("last_active","Usage: !last_active nick",last_active_cmd)

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