1
2 #include <petscwebclient.h>
3
4 /*@C
5 PetscTextBelt - 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 . -textbelt <phonenumber[,message]>
19
20 Level: intermediate
21
22 Notes:
23 TextBelt is run for testing purposes only, please do not use this feature often
24
25 As of November 2016 this service does not seem to be actually transmitting the SMS, which is unfortunate since it is such a great service. Consider
26 registering and using PetscTellMyCell() instead. Or email us with other alternatives we might add or make a pull request.
27
28 Developer Notes:
29 I do not know how to make the buff[] long enough to receive the "success" string but short enough that the code does not hang
30 waiting for part of the message to arrive that does not exist, hence the success flg may be improperly set to false even
31 though the message was delivered.
32
33 .seealso: PetscTellMyCell(), PetscOpenSocket(), PetscHTTPRequest()
34 @*/
PetscTextBelt(MPI_Comm comm,const char number[],const char message[],PetscBool * flg)35 PetscErrorCode PetscTextBelt(MPI_Comm comm,const char number[],const char message[],PetscBool *flg)
36 {
37 PetscErrorCode ierr;
38 size_t nlen,mlen,blen;
39 PetscMPIInt rank;
40
41 PetscFunctionBegin;
42 ierr = PetscStrlen(number,&nlen);CHKERRQ(ierr);
43 if (nlen != 10) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Number %s is not ten digits",number);
44 ierr = PetscStrlen(message,&mlen);CHKERRQ(ierr);
45 if (mlen > 100) SETERRQ1(comm,PETSC_ERR_ARG_WRONG,"Message %s is too long",message);
46 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
47 if (!rank) {
48 int sock;
49 char buff[474],*body;
50 PetscInt i;
51
52 ierr = PetscMalloc1(mlen+nlen+100,&body);CHKERRQ(ierr);
53 ierr = PetscStrcpy(body,"number=");CHKERRQ(ierr);
54 ierr = PetscStrcat(body,number);CHKERRQ(ierr);
55 ierr = PetscStrcat(body,"&");CHKERRQ(ierr);
56 ierr = PetscStrcat(body,"message=");CHKERRQ(ierr);
57 ierr = PetscStrcat(body,message);CHKERRQ(ierr);
58 ierr = PetscStrlen(body,&blen);CHKERRQ(ierr);
59 for (i=0; i<(int)blen; i++) {
60 if (body[i] == ' ') body[i] = '+';
61 }
62 ierr = PetscOpenSocket("textbelt.com",80,&sock);CHKERRQ(ierr);
63 ierr = PetscHTTPRequest("POST","textbelt.com/text",NULL,"application/x-www-form-urlencoded",body,sock,buff,sizeof(buff));CHKERRQ(ierr);
64 close(sock);
65 ierr = PetscFree(body);CHKERRQ(ierr);
66 if (flg) {
67 char *found;
68 ierr = PetscStrstr(buff,"\"success\":tr",&found);CHKERRQ(ierr);
69 *flg = found ? PETSC_TRUE : PETSC_FALSE;
70 }
71 }
72 PetscFunctionReturn(0);
73 }
74