Exemplo n.º 1
0
 /* validate the syntax of the specified list of multiple email addresses */
 public static boolean validateAddresses(String addrs, boolean acceptSMS) {
   if (StringTools.isBlank(addrs)) {
     // blank is ok in this case
     return true;
   } else if (acceptSMS) {
     // allow "sms:123456789" format
     String addrArry[] = StringTools.parseStringArray(addrs, ',');
     if (addrArry.length == 0) {
       return false;
     }
     for (int i = 0; i < addrArry.length; i++) {
       String em = addrArry[i].trim();
       if (StringTools.isBlank(em)) {
         // individual addresses not allowed
         return false;
       } else if (SMSOutboundGateway.StartsWithSMS(em)) {
         // TODO: for now, accept as-is
       } else if (!validateAddress(em)) {
         return false;
       }
     }
     return true;
   } else {
     // true email addresses only
     try {
       return SendMail.validateAddresses(addrs);
     } catch (Throwable t) { // NoClassDefFoundException, ClassNotFoundException
       // this will fail if JavaMail support for SendMail is not available.
       Print.logError("*** SendMail error: " + t);
       return false;
     }
   }
 }
Exemplo n.º 2
0
  /** ** Initialize outbound SMS gateway handlers */
  public static void _startupInit() {
    Print.logDebug("SMSOutboundGateway initializing ...");
    final String SMSKey_ = SMSOutboundGateway.PROP_SmsGatewayHandler_;

    /* already initialized? */
    if (SmsGatewayHandlerMap != null) {
      return;
    }

    // -----------------------------------------------
    // The following shows several example of outbound SMS gateway support.
    // The only method that needs to be overridden and implemented is
    //   public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr)
    // The "device" is the Device record instance to which the SMS message should be sent,
    // and "commandStr" is the SMS text (device command) which is to be sent to the device.
    // -----------------------------------------------

    /* standard "Body" command */
    // Property:
    //   [email protected]
    // Notes:
    //   This outbound SMS method sends the SMS text in an email message body to the device
    //   "smsEmailAddress".  If the device "smsEmailAddress" is blank, then the "To" email
    //   address is constructed from the device "simPhoneNumber" and the email address
    //   specified on the property "SmsGatewayHandler.emailBody.smsEmailAddress".
    SMSOutboundGateway.AddSMSGateway(
        "emailBody",
        new SMSOutboundGateway() {
          public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) {
            if (device == null) {
              return DCServerFactory.ResultCode.INVALID_DEVICE;
            }
            String frEmail = this.getFromEmailAddress(device);
            String toEmail = this.getSmsEmailAddress(device);
            if (StringTools.isBlank(toEmail)) {
              String smsEmail =
                  this.getStringProperty(device, SMSKey_ + "emailBody.smsEmailAddress", "");
              toEmail =
                  smsEmail.startsWith("@") ? (device.getSimPhoneNumber() + smsEmail) : smsEmail;
            }
            return this.sendEmail(frEmail, toEmail, "", commandStr);
          }
        });

    /* standard "Subject" command */
    // Property:
    //   SmsGatewayHandler.emailSubject.smsEmailAddress=
    // Notes:
    //   This outbound SMS method sends the SMS text in an email message subject to the device
    //   "smsEmailAddress".  If the device "smsEmailAddress" is blank, then the "To" email
    //   address is constructed from the device "simPhoneNumber" and the email address
    //   specified on the property "SmsGatewayHandler.emailSubject.smsEmailAddress".
    SMSOutboundGateway.AddSMSGateway(
        "emailSubject",
        new SMSOutboundGateway() {
          public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) {
            if (device == null) {
              return DCServerFactory.ResultCode.INVALID_DEVICE;
            }
            String frEmail = this.getFromEmailAddress(device);
            String toEmail = this.getSmsEmailAddress(device);
            if (StringTools.isBlank(toEmail)) {
              String smsEmail =
                  this.getStringProperty(device, SMSKey_ + "emailSubject.smsEmailAddress", "");
              toEmail =
                  smsEmail.startsWith("@") ? (device.getSimPhoneNumber() + smsEmail) : smsEmail;
            }
            return this.sendEmail(frEmail, toEmail, commandStr, "");
          }
        });

    /* HTTP SMS */
    // Property:
    //
    // SmsGatewayHandler.httpURL.url=http://localhost:12345/smsredirector/sendsms?flash=0&acctuser=user&tracking_Pwd=pass&source=5551212&destination=${mobile}&message=${message}
    //
    // SmsGatewayHandler.httpURL.url=http://localhost:12345/sendsms?user=user&pass=pass&source=5551212&dest=${mobile}&text=${message}
    // Notes:
    //   This outbound SMS method sends the SMS text in an HTTP "GET" request to the URL
    //   specified on the property "SmsGatewayHandler.httpURL.url".  The following replacement
    //   variables may be specified in the URL string:
    //      ${mobile}  - replaced with the Device "simPhoneNumber" field contents
    //      ${message} - replaced with the SMS text/command to be sent to the device.
    //   It is expected that the server handling the request understands how to parse and
    //   interpret the various fields in the URL.
    SMSOutboundGateway.AddSMSGateway(
        "httpURL",
        new SMSOutboundGateway() {
          public DCServerFactory.ResultCode sendSMSCommand(Device device, String commandStr) {
            if (device == null) {
              return DCServerFactory.ResultCode.INVALID_DEVICE;
            }
            String KeyURL = SMSKey_ + "httpURL.url";
            String mobile = URIArg.encodeArg(device.getSimPhoneNumber());
            String message = URIArg.encodeArg(commandStr);
            String httpURL = this.getStringProperty(device, KeyURL, "");
            if (StringTools.isBlank(httpURL)) {
              Print.logWarn("'" + KeyURL + "' not specified");
              return DCServerFactory.ResultCode.INVALID_SMS;
            }
            httpURL = StringTools.replace(httpURL, "${mobile}", mobile);
            httpURL = StringTools.replace(httpURL, "${message}", message);
            try {
              Print.logDebug("SMS Gateway URL: " + httpURL);
              byte response[] = HTMLTools.readPage_GET(httpURL, 10000);
              // TODO: check response?
              return DCServerFactory.ResultCode.SUCCESS;
            } catch (UnsupportedEncodingException uee) {
              Print.logError("URL Encoding: " + uee);
              return DCServerFactory.ResultCode.TRANSMIT_FAIL;
            } catch (NoRouteToHostException nrthe) {
              Print.logError("Unreachable Host: " + httpURL);
              return DCServerFactory.ResultCode.UNKNOWN_HOST;
            } catch (UnknownHostException uhe) {
              Print.logError("Unknown Host: " + httpURL);
              return DCServerFactory.ResultCode.UNKNOWN_HOST;
            } catch (FileNotFoundException fnfe) {
              Print.logError("Invalid URL (not found): " + httpURL);
              return DCServerFactory.ResultCode.INVALID_SMS;
            } catch (MalformedURLException mue) {
              Print.logError("Invalid URL (malformed): " + httpURL);
              return DCServerFactory.ResultCode.INVALID_SMS;
            } catch (Throwable th) {
              Print.logError("HTML SMS error: " + th);
              return DCServerFactory.ResultCode.TRANSMIT_FAIL;
            }
          }
        });
  }