Beispiel #1
0
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) {
    Datastore.instance().startRequest();
    try {
      MimeMessage mime = null;
      try {
        mime =
            new MimeMessage(
                Session.getDefaultInstance(new Properties(), null), req.getInputStream());
      } catch (Exception e) {
        LOG.log(Level.SEVERE, "error while parsing incoming email", e);
        return;
      }

      EmailMessage email = parseToBetterFormat(mime);
      if (email == null) {
        // parseToBetterFormat already logged why the email couldn't be parsed
        return;
      }

      for (InternetAddress ia : email.to) {
        Message msg = extractPchappMessageFromEmail(email, ia);
        if (msg != null) {
          Command.getCommandHandler(msg).doCommand(msg);
          msg.user.maybeMarkAsSeen();
        }
      }
    } finally {
      Datastore.instance().endRequest();
    }
  }
Beispiel #2
0
  public Message extractPchappMessageFromEmail(EmailMessage email, InternetAddress to) {
    String recipient = to.getAddress();
    // HACK
    if (recipient.equals("*****@*****.**")) {
      recipient = "*****@*****.**";
    }
    if (!recipient.endsWith(Configuration.mailDomain)) {
      LOG.log(Level.SEVERE, "ignoring incoming email with unrecognized domain in to: " + recipient);
      return null;
    }
    String channelName = recipient.split("@")[0];

    Channel channel = Datastore.instance().getChannelByName(channelName);
    if (channel == null) {
      LOG.warning("unknown channel " + channelName + " from email sent to " + recipient);
      return null;
    }

    String memberPhoneNumber = tryExtractPhoneNumber(email.from.getAddress());
    if (memberPhoneNumber == null) {
      Member member = channel.getMemberByJID(new JID(email.from.getAddress()));
      if (member == null) {
        LOG.warning(
            email.from
                + " attempted to email channel "
                + channelName
                + "but they're not in that channel. Dropping on the floor.");
        return null;
      }
      String content = "";
      if (!Strings.isNullOrEmpty(email.subject)) {
        content += "Subject: " + email.subject;
      }
      if (!Strings.isNullOrEmpty(email.body)) {
        content += " / Body: " + email.body;
      }

      return new Message.Builder()
          .setContent(content)
          .setUserJID(new JID(member.getJID()))
          .setUser(Datastore.instance().getOrCreateUser(email.from.getAddress()))
          .setServerJID(channel.serverJID())
          .setChannel(channel)
          .setMember(member)
          .setMessageType(MessageType.EMAIL)
          .build();
    } else {
      // member might be null if we don't know the phone number
      Member member = channel.getMemberByPhoneNumber(memberPhoneNumber);
      if (member == null) {
        LOG.severe(
            "Got an SMS from "
                + memberPhoneNumber
                + " but I don't know what channel they're in. Dropping on floor.");
      }

      // GV emails have a --\nGoogle Voice footer, so try and look for that.
      int end = email.body.indexOf("\n--\n");
      String content = null;
      if (end != -1) {
        content = email.body.substring(0, end).trim();
      } else {
        content = email.body;
      }

      return new Message.Builder()
          .setContent(content)
          .setUserJID(member != null ? new JID(member.getJID()) : null)
          .setUser(Datastore.instance().getUserByPhoneNumber(memberPhoneNumber))
          .setServerJID(channel.serverJID())
          .setChannel(channel)
          .setMember(member)
          .setMessageType(MessageType.SMS)
          .build();
    }
  }