private void doProcessPacket(Packet packet) {
      final Message message = ((Message) packet);
      if (message.getType() == Message.Type.ERROR) {
        UIUtil.invokeLater(
            () -> {
              String from =
                  (message.getFrom() != null) ? getMsg("from.0.lf", message.getFrom()) : "";
              LOG.warn(
                  getMsg(
                      "jabber.error.text",
                      from,
                      (message.getError() == null ? "N/A" : message.getError().toString())));
            });
        return;
      }

      if (myIgnoreList.isIgnored(packet.getFrom())) {
        return;
      }

      Element element = null;
      for (PacketExtension o : message.getExtensions()) {
        if (o instanceof JDOMExtension) {
          element = ((JDOMExtension) o).getElement();
        }
      }

      if (element != null && !RESPONSE.equals(element.getName())) {
        processAndSendResponse(element, message);
      } else if (element == null && message.getBody() != null) {
        // Some simple Jabber Message
        MessageEvent event =
            EventFactory.createMessageEvent(
                JabberTransport.this, getFrom(message), message.getBody());
        if (message.getThread() != null) {
          myUser2Thread.put(getFrom(message), message.getThread());
        }

        getBroadcaster().fireEvent(event);
      }
    }
  private static void processResponse(XmlMessage xmlMessage, PacketCollector collector) {
    boolean gotResponse = false;

    while (!gotResponse) {

      Message response = (Message) collector.nextResult(RESPONSE_TIMEOUT);
      if (response == null) break;

      final Collection<PacketExtension> extensions = response.getExtensions();
      for (PacketExtension o : extensions) {
        if (o instanceof JDOMExtension) {
          JDOMExtension extension = (JDOMExtension) o;
          if (RESPONSE.equals(extension.getElement().getName())) {
            xmlMessage.processResponse(extension.getElement());
            gotResponse = true;
            break;
          }
        }
      }
    }
  }
