Actual source code: textbelt.c

petsc-3.5.4 2015-05-23
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:    Notes: TextBelt is run for testing purposes only, please do not use this feature often

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

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

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