1 
2 #include <petscwebclient.h>
3 
4 /*@C
5      PetscTellMyCell - Sends an SMS to an American/Canadian phone number
6 
7    Not collective, only the first process in MPI_Comm does anything
8 
9    Input Parameters:
10 +  comm - the MPI communicator
11 .  number - the 10 digit telephone number
12 -  message - the message
13 
14    Output Parameter:
15 .   flg - PETSC_TRUE if the text was sent
16 
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
21 
22    Level: intermediate
23 
24    Notes:
25     You must register for an account at tellmycell.com (you get 10 free texts with registration)
26 
27    You must provide -tellmycell_user <Username> and -tellmycell_password <Password> in the options database
28 
29    It would be nice to provide this as a free service but that would require making the PETSc TellMyCell password public.
30 
31    Developer Notes:
32     Perhaps the Username and Password should be arguments to this function.
33 
34 .seealso: PetscTextBelt(), PetscHTTPSRequest(), PetscHTTPSConnect(), PetscSSLInitializeContext()
35 @*/
PetscTellMyCell(MPI_Comm comm,const char number[],const char message[],PetscBool * flg)36 PetscErrorCode PetscTellMyCell(MPI_Comm comm,const char number[],const char message[],PetscBool *flg)
37 {
38   PetscErrorCode ierr;
39   size_t         nlen,mlen,blen;
40   PetscMPIInt    rank;
41   char           Username[64],Password[64];
42 
43   PetscFunctionBegin;
44   ierr = PetscStrlen(number,&nlen);CHKERRQ(ierr);
45   if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number);
46   ierr = PetscStrlen(message,&mlen);CHKERRQ(ierr);
47   if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message  %s is too long",message);
48   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
49   if (!rank) {
50     int       sock;
51     char      buff[1000],*body;
52     PetscInt  i;
53     SSL_CTX   *ctx;
54     SSL       *ssl;
55     PetscBool set;
56 
57     ierr = PetscOptionsGetString(NULL,NULL,"-tellmycell_user",Username,sizeof(Username),&set);CHKERRQ(ierr);
58     if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"You must pass in a tellmycell user name with -tellmycell_user <Username>");
59     ierr = PetscOptionsGetString(NULL,NULL,"-tellmycell_password",Password,sizeof(Password),&set);CHKERRQ(ierr);
60     if (!set) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"You must pass in a tellmycell password with -tellmycell_password <Password>");
61     ierr = PetscMalloc1(mlen+nlen+100,&body);CHKERRQ(ierr);
62     ierr = PetscStrcpy(body,"User=");CHKERRQ(ierr);
63     ierr = PetscStrcat(body,Username);CHKERRQ(ierr);
64     ierr = PetscStrcat(body,"&Password=");CHKERRQ(ierr);
65     ierr = PetscStrcat(body,Password);CHKERRQ(ierr);
66     ierr = PetscStrcat(body,"&PhoneNumbers[]=");CHKERRQ(ierr);
67     ierr = PetscStrcat(body,number);CHKERRQ(ierr);
68     ierr = PetscStrcat(body,"&");CHKERRQ(ierr);
69     ierr = PetscStrcat(body,"Message=");CHKERRQ(ierr);
70     ierr = PetscStrcat(body,message);CHKERRQ(ierr);
71     ierr = PetscStrlen(body,&blen);CHKERRQ(ierr);
72     for (i=0; i<(int)blen; i++) {
73       if (body[i] == ' ') body[i] = '+';
74     }
75     ierr = PetscSSLInitializeContext(&ctx);CHKERRQ(ierr);
76     ierr = PetscHTTPSConnect("app.tellmycell.com",443,ctx,&sock,&ssl);CHKERRQ(ierr);
77     ierr = PetscHTTPSRequest("POST","app.tellmycell.com/sending/messages?format=json",NULL,"application/x-www-form-urlencoded",body,ssl,buff,sizeof(buff));CHKERRQ(ierr);
78     ierr = PetscSSLDestroyContext(ctx);CHKERRQ(ierr);
79     close(sock);
80     ierr = PetscFree(body);CHKERRQ(ierr);
81     if (flg) {
82       char *found;
83       ierr = PetscStrstr(buff,"\"success\":tr",&found);CHKERRQ(ierr);
84       *flg = found ? PETSC_TRUE : PETSC_FALSE;
85     }
86   }
87   PetscFunctionReturn(0);
88 }
89