Search in sources :

Example 6 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class MSNListener method contactRemovedMe.

/**
     * Someone removed us from their contact list.
     */
public void contactRemovedMe(MsnMessenger messenger, MsnContact friend) {
    Log.debug("MSN: Contact removed me: " + friend.getFriendlyName());
    Presence p = new Presence();
    p.setType(Presence.Type.unsubscribe);
    p.setTo(getSession().getJID());
    p.setFrom(getSession().getTransport().convertIDToJID(friend.getEmail().toString()));
    getSession().getTransport().sendPacket(p);
}
Also used : Presence(org.xmpp.packet.Presence)

Example 7 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class SimpleListener method processRequest.

public void processRequest(RequestEvent requestEvent) {
    ServerTransaction serverTransaction = requestEvent.getServerTransaction();
    Dialog dialog = null;
    if (serverTransaction != null) {
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Getting dialog");
        dialog = serverTransaction.getDialog();
    }
    int responseCode = 200;
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  Received a request event:  \n" + requestEvent.getRequest().toString());
    String fromAddr = "";
    Request request = requestEvent.getRequest();
    if (request.getHeader(FromHeader.NAME) != null) {
        FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
        Address fromAddress = fromHeader.getAddress();
        //			String displayName = fromAddress.getDisplayName();
        URI fromUri = fromAddress.getURI();
        if (fromUri != null) {
            if (fromUri.isSipURI()) {
                SipURI fromSipUri = (SipURI) fromUri;
                fromAddr = fromSipUri.getUser() + "@" + fromSipUri.getHost();
            } else {
                fromAddr = fromUri.toString();
            }
        }
    }
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  FromAddr = " + fromAddr);
    Log.debug("SimpleListener(" + myUsername + ").processRequest:  Request method = '" + request.getMethod() + "'");
    if (request.getMethod().equals(Request.MESSAGE)) {
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Starting MESSAGE request handling process.");
        JID senderJid = getSession().getTransport().convertIDToJID(fromAddr);
        String msgContent = new String((byte[]) request.getContent());
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  Forwarding MESSAGE request as XMPP message, setting from = " + senderJid + " and content = '" + msgContent + "'");
        getSession().getTransport().sendMessage(getSession().getJID(), senderJid, msgContent);
        getSession().sendResponse(responseCode, request, serverTransaction);
    } else if (request.getMethod().equals(Request.NOTIFY)) {
        SubscriptionStateHeader subscriptionStateHeader = (SubscriptionStateHeader) request.getHeader(SubscriptionStateHeader.NAME);
        Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY request handling process started.");
        if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.ACTIVE)) {
            Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY Active!");
            int expires = subscriptionStateHeader.getExpires();
            Log.debug("SimpleListener(" + myUsername + ").processRequest:  NOTIFY Expiry = " + expires);
            try {
                if (expires > 0) {
                    String content = "";
                    if (request.getContent() != null)
                        content = new String((byte[]) request.getContent());
                    if (content.length() > 0) {
                        SimplePresence simplePresence = SimplePresence.parseSimplePresence(content);
                        try {
                            SimpleBuddy buddy = getSession().getBuddyManager().getBuddy(getSession().getTransport().convertIDToJID(fromAddr));
                            String verboseStatus = null;
                            if (simplePresence.getTupleStatus().isOpen()) {
                                switch(simplePresence.getRpid()) {
                                    case ON_THE_PHONE:
                                        // TODO: Translate this
                                        verboseStatus = "On Phone";
                                }
                            }
                            buddy.setPresenceAndStatus(((SimpleTransport) getSession().getTransport()).convertSIPStatusToXMPP(simplePresence), verboseStatus);
                        } catch (NotFoundException e) {
                            // Not in our contact list.  Ignore.
                            Log.debug("SIMPLE: Received presense notification for contact we don't care about: " + fromAddr);
                        }
                    }
                } else {
                    Presence p = new Presence();
                    p.setType(Presence.Type.unsubscribed);
                    p.setTo(getSession().getJID());
                    p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
                    getSession().getTransport().sendPacket(p);
                }
                Log.debug("SimpleListener(" + myUsername + ").processRequest:  Sending XMPP presence packet.");
            } catch (Exception ex) {
                Log.debug("SimpleListener(" + myUsername + ").processRequest:  Exception occured when processing NOTIFY packet...", ex);
            }
        } else if (subscriptionStateHeader.getState().equalsIgnoreCase(SubscriptionStateHeader.TERMINATED)) {
            Presence p = new Presence();
            p.setType(Presence.Type.unsubscribed);
            p.setTo(getSession().getJID());
            p.setFrom(getSession().getTransport().convertIDToJID(fromAddr));
            getSession().getTransport().sendPacket(p);
        }
        getSession().sendResponse(responseCode, request, serverTransaction);
    } else if (request.getMethod().equals(Request.SUBSCRIBE)) {
        Log.debug("SimpleListener for " + myUsername + ":  SUBSCRIBE request handling process.");
        ServerTransaction transaction = getSession().sendResponse(202, request, serverTransaction);
        Log.debug("SimpleListener for " + myUsername + ":  SUBSCRIBE should be followed by a NOTIFY");
        // Send NOTIFY packet.
        try {
            if (transaction != null)
                getSession().sendNotify(transaction.getDialog());
            else
                getSession().sendNotify(dialog);
        } catch (Exception e) {
            Log.debug("SimpleListener for " + myUsername + ":  Unable to prepare NOTIFY packet.", e);
        }
    }
}
Also used : Address(javax.sip.address.Address) JID(org.xmpp.packet.JID) FromHeader(javax.sip.header.FromHeader) SubscriptionStateHeader(javax.sip.header.SubscriptionStateHeader) Request(javax.sip.message.Request) NotFoundException(org.jivesoftware.util.NotFoundException) SipURI(javax.sip.address.SipURI) URI(javax.sip.address.URI) SipURI(javax.sip.address.SipURI) NotFoundException(org.jivesoftware.util.NotFoundException) Dialog(javax.sip.Dialog) Presence(org.xmpp.packet.Presence) ServerTransaction(javax.sip.ServerTransaction)

Example 8 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class SimpleSession method contactUnsubscribed.

public void contactUnsubscribed(String targetSipAddress) {
    try {
        //			Roster     roster     = getTransport().getRosterManager().getRoster(getJID().getNode());
        JID contactJID = getTransport().convertIDToJID(targetSipAddress);
        //			RosterItem item       = roster.getRosterItem(contactJID);
        Log.debug("SimpleSession(" + getJID().getNode() + ").contactUnsubscribed:  Preparing presence packet...");
        Presence presence = new Presence();
        presence.setFrom(contactJID);
        presence.setTo(getJID());
        presence.setType(Presence.Type.unsubscribed);
        getTransport().sendPacket(presence);
        Log.debug("SimpleSession(" + getJID().getNode() + ").contactUnsubscribed:  Presence packet sent ==> \n" + presence.toXML());
    //			Log.debug("SimpleSession(" + jid.getNode() + ").contactUnsubscribed:  Synchronizing SIP user roster...");
    //			String rosteruserid = ((SimpleTransport) transport).convertJIDToID(item.getJid());
    //			myRoster.removeEntry(rosteruserid);
    //			Log.debug("SimpleSession(" + jid.getNode() + ").contactUnsubscribed:  Finished synchronizing SIP user roster.");
    //			syncContactGroups(contact, item.getGroups());
    } catch (Exception e) {
        Log.debug("SimpleSession(" + getJID().getNode() + ").contactUnsubscribed:  Exception occured when adding pending contact " + targetSipAddress, e);
    //			JID contactJID = getTransport().convertIDToJID(targetSipAddress);
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 9 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class SimpleSession method contactSubscribed.

//	private Request prepareNotifyRequest(Dialog dialog, SimplePresence simplePresence) throws ParseException {
//		Request request = prepareNotifyRequest(dialog);
//		request.setContent(simplePresence.toXML(), headerFactory.createContentTypeHeader("application", "pidf+xml"));
//
//		return request;
//	}
public void contactSubscribed(String targetSipAddress) {
    try {
        //			Roster     roster     = getTransport().getRosterManager().getRoster(getJID().getNode());
        JID contactJID = getTransport().convertIDToJID(targetSipAddress);
        //            RosterItem item       = roster.getRosterItem(contactJID);
        Log.debug("SimpleSession(" + jid.getNode() + ").contactSubscribed:  Preparing presence packet...");
        Presence presence = new Presence();
        presence.setFrom(contactJID);
        presence.setTo(getJID());
        presence.setType(Presence.Type.subscribed);
        getTransport().sendPacket(presence);
        Log.debug("SimpleSession(" + jid.getNode() + ").contactSubscribed:  Presence packet sent ==> \n" + presence.toXML());
    //			Log.debug("SimpleSession(" + jid.getNode() + ").contactSubscribed:  Synchronizing SIP user roster...");
    //			String rosteruserid = ((SimpleTransport) transport).convertJIDToID(item.getJid());
    //			if (myRoster.getEntry(rosteruserid) == null) {
    //				SimpleBuddy simpleRosterItem = new SimpleBuddy(rosteruserid, item.getNickname(), 1L);
    //				myRoster.addEntry(rosteruserid, simpleRosterItem);
    //			}
    //			Log.debug("SimpleSession(" + jid.getNode() + ").contactSubscribed:  Finished synchronizing SIP user roster.");
    //			syncContactGroups(contact, item.getGroups());
    } catch (Exception e) {
        Log.debug("SimpleSession(" + jid.getNode() + ").contactSubscribed:  Exception occured when adding pending contact " + targetSipAddress, e);
    //			JID contactJID = getTransport().convertIDToJID(targetSipAddress);
    }
}
Also used : JID(org.xmpp.packet.JID) Presence(org.xmpp.packet.Presence) InvalidArgumentException(javax.sip.InvalidArgumentException) ParseException(java.text.ParseException) NotFoundException(org.jivesoftware.util.NotFoundException) TooManyListenersException(java.util.TooManyListenersException) SipException(javax.sip.SipException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException)

Example 10 with Presence

use of org.xmpp.packet.Presence in project Openfire by igniterealtime.

the class XMPPPresenceHandler method handlePresenceSubscription.

/**
	 * Handles incoming presence stanzas that relate to subscription status.
	 * Ignores others.
	 * 
	 * @param presence
	 *            the stanza
	 */
private void handlePresenceSubscription(final org.jivesoftware.smack.packet.Presence presence) {
    final Presence p = new Presence();
    p.setTo(session.getJID());
    p.setFrom(session.getTransport().convertIDToJID(presence.getFrom()));
    switch(presence.getType()) {
        case subscribe:
            p.setType(Presence.Type.subscribe);
            break;
        case subscribed:
            final XMPPBuddy buddy = new XMPPBuddy(session.getBuddyManager(), presence.getFrom());
            session.getBuddyManager().storeBuddy(buddy);
            p.setType(Presence.Type.subscribed);
            break;
        case unsubscribe:
            p.setType(Presence.Type.unsubscribe);
            break;
        case unsubscribed:
            p.setType(Presence.Type.unsubscribed);
            break;
        case error:
            p.setType(Presence.Type.error);
            break;
        default:
            // don't send anything.
            return;
    }
    session.getTransport().sendPacket(p);
}
Also used : Presence(org.xmpp.packet.Presence)

Aggregations

Presence (org.xmpp.packet.Presence)109 JID (org.xmpp.packet.JID)38 Element (org.dom4j.Element)34 UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)20 Message (org.xmpp.packet.Message)17 IQ (org.xmpp.packet.IQ)16 UpdatePresence (org.jivesoftware.openfire.muc.cluster.UpdatePresence)14 NotFoundException (org.jivesoftware.util.NotFoundException)12 ArrayList (java.util.ArrayList)11 MUCRole (org.jivesoftware.openfire.muc.MUCRole)10 GroupNotFoundException (org.jivesoftware.openfire.group.GroupNotFoundException)9 DefaultElement (org.dom4j.tree.DefaultElement)8 IOException (java.io.IOException)7 SQLException (java.sql.SQLException)7 GroupJID (org.jivesoftware.openfire.group.GroupJID)7 NotAllowedException (org.jivesoftware.openfire.muc.NotAllowedException)7 Date (java.util.Date)6 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)6 ConflictException (org.jivesoftware.openfire.muc.ConflictException)6 ForbiddenException (org.jivesoftware.openfire.muc.ForbiddenException)6