Kannel: Open Source WAP and SMS gateway  svn-r5262
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&from=%E&api_id=%E&user=%E&password=%E",
87  conndata->send_url, sms->sms.receiver, sms->sms.sender,
88  conndata->system_id, conndata->username, conndata->password);
89 
90  /* append MD5 digest as msg ID from our UUID */
91  uuid_unparse(sms->sms.id, id);
92  os = octstr_create(id);
93  octstr_replace(os, octstr_imm("-"), octstr_imm(""));
94  octstr_format_append(url, "&cliMsgId=%E", os);
95  octstr_destroy(os);
96 
97  /* add UDH header */
98  if(octstr_len(sms->sms.udhdata)) {
99  octstr_format_append(url, "&udh=%H", sms->sms.udhdata);
100  }
101 
102  if(sms->sms.coding == DC_8BIT) {
103  octstr_format_append(url, "&data=%H&mclass=%d", sms->sms.msgdata, sms->sms.mclass);
104  } else if(sms->sms.coding == DC_UCS2) {
105  octstr_format_append(url, "&unicode=1&text=%H", sms->sms.msgdata);
106  } else {
107  octstr_format_append(url, "&text=%E", sms->sms.msgdata);
108  if (conndata->alt_charset) {
109  octstr_format_append(url, "&charset=%E", conndata->alt_charset);
110  } else {
111  octstr_append_cstr(url, "&charset=UTF-8");
112  }
113  }
114 
115  if (DLR_IS_ENABLED_DEVICE(sms->sms.dlr_mask))
116  octstr_format_append(url, "&callback=3&deliv_ack=1");
117 
118  headers = http_create_empty_headers();
119  debug("smsc.http.clickatell", 0, "HTTP[%s]: Sending request <%s>",
120  octstr_get_cstr(conn->id), octstr_get_cstr(url));
121 
122  /*
123  * Clickatell requires optionally an SSL-enabled HTTP client call, this is handled
124  * transparently by the Kannel HTTP layer module.
125  */
126  http_start_request(conndata->http_ref, HTTP_METHOD_GET, url, headers, NULL, 0, sms, NULL);
127 
128  octstr_destroy(url);
129  http_destroy_headers(headers);
130 
131  return 0;
132 }
133 
134 
135 /*
136  * Parse a line in the format: ID: XXXXXXXXXXXXXXXXXX
137  * and return a Dict with the 'ID' as key and the value as value,
138  * otherwise return NULL if a parsing error occures.
139  */
141 {
142  Dict *param = NULL;
143  List *words = NULL;
144  long len;
145  Octstr *word, *value;
146 
147  words = octstr_split_words(body);
148  if ((len = gwlist_len(words)) > 1) {
149  word = gwlist_extract_first(words);
150  if (octstr_compare(word, octstr_imm("ID:")) == 0) {
151  value = gwlist_extract_first(words);
152  param = dict_create(4, (void(*)(void *)) octstr_destroy);
153  dict_put(param, octstr_imm("ID"), value);
154  } else if (octstr_compare(word, octstr_imm("ERR:")) == 0) {
155  value = gwlist_extract_first(words);
156  param = dict_create(4, (void(*)(void *)) octstr_destroy);
157  dict_put(param, octstr_imm("ERR"), value);
158  }
159  octstr_destroy(word);
160  }
161  gwlist_destroy(words, (void(*)(void *)) octstr_destroy);
162 
163  return param;
164 }
165 
166 
167 static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status,
168  List *headers, Octstr *body)
169 {
170  if (status == HTTP_OK || status == HTTP_ACCEPTED) {
171  Dict *param;
172  Octstr *msgid;
173 
174  if ((param = clickatell_parse_body(body)) != NULL &&
175  (msgid = dict_get(param, octstr_imm("ID"))) != NULL &&
176  msgid != NULL) {
177 
178  /* SMSC ACK.. now we have the message id. */
179  if (DLR_IS_ENABLED_DEVICE(msg->sms.dlr_mask))
180  dlr_add(conn->id, msgid, msg, 0);
181 
182  bb_smscconn_sent(conn, msg, NULL);
183 
184  } else {
185  error(0, "HTTP[%s]: Message was malformed or error was returned. SMSC response `%s'.",
186  octstr_get_cstr(conn->id), octstr_get_cstr(body));
188  }
189  dict_destroy(param);
190 
191  } else {
192  error(0, "HTTP[%s]: Message was rejected. SMSC response `%s'.",
193  octstr_get_cstr(conn->id), octstr_get_cstr(body));
194  bb_smscconn_send_failed(conn, msg,
196  }
197 }
198 
199 /* MO related function */
201  List *headers, Octstr *body, List *cgivars)
202 {
203  List *reply_headers;
204  int ret;
205  Octstr *apimsgid, *status, *timestamp, *retmsg, *dest, *charge;
206  Octstr *api_id, *from, *to, *text, *charset, *udh;
207  int httpstatus = HTTP_UNAUTHORIZED, dlrstat;
208  Msg *dlrmsg, *momsg;
209  struct tm tm;
210 
211  /* dlr parameters */
212  apimsgid = http_cgi_variable(cgivars, "apiMsgId");
213  status = http_cgi_variable(cgivars, "status");
214  /* timestamp is for both DLR & MO */
215  timestamp = http_cgi_variable(cgivars, "timestamp");
216  dest = http_cgi_variable(cgivars, "to");
217  charge = http_cgi_variable(cgivars, "charge");
218  /* MO parameters */
219  api_id = http_cgi_variable(cgivars, "api_id");
220  from = http_cgi_variable(cgivars, "from");
221  to = http_cgi_variable(cgivars, "to");
222  text = http_cgi_variable(cgivars, "text");
223  charset = http_cgi_variable(cgivars, "charset");
224  udh = http_cgi_variable(cgivars, "udh");
225 
226  debug("smsc.http.clickatell", 0, "HTTP[%s]: Received a request",
227  octstr_get_cstr(conn->id));
228 
229  if (api_id != NULL && from != NULL && to != NULL && timestamp != NULL && text != NULL && charset != NULL && udh != NULL) {
230  /* we received an MO message */
231  debug("smsc.http.clickatell", 0, "HTTP[%s]: Received MO message from %s: <%s>",
232  octstr_get_cstr(conn->id), octstr_get_cstr(from), octstr_get_cstr(text));
233  momsg = msg_create(sms);
234  momsg->sms.sms_type = mo;
235  momsg->sms.sender = octstr_duplicate(from);
236  momsg->sms.receiver = octstr_duplicate(to);
237  momsg->sms.msgdata = octstr_duplicate(text);
238  momsg->sms.charset = octstr_duplicate(charset);
239  momsg->sms.service = octstr_duplicate(api_id);
240  momsg->sms.binfo = octstr_duplicate(api_id);
241  momsg->sms.smsc_id = octstr_duplicate(conn->id);
242  if (octstr_len(udh) > 0) {
243  momsg->sms.udhdata = octstr_duplicate(udh);
244  }
245  strptime(octstr_get_cstr(timestamp), "%Y-%m-%d %H:%M:%S", &tm);
246  momsg->sms.time = gw_mktime(&tm);
247 
248  /* note: implicit msg_destroy */
249  ret = bb_smscconn_receive(conn, momsg);
250  httpstatus = HTTP_OK;
251  retmsg = octstr_create("Thanks");
252  } else if (apimsgid == NULL || status == NULL || timestamp == NULL || dest == NULL) {
253  error(0, "HTTP[%s]: Insufficient args.",
254  octstr_get_cstr(conn->id));
255  httpstatus = HTTP_OK;
256  retmsg = octstr_create("Insufficient arguments, rejected.");
257  } else {
258  switch (atoi(octstr_get_cstr(status))) {
259  case 1: /* message unknown */
260  case 5: /* error with message */
261  case 6: /* user cancelled message */
262  case 7: /* error delivering message */
263  case 9: /* routing error */
264  case 10: /* message expired */
265  dlrstat = 2; /* delivery failure */
266  break;
267  case 2: /* message queued */
268  case 3: /* delivered */
269  case 11: /* message queued for later delivery */
270  dlrstat = 4; /* message buffered */
271  break;
272  case 4: /* received by recipient */
273  case 8: /* OK */
274  dlrstat = 1; /* message received */
275  break;
276  default: /* unknown status code */
277  dlrstat = 16; /* smsc reject */
278  break;
279  }
280  dlrmsg = dlr_find(conn->id,
281  apimsgid, /* smsc message id */
282  dest, /* destination */
283  dlrstat, 0);
284 
285  if (dlrmsg != NULL) {
286  /* dlrmsg->sms.msgdata = octstr_duplicate(apimsgid); */
287  dlrmsg->sms.sms_type = report_mo;
288  dlrmsg->sms.time = atoi(octstr_get_cstr(timestamp));
289  if (charge) {
290  /* unsure if smsbox relays the binfo field to dlrs.
291  But it is here in case they will start to do it. */
292  dlrmsg->sms.binfo = octstr_duplicate(charge);
293  }
294 
295  ret = bb_smscconn_receive(conn, dlrmsg);
296  httpstatus = (ret == 0 ? HTTP_OK : HTTP_FORBIDDEN);
297  retmsg = octstr_create("Sent");
298  } else {
299  error(0,"HTTP[%s]: got DLR but could not find message or was not interested "
300  "in it id<%s> dst<%s>, type<%d>",
301  octstr_get_cstr(conn->id), octstr_get_cstr(apimsgid),
302  octstr_get_cstr(dest), dlrstat);
303  httpstatus = HTTP_OK;
304  retmsg = octstr_create("Thanks");
305  }
306  }
307 
308  reply_headers = gwlist_create();
309  http_header_add(reply_headers, "Content-Type", "text/plain");
310  debug("smsc.http.clickatell", 0, "HTTP[%s]: Sending reply `%s'.",
311  octstr_get_cstr(conn->id), octstr_get_cstr(retmsg));
312  http_send_reply(client, httpstatus, reply_headers, retmsg);
313 
314  octstr_destroy(retmsg);
315  http_destroy_headers(reply_headers);
316 }
317 
320  .parse_reply = clickatell_parse_reply,
321  .receive_sms = clickatell_receive_sms,
322 };
323 
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:200
void error(int err, const char *fmt,...)
Definition: log.c:648
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2647
Octstr * alt_charset
Definition: smsc_http_p.h:104
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
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:249
static void client(int port)
Definition: test_udp.c:77
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
struct smsc_http_fn_callbacks smsc_http_clickatell_callback
Definition: clickatell.c:318
#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:99
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1509
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:140
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:2813
void http_destroy_headers(List *headers)
Definition: http.c:2856
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:1745
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2672
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
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:478
char * text
Definition: smsc_cimd2.c:921
List * http_create_empty_headers(void)
Definition: http.c:2849
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:1600
Octstr * octstr_format(const char *fmt,...)
Definition: octstr.c:2462
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: clickatell.c:167
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
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:279
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:2505
#define gwlist_create()
Definition: list.h:136
Octstr * username
Definition: smsc_http_p.h:97
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
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 DC_UCS2
Definition: sms.h:112
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
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.