Actual source code: tellmycell.c


  2: #include <petscwebclient.h>

  4: /*@C
  5:      PetscTellMyCell - Sends an SMS to an American/Canadian phone number

  7:    Not collective, only the first process in `MPI_Comm` does anything

  9:    Input Parameters:
 10: +  comm - the MPI communicator
 11: .  number - the 10 digit telephone number
 12: -  message - the message

 14:    Output Parameter:
 15: .   flg - `PETSC_TRUE` if the text was sent

 17:    Options Database Keys:
 18: +   -tellmycell <number[,message]> - send a message to the give number when the program ends
 19: .   -tellmycell_user <Username> - this value is created when registering at tellmycell.com
 20: -   -tellmycell_password <Password> - this value is created when registering at tellmycell.com

 22:    Level: intermediate

 24:    Notes:
 25:     You must register for an account at tellmycell.com (you get 10 free texts with registration)

 27:    You must provide -tellmycell_user <Username> and -tellmycell_password <Password> in the options database

 29:    It would be nice to provide this as a free service but that would require making the PETSc TellMyCell password public.

 31:    Developer Note:
 32:     Perhaps the Username and Password should be arguments to this function.

 34: .seealso: `PetscTextBelt()`, `PetscHTTPSRequest()`, `PetscHTTPSConnect()`, `PetscSSLInitializeContext()`
 35: @*/
 36: PetscErrorCode PetscTellMyCell(MPI_Comm comm, const char number[], const char message[], PetscBool *flg)
 37: {
 38:   size_t      nlen, mlen, blen;
 39:   PetscMPIInt rank;
 40:   char        Username[64], Password[64];

 42:   PetscStrlen(number, &nlen);
 44:   PetscStrlen(message, &mlen);
 46:   MPI_Comm_rank(comm, &rank);
 47:   if (rank == 0) {
 48:     int       sock;
 49:     char      buff[1000], *body;
 50:     PetscInt  i;
 51:     SSL_CTX  *ctx;
 52:     SSL      *ssl;
 53:     PetscBool set;

 55:     PetscOptionsGetString(NULL, NULL, "-tellmycell_user", Username, sizeof(Username), &set);
 57:     PetscOptionsGetString(NULL, NULL, "-tellmycell_password", Password, sizeof(Password), &set);
 59:     PetscMalloc1(mlen + nlen + 100, &body);
 60:     PetscStrcpy(body, "User=");
 61:     PetscStrcat(body, Username);
 62:     PetscStrcat(body, "&Password=");
 63:     PetscStrcat(body, Password);
 64:     PetscStrcat(body, "&PhoneNumbers[]=");
 65:     PetscStrcat(body, number);
 66:     PetscStrcat(body, "&");
 67:     PetscStrcat(body, "Message=");
 68:     PetscStrcat(body, message);
 69:     PetscStrlen(body, &blen);
 70:     for (i = 0; i < (int)blen; i++) {
 71:       if (body[i] == ' ') body[i] = '+';
 72:     }
 73:     PetscSSLInitializeContext(&ctx);
 74:     PetscHTTPSConnect("app.tellmycell.com", 443, ctx, &sock, &ssl);
 75:     PetscHTTPSRequest("POST", "app.tellmycell.com/sending/messages?format=json", NULL, "application/x-www-form-urlencoded", body, ssl, buff, sizeof(buff));
 76:     PetscSSLDestroyContext(ctx);
 77:     close(sock);
 78:     PetscFree(body);
 79:     if (flg) {
 80:       char *found;
 81:       PetscStrstr(buff, "\"success\":tr", &found);
 82:       *flg = found ? PETSC_TRUE : PETSC_FALSE;
 83:     }
 84:   }
 85:   return 0;
 86: }