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