Actual source code: tellmycell.c

petsc-3.13.6 2020-09-29
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:
 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 Notes:
 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: {
 39:   size_t         nlen,mlen,blen;
 40:   PetscMPIInt    rank;
 41:   char           Username[64],Password[64];

 44:   PetscStrlen(number,&nlen);
 45:   if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number);
 46:   PetscStrlen(message,&mlen);
 47:   if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message  %s is too long",message);
 48:   MPI_Comm_rank(comm,&rank);
 49:   if (!rank) {
 50:     int       sock;
 51:     char      buff[1000],*body;
 52:     PetscInt  i;
 53:     SSL_CTX   *ctx;
 54:     SSL       *ssl;
 55:     PetscBool set;

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