Actual source code: textbelt.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: #include <petscwebclient.h>

  6: /*@C
  7:      PetscTextBelt - Sends an SMS to an American phone number

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

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

 16:    Output Parameter:
 17: .   flg - PETSC_TRUE if the text was sent

 19:    Level: intermediate

 21:    Notes: TextBelt is run for testing purposes only, please do not use this feature often

 23: @*/
 24: PetscErrorCode PetscTextBelt(MPI_Comm comm,const char number[],const char message[],PetscBool *flg)
 25: {
 27:   size_t         nlen,mlen,blen;
 28:   PetscMPIInt    rank;

 31:   PetscStrlen(number,&nlen);
 32:   if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number);
 33:   PetscStrlen(message,&mlen);
 34:   if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message  %s is too long",message);
 35:   MPI_Comm_rank(comm,&rank);
 36:   if (!rank) {
 37:     int       sock;
 38:     char      buff[186],*body;
 39:     PetscInt  i;

 41:     PetscMalloc1(mlen+nlen+100,&body);
 42:     PetscStrcpy(body,"number=");
 43:     PetscStrcat(body,number);
 44:     PetscStrcat(body,"&");
 45:     PetscStrcat(body,"message=");
 46:     PetscStrcat(body,message);
 47:     PetscStrlen(body,&blen);
 48:     for (i=0; i<(int)blen; i++) {
 49:       if (body[i] == ' ') body[i] = '+';
 50:     }
 51:     PetscOpenSocket("textbelt.com",80,&sock);
 52:     PetscHTTPRequest("POST","textbelt.com/text",NULL,"application/x-www-form-urlencoded",body,sock,buff,sizeof(buff));
 53:     close(sock);
 54:     PetscFree(body);
 55:     if (flg) {
 56:       char *found;
 57:       PetscStrstr(buff,"\"success\":tr",&found);
 58:       *flg = found ? PETSC_TRUE : PETSC_FALSE;
 59:     }
 60:   }
 61:   return(0);
 62: }