Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
clickatell.c File Reference
#include "gwlib/gwlib.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

static Dict* clickatell_parse_body ( Octstr body)
static

Definition at line 130 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().

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 }
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:281
void * gwlist_extract_first(List *list)
Definition: list.c:305
Definition: dict.c:116
List * octstr_split_words(const Octstr *ostr)
Definition: octstr.c:1600
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
Definition: list.c:102
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
static void clickatell_parse_reply ( SMSCConn conn,
Msg msg,
int  status,
List headers,
Octstr body 
)
static

Definition at line 157 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, octstr_duplicate, octstr_get_cstr, octstr_imm(), SMSCCONN_FAILED_MALFORMED, and SMSCCONN_FAILED_REJECTED.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Octstr * id
Definition: smscconn_p.h:174
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
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 * octstr_imm(const char *cstr)
Definition: octstr.c:281
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:279
void bb_smscconn_send_failed(SMSCConn *conn, Msg *sms, int reason, Octstr *reply)
Definition: bb_smscconn.c:328
static void clickatell_receive_sms ( SMSCConn conn,
HTTPClient client,
List headers,
Octstr body,
List cgivars 
)
static

Definition at line 190 of file clickatell.c.

References bb_smscconn_receive(), charset, debug(), dlr_find(), 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.

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 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void http_header_add(List *headers, char *name, char *contents)
Definition: http.c:2863
Definition: msg.h:106
Definition: msg.h:109
Octstr * id
Definition: smscconn_p.h:174
#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:2813
void http_destroy_headers(List *headers)
Definition: http.c:2856
static Octstr * from
Definition: mtbatch.c:95
void http_send_reply(HTTPClient *client, int status, List *headers, Octstr *body)
Definition: http.c:2671
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:478
char * text
Definition: smsc_cimd2.c:921
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
#define gwlist_create()
Definition: list.h:136
Definition: list.c:102
static int clickatell_send_sms ( SMSCConn conn,
Msg sms 
)
static

Definition at line 68 of file clickatell.c.

References conndata::alt_charset, smscconn::data, DC_8BIT, DC_UCS2, debug(), DLR_IS_ENABLED_DEVICE, http_create_empty_headers(), http_destroy_headers(), HTTP_METHOD_GET, conndata::http_ref, http_start_request(), smscconn::id, 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, conndata::system_id, url, conndata::username, UUID_STR_LEN, and uuid_unparse().

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 }
void octstr_replace(Octstr *haystack, Octstr *needle, Octstr *repl)
Definition: octstr.c:2647
Octstr * alt_charset
Definition: smsc_http.c:161
Octstr * id
Definition: smscconn_p.h:174
void * data
Definition: smscconn_p.h:249
HTTPCaller * http_ref
Definition: smsc_http.c:143
#define DLR_IS_ENABLED_DEVICE(dlr)
Definition: dlr.h:82
#define DC_8BIT
Definition: sms.h:111
void uuid_unparse(const uuid_t uu, char *out)
Definition: gw_uuid.c:561
Octstr * system_id
Definition: smsc_http.c:156
void octstr_append_cstr(Octstr *ostr, const char *cstr)
Definition: octstr.c:1509
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void http_destroy_headers(List *headers)
Definition: http.c:2856
void http_start_request(HTTPCaller *caller, int method, Octstr *url, List *headers, Octstr *body, int follow, void *id, Octstr *certkeyfile)
Definition: http.c:1745
Octstr * octstr_imm(const char *cstr)
Definition: octstr.c:281
List * http_create_empty_headers(void)
Definition: http.c:2849
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
#define UUID_STR_LEN
Definition: gw_uuid.h:19
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
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
Octstr * username
Definition: smsc_http.c:154
Octstr * send_url
Definition: smsc_http.c:150
static Octstr * url
Definition: test_xmlrpc.c:84
Definition: list.c:102
#define DC_UCS2
Definition: sms.h:112

Variable Documentation

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:190
static int clickatell_send_sms(SMSCConn *conn, Msg *sms)
Definition: clickatell.c:68
static void clickatell_parse_reply(SMSCConn *conn, Msg *msg, int status, List *headers, Octstr *body)
Definition: clickatell.c:157

Definition at line 308 of file clickatell.c.

Referenced by smsc_http_create().

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