def presenceHandler(self, prs_obj): type = prs_obj.getType() who = prs_obj.getFrom().getStripped() if type == 'subscribe': msg_dia = Msg_dialog(None, "subscribe request from %s" % (who), MSG_DIA_TYPE_YESNO) while (msg_dia.done is None): self.process() if (msg_dia.done == MSG_DIA_RET_OK): self.send(jabber.Presence(to=who, type='subscribed')) if who not in self.getRoster().getJIDs() or \ self.getRoster().getSub(who) != 'both': self.send(jabber.Presence(to=who, type='subscribe')) msg_dia.close() elif type == 'unsubscribe' and not self._unsub_lock: self._unsub_lock = 1 ## HACK ! msg_dia = Msg_dialog(None, "unsubscribe request from %s" % (who), MSG_DIA_TYPE_YESNO) while (msg_dia.done is None): self.process() if (msg_dia.done == MSG_DIA_RET_OK): self.send(jabber.Presence(to=who, type='unsubscribed')) msg_dia.close() else: pass self.dispatch_to_gui(prs_obj) self._unsub_lock = 0
def presenceCB(con, prs): """Called when a presence is recieved""" who = str(prs.getFrom()) type = prs.getType() if type == None: type = 'available' # subscription request: # - accept their subscription # - send request for subscription to their presence if type == 'subscribe': print colorize(u"subscribe request from %s" % (who), 'blue') con.send(jabber.Presence(to=who, type='subscribed')) con.send(jabber.Presence(to=who, type='subscribe')) # unsubscription request: # - accept their unsubscription # - send request for unsubscription to their presence elif type == 'unsubscribe': print colorize(u"unsubscribe request from %s" % (who), 'blue') con.send(jabber.Presence(to=who, type='unsubscribed')) con.send(jabber.Presence(to=who, type='unsubscribe')) elif type == 'subscribed': print colorize(u"we are now subscribed to %s" % (who), 'blue') elif type == 'unsubscribed': print colorize(u"we are now unsubscribed to %s" % (who), 'blue') elif type == 'available': print colorize(u"%s is available (%s / %s)" % \ (who, prs.getShow(), prs.getStatus()),'blue') elif type == 'unavailable': print colorize(u"%s is unavailable (%s / %s)" % \ (who, prs.getShow(), prs.getStatus()),'blue')
def jbcPresence(self, c, prs): who = str(prs.getFrom()) type = prs.getType() if type == 'subscribe': c.send(jabber.Presence(to=who, type='subscribed')) c.send(jabber.Presence(to=who, type='subscribe')) elif type == 'unsubscribe': c.send(jabber.Presence(to=who, type='unsubscribed')) c.send(jabber.Presence(to=who, type='unsubscribe'))
def presenceCB(con, prs): """Called when a presence is recieved""" who = prs.getFrom() type = prs.getType() if type == None: type = 'available' # subscription request: # - accept their subscription # - send request for subscription to their presence if type == 'subscribe': pg_log(colorize("subscribe request from %s" % (who), 'blue')) con.send(jabber.Presence(to=str(who), type='subscribed')) con.send(jabber.Presence(to=str(who), type='subscribe')) # unsubscription request: # - accept their unsubscription # - send request for unsubscription to their presence elif type == 'unsubscribe': pg_log(colorize("unsubscribe request from %s" % (who), 'blue')) con.send(jabber.Presence(to=str(who), type='unsubscribed')) con.send(jabber.Presence(to=str(who), type='unsubscribe')) elif type == 'subscribed': pg_log(colorize("we are now subscribed to %s" % (who), 'blue')) elif type == 'unsubscribed': pg_log(colorize("we are now unsubscribed to %s" % (who), 'blue')) elif type == 'available': sta = '%s / %s' % (prs.getShow(), prs.getStatus()) pg_log(colorize("%s is available (%s)" % (who, sta), 'blue')) buddy = getBuddy(who) buddy.listed = True buddy.item.text = '%s (%s)' % (who, sta) buddy.log({'type': 'available', 'from': who, 'body': sta}) app.server.update() elif type == 'unavailable': sta = '%s / %s' % (prs.getShow(), prs.getStatus()) pg_log(colorize("%s is unavailable (%s)" % (who, sta), 'blue')) buddy = getBuddy(who) app.server.attachwidget(0, buddy.item, 0) buddy.listed = False buddy.log({'type': 'unavailable', 'from': who, 'body': sta}) app.server.update()
def removeCB(self, *args): who = self.getTab(0).get_roster_selection() if not who: return msg_dia = Msg_dialog(None, "unsubscribe %s ?" % (who), MSG_DIA_TYPE_YESNO) while (msg_dia.done is None): self.jabberObj.process() if (msg_dia.done == MSG_DIA_RET_OK): self.jabberObj.send(jabber.Presence(to=who, type='unsubscribe')) msg_dia.close()
def custstatusCB(self, *args): dia = Status_dialog() while dia.done is None: self.jabberObj.process() type, show = dia.done pres = jabber.Presence(type=type) pres.setShow(show) self.jabberObj.presence_details = [type, show] self.jabberObj.send(pres) dia.close()
def statusCB(self, action, widget): if self.checkItemCalled == 1: ## Make sure set_active does not return ## recall the callback self.checkItemCalled = 1 ## More nasty workarounds for ItemFactory Radiobutton problems for path in ['/Tools/Status/Available', '/Tools/Status/Unavailable']: if widget != self.itemf.get_widget(path): self.itemf.get_widget(path).set_active(gtk.FALSE) self.checkItemCalled = 0 if action == 1: # available self.jabberObj.presence_details = ['available', None] pres = jabber.Presence() else: self.jabberObj.presence_details = ['unavailable', None] pres = jabber.Presence(type='unavailable') self.jabberObj.send(pres)
def process(self, time=0.1): while gtk.events_pending(): gtk.mainiteration() jabber.Client.process(self, time) if self.sleeper: state_pres = None self.sleeper.poll() state = self.sleeper.getState() if state != self.sleeper_state: if state == sleepy.STATE_WOKEN: state_pres = jabber.Presence(type='available') state_pres.setStatus('online') state_pres.setShow('') if state == sleepy.STATE_SLEEPING: state_pres = jabber.Presence(type='available') state_pres.setStatus('away') state_pres.setShow('Away from computer') if state_pres: self.send(state_pres) self.sleeper_state = state
def sendPresence(self, status="Available", to=None): #presence = jabber.Presence(type='available') presence = jabber.Presence(to) presence.setStatus(status) if status == "Available": # apparently "normal" is the default #presence.setShow("normal") pass elif status == "Do Not Disturb": presence.setShow("dnd") else: presence.setShow("away") self.con.send(presence)
def acmd_invite(who, msg): '"/invite nick" Invite someone to join this room' msg = msg.strip() jid = getjid(msg) if isadmin(who.getStripped()): if msg: con.send(jabber.Presence(to=jid, type='subscribe')) adduser(jid) systoone(who, _('Invited <%s>').para(jid)) else: raise MSG_COMMAND else: raise ADMIN_COMMAND
def send_presence(self,show='',status='',type='',to=''): p = jabber.Presence() if show: p.setShow(show) if status: p.setStatus(status) if type: p.setType(type) totxt='' lock(self.mutex) if to: p.setTo(self.alias_to_uid(to)) totext=' to %s' % p.getTo() p.setFrom( self.sender_adress() ) self.jabber.send(p) log( "%s sent presence%s." % (self.sender_adress(), totxt) ) unlock(self.mutex)
def presenceCB(self, con, prs): """Called when a presence is recieved""" who = str(prs.getFrom()) type = prs.getType() if type is None: type = 'available' txt = None # subscription request: # - accept their subscription # - send request for subscription to their presence if type == 'subscribe': print "subscribe request from %s" % (who) con.send(jabber.Presence(to=who, type='subscribed')) con.send(jabber.Presence(to=who, type='subscribe')) txt = "subscribe request from %s" % (who) # unsubscription request: # - accept their unsubscription # - send request for unsubscription to their presence elif type == 'unsubscribe': print "unsubscribe request from %s" % (who) con.send(jabber.Presence(to=who, type='unsubscribed')) con.send(jabber.Presence(to=who, type='unsubscribe')) txt = "unsubscribe request from %s" % (who) elif type == 'subscribed': print "we are now subscribed to %s" % (who) txt = "we are now subscribed to %s" % (who) elif type == 'unsubscribed': print "we are now unsubscribed to %s" % (who) txt = "we are now unsubscribed to %s" % (who) elif type == 'available': print "%s is available (%s / %s)" % (who, prs.getShow(), prs.getStatus()) txt = "%s is available (%s / %s)" % (who, prs.getShow(), prs.getStatus()) # who is of the form 'username@domain/resource' jid = str(who.split('/')[0]) # KEA 2002-11-30 # should presences for the same jid such as [email protected]/Exodus # and [email protected]/default be treated separately? # for now, skip duplicate jids for both # available and unavailable # # I'm not showing gateways either # and still need to figure out the proper handling # of them if '@' in jid and jid != self.shortJID: if jid in self.groupchatJIDS: nickname = str(who.split('/')[1]) print "*******GROUPCHAT", jid, who, prs.getStatus( ), prs.getShow() self.groupchatJIDS[jid][nickname] = (prs.getStatus(), prs.getShow()) self._parent.rosterQueue.put( (jid, self.groupchatJIDS[jid])) wx.WakeUpIdle() else: self.roster[jid] = [ 'online', prs.getStatus(), prs.getShow() ] self.updateRosterDisplay() elif type == 'unavailable': print "%s is unavailable (%s / %s)" % (who, prs.getShow(), prs.getStatus()) txt = "%s is unavailable (%s / %s)" % (who, prs.getShow(), prs.getStatus()) # who is of the form 'username@domain/resource' jid = str(who.split('/')[0]) if '@' in jid and jid != self.shortJID: if jid in self.groupchatJIDS: nickname = str(who.split('/')[1]) print "*******GROUPCHAT", jid, who, prs.getStatus( ), prs.getShow() try: del self.groupchatJIDS[jid][nickname] self._parent.rosterQueue.put( (jid, self.groupchatJIDS[jid])) wx.WakeUpIdle() except: pass else: self.roster[jid] = [ 'offline', prs.getStatus(), prs.getShow() ] self.updateRosterDisplay() if txt is not None: # need to decide whether we want a separate queue for # status messages, for now just let the messages be # printed to the console #self._parent.msgQueue.put(txt) #wx.WakeUpIdle() pass
def iqCB(con, iq): print "Iq:", str(iq) resultIq = jabber.Iq(to=iq.getFrom()) resultIq.setID(iq.getID()) resultIq.setFrom(iq.getTo()) query_result = resultIq.setQuery(iq.getQuery()) type = iq.getType() query_ns = iq.getQuery() # switch on type: get, set, result error # switch on namespaces if query_ns == jabber.NS_REGISTER: if type == 'get': resultIq.setType('result') # generate a key to be passed to the user; it will be checked # later. yes, we're storing a sha object iq_from = str(iq.getFrom()) keys[iq_from] = sha.new(iq_from) # tell the client the fields we want fields = { 'username': None, 'password': None, 'instructions': 'Enter your username, password, IMAP hostname, directory, and :-separated list of folders to check', 'key': keys[iq_from].hexdigest(), 'hostname': None, 'directory': None, 'folders': 'INBOX' } for field in fields.keys(): field_node = query_result.insertTag(field) if fields[field]: field_node.putData(fields[field]) con.send(resultIq) elif type == 'set': # before anything else, verify the key client_key_node = iq.getQueryNode().getTag('key') if not client_key_node: resultIq.setType('error') resultIq.setError('no key given!') con.send(resultIq) else: # verify key if keys[str(iq.getFrom())].hexdigest( ) == client_key_node.getData(): # key is good if iq.getQueryNode().getTag('remove'): # user is trying to unregister # TODO. :-) del clients[iq.getFrom().getStripped()] else: # someone is trying to register jid = iq.getFrom().getStripped() username = iq.getQueryNode().getTag('username') if username: username = str(username.getData()) password = iq.getQueryNode().getTag('password') if password: password = str(password.getData()) hostname = iq.getQueryNode().getTag('hostname') if hostname: hostname = str(hostname.getData()) directory = iq.getQueryNode().getTag('directory') if directory: directory = str(directory.getData()) folders = iq.getQueryNode().getTag('folders') if folders: folders = str(folders.getData()) client = MCClient(jid, username, password, hostname, directory, folders) clients[client.jid] = client # subscribe to the client's presence sub_req = jabber.Presence(iq.getFrom(), type='subscribe') sub_req.setFrom(str(iq.getTo()) + "/registered") con.send(sub_req) resultIq.setType('result') con.send(resultIq) else: resultIq.setType('error') resultIq.setError('invalid key', 400) con.send(resultIq) # done with key; delete it del keys[str(iq.getFrom())] else: print "don't know how to handle type", type, "for query", query_ns elif (query_ns == jabber.NS_AGENT) and (type == 'get'): # someone wants information about us resultIq.setType('result') responses = { 'name': "Mailchecker", # 'url': None, 'description': "This is the mailchecker component", 'transport': "don't know what should go here...", 'register': None, # we can be registered with 'service': 'test' # nothing really standardized here... } for response in responses.keys(): resp_node = query_result.insertTag(response) if responses[response]: resp_node.putData(responses[response]) con.send(resultIq) else: print "don't know how to handle type", type, "for query", query_ns
def presenceCB(con, pres): print "Presence:", str(pres) # presence reply to use later on p = jabber.Presence(to=pres.getFrom()) p.setFrom(pres.getTo()) type = pres.getType() # find the client object if str(pres.getFrom().getStripped()) in clients.keys(): client = clients[pres.getFrom().getStripped()] else: print("not able to find client for " + pres.getFrom().getStripped()) client = None if type != 'unsubscribed': type = 'unsubscribe' if not type: type = 'available' print(pres.getFrom().getStripped() + " is " + type) if type == 'unavailable': # user went offline client.setOnline(0) p.setType('unavailable') con.send(p) elif type == 'subscribe': # user wants to subscribe to our presence; oblige, and ask for his p.setType('subscribed') con.send(p) p.setType('subscribe') con.send(p) elif type == 'unsubscribe': p.setType('unsubscribed') con.send(p) p.setType('unsubscribe') con.send(p) elif type == 'unsubscribed': # now unsubscribe from the user's presence pass elif type == 'probe': # send our presence p.setType('available') con.send(p) elif type == 'available': # user is online client.setStatus(pres.getStatus()) client.setShow(pres.getShow()) p.setType('available') con.send(p)
else: print "Connected" con.process(1) if con.auth('secret'): print "connected" else: print "problems with handshake: ", con.lastErr, con.lastErrCode sys.exit(1) # con.registerHandler('message',messageCB) con.registerHandler('presence', presenceCB) con.registerHandler('iq', iqCB) p = jabber.Presence(type='available') p.setFrom('mailcheck/registered') for c in clients.keys(): p.setTo(clients[c].jid) con.send(p) try: while 1: con.process(10) # whoo baby, is this a kludge. Should really have a bona-fide event # loop or thread that processes the user's email checking, or at least # build up a timer type element that only runs this once every five # minutes or so... for c in clients.keys(): clients[c].doit()
def presenceCB(con, prs): if conf.general.debug > 3: print '>>>', time.strftime('%Y-%m-%d %H:%M:%S'), '[PRESENCE]', prs userinfo = conf['userinfo'] who = unicode(prs.getFrom()) whoid = getjid(who) type = prs.getType() # TODO: Try only acking their subscription when they ack ours. if type == 'subscribe': print ">>> Subscribe from", whoid, if isbanned(prs.getFrom().getStripped()): print "Banned" systoone(who, _('You are banned')) boot(prs.getFrom().getStripped()) elif conf.general['private'] and not isuser( prs.getFrom().getStripped()): print "Uninvited" systoone(who, _('This is a private conference bot')) boot(prs.getFrom().getStripped()) else: print "Accepted" con.send(jabber.Presence(to=who, type='subscribed')) con.send(jabber.Presence(to=who, type='subscribe')) systoall( _('<%s> joins this room.').para(getdisplayname(who)), [who]) userjid[whoid] = who elif type == 'unsubscribe': if userjid.has_key(whoid): del userjid[whoid] boot(prs.getFrom().getStripped()) print ">>> Unsubscribe from", who elif type == 'subscribed': if i18n.isobj(welcome): wel = welcome.getvalue() else: wel = welcome systoone(who, wel % {'version': version}) systoone( who, unicode( _('''Topic: %(topic)s %(lastlog)s''').para({ "topic": unicode(conf.general['topic']), "lastlog": unicode("\n".join(lastlog)), }) + '\n---------------------------')) sendstatus(who, _('here'), _('joining')) userjid[whoid] = who elif type == 'unsubscribed': if userjid.has_key(whoid): del userjid[whoid] boot(prs.getFrom().getStripped()) systoall(_('<%s> has left').para(getdisplayname(who))) elif type == 'available' or type == None: show = prs.getShow() if show in [None, 'chat', 'available', 'online']: sendstatus(who, _('here'), prs.getStatus()) elif show in ['xa']: sendstatus(who, _('away'), prs.getStatus()) elif show in ['away']: sendstatus(who, _('away'), prs.getStatus()) elif show in ['dnd']: sendstatus(who, _('away'), prs.getStatus()) else: sendstatus(who, _('away'), show + " [[%s]]" % prs.getStatus()) userjid[whoid] = who elif type == 'unavailable': status = prs.getShow() sendstatus(who, _('away'), status) else: if conf.general.debug > 3: print ">>> Unknown presence:", who, type
def sendpresence(msg): p = jabber.Presence() p.setStatus(msg) con.send(p)
def boot(jid): "Remove a user from the chatroom" con.send(jabber.Presence(to=jid, type='unsubscribe')) con.send(jabber.Presence(to=jid, type='unsubscribed')) if statuses.has_key(getdisplayname(jid)): del statuses[getdisplayname(jid)]
connect() # We announce ourselves to a url, this url then keeps track of all # the conference bots that are running, and provides a directory # for people to browse. if time.time() - last_update > 4 * 60 * 60 and not general[ 'private']: # every 4 hours if not running: t = threading.Thread(target=register_site) t.setDaemon(True) t.start() # Send some kind of dummy message every few minutes to make # sure that the connection is still up, and to tell google talk # we're still here. if time.time() - last_ping > 120: # every 2 minutes # Say we're online. p = jabber.Presence() p.setFrom(JID) con.send(p) sendpresence(conf.general['status']) last_ping = time.time() if time.time() - last_testing > 120: # every 40 seconds #test quality if ontesting: #mean that callback message doesn't be processed, so reconnect again print '>>>', time.strftime( '%Y-%m-%d %H:%M:%S' ), 'RECONNECT... network delay it too long: %d\'s' % ( time.time() - last_testing) raise RECONNECT_COMMAND else: ontesting = True
def setJabberState(self, state): p = jabber.Presence() p.setStatus(state) self.jc.send(p)
def doCmd(con, txt): global Who if txt[0] == '/': cmd = split(txt) if cmd[0] == '/select': Who = cmd[1] print "%s selected" % cmd[1] elif cmd[0] == '/presence': to = cmd[1] type = cmd[2] con.send(jabber.Presence(to, type)) elif cmd[0] == '/status': p = jabber.Presence() MyStatus = ' '.join(cmd[1:]) p.setStatus(MyStatus) con.send(p) elif cmd[0] == '/show': p = jabber.Presence() MyShow = ' '.join(cmd[1:]) p.setShow(MyShow) con.send(p) elif cmd[0] == '/subscribe': to = cmd[1] con.send(jabber.Presence(to, 'subscribe')) elif cmd[0] == '/unsubscribe': to = cmd[1] con.send(jabber.Presence(to, 'unsubscribe')) elif cmd[0] == '/roster': con.requestRoster() _roster = con.getRoster() for jid in _roster.getJIDs(): print colorize( u"%s :: %s (%s/%s)" % ( jid, _roster.getOnline(jid), _roster.getStatus(jid), _roster.getShow(jid), ), 'blue') elif cmd[0] == '/agents': print con.requestAgents() elif cmd[0] == '/register': agent = '' if len(cmd) > 1: agent = cmd[1] con.requestRegInfo(agent) print con.getRegInfo() elif cmd[0] == '/exit': con.disconnect() print colorize("Bye!", 'red') sys.exit(0) elif cmd[0] == '/help': print "commands are:" print " /select <jabberid>" print " - selects who to send messages to" print " /subscribe <jid>" print " - subscribe to jid's presence" print " /unsubscribe <jid>" print " - unsubscribe to jid's presence" print " /presence <jabberid> <type>" print " - sends a presence of <type> type to the jabber id" print " /status <status>" print " - set your presence status message" print " /show <status>" print " - set your presence show message" print " /roster" print " - requests roster from the server and " print " display a basic dump of it." print " /exit" print " - exit cleanly" else: print colorize("uh?", 'red') else: if Who != '': msg = jabber.Message(Who, strip(txt)) msg.setType('chat') print "<%s> %s" % (JID, msg.getBody()) con.send(msg) else: print colorize('Nobody selected', 'red')
def doCmd(con, txt): global Who cmd = split(txt) if cmd[0] == 'presence': to = cmd[1] type = cmd[2] con.send(jabber.Presence(to, type)) elif cmd[0] == 'status': p = jabber.Presence() MyStatus = ' '.join(cmd[1:]) p.setStatus(MyStatus) con.send(p) elif cmd[0] == 'show': p = jabber.Presence() MyShow = ' '.join(cmd[1:]) p.setShow(MyShow) con.send(p) elif cmd[0] == 'subscribe': to = cmd[1] con.send(jabber.Presence(to, 'subscribe')) elif cmd[0] == 'unsubscribe': to = cmd[1] con.send(jabber.Presence(to, 'unsubscribe')) elif cmd[0] == 'roster': con.requestRoster() _roster = con.getRoster() for jid in _roster.getJIDs(): pg_log( colorize( "%s :: %s (%s/%s)" % ( jid, _roster.getOnline(jid), _roster.getStatus(jid), _roster.getShow(jid), ), 'blue')) elif cmd[0] == 'agents': pg_log( ` con.requestAgents() `) elif cmd[0] == 'register': agent = '' if len(cmd) > 1: agent = cmd[1] con.requestRegInfo(agent) pg_log( ` con.getRegInfo() `) elif cmd[0] == 'exit': con.disconnect() pg_log(colorize("Bye!", 'red')) sys.exit(0) elif cmd[0] == 'help': pg_log("commands are:") pg_log(" subscribe <jid>") pg_log(" - subscribe to jid's presence") pg_log(" unsubscribe <jid>") pg_log(" - unsubscribe to jid's presence") pg_log(" agents") pg_log(" - list agents") pg_log(" register <agent>") pg_log(" - register with an agent") pg_log(" presence <jabberid> <type>") pg_log(" - sends a presence of <type> type to the jabber id") pg_log(" status <status>") pg_log(" - set your presence status message") pg_log(" show <status>") pg_log(" - set your presence show message") pg_log(" roster") pg_log(" - requests roster from the server and ") pg_log(" display a basic dump of it.") pg_log(" exit") pg_log(" - exit cleanly") else: pg_log(colorize("uh?", 'red'))
def addCB(self, *args): add_dia = Add_dialog(self, self.jabberObj) while (add_dia.done is None): self.jabberObj.process() self.jabberObj.send(jabber.Presence(add_dia.done, 'subscribe')) add_dia.close()