Kannel: Open Source WAP and SMS gateway  svn-r5335
clickatell.c File Reference
#include "gwlib/gwlib.h"
#include "smscconn.h"
#include "smscconn_p.h"
#include "bb_smscconn_cb.h"
#include "msg.h"
#include "sms.h"
#include "dlr.h"
#include "urltrans.h"
#include "meta_data.h"
#include "../smsc_http_p.h"

Go to the source code of this file.

Functions

static int clickatell_send_sms (SMSCConn *conn, Msg *sms)
 
static Dictclickatell_parse_body (Octstr *body)
 
static void clickatell_parse_reply (SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
 
static void clickatell_receive_sms (SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
 

Variables

struct smsc_http_fn_callbacks smsc_http_clickatell_callback
 

Function Documentation

◆ clickatell_parse_body()

static Dict* clickatell_parse_body ( Octstr body)
static

Definition at line 161 of file clickatell.c.

References dict_create(), dict_put(), gwlist_destroy(), gwlist_extract_first(), gwlist_len(), octstr_compare(), octstr_destroy(), octstr_imm(), and octstr_split_words().

Referenced by clickatell_parse_reply().

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 }
Dict * dict_create(long size_hint, void(*destroy_value)(void *))
Definition: dict.c:192
void dict_put(Dict *dict, Octstr *key, void *value)
Definition: dict.c:240
long gwlist_len(List *list)
Definition: list.c:166
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
void * gwlist_extract_first(List *list)
Definition: list.c:305
Definition: dict.c:116
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1602
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
Definition: octstr.c:118
Definition: list.c:102
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:871
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

◆ clickatell_parse_reply()

static void clickatell_parse_reply ( SMSCConn conn,
Msg msg,
int  status,
List headers,
Octstr body 
)
static

Definition at line 188 of file clickatell.c.

References bb_smscconn_send_failed(), bb_smscconn_sent(), clickatell_parse_body(), dict_destroy(), dict_get(), dlr_add(), DLR_IS_ENABLED_DEVICE, error(), HTTP_ACCEPTED, HTTP_OK, smscconn::id, msg, octstr_duplicate, octstr_get_cstr, octstr_imm(), SMSCCONN_FAILED_MALFORMED, and SMSCCONN_FAILED_REJECTED.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:648
Octstr * id
Definition: smscconn_p.h:174
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 * octstr_imm(const char *cstr)
Definition: octstr.c:283
Definition: http.h:142
void * dict_get(Dict *dict, Octstr *key)
Definition: dict.c:286
Definition: dict.c:116
#define octstr_duplicate(ostr)
Definition: octstr.h:187
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
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:329
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86

◆ clickatell_receive_sms()

static void clickatell_receive_sms ( SMSCConn conn,
HTTPClient client,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 221 of file clickatell.c.

References bb_smscconn_receive(), charset, client(), debug(), DLR_BUFFERED, DLR_EXPIRED, DLR_FAIL, dlr_find(), DLR_SUCCESS, DLR_UNKNOWN, error(), from, gw_mktime(), gwlist_create, http_cgi_variable(), http_destroy_headers(), HTTP_FORBIDDEN, http_header_add(), HTTP_OK, http_send_reply(), HTTP_UNAUTHORIZED, smscconn::id, mo, msg_create, octstr_create, octstr_destroy(), octstr_duplicate, octstr_get_cstr, octstr_len(), report_mo, and text.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:648
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2886
Definition: msg.h:106
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
static void client(int port)
Definition: test_udp.c:77
#define DLR_EXPIRED
Definition: dlr.h:77
#define msg_create(type)
Definition: msg.h:136
Msg * dlr_find(const Octstr *smsc, const Octstr *ts, const Octstr *dst, int typ, int use_dst)
Definition: dlr.c:387
#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_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2695
Definition: http.h:142
Definition: msg.h:79
time_t gw_mktime(struct tm *tm)
Definition: protected.c:153
long bb_smscconn_receive(SMSCConn *conn, Msg *sms)
Definition: bb_smscconn.c:477
char * text
Definition: smsc_cimd2.c:921
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
#define gwlist_create()
Definition: list.h:136
#define DLR_UNKNOWN
Definition: dlr.h:78
#define DLR_BUFFERED
Definition: dlr.h:74
Definition: list.c:102
#define DLR_FAIL
Definition: dlr.h:73

◆ clickatell_send_sms()

static int clickatell_send_sms ( SMSCConn conn,
Msg sms 
)
static

Definition at line 78 of file clickatell.c.

References conndata::alt_charset, smscconn::data, DC_8BIT, DC_UCS2, debug(), DLR_BUFFERED, DLR_EXPIRED, DLR_FAIL, DLR_IS_ENABLED_DEVICE, DLR_SUCCESS, http_create_empty_headers(), http_destroy_headers(), HTTP_METHOD_GET, conndata::http_ref, http_start_request(), smscconn::id, conndata::mobile_originated, conndata::no_sender, octstr_append_cstr(), octstr_create, octstr_destroy(), octstr_format(), octstr_format_append(), octstr_get_cstr, octstr_imm(), octstr_len(), octstr_replace(), conndata::password, conndata::send_url, SMS_PARAM_UNDEFINED, conndata::system_id, url, conndata::username, UUID_STR_LEN, and uuid_unparse().

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 }
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2649
Octstr * alt_charset
Definition: smsc_http_p.h:103
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:250
HTTPCaller * http_ref
Definition: smsc_http_p.h:86
#define DLR_EXPIRED
Definition: dlr.h:77
#define DC_8BIT
Definition: sms.h:111
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:562
Octstr * system_id
Definition: smsc_http_p.h:107
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1511
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void http_destroy_headers(List *headers)
Definition: http.c:2879
#define DLR_SUCCESS
Definition: dlr.h:72
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1760
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:283
List * http_create_empty_headers(void)
Definition: http.c:2872
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
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
Definition: octstr.c:118
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
Octstr * username
Definition: smsc_http_p.h:97
#define DLR_BUFFERED
Definition: dlr.h:74
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
#define DLR_FAIL
Definition: dlr.h:73
#define DC_UCS2
Definition: sms.h:112
int mobile_originated
Definition: smsc_http_p.h:108

Variable Documentation

◆ smsc_http_clickatell_callback

struct smsc_http_fn_callbacks smsc_http_clickatell_callback
Initial value:
= {
.send_sms = clickatell_send_sms,
.parse_reply = clickatell_parse_reply,
.receive_sms = clickatell_receive_sms,
}
static void clickatell_receive_sms(SMSCConn *conn, HTTPClient *client, List *headers, Octstr *body, List *cgivars)
Definition: clickatell.c:221
static int clickatell_send_sms(SMSCConn *conn, Msg *sms)
Definition: clickatell.c:78
static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: clickatell.c:188

Definition at line 345 of file clickatell.c.

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.