def dispatch_error_message(self, msg, msgtxt, session, frm, tim): error_msg = msg.getErrorMsg() if not error_msg: error_msg = msgtxt msgtxt = None subject = msg.getSubject() if session.is_loggable(): app.logger.insert_into_logs(self.name, nbxmpp.JID(frm).getStripped(), tim, KindConstant.ERROR, message=error_msg, subject=subject) app.nec.push_incoming_event( MessageErrorEvent(None, conn=self, fjid=frm, error_code=msg.getErrorCode(), error_msg=error_msg, msg=msgtxt, time_=tim, session=session, stanza=msg))
def request_command_list(self): """ Request the command list. Change stage on delivery """ query = nbxmpp.Iq(typ='get', to=nbxmpp.JID(self.jid), queryNS=nbxmpp.NS_DISCO_ITEMS) query.setQuerynode(nbxmpp.NS_COMMANDS) def callback(response): '''Called on response to query.''' # FIXME: move to connection_handlers.py # is error => error stage error = response.getError() if error: # extracting error description self.stage5(errorid=error) return # no commands => no commands stage # commands => command selection stage query = response.getTag('query') if query and query.getAttr('node') == nbxmpp.NS_COMMANDS: items = query.getTags('item') else: items = [] if len(items)==0: self.commandlist = [] self.stage4() else: self.commandlist = [(t.getAttr('node'), t.getAttr('name')) \ for t in items] self.stage2() self.account.connection.SendAndCallForResponse(query, callback)
def OpenStream(self, sid, to, fp, blocksize=4096): """ Start new stream. You should provide stream id 'sid', the endpoind jid 'to', the file object containing info for send 'fp'. Also the desired blocksize can be specified. Take into account that recommended stanza size is 4k and IBB uses base64 encoding that increases size of data by 1/3. """ if not nbxmpp.JID(to).getResource(): return file_props = FilesProp.getFilePropBySid(sid) file_props.direction = '|>' + to file_props.block_size = blocksize file_props.fp = fp file_props.seq = 0 file_props.error = 0 file_props.paused = False file_props.received_len = 0 file_props.last_time = time.time() file_props.connected = True file_props.completed = False file_props.disconnect_cb = None file_props.continue_cb = None syn = nbxmpp.Protocol('iq', to, 'set', payload=[nbxmpp.Node( nbxmpp.NS_IBB + ' open', {'sid': file_props.transport_sid, 'block-size': blocksize, 'stanza': 'iq'})]) self.connection.send(syn) file_props.syn_id = syn.getID() return file_props
def get_recent_groupchats(account): recent_groupchats = config.get_per( 'accounts', account, 'recent_groupchats').split() recent_list = [] for groupchat in recent_groupchats: jid = nbxmpp.JID(groupchat) recent = RecentGroupchat( jid.getNode(), jid.getDomain(), jid.getResource()) recent_list.append(recent) return recent_list
def __make_jingle(self, action, reason=None): stanza = nbxmpp.Iq(typ='set', to=nbxmpp.JID(self.peerjid), frm=self.ourjid) attrs = {'action': action, 'sid': self.sid, 'initiator' : self.initiator} jingle = stanza.addChild('jingle', attrs=attrs, namespace=nbxmpp.NS_JINGLE) if reason is not None: jingle.addChild(node=reason) return stanza, jingle
def get_item_pref(self, jid): jid = nbxmpp.JID(jid) if str(jid) in self.items: return self.items[jid] if jid.getStripped() in self.items: return self.items[jid.getStripped()] if jid.getDomain() in self.items: return self.items[jid.getDomain()] return self.default
def get_recent_groupchats(account): recent_groupchats = config.get_per('accounts', account, 'recent_groupchats').split() RecentGroupchat = namedtuple('RecentGroupchat', ['room', 'server', 'nickname']) recent_list = [] for groupchat in recent_groupchats: jid = nbxmpp.JID(groupchat) recent = RecentGroupchat(jid.getNode(), jid.getDomain(), jid.getResource()) recent_list.append(recent) return recent_list
def _get_from(self, room_jid, stanza): try: from_ = nbxmpp.JID(helpers.parse_jid(stanza.getAttr('from'))) except helpers.InvalidFormat: log.warning('Invalid JID on invite: %s, ignoring it', stanza.getAttr('from')) raise nbxmpp.NodeProcessed known_contact = app.contacts.get_contacts(self._account, room_jid) ignore = app.config.get_per('accounts', self._account, 'ignore_unknown_contacts') if ignore and not known_contact: log.info('Ignore invite from unknown contact %s', from_) raise nbxmpp.NodeProcessed return from_
def _parse_gc_attrs(self, message): with_ = message.getFrom() nick = message.getFrom().getResource() # Get the real jid if we have it real_jid = None muc_user = message.getTag('x', namespace=nbxmpp.NS_MUC_USER) if muc_user is not None: real_jid = muc_user.getTagAttr('item', 'jid') if real_jid is not None: real_jid = nbxmpp.JID(real_jid) return { 'with_': with_, 'nick': nick, 'real_jid': real_jid, 'kind': KindConstant.GC_MSG }
def make_new_session(self, jid, thread_id=None, type_='chat', cls=None): """ Create and register a new session thread_id=None to generate one. type_ should be 'chat' or 'pm'. """ if not cls: cls = app.default_session_type sess = cls(self, nbxmpp.JID(jid), thread_id, type_) # determine if this session is a pm session # if not, discard the resource so that all sessions are stored bare if type_ != 'pm': jid = app.get_jid_without_resource(jid) if not jid in self.sessions: self.sessions[jid] = {} self.sessions[jid][sess.thread_id] = sess return sess
def get_own_jid(self): return nbxmpp.JID(self.username + '@' + self.host)
def is_same_jid(self, jid): """ Test if the bare jid given is the same as our bare jid """ return nbxmpp.JID(jid).getStripped() == self.get_own_bare_jid()
def get_own_jid(self, *args, **kwargs): return nbxmpp.JID(self.username + '@' + self.host)
def _nec_presence_received(self, obj): account = obj.conn.name if account != self.name: return jid = obj.jid resource = obj.resource or '' statuss = [ 'offline', 'error', 'online', 'chat', 'away', 'xa', 'dnd', 'invisible' ] obj.old_show = 0 obj.new_show = statuss.index(obj.show) obj.contact_list = [] highest = app.contacts.get_contact_with_highest_priority(account, jid) obj.was_highest = (highest and highest.resource == resource) # Update contact obj.contact_list = app.contacts.get_contacts(account, jid) obj.contact = None resources = [] for c in obj.contact_list: resources.append(c.resource) if c.resource == resource: obj.contact = c break if obj.contact: if obj.contact.show in statuss: obj.old_show = statuss.index(obj.contact.show) # nick changed if obj.contact_nickname is not None and \ obj.contact.contact_name != obj.contact_nickname: obj.contact.contact_name = obj.contact_nickname obj.need_redraw = True elif obj.old_show != obj.new_show or obj.contact.status != \ obj.status: obj.need_redraw = True else: obj.contact = app.contacts.get_first_contact_from_jid(account, jid) if not obj.contact: # Presence of another resource of our jid # Create self contact and add to roster if resource == obj.conn.server_resource: return # Ignore offline presence of unknown self resource if obj.new_show < 2: return obj.contact = app.contacts.create_self_contact( jid=jid, account=account, show=obj.show, status=obj.status, priority=obj.prio, keyID=obj.keyID, resource=obj.resource) app.contacts.add_contact(account, obj.contact) obj.contact_list.append(obj.contact) elif obj.contact.show in statuss: obj.old_show = statuss.index(obj.contact.show) if (resources != [''] and (len(obj.contact_list) != 1 or \ obj.contact_list[0].show not in ('not in roster', 'offline'))) and \ not app.jid_is_transport(jid): # Another resource of an existing contact connected obj.old_show = 0 obj.contact = app.contacts.copy_contact(obj.contact) obj.contact_list.append(obj.contact) obj.contact.resource = resource obj.need_add_in_roster = True if not app.jid_is_transport(jid) and len(obj.contact_list) == 1: # It's not an agent if obj.old_show == 0 and obj.new_show > 1: if not jid in app.newly_added[account]: app.newly_added[account].append(jid) if jid in app.to_be_removed[account]: app.to_be_removed[account].remove(jid) elif obj.old_show > 1 and obj.new_show == 0 and \ obj.conn.connected > 1: if not jid in app.to_be_removed[account]: app.to_be_removed[account].append(jid) if jid in app.newly_added[account]: app.newly_added[account].remove(jid) obj.need_redraw = True obj.contact.show = obj.show obj.contact.status = obj.status obj.contact.priority = obj.prio attached_keys = app.config.get_per('accounts', account, 'attached_gpg_keys').split() if jid in attached_keys: obj.contact.keyID = attached_keys[attached_keys.index(jid) + 1] else: # Do not override assigned key obj.contact.keyID = obj.keyID obj.contact.contact_nickname = obj.contact_nickname obj.contact.idle_time = obj.idle_time if app.jid_is_transport(jid): return # It isn't an agent # reset chatstate if needed: # (when contact signs out or has errors) if obj.show in ('offline', 'error'): obj.contact.our_chatstate = obj.contact.chatstate = None # TODO: This causes problems when another # resource signs off! self.stop_all_active_file_transfers(obj.contact) if app.config.get('log_contact_status_changes') and \ app.config.should_log(self.name, obj.jid): show = app.logger.convert_show_values_to_db_api_values(obj.show) if show is not None: app.logger.insert_into_logs(self.name, nbxmpp.JID(obj.jid).getStripped(), time_time(), KindConstant.STATUS, message=obj.status, show=show)
def isSameJID(self, jid): """ Test if the bare jid given is the same as our bare jid """ return nbxmpp.JID(jid).getStripped() == self.getOurBareJID()
def generate(self): self.ptype = self.presence_obj.ptype self.fjid = self.presence_obj.fjid self.jid = self.presence_obj.jid self.room_jid = self.presence_obj.jid self.nick = self.presence_obj.resource self.show = self.presence_obj.show self.status = self.presence_obj.status self.avatar_sha = self.presence_obj.avatar_sha self.errcode = self.presence_obj.errcode self.errmsg = self.presence_obj.errmsg self.errcon = self.stanza.getError() self.get_gc_control() self.gc_contact = app.contacts.get_gc_contact(self.conn.name, self.room_jid, self.nick) if self.ptype == 'error': return True if self.ptype and self.ptype != 'unavailable': return if app.config.get('log_contact_status_changes') and \ app.config.should_log(self.conn.name, self.room_jid): if self.gc_contact: jid = self.gc_contact.jid else: jid = self.stanza.getJid() st = self.status if jid: # we know real jid, save it in db st += ' (%s)' % jid show = app.logger.convert_show_values_to_db_api_values(self.show) if show is not None: fjid = nbxmpp.JID(self.fjid) app.logger.insert_into_logs(self.conn.name, fjid.getStripped(), time_time(), KindConstant.GCSTATUS, contact_name=fjid.getResource(), message=st, show=show) # NOTE: if it's a gc presence, don't ask vcard here. # We may ask it to real jid in gui part. self.status_code = [] ns_muc_user_x = self.stanza.getTag('x', namespace=nbxmpp.NS_MUC_USER) if ns_muc_user_x: destroy = ns_muc_user_x.getTag('destroy') else: destroy = None if ns_muc_user_x and destroy: # Room has been destroyed. see # http://www.xmpp.org/extensions/xep-0045.html#destroyroom self.reason = _('Room has been destroyed') r = destroy.getTagData('reason') if r: self.reason += ' (%s)' % r if destroy.getAttr('jid'): try: jid = helpers.parse_jid(destroy.getAttr('jid')) self.reason += '\n' + \ _('You can join this room instead: %s') % jid except helpers.InvalidFormat: pass self.status_code = ['destroyed'] else: self.reason = self.stanza.getReason() conditions = self.stanza.getStatusConditions() if conditions: self.status_code = [] for condition in conditions: if condition in CONDITION_TO_CODE: self.status_code.append(CONDITION_TO_CODE[condition]) else: self.status_code = self.stanza.getStatusCode() self.role = self.stanza.getRole() self.affiliation = self.stanza.getAffiliation() self.real_jid = self.stanza.getJid() self.actor = self.stanza.getActor() self.new_nick = self.stanza.getNewNick() return True
def setUp(self): self.jid = nbxmpp.JID('[email protected]/Gajim') self.conn = MockConnection(account_name, {'send_stanza': None}) self.sess = StanzaSession(self.conn, self.jid, None, 'chat')