Kannel: Open Source WAP and SMS gateway  svn-r5335
clickatell.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*----------------------------------------------------------------
58  * Clickatell - http://api.clickatell.com/
59  *
60  * Rene Kluwen <rene.kluwen@chimit.nl>
61  * Stipe Tolj <st@tolj.org>, <stolj@kannel.org>
62  */
63 
64 #include "gwlib/gwlib.h"
65 #include "smscconn.h"
66 #include "smscconn_p.h"
67 #include "bb_smscconn_cb.h"
68 #include "msg.h"
69 #include "sms.h"
70 #include "dlr.h"
71 #include "urltrans.h"
72 #include "meta_data.h"
73 
74 #include "../smsc_http_p.h"
75 
76 
77 /* MT related function */
78 static int clickatell_send_sms(SMSCConn *conn, Msg *sms)
79 {
80  ConnData *conndata = conn->data;
81  Octstr *url, *os;
82  char id[UUID_STR_LEN + 1];
83  List *headers;
84 
85  /* form the basic URL */
86  url = octstr_format("%S/sendmsg?to=%E&api_id=%E&user=%E&password=%E",
87  conndata->send_url, sms->sms.receiver,
89 
90  /* append 'from' parameter */
91  if (!conndata->no_sender)
92  octstr_format_append(url, "&from=%E", sms->sms.sender);
93 
94  /* append 'mo' parameter */
96  octstr_append_cstr(url, "&mo=1");
97 
98  /* append 'validity' parameter */
99  if (sms->sms.validity != SMS_PARAM_UNDEFINED)
100  octstr_format_append(url, "&validity=%ld", (sms->sms.validity - time(NULL)) / 60);
101 
102  /* append MD5 digest as msg ID from our UUID */
103  uuid_unparse(sms->sms.id, id);
104  os = octstr_create(id);
105  octstr_replace(os, octstr_imm("-"), octstr_imm(""));
106  octstr_format_append(url, "&cliMsgId=%E", os);
107  octstr_destroy(os);
108 
109  /* append UDH header */
110  if(octstr_len(sms->sms.udhdata)) {
111  octstr_format_append(url, "&udh=%H", sms->sms.udhdata);
112  }
113 
114  if(sms->sms.coding == DC_8BIT) {
115  octstr_format_append(url, "&data=%H&mclass=%d", sms->sms.msgdata, sms->sms.mclass);
116  } else if(sms->sms.coding == DC_UCS2) {
117  octstr_format_append(url, "&unicode=1&text=%H", sms->sms.msgdata);
118  } else {
119  octstr_format_append(url, "&text=%E", sms->sms.msgdata);
120  if (conndata->alt_charset) {
121  octstr_format_append(url, "&charset=%E", conndata->alt_charset);
122  } else {
123  octstr_append_cstr(url, "&charset=UTF-8");
124  }
125  }
126 
127  /* append 'callback' parameter */
128  if (DLR_IS_ENABLED_DEVICE(sms->sms.dlr_mask)) {
129  int callback = 0;
130  if (sms->sms.dlr_mask & DLR_BUFFERED)
131  callback |= 0x01;
132  if (sms->sms.dlr_mask & (DLR_SUCCESS | DLR_EXPIRED))
133  callback |= 0x02;
134  if (sms->sms.dlr_mask & DLR_FAIL)
135  callback |= 0x04;
136  octstr_format_append(url, "&callback=%d", callback);
137  }
138 
139  headers = http_create_empty_headers();
140  debug("smsc.http.clickatell", 0, "HTTP[%s]: Sending request <%s>",
142 
143  /*
144  * Clickatell requires optionally an SSL-enabled HTTP client call, this is handled
145  * transparently by the Kannel HTTP layer module.
146  */
147  http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers, NULL, 0, sms, NULL);
148 
150  http_destroy_headers(headers);
151 
152  return 0;
153 }
154 
155 
156 /*
157  * Parse a line in the format: ID: XXXXXXXXXXXXXXXXXX
158  * and return a Dict with the 'ID' as key and the value as value,
159  * otherwise return NULL if a parsing error occures.
160  */
162 {
163  Dict *param = NULL;
164  List *words = NULL;
165  long len;
166  Octstr *word, *value;
167 
168  words = octstr_split_words(body);
169  if ((len = gwlist_len(words)) > 1) {
170  word = gwlist_extract_first(words);
171  if (octstr_compare(word, octstr_imm("ID:")) == 0) {
172  value = gwlist_extract_first(words);
173  param = dict_create(4, (void(*)(void *)) octstr_destroy);
174  dict_put(param, octstr_imm("ID"), value);
175  } else if (octstr_compare(word, octstr_imm("ERR:")) == 0) {
176  value = gwlist_extract_first(words);
177  param = dict_create(4, (void(*)(void *)) octstr_destroy);
178  dict_put(param, octstr_imm("ERR"), value);
179  }
180  octstr_destroy(word);
181  }
182  gwlist_destroy(words, (void(*)(void *)) octstr_destroy);
183 
184  return param;
185 }
186 
187 
188 static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status,
189  List *headers, Octstr *body)
190 {
191  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
192  Dict *param;
193  Octstr *msgid;
194 
195  if ((param = clickatell_parse_body(body)) != NULL &&
196  (msgid = dict_get(param, octstr_imm("ID"))) != NULL &&
197  msgid != NULL) {
198 
199  /* SMSC ACK.. now we have the message id. */
200  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
201  dlr_add(conn->id, msgid, msg, 0);
202 
203  bb_smscconn_sent(conn, msg, NULL);
204 
205  } else {
206  error(0, "HTTP[%s]: Message was malformed or error was returned. SMSC response `%s'.",
207  octstr_get_cstr(conn->id), octstr_get_cstr(body));
209  }
210  dict_destroy(param);
211 
212  } else {
213  error(0, "HTTP[%s]: Message was rejected. SMSC response `%s'.",
214  octstr_get_cstr(conn->id), octstr_get_cstr(body));
217  }
218 }
219 
220 /* MO related function */
222  List *headers, Octstr *body, List *cgivars)
223 {
224  List *reply_headers;
225  int ret;
226  Octstr *apimsgid, *status, *timestamp, *retmsg, *dest, *charge;
227  Octstr *api_id, *from, *to, *text, *charset, *udh;
228  int httpstatus = HTTP_UNAUTHORIZED, dlrstat;
229  Msg *dlrmsg, *momsg;
230  struct tm tm;
231 
232  /* dlr parameters */
233  apimsgid = http_cgi_variable(cgivars, "apiMsgId");
234  status = http_cgi_variable(cgivars, "status");
235  /* timestamp is for both DLR & MO */
236  timestamp = http_cgi_variable(cgivars, "timestamp");
237  dest = http_cgi_variable(cgivars, "to");
238  charge = http_cgi_variable(cgivars, "charge");
239  /* MO parameters */
240  api_id = http_cgi_variable(cgivars, "api_id");
241  from = http_cgi_variable(cgivars, "from");
242  to = http_cgi_variable(cgivars, "to");
243  text = http_cgi_variable(cgivars, "text");
244  charset = http_cgi_variable(cgivars, "charset");
245  udh = http_cgi_variable(cgivars, "udh");
246 
247  debug("smsc.http.clickatell", 0, "HTTP[%s]: Received a request",
248  octstr_get_cstr(conn->id));
249 
250  if (api_id != NULL && from != NULL && to != NULL && timestamp != NULL && text != NULL && charset != NULL && udh != NULL) {
251  /* we received an MO message */
252  debug("smsc.http.clickatell", 0, "HTTP[%s]: Received MO message from %s: <%s>",
254  momsg = msg_create(sms);
255  momsg->sms.sms_type = mo;
256  momsg->sms.sender = octstr_duplicate(from);
257  momsg->sms.receiver = octstr_duplicate(to);
258  momsg->sms.msgdata = octstr_duplicate(text);
259  momsg->sms.charset = octstr_duplicate(charset);
260  momsg->sms.service = octstr_duplicate(api_id);
261  momsg->sms.binfo = octstr_duplicate(api_id);
262  momsg->sms.smsc_id = octstr_duplicate(conn->id);
263  if (octstr_len(udh) > 0) {
264  momsg->sms.udhdata = octstr_duplicate(udh);
265  }
266  strptime(octstr_get_cstr(timestamp), "%Y-%m-%d %H:%M:%S", &tm);
267  momsg->sms.time = gw_mktime(&tm);
268 
269  /* note: implicit msg_destroy */
270  ret = bb_smscconn_receive(conn, momsg);
271  httpstatus = HTTP_OK;
272  retmsg = octstr_create("Thanks");
273  } else if (apimsgid == NULL || status == NULL || timestamp == NULL || dest == NULL) {
274  error(0, "HTTP[%s]: Insufficient args.",
275  octstr_get_cstr(conn->id));
276  httpstatus = HTTP_OK;
277  retmsg = octstr_create("Insufficient arguments, rejected.");
278  } else {
279  /* we received a DLR */
280  switch (atoi(octstr_get_cstr(status))) {
281  case 1: /* message unknown */
282  case 5: /* error with message */
283  case 6: /* user cancelled message */
284  case 7: /* error delivering message */
285  case 9: /* routing error */
286  case 12: /* out of credit */
287  case 13: /* message cancelled by Clickatell */
288  case 14: /* maximum MT limit exceeded */
289  dlrstat = DLR_FAIL; /* delivery failure */
290  break;
291  case 10: /* message expired */
292  dlrstat = DLR_EXPIRED;
293  break;
294  case 2: /* message queued */
295  case 3: /* delivered to gateway */
296  case 11: /* message queued for later delivery */
297  dlrstat = DLR_BUFFERED; /* message buffered */
298  break;
299  case 4: /* received by recipient */
300  case 8: /* OK */
301  dlrstat = DLR_SUCCESS; /* message received */
302  break;
303  default: /* unknown status code */
304  dlrstat = DLR_UNKNOWN;
305  break;
306  }
307  dlrmsg = dlr_find(conn->id,
308  apimsgid, /* smsc message id */
309  dest, /* destination */
310  dlrstat, 0);
311 
312  if (dlrmsg != NULL) {
313  /* dlrmsg->sms.msgdata = octstr_duplicate(apimsgid); */
314  dlrmsg->sms.sms_type = report_mo;
315  dlrmsg->sms.time = atoi(octstr_get_cstr(timestamp));
316  if (charge) {
317  /* unsure if smsbox relays the binfo field to dlrs.
318  But it is here in case they will start to do it. */
319  dlrmsg->sms.binfo = octstr_duplicate(charge);
320  }
321 
322  ret = bb_smscconn_receive(conn, dlrmsg);
323  httpstatus = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
324  retmsg = octstr_create("Sent");
325  } else {
326  error(0,"HTTP[%s]: got DLR but could not find message or was not interested "
327  "in it id<%s> dst<%s>, type<%d>",
328  octstr_get_cstr(conn->id), octstr_get_cstr(apimsgid),
329  octstr_get_cstr(dest), dlrstat);
330  httpstatus = HTTP_OK;
331  retmsg = octstr_create("Thanks");
332  }
333  }
334 
335  reply_headers = gwlist_create();
336  http_header_add(reply_headers, "Content-Type", "text/plain");
337  debug("smsc.http.clickatell", 0, "HTTP[%s]: Sending reply `%s'.",
338  octstr_get_cstr(conn->id), octstr_get_cstr(retmsg));
339  http_send_reply(client, httpstatus, reply_headers, retmsg);
340 
341  octstr_destroy(retmsg);
342  http_destroy_headers(reply_headers);
343 }
344 
347  .parse_reply = clickatell_parse_reply,
348  .receive_sms = clickatell_receive_sms,
349 };
350 
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
static void clickatell_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: clickatell.c:221
void error(int err, const char *fmt,...)
Definition: log.c:648
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2649
Octstr * alt_charset
Definition: smsc_http_p.h:103
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2886
int(* send_sms)(SMSCConn *conn, Msg *msg)
Definition: smsc_http_p.h:76
Definition: msg.h:106
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
long gwlist_len(List *list)
Definition: list.c:166
void * data
Definition: smscconn_p.h:250
static void client(int port)
Definition: test_udp.c:77
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
#define DLR_EXPIRED
Definition: dlr.h:77
struct smsc_http_fn_callbacks smsc_http_clickatell_callback
Definition: clickatell.c:345
#define DC_8BIT
Definition: sms.h:111
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
#define msg_create(type)
Definition: msg.h:136
Octstr * system_id
Definition: smsc_http_p.h:107
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
Msg * dlr_find(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int typ, int use_dst)
Definition: dlr.c:387
static Dict * clickatell_parse_body(Octstr *body)
Definition: clickatell.c:161
void dlr_add(const Octstr *smsc, const Octstr *ts, Msg *msg, int use_dst)
Definition: dlr.c:330
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Octstr * charset
Definition: test_ota.c:68
Octstr * http_cgi_variable(List *list, char *name)
Definition: http.c:2836
void http_destroy_headers(List *headers)
Definition: http.c:2879
#define DLR_SUCCESS
Definition: dlr.h:72
static Octstr * from
Definition: mtbatch.c:95
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1760
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2695
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: http.h:142
Definition: msg.h:79
void * gwlist_extract_first(List *list)
Definition: list.c:305
time_t gw_mktime(struct tm *tm)
Definition: protected.c:153
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:477
char * text
Definition: smsc_cimd2.c:921
List * http_create_empty_headers(void)
Definition: http.c:2872
Definition: dict.c:116
#define octstr_duplicate(ostr)
Definition: octstr.h:187
static int clickatell_send_sms(SMSCConn *conn, Msg *sms)
Definition: clickatell.c:78
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1602
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2464
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
#define SMS_PARAM_UNDEFINED
Definition: sms.h:91
int no_sender
Definition: smsc_http_p.h:99
static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: clickatell.c:188
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
void dict_destroy(Dict *dict)
Definition: dict.c:215
Definition: octstr.c:118
void bb_smscconn_sent(SMSCConn *conn, Msg *sms, Octstr *reply)
Definition: bb_smscconn.c:281
Octstr * password
Definition: smsc_http_p.h:98
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
void octstr_format_append(Octstr *os, const char *fmt,...)
Definition: octstr.c:2507
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http_p.h:97
#define DLR_UNKNOWN
Definition: dlr.h:78
#define DLR_BUFFERED
Definition: dlr.h:74
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:329
Octstr * send_url
Definition: smsc_http_p.h:93
static Octstr * url
Definition: test_xmlrpc.c:84
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
Definition: list.c:102
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
#define DLR_FAIL
Definition: dlr.h:73
#define DC_UCS2
Definition: sms.h:112
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
int mobile_originated
Definition: smsc_http_p.h:108
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.