Esempio n. 1
0
  private void handlePacket(Packet packet) {
    if (packet instanceof Message) {
      Message msg = (Message) packet;
      // Check to see if the user left the queue.
      PacketExtension pe = msg.getExtension("depart-queue", "http://jabber.org/protocol/workgroup");
      PacketExtension queueStatus =
          msg.getExtension("queue-status", "http://jabber.org/protocol/workgroup");

      if (pe != null) {
        fireQueueDepartedEvent();
      } else if (queueStatus != null) {
        QueueUpdate queueUpdate = (QueueUpdate) queueStatus;
        if (queueUpdate.getPosition() != -1) {
          fireQueuePositionEvent(queueUpdate.getPosition());
        }
        if (queueUpdate.getRemaingTime() != -1) {
          fireQueueTimeEvent(queueUpdate.getRemaingTime());
        }
      } else {
        // Check if a room invitation was sent and if the sender is the workgroup
        MUCUser mucUser = (MUCUser) msg.getExtension("x", "http://jabber.org/protocol/muc#user");
        MUCUser.Invite invite = mucUser != null ? mucUser.getInvite() : null;
        if (invite != null && workgroupJID.equals(invite.getFrom())) {
          String sessionID = null;
          Map<String, List<String>> metaData = null;

          pe = msg.getExtension(SessionID.ELEMENT_NAME, SessionID.NAMESPACE);
          if (pe != null) {
            sessionID = ((SessionID) pe).getSessionID();
          }

          pe = msg.getExtension(MetaData.ELEMENT_NAME, MetaData.NAMESPACE);
          if (pe != null) {
            metaData = ((MetaData) pe).getMetaData();
          }

          WorkgroupInvitation inv =
              new WorkgroupInvitation(
                  connection.getUser(),
                  msg.getFrom(),
                  workgroupJID,
                  sessionID,
                  msg.getBody(),
                  msg.getFrom(),
                  metaData);

          fireInvitationEvent(inv);
        }
      }
    }
  }
Esempio n. 2
0
  /**
   * Handles a message received from the remote party.
   *
   * @param msg The Message object that was received.
   */
  private void doMessageReceived(Message msg) {
    assert (SwingUtilities.isEventDispatchThread()) : "not in UI thread";

    if (msg.getType() == Message.Type.ERROR) {
      JOptionPane.showMessageDialog(
          this,
          msg.getError().getMessage(),
          JavolinApp.getAppName() + ": Error",
          JOptionPane.ERROR_MESSAGE);
    } else if (msg.getType() == Message.Type.HEADLINE) {
      // Do nothing
    } else {
      String jid = msg.getFrom();
      if (jid != null) setUserResource(jid);

      Date date = null;
      PacketExtension ext = msg.getExtension("x", "jabber:x:delay");
      if (ext != null && ext instanceof DelayInformation) {
        date = ((DelayInformation) ext).getStamp();
      }

      mLog.message(mRemoteIdFull, mRemoteNick, msg.getBody(), date);
      Audio.playMessage();
    }
  }
 /**
  * Adds an XHTML body to the message.
  *
  * @param message the message that will receive the XHTML body
  * @param body the string to add as an XHTML body to the message
  */
 public static void addBody(Message message, String body) {
   XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
   if (xhtmlExtension == null) {
     // Create an XHTMLExtension and add it to the message
     xhtmlExtension = new XHTMLExtension();
     message.addExtension(xhtmlExtension);
   }
   // Add the required bodies to the message
   xhtmlExtension.addBody(body);
 }
 /**
  * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
  */
 @Override
 public void processPacket(Packet packet) {
   Message incomingMessage = (Message) packet;
   GcmPacketExtension gcmPacket =
       (GcmPacketExtension) incomingMessage.getExtension(GcmPacketExtension.GCM_NAMESPACE);
   String json = gcmPacket.getJson();
   JsonReader jsonReader = Json.createReader(new StringReader(json));
   JsonObject jsonObject = jsonReader.readObject();
   Object messageType = jsonObject.get("message_type");
   System.out.println(messageType);
 }
Esempio n. 5
0
  public void insertMessage(Message message) {
    // Debug info
    super.insertMessage(message);
    MessageEvent messageEvent = (MessageEvent) message.getExtension("x", "jabber:x:event");
    if (messageEvent != null) {
      checkEvents(message.getFrom(), message.getPacketID(), messageEvent);
    }

    getTranscriptWindow()
        .insertMessage(participantNickname, message, ChatManager.FROM_COLOR, Color.white);

    // Set the participant jid to their full JID.
    participantJID = message.getFrom();
  }
Esempio n. 6
0
 public void processMessage(Chat chat, Message message)
 {
     message = message.getExtension("http://jabber.org/protocol/chatstates");
     if (message == null)
     {
         return;
     }
     try
     {
         message = ChatState.valueOf(message.getElementName());
     }
     // Misplaced declaration of an exception variable
     catch (Chat chat)
     {
         return;
     }
     fireNewChatState(chat, message);
 }
 /**
  * Returns true if the message contains an XHTML extension.
  *
  * @param message the message to check if contains an XHTML extentsion or not
  * @return a boolean indicating whether the message is an XHTML message
  */
 public static boolean isXHTMLMessage(Message message) {
   return message.getExtension("html", namespace) != null;
 }
 /**
  * Returns an Iterator for the XHTML bodies in the message. Returns null if the message does not
  * contain an XHTML extension.
  *
  * @param message an XHTML message
  * @return an Iterator for the bodies in the message or null if none.
  */
 public static List<String> getBodies(Message message) {
   XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace);
   if (xhtmlExtension != null) return xhtmlExtension.getBodies();
   else return null;
 }
Esempio n. 9
0
 /**
  * Obtain a Carbon from a message, if available.
  *
  * <p>Only {@link Message} instances can contain a Carbon extensions.
  *
  * @param msg Message object to check for carbons
  * @return a Carbon if available, null otherwise.
  */
 public static CarbonExtension from(Message msg) {
   CarbonExtension cc = msg.getExtension(Direction.received.name(), NAMESPACE);
   if (cc == null) cc = msg.getExtension(Direction.sent.name(), NAMESPACE);
   return cc;
 }
Esempio n. 10
0
 public static XHTMLExtension from(Message message)
 {
     return (XHTMLExtension)message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
 }
    /**
     * Handles incoming messages and dispatches whatever events are necessary.
     *
     * @param packet the packet that we need to handle (if it is a message).
     */
    public void processPacket(Packet packet) {
      if (!(packet instanceof org.jivesoftware.smack.packet.Message)) return;

      org.jivesoftware.smack.packet.Message msg = (org.jivesoftware.smack.packet.Message) packet;

      boolean isForwardedSentMessage = false;
      if (msg.getBody() == null) {

        CarbonPacketExtension carbonExt =
            (CarbonPacketExtension) msg.getExtension(CarbonPacketExtension.NAMESPACE);
        if (carbonExt == null) return;

        isForwardedSentMessage =
            (carbonExt.getElementName() == CarbonPacketExtension.SENT_ELEMENT_NAME);
        List<ForwardedPacketExtension> extensions =
            carbonExt.getChildExtensionsOfType(ForwardedPacketExtension.class);
        if (extensions.isEmpty()) return;
        ForwardedPacketExtension forwardedExt = extensions.get(0);
        msg = forwardedExt.getMessage();
        if (msg == null || msg.getBody() == null) return;
      }

      Object multiChatExtension = msg.getExtension("x", "http://jabber.org/protocol/muc#user");

      // its not for us
      if (multiChatExtension != null) return;

      String userFullId = isForwardedSentMessage ? msg.getTo() : msg.getFrom();

      String userBareID = StringUtils.parseBareAddress(userFullId);

      boolean isPrivateMessaging = false;
      ChatRoom privateContactRoom = null;
      OperationSetMultiUserChatJabberImpl mucOpSet =
          (OperationSetMultiUserChatJabberImpl)
              jabberProvider.getOperationSet(OperationSetMultiUserChat.class);
      if (mucOpSet != null) privateContactRoom = mucOpSet.getChatRoom(userBareID);

      if (privateContactRoom != null) {
        isPrivateMessaging = true;
      }

      if (logger.isDebugEnabled()) {
        if (logger.isDebugEnabled())
          logger.debug("Received from " + userBareID + " the message " + msg.toXML());
      }

      Message newMessage = createMessage(msg.getBody(), DEFAULT_MIME_TYPE, msg.getPacketID());

      // check if the message is available in xhtml
      PacketExtension ext = msg.getExtension("http://jabber.org/protocol/xhtml-im");

      if (ext != null) {
        XHTMLExtension xhtmlExt = (XHTMLExtension) ext;

        // parse all bodies
        Iterator<String> bodies = xhtmlExt.getBodies();
        StringBuffer messageBuff = new StringBuffer();
        while (bodies.hasNext()) {
          String body = bodies.next();
          messageBuff.append(body);
        }

        if (messageBuff.length() > 0) {
          // we remove body tags around message cause their
          // end body tag is breaking
          // the visualization as html in the UI
          String receivedMessage =
              messageBuff
                  .toString()
                  // removes body start tag
                  .replaceAll("\\<[bB][oO][dD][yY].*?>", "")
                  // removes body end tag
                  .replaceAll("\\</[bB][oO][dD][yY].*?>", "");

          // for some reason &apos; is not rendered correctly
          // from our ui, lets use its equivalent. Other
          // similar chars(< > & ") seem ok.
          receivedMessage = receivedMessage.replaceAll("&apos;", "&#39;");

          newMessage = createMessage(receivedMessage, HTML_MIME_TYPE, msg.getPacketID());
        }
      }

      PacketExtension correctionExtension = msg.getExtension(MessageCorrectionExtension.NAMESPACE);
      String correctedMessageUID = null;
      if (correctionExtension != null) {
        correctedMessageUID =
            ((MessageCorrectionExtension) correctionExtension).getCorrectedMessageUID();
      }

      Contact sourceContact =
          opSetPersPresence.findContactByID((isPrivateMessaging ? userFullId : userBareID));
      if (msg.getType() == org.jivesoftware.smack.packet.Message.Type.error) {
        // error which is multichat and we don't know about the contact
        // is a muc message error which is missing muc extension
        // and is coming from the room, when we try to send message to
        // room which was deleted or offline on the server
        if (isPrivateMessaging && sourceContact == null) {
          if (privateContactRoom != null) {
            XMPPError error = packet.getError();
            int errorResultCode = ChatRoomMessageDeliveryFailedEvent.UNKNOWN_ERROR;

            if (error != null && error.getCode() == 403) {
              errorResultCode = ChatRoomMessageDeliveryFailedEvent.FORBIDDEN;
            }

            String errorReason = error.getMessage();

            ChatRoomMessageDeliveryFailedEvent evt =
                new ChatRoomMessageDeliveryFailedEvent(
                    privateContactRoom, null, errorResultCode, errorReason, new Date(), newMessage);
            ((ChatRoomJabberImpl) privateContactRoom).fireMessageEvent(evt);
          }

          return;
        }

        if (logger.isInfoEnabled()) logger.info("Message error received from " + userBareID);

        int errorResultCode = MessageDeliveryFailedEvent.UNKNOWN_ERROR;
        if (packet.getError() != null) {
          int errorCode = packet.getError().getCode();

          if (errorCode == 503) {
            org.jivesoftware.smackx.packet.MessageEvent msgEvent =
                (org.jivesoftware.smackx.packet.MessageEvent)
                    packet.getExtension("x", "jabber:x:event");
            if (msgEvent != null && msgEvent.isOffline()) {
              errorResultCode = MessageDeliveryFailedEvent.OFFLINE_MESSAGES_NOT_SUPPORTED;
            }
          }
        }

        if (sourceContact == null) {
          sourceContact = opSetPersPresence.createVolatileContact(userFullId, isPrivateMessaging);
        }

        MessageDeliveryFailedEvent ev =
            new MessageDeliveryFailedEvent(
                newMessage, sourceContact, correctedMessageUID, errorResultCode);

        // ev = messageDeliveryFailedTransform(ev);

        if (ev != null) fireMessageEvent(ev);
        return;
      }
      putJidForAddress(userFullId, msg.getThread());

      // In the second condition we filter all group chat messages,
      // because they are managed by the multi user chat operation set.
      if (sourceContact == null) {
        if (logger.isDebugEnabled())
          logger.debug("received a message from an unknown contact: " + userBareID);
        // create the volatile contact
        sourceContact = opSetPersPresence.createVolatileContact(userFullId, isPrivateMessaging);
      }

      Date timestamp = new Date();
      // Check for XEP-0091 timestamp (deprecated)
      PacketExtension delay = msg.getExtension("x", "jabber:x:delay");
      if (delay != null && delay instanceof DelayInformation) {
        timestamp = ((DelayInformation) delay).getStamp();
      }
      // check for XEP-0203 timestamp
      delay = msg.getExtension("delay", "urn:xmpp:delay");
      if (delay != null && delay instanceof DelayInfo) {
        timestamp = ((DelayInfo) delay).getStamp();
      }

      ContactResource resource = ((ContactJabberImpl) sourceContact).getResourceFromJid(userFullId);

      EventObject msgEvt = null;
      if (!isForwardedSentMessage)
        msgEvt =
            new MessageReceivedEvent(
                newMessage,
                sourceContact,
                resource,
                timestamp,
                correctedMessageUID,
                isPrivateMessaging,
                privateContactRoom);
      else msgEvt = new MessageDeliveredEvent(newMessage, sourceContact, timestamp);
      // msgReceivedEvt = messageReceivedTransform(msgReceivedEvt);
      if (msgEvt != null) fireMessageEvent(msgEvt);
    }
Esempio n. 12
0
 /**
  * Obtain a Carbon from a message, if available.
  *
  * @param msg Message object to check for carbons
  * @return a Carbon if available, null otherwise.
  */
 public static CarbonExtension getCarbon(Message msg) {
   CarbonExtension cc = (CarbonExtension) msg.getExtension("received", CarbonExtension.NAMESPACE);
   if (cc == null) cc = (CarbonExtension) msg.getExtension("sent", CarbonExtension.NAMESPACE);
   return cc;
 }