Actual source code: tellmycell.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  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:
 18: +   -tellmycell <number[,message]>
 19: .   -tellmycell_user <Username> set when registering at tellmycell.com
 20: -   -tellmycell_password <Password> set when registering at tellmycell.com

 22:    Level: intermediate

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

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

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

 30:    Developer Notes:  Perhaps the Username and Password should be arguments to this function.

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

 42:   PetscStrlen(number,&nlen);
 43:   if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number);
 44:   PetscStrlen(message,&mlen);
 45:   if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message  %s is too long",message);
 46:   MPI_Comm_rank(comm,&rank);
 47:   if (!rank) {
 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);
 56:     if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"You must pass in a tellmycell user name with -tellmycell_user <Username>");
 57:     PetscOptionsGetString(NULL,NULL,"-tellmycell_password",Password,sizeof(Password),&set);
 58:     if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"You must pass in a tellmycell password with -tellmycell_password <Password>");
 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: }