#!/usr/bin/python # -*- coding: utf-8 -*- import brathuhn import time from datetime import datetime from persistent import PersistentContainer import config ############################################## # change values to your need # DELAY ... time between checks for outdated # HOLDBACK ... how long will brathuhn wait # to tell someone DELAY = 60*60*24 HOLDBACK = 60*60*24*7 # TODO brathuhn should tell if it deletes messages ############################################## container = PersistentContainer(__name__+'_data',config.PCACHE) if not "store" in container: container["store"] = [] def tell(msg, args): global container store = container["store"] if( len(args) == 2): brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" nick text",'groupchat') return userJID = brathuhn.getUsers().items() for a in userJID: if( args[1] == a[0].getResource()): brathuhn.sendMsg(brathuhn.ROOM,"User "+args[1]+" is already here, tell them yourself!",'groupchat') return 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 send "'+message+'" to "'+nick+'"!','groupchat') def tellCB(nick, msg, status): global container store = container["store"] if(status != brathuhn.STATUS_JOIN): return user = nick.getResource() rem = [] for l in store: if(l[0] == unicode(user)): timestamp = "{0:02}:{1:02}, {2}-{3:02}-{4:02}".format(l[3].hour, l[3].minute, l[3].year, l[3].month, l[3].day) brathuhn.sendMsg(brathuhn.ROOM,(l[0]+": User "+l[2]+" told me at "+timestamp+" to tell you: \""+l[1]+"\""),'groupchat') rem.append(l) for r in rem: store.remove(r) container["store"] = store def cleartell(): global container store = container["store"] now = datetime.today() rem = [] for l in store: if(now - l[3] < HOLDBACK): rem.append(l) for r in rem: store.remove(r) container["store"] = store brathuhn.callAt(cleartell, time.time()+DELAY, []) ############################################## brathuhn.addCommand("tell","Usage: !tell nick text", tell) brathuhn.addStatusListener(tellCB) brathuhn.callAt(cleartell, time.time()+DELAY, []) ############################################## #def flushtell(msg, args): # global container # store = container["store"] # store = [] # container["store"] = store # #brathuhn.addCommand("flush","Usage: !flush ", flushtell) #def showtell(msg, args): # global container # store = container["store"] # # for l in store: # timestamp = "{:02}:{:02}, {}-{:02}-{:02}".format(l[3].hour, l[3].minute, l[3].year, l[3].month, l[3].day) # brathuhn.sendMsg(brathuhn.ROOM,(l[2]+" -> "+ l[0]+"; "+l[1]+"; "+timestamp),'groupchat') # #brathuhn.addCommand("show","Usage: !show ", showtell)