Search in sources :

Example 16 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class PresenceSubscribeHandler method process.

@Override
public void process(Presence presence) throws PacketException {
    if (presence == null) {
        throw new IllegalArgumentException("Argument 'presence' cannot be null.");
    }
    final Presence.Type type = presence.getType();
    if (type != Presence.Type.subscribe && type != Presence.Type.unsubscribe && type != Presence.Type.subscribed && type != Presence.Type.unsubscribed) {
        throw new IllegalArgumentException("Packet processed by PresenceSubscribeHandler is " + "not of a subscription-related type, but: " + type);
    }
    // RFC-6121 paragraph 3: "When a server processes or generates an outbound presence stanza
    // of type "subscribe", "subscribed", "unsubscribe", or "unsubscribed", the server MUST stamp
    // the outgoing presence stanza with the bare JID <localpart@domainpart> of the sending entity,
    // not the full JID <localpart@domainpart/resourcepart>."
    presence.setFrom(presence.getFrom().toBareJID());
    // JID and modify the 'to' address accordingly.
    if (presence.getTo() != null) {
        presence.setTo(presence.getTo().toBareJID());
    }
    final JID senderJID = presence.getFrom();
    final JID recipientJID = presence.getTo();
    try {
        // Reject presence subscription requests sent to the local server itself.
        if (recipientJID == null || recipientJID.toString().equals(serverName)) {
            if (type == Presence.Type.subscribe) {
                Presence reply = new Presence();
                reply.setTo(senderJID);
                reply.setFrom(recipientJID);
                reply.setType(Presence.Type.unsubscribed);
                deliverer.deliver(reply);
            }
            return;
        }
        try {
            Roster senderRoster = getRoster(senderJID);
            if (senderRoster != null) {
                manageSub(recipientJID, true, type, senderRoster);
            }
            Roster recipientRoster = getRoster(recipientJID);
            boolean recipientSubChanged = false;
            if (recipientRoster != null) {
                recipientSubChanged = manageSub(senderJID, false, type, recipientRoster);
            }
            // and the recipient user has not changed its subscription state.
            if (!(type == Presence.Type.subscribed && recipientRoster != null && !recipientSubChanged)) {
                // See http://tools.ietf.org/html/rfc3921#section-7 and/or OF-38 
                if (type == Presence.Type.subscribe && recipientRoster != null && !recipientSubChanged) {
                    try {
                        RosterItem.SubType subType = recipientRoster.getRosterItem(senderJID).getSubStatus();
                        if (subType == RosterItem.SUB_FROM || subType == RosterItem.SUB_BOTH) {
                            return;
                        }
                    } catch (UserNotFoundException e) {
                        // Weird case: Roster item does not exist. Should never happen
                        Log.error("User does not exist while trying to update roster item. " + "This should never happen (this indicates a programming " + "logic error). Processing stanza: " + presence.toString(), e);
                    }
                }
                // Try to obtain a handler for the packet based on the routes. If the handler is
                // a module, the module will be able to handle the packet. If the handler is a
                // Session the packet will be routed to the client. If a route cannot be found
                // then the packet will be delivered based on its recipient and sender.
                List<JID> jids = routingTable.getRoutes(recipientJID, null);
                if (!jids.isEmpty()) {
                    for (JID jid : jids) {
                        Presence presenteToSend = presence.createCopy();
                        // Stamp the presence with the user's bare JID as the 'from' address,
                        // as required by section 8.2.5 of RFC 3921
                        presenteToSend.setFrom(senderJID.toBareJID());
                        routingTable.routePacket(jid, presenteToSend, false);
                    }
                } else {
                    deliverer.deliver(presence.createCopy());
                }
                if (type == Presence.Type.subscribed) {
                    // Send the presence of the local user to the remote user. The remote user
                    // subscribed to the presence of the local user and the local user accepted
                    JID prober = localServer.isLocal(recipientJID) ? recipientJID.asBareJID() : recipientJID;
                    if (presenceManager.canProbePresence(prober, senderJID.getNode())) {
                        presenceManager.probePresence(prober, senderJID);
                        PresenceEventDispatcher.subscribedToPresence(recipientJID, senderJID);
                    } else {
                        Presence nonProbablePresence = new Presence();
                        nonProbablePresence.setStatus("unavailable");
                        nonProbablePresence.setFrom(senderJID);
                        nonProbablePresence.setTo(recipientJID);
                        presenceManager.handleProbe(nonProbablePresence);
                    }
                }
            }
            if (type == Presence.Type.unsubscribed) {
                // Send unavailable presence from all of the local user's available resources
                // to the remote user
                presenceManager.sendUnavailableFromSessions(recipientJID, senderJID);
                PresenceEventDispatcher.unsubscribedToPresence(senderJID, recipientJID);
            }
        } catch (SharedGroupException e) {
            Presence result = presence.createCopy();
            JID sender = result.getFrom();
            result.setFrom(presence.getTo());
            result.setTo(sender);
            result.setError(PacketError.Condition.not_acceptable);
            deliverer.deliver(result);
        }
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster) Presence(org.xmpp.packet.Presence) SharedGroupException(org.jivesoftware.openfire.SharedGroupException) UserAlreadyExistsException(org.jivesoftware.openfire.user.UserAlreadyExistsException) PacketException(org.jivesoftware.openfire.PacketException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) SharedGroupException(org.jivesoftware.openfire.SharedGroupException)

Example 17 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class PresenceSubscribeHandler method getRoster.

/**
     * <p>Obtain the roster for the given address or null if the address doesn't have a roster.</p>
     *
     * @param address The address to check
     * @return The roster or null if the address is not managed on the server
     */
private Roster getRoster(JID address) {
    String username;
    Roster roster = null;
    if (localServer.isLocal(address) && userManager.isRegisteredUser(address.getNode())) {
        username = address.getNode();
        try {
            roster = rosterManager.getRoster(username);
        } catch (UserNotFoundException e) {
        // Do nothing
        }
    }
    return roster;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) Roster(org.jivesoftware.openfire.roster.Roster)

Example 18 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class IQRosterHandler method removeItem.

/**
     * Remove the roster item from the sender's roster (and possibly the recipient's).
     * Actual roster removal is done in the removeItem(Roster,RosterItem) method.
     *
     * @param roster The sender's roster.
     * @param sender The JID of the sender of the removal request
     * @param item   The removal item element
     * @return The removed item or null, if not item has been removed.
     */
private RosterItem removeItem(org.jivesoftware.openfire.roster.Roster roster, JID sender, org.xmpp.packet.Roster.Item item) throws SharedGroupException {
    JID recipient = item.getJID();
    // Remove recipient from the sender's roster
    RosterItem removedItem = roster.deleteRosterItem(item.getJID(), true);
    // Forward set packet to the subscriber
    if (localServer.isLocal(recipient)) {
        // Recipient is local so let's handle it here
        try {
            Roster recipientRoster = userManager.getUser(recipient.getNode()).getRoster();
            // Instead of deleting the sender in the recipient's roster, update it.
            // http://issues.igniterealtime.org/browse/OF-720
            RosterItem rosterItem = recipientRoster.getRosterItem(sender);
            // If the receiver doesn't have subscribed yet, delete the sender from the receiver's roster, too.
            if (rosterItem.getRecvStatus().equals(RosterItem.RECV_SUBSCRIBE)) {
                recipientRoster.deleteRosterItem(sender, true);
            } else // Otherwise only update it, so that the sender is not deleted from the receivers roster.
            {
                rosterItem.setAskStatus(RosterItem.ASK_NONE);
                rosterItem.setRecvStatus(RosterItem.RECV_NONE);
                rosterItem.setSubStatus(RosterItem.SUB_NONE);
                recipientRoster.updateRosterItem(rosterItem);
            }
        } catch (UserNotFoundException e) {
        // Do nothing
        }
    } else {
        // Recipient is remote so we just forward the packet to them
        String serverDomain = localServer.getServerInfo().getXMPPDomain();
        // Check if the recipient may be hosted by this server
        if (!recipient.getDomain().contains(serverDomain)) {
        // TODO Implete when s2s is implemented
        } else {
            Packet removePacket = createRemoveForward(sender, recipient);
            router.route(removePacket);
        }
    }
    return removedItem;
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) RosterItem(org.jivesoftware.openfire.roster.RosterItem) Packet(org.xmpp.packet.Packet) JID(org.xmpp.packet.JID) Roster(org.jivesoftware.openfire.roster.Roster)

Example 19 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class EmailTranscriptEvent method chatSupportFinished.

public void chatSupportFinished(Workgroup workgroup, String sessionID) {
    Log.debug("Chat Support Finished, sending transcripts");
    final EmailService emailService = EmailService.getInstance();
    String property = JiveGlobals.getProperty("mail.configured");
    if (!ModelUtil.hasLength(property)) {
        Log.debug("Mail settings are not configured, transcripts will not be sent.");
        return;
    }
    final ChatSession chatSession = ChatTranscriptManager.getChatSession(sessionID);
    if (chatSession == null || chatSession.getFirstSession() == null) {
        return;
    }
    final StringBuilder builder = new StringBuilder();
    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyy hh:mm a");
    // Get duration of conversation
    Date date = new Date(chatSession.getFirstSession().getStartTime());
    int duration = getChatDuration(date, chatSession);
    TreeMap<String, List<String>> map = new TreeMap<String, List<String>>(chatSession.getMetadata());
    String body = JiveGlobals.getProperty("chat.transcript.body");
    if (ModelUtil.hasLength(body)) {
        builder.append(body).append("\n\n");
    }
    builder.append("formname=chat transcript\n");
    extractAndDisplay(builder, "question", map);
    display(builder, "fullname", chatSession.getCustomerName());
    extractAndDisplay(builder, "email", map);
    extractAndDisplay(builder, "Location", map);
    extractAndDisplay(builder, "userID", map);
    extractAndDisplay(builder, "username", map);
    extractAndDisplay(builder, "workgroup", map);
    display(builder, "chatduration", String.valueOf(duration));
    display(builder, "chatdate", formatter.format(date));
    if (chatSession.getFirstSession() != null && chatSession.getFirstSession().getAgentJID() != null) {
        try {
            display(builder, "agent", new JID(chatSession.getFirstSession().getAgentJID()).toBareJID());
        } catch (Exception e) {
            Log.debug("Could not display agent in transcript.", e);
        }
    }
    for (Iterator<Map.Entry<String, List<String>>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
        Map.Entry<String, List<String>> entry = iterator.next();
        display(builder, entry.getKey(), getListItem(entry.getValue()));
    }
    builder.append("ctranscript=\n");
    builder.append(ChatTranscriptManager.getTextTranscriptFromSessionID(sessionID));
    String subject = JiveGlobals.getProperty("chat.transcript.subject");
    String from = JiveGlobals.getProperty("chat.transcript.from");
    String to = JiveGlobals.getProperty("chat.transcript.to");
    if (!ModelUtil.hasLength(subject) || !ModelUtil.hasLength(from)) {
        Log.debug("Transcript settings (chat.transcript.subject, chat.transcript.from) are not configured, " + "transcripts will not be sent.");
        return;
    }
    if (ModelUtil.hasLength(to)) {
        emailService.sendMessage("Chat Transcript", to, "Chat Transcript", from, subject, builder.toString(), null);
        Log.debug("Transcript sent to " + to);
    }
    // NOTE: Do not sent to the customer. They will receive a prompt for a seperate chat transcript
    // that does not contain agent information.
    // Send to Agents
    UserManager um = UserManager.getInstance();
    for (Iterator<AgentChatSession> iterator = chatSession.getAgents(); iterator.hasNext(); ) {
        AgentChatSession agentSession = iterator.next();
        try {
            User user = um.getUser(new JID(agentSession.getAgentJID()).getNode());
            emailService.sendMessage("Chat Transcript", user.getEmail(), "Chat Transcript", from, subject, builder.toString(), null);
            Log.debug("Transcript sent to agent " + agentSession.getAgentJID());
        } catch (UserNotFoundException e) {
            Log.error("Email Transcript Not Sent:" + "Could not load agent user object for jid " + agentSession.getAgentJID());
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) JID(org.xmpp.packet.JID) TreeMap(java.util.TreeMap) ChatSession(org.jivesoftware.openfire.fastpath.history.ChatSession) AgentChatSession(org.jivesoftware.openfire.fastpath.history.AgentChatSession) Date(java.util.Date) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) AgentChatSession(org.jivesoftware.openfire.fastpath.history.AgentChatSession) UserManager(org.jivesoftware.openfire.user.UserManager) List(java.util.List) EmailService(org.jivesoftware.util.EmailService) SimpleDateFormat(java.text.SimpleDateFormat) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 20 with UserNotFoundException

use of org.jivesoftware.openfire.user.UserNotFoundException in project Openfire by igniterealtime.

the class BookmarkInterceptor method addBookmarkElement.

/**
     * Adds a Bookmark to the users defined list of bookmarks.
     *
     * @param jid      the users jid.
     * @param bookmark the bookmark to be added.
     * @param element  the storage element to append to.
     */
private void addBookmarkElement(JID jid, Bookmark bookmark, Element element) {
    final UserManager userManager = UserManager.getInstance();
    try {
        userManager.getUser(jid.getNode());
    } catch (UserNotFoundException e) {
        return;
    }
    // do not add duplicate bookmarks.
    if (bookmark.getType() == Bookmark.Type.url) {
        Element urlBookmarkElement = urlExists(element, bookmark.getValue());
        if (urlBookmarkElement == null) {
            urlBookmarkElement = element.addElement("url");
            urlBookmarkElement.addAttribute("name", bookmark.getName());
            urlBookmarkElement.addAttribute("url", bookmark.getValue());
            // Add an RSS attribute to the bookmark if it's defined. RSS isn't an
            // official part of the Bookmark JEP, but we define it as a logical
            // extension.
            boolean rss = Boolean.valueOf(bookmark.getProperty("rss"));
            if (rss) {
                urlBookmarkElement.addAttribute("rss", Boolean.toString(rss));
            }
        }
        appendSharedElement(urlBookmarkElement);
    } else // Otherwise it's a conference bookmark.
    {
        try {
            Element conferenceElement = conferenceExists(element, bookmark.getValue());
            // reply.
            if (conferenceElement == null) {
                conferenceElement = element.addElement("conference");
                conferenceElement.addAttribute("name", bookmark.getName());
                boolean autojoin = Boolean.valueOf(bookmark.getProperty("autojoin"));
                conferenceElement.addAttribute("autojoin", Boolean.toString(autojoin));
                conferenceElement.addAttribute("jid", bookmark.getValue());
                boolean nameasnick = Boolean.valueOf(bookmark.getProperty("nameasnick"));
                if (nameasnick) {
                    User currentUser = userManager.getUser(jid.getNode());
                    Element nick = conferenceElement.addElement("nick");
                    nick.addText(currentUser.getName());
                }
            }
            appendSharedElement(conferenceElement);
        } catch (Exception e) {
            Log.error(e.getMessage(), e);
        }
    }
}
Also used : UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) User(org.jivesoftware.openfire.user.User) UserManager(org.jivesoftware.openfire.user.UserManager) Element(org.dom4j.Element) PacketRejectedException(org.jivesoftware.openfire.interceptor.PacketRejectedException) UserNotFoundException(org.jivesoftware.openfire.user.UserNotFoundException) GroupNotFoundException(org.jivesoftware.openfire.group.GroupNotFoundException)

Aggregations

UserNotFoundException (org.jivesoftware.openfire.user.UserNotFoundException)118 JID (org.xmpp.packet.JID)50 Element (org.dom4j.Element)28 Roster (org.jivesoftware.openfire.roster.Roster)27 RosterItem (org.jivesoftware.openfire.roster.RosterItem)26 User (org.jivesoftware.openfire.user.User)25 UserAlreadyExistsException (org.jivesoftware.openfire.user.UserAlreadyExistsException)23 IQ (org.xmpp.packet.IQ)15 ArrayList (java.util.ArrayList)14 UnauthorizedException (org.jivesoftware.openfire.auth.UnauthorizedException)12 SharedGroupException (org.jivesoftware.openfire.SharedGroupException)11 Group (org.jivesoftware.openfire.group.Group)10 UserManager (org.jivesoftware.openfire.user.UserManager)10 Workgroup (org.jivesoftware.xmpp.workgroup.Workgroup)10 Presence (org.xmpp.packet.Presence)10 NotFoundException (org.jivesoftware.util.NotFoundException)9 SQLException (java.sql.SQLException)8 List (java.util.List)8 IOException (java.io.IOException)7 Connection (java.sql.Connection)7