Actual source code: textbelt.c
2: #include <petscwebclient.h>
4: /*@C
5: PetscTextBelt - 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 Key:
18: . -textbelt <phonenumber[,message]> - sends a message to this number when the program ends
20: Level: intermediate
22: Notes:
23: TextBelt is run for testing purposes only, please do not use this feature often
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.
28: Developer Note:
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.
33: .seealso: `PetscTellMyCell()`, `PetscOpenSocket()`, `PetscHTTPRequest()`
34: @*/
35: PetscErrorCode PetscTextBelt(MPI_Comm comm, const char number[], const char message[], PetscBool *flg)
36: {
37: size_t nlen, mlen, blen;
38: PetscMPIInt rank;
40: PetscFunctionBegin;
41: PetscCall(PetscStrlen(number, &nlen));
42: PetscCheck(nlen == 10, comm, PETSC_ERR_ARG_WRONG, "Number %s is not ten digits", number);
43: PetscCall(PetscStrlen(message, &mlen));
44: PetscCheck(mlen <= 100, comm, PETSC_ERR_ARG_WRONG, "Message %s is too long", message);
45: PetscCallMPI(MPI_Comm_rank(comm, &rank));
46: if (rank == 0) {
47: int sock;
48: char buff[474], *body;
49: PetscInt i;
51: PetscCall(PetscMalloc1(mlen + nlen + 100, &body));
52: PetscCall(PetscStrcpy(body, "number="));
53: PetscCall(PetscStrcat(body, number));
54: PetscCall(PetscStrcat(body, "&"));
55: PetscCall(PetscStrcat(body, "message="));
56: PetscCall(PetscStrcat(body, message));
57: PetscCall(PetscStrlen(body, &blen));
58: for (i = 0; i < (int)blen; i++) {
59: if (body[i] == ' ') body[i] = '+';
60: }
61: PetscCall(PetscOpenSocket("textbelt.com", 80, &sock));
62: PetscCall(PetscHTTPRequest("POST", "textbelt.com/text", NULL, "application/x-www-form-urlencoded", body, sock, buff, sizeof(buff)));
63: close(sock);
64: PetscCall(PetscFree(body));
65: if (flg) {
66: char *found;
67: PetscCall(PetscStrstr(buff, "\"success\":tr", &found));
68: *flg = found ? PETSC_TRUE : PETSC_FALSE;
69: }
70: }
71: PetscFunctionReturn(PETSC_SUCCESS);
72: }