Ejemplo n.º 3
0
 /** Handles "message" stanzas. */
 private void handleMessage(Message message) {
   for (PacketExtension ext : message.getExtensions())
     if (ext instanceof LogPacketExtension)
       handleLogRequest((LogPacketExtension) ext, message.getFrom());
 }
  /*
   * Handle a single smack packet, discarding anything but Message.
   * @param packet The smack packet.
   * (non-Javadoc)
   * @see org.jivesoftware.smack.PacketListener#processPacket(org.jivesoftware.smack.packet.Packet)
   */
  @Override
  public void processPacket(Packet packet) {

    Log.i("processPacket", packet.toXML());
    if (packet instanceof Presence) {

      double inLat = 0, inLong = 0;
      boolean isMUC = false, isGEO = false;

      Presence presence = (Presence) packet;

      // In a MUC, the "FROM" is the Resource
      String presenceFrom = StringUtils.parseResource(presence.getFrom());
      String presenceTo = StringUtils.parseName(presence.getTo());

      // if presence packet is from yourself, just bail
      if (presenceFrom.equals(presenceTo)) return;

      for (PacketExtension extension : presence.getExtensions()) {
        if (extension instanceof GeoLoc) {
          GeoLoc loc = (GeoLoc) extension;

          inLat = loc.getLat();
          inLong = loc.getLon();

          isGEO = true;

          Log.d("CNL", "ERIK: GEOLOC EXTENSION FOUND, LAT: " + inLat);
        }
        if (extension instanceof MUCUser) {
          // MUCUser muc = (MUCUser) extension;   no need to create this object
          isMUC = true;

          Log.d("CNL", "ERIK: MUC EXTENSION FOUND, presence type=" + presence.getType());
        }
      }

      //  If a MUC available presence packet comes in, add/update database
      if (isMUC == true && presence.getType().toString().equals("available") && isGEO == true) {

        updateDatabase(presenceFrom, inLat, inLong, null);
      }

      // if a MUC Unavailable presence packet comes in, remove user from database
      else if (isMUC == true && presence.getType().toString().equals("unavailable")) {

        if (this.database.delete("user_info", "name='" + presenceFrom + "'", null) > 0) {
          Log.d("CNL", "ERIK: DATABASE UPDATED, USER " + presenceFrom + " DELETED");
        } else Log.d("CNL", "ERIK: DATABASE SEARCHED, USER " + presenceFrom + " NOT FOUND");
      }
    }

    if (packet instanceof IQ) {
      IQ iq = (IQ) packet;
      Log.d("CNL", "ERIK: IQ PACKET RECEIVED: " + iq.getExtensions());
    }

    if (packet instanceof Message) {
      Message msg = (Message) packet;
      String text = msg.getBody();

      if (text == null || text.trim().length() == 0) {
        Log.d("CNL", "ERIK: MESSAGE PACKET LACKS A MESSAGE!!!");
        return;
      }

      // Extract name
      String messageFrom = StringUtils.parseResource(msg.getFrom());

      // Extract lat and lon from message
      double inLat = getLat(msg);
      double inLon = getLon(msg);
      Boolean isEmergency = null;
      if (text.startsWith(this.context.getString(R.string.emergency_message))) {
        isEmergency = Boolean.TRUE;
      } else if (text.startsWith(this.context.getString(R.string.cancel_message))) {
        isEmergency = Boolean.FALSE;
      }

      Log.i("CNL", "recovered name=[" + messageFrom + "], lat/lon=" + inLat + "," + inLon);

      updateDatabase(messageFrom, inLat, inLon, isEmergency);

      String bareFrom = XMPPUtils.getBareJid(msg.getFrom());
      String msgFrom = StringUtils.parseResource(msg.getFrom());
      String bareTo = XMPPUtils.getBareJid(msg.getTo());
      String msgTo = StringUtils.parseName(msg.getTo());

      if (msg.getType().toString().equals("groupchat")) {
        Log.d(
            "CNL",
            "ERIK: MUC MESSAGE PACKET RECEIVED, CONTAINS EXTENSIONS: " + msg.getExtensions());
        if (msgFrom.equals(msgTo)) return;

        // Picture receiving code here.............................!!!!!!!!!!

        for (PacketExtension extension : msg.getExtensions()) {
          if (extension instanceof DataPacketExtension) {
            DataPacketExtension data = (DataPacketExtension) extension;

            byte[] imageBytes = data.getDecodedData();
            String imagePath = Environment.getExternalStorageDirectory() + "/mmmc/";

            // String imageName = text;
            File f = new File(imagePath, text);
            OutputStream out = null;

            try {
              out = new BufferedOutputStream(new FileOutputStream(f));
              out.write(imageBytes);
            } catch (IOException ioe) {
              ioe.printStackTrace();
            } finally {
              if (out != null) {
                try {
                  out.close();
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            }

            // Log.d("CNL", "ERIK: MUC EXTENSION FOUND");
            ContentValues values = new ContentValues();
            values.put("ts", System.currentTimeMillis());
            values.put("jid", bareFrom);

            // don't put who it's from, put the resource it came from (user in a MUC)
            values.put("src", msgFrom);

            values.put("dst", msg.getTo());
            values.put("via", bareTo);
            values.put("msg", "Picture received: " + text.trim());
            this.database.insert("msg", "_id", values);

            Log.d("CNL", "ERIK: DATA EXTENSION FOUND, IMAGE SAVED");
            return;
          }
        }

        // Log.d("CNL", "ERIK: MUC EXTENSION FOUND");

        // Insert new message into database
        ContentValues values = new ContentValues();
        values.put("ts", System.currentTimeMillis());
        values.put("jid", bareFrom);

        // don't put who it's from, put the resource it came from (user in a MUC)
        values.put("src", msgFrom);

        values.put("dst", msg.getTo());
        values.put("via", bareTo);
        values.put("msg", text.trim());
        this.database.insert("msg", "_id", values);
      } else Log.d("CNL", "ERIK: NON-MUC MESSAGE PACKET RECEIVED: " + bareFrom);

      Builder builder = new Uri.Builder();
      builder.scheme("content");
      builder.authority("jabber-chat-db");
      builder.appendPath(bareTo);
      builder.appendPath(bareFrom);
      this.context.getContentResolver().notifyChange(builder.build(), null);
      setNotification(bareFrom, bareTo);
    }
  }