📄 smsbox.c
字号:
if (dlr_url != NULL) { if (urltrans_accept_x_kannel_headers(trans)) { octstr_destroy(msg->sms.dlr_url); msg->sms.dlr_url = dlr_url; } else { warning(0, "Tried to change dlr_url to '%s', denied.", octstr_get_cstr(dlr_url)); octstr_destroy(dlr_url); } } if (smsc != NULL) { if (urltrans_accept_x_kannel_headers(trans)) { octstr_destroy(msg->sms.smsc_id); msg->sms.smsc_id = smsc; } else { warning(0, "Tried to change SMSC to '%s', denied.", octstr_get_cstr(smsc)); octstr_destroy(smsc); } } if (from != NULL) { if (urltrans_accept_x_kannel_headers(trans)) { octstr_destroy(msg->sms.sender); msg->sms.sender = from; } else { warning(0, "Tried to change sender to '%s', denied.", octstr_get_cstr(from)); octstr_destroy(from); } } if (to != NULL) { if (urltrans_accept_x_kannel_headers(trans)) { octstr_destroy(msg->sms.receiver); msg->sms.receiver = to; } else { warning(0, "Tried to change receiver to '%s', denied.", octstr_get_cstr(to)); octstr_destroy(to); } } if (udh != NULL) { if (urltrans_accept_x_kannel_headers(trans)) { octstr_destroy(msg->sms.udhdata); msg->sms.udhdata = udh; } else { warning(0, "Tried to set UDH field, denied."); O_DESTROY(udh); } } if (mclass != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.mclass = mclass; else warning(0, "Tried to set MClass field, denied."); } if (pid != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.pid = pid; else warning(0, "Tried to set PID field, denied."); } if (rpi != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.rpi = rpi; else warning(0, "Tried to set RPI field, denied."); } if (alt_dcs != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.alt_dcs = alt_dcs; else warning(0, "Tried to set Alt-DCS field, denied."); } if (mwi != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.mwi = mwi; else warning(0, "Tried to set MWI field, denied."); } if (coding != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.coding = coding; else warning(0, "Tried to set Coding field, denied."); } if (compress != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.compress = compress; else warning(0, "Tried to set Compress field, denied."); } /* Compatibility Mode */ if ( msg->sms.coding == DC_UNDEF) { if(octstr_len(udh)) msg->sms.coding = DC_8BIT; else msg->sms.coding = DC_7BIT; } if (validity != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.validity = validity; else warning(0, "Tried to change validity to '%d', denied.", validity); } if (deferred != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.deferred = deferred; else warning(0, "Tried to change deferred to '%d', denied.", deferred); } if (dlr_mask != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) { msg->sms.dlr_mask = dlr_mask; } else warning(0, "Tried to change dlr_mask to '%d', denied.", dlr_mask); } if (account) { if (urltrans_accept_x_kannel_headers(trans)) { msg->sms.account = account; } else { warning(0, "Tried to change account to '%s', denied.", octstr_get_cstr(account)); octstr_destroy(account); } } if (binfo) { if (urltrans_accept_x_kannel_headers(trans)) { msg->sms.binfo = binfo; } else { warning(0, "Tried to change billing info to '%s', denied.", octstr_get_cstr(binfo)); octstr_destroy(binfo); } } if (priority != SMS_PARAM_UNDEFINED) { if (urltrans_accept_x_kannel_headers(trans)) msg->sms.priority = priority; else warning(0, "Tried to change priority to '%d', denied.", priority); }}/*********************************************************************** * Thread to handle failed HTTP requests and retries to deliver the * information to the HTTP server. The thread uses the smsbox_http_requests * queue that is spooled by url_result_thread in case the HTTP requests * fails. */static void http_queue_thread(void *arg){ void *id; Msg *msg; URLTranslation *trans; Octstr *req_url; List *req_headers; Octstr *req_body; unsigned long retries; int method; while ((id = list_consume(smsbox_http_requests)) != NULL) { /* * Sleep for a while in order not to block other operting requests. * Defaults to 10 sec. if not given via http-queue-delay directive in * smsbox group. */ if (http_queue_delay > 0) gwthread_sleep(http_queue_delay); debug("sms.http",0,"HTTP: Queue contains %ld outstanding requests", list_len(smsbox_http_requests)); /* * Get all required HTTP request data from the queue and reconstruct * the id pointer for later lookup in url_result_thread. */ get_receiver(id, &msg, &trans, &method, &req_url, &req_headers, &req_body, &retries); if (retries < max_http_retries) { id = remember_receiver(msg, trans, method, req_url, req_headers, req_body, ++retries); debug("sms.http",0,"HTTP: Retrying request <%s> (%ld/%ld)", octstr_get_cstr(req_url), retries, max_http_retries); /* re-queue this request to the HTTPCaller list */ http_start_request(caller, method, req_url, req_headers, req_body, 1, id, NULL); } msg_destroy(msg); octstr_destroy(req_url); http_destroy_headers(req_headers); octstr_destroy(req_body); }}static void url_result_thread(void *arg){ Octstr *final_url, *req_body, *type, *replytext; List *reply_headers; int status, method; void *id; Msg *msg; URLTranslation *trans; Octstr *req_url; List *req_headers; Octstr *text_html, *text_plain, *text_wml, *text_xml; Octstr *octet_stream; int octets; unsigned long retries; unsigned int queued; /* indicate if processes reply is requeued */ Octstr *reply_body, *charset; Octstr *udh, *from, *to, *dlr_url, *account, *smsc, *binfo; int dlr_mask, mclass, mwi, coding, compress, pid, alt_dcs, rpi; int validity, deferred, priority; text_html = octstr_imm("text/html"); text_wml = octstr_imm("text/vnd.wap.wml"); text_plain = octstr_imm("text/plain"); text_xml = octstr_imm("text/xml"); octet_stream = octstr_imm("application/octet-stream"); for (;;) { queued = 0; id = http_receive_result(caller, &status, &final_url, &reply_headers, &reply_body); if (id == NULL) break; octets = 0; from = to = udh = smsc = dlr_url = account = binfo = charset = NULL; mclass = mwi = coding = compress = pid = alt_dcs = rpi = dlr_mask = validity = deferred = priority = SMS_PARAM_UNDEFINED; get_receiver(id, &msg, &trans, &method, &req_url, &req_headers, &req_body, &retries); if (status == HTTP_OK || status == HTTP_ACCEPTED) { http_header_get_content_type(reply_headers, &type, &charset); if (octstr_case_compare(type, text_html) == 0 || octstr_case_compare(type, text_wml) == 0) { if (trans != NULL) strip_prefix_and_suffix(reply_body, urltrans_prefix(trans), urltrans_suffix(trans)); replytext = html_to_sms(reply_body); octstr_strip_blanks(replytext); get_x_kannel_from_headers(reply_headers, &from, &to, &udh, NULL, NULL, &smsc, &mclass, &mwi, &coding, &compress, &validity, &deferred, &dlr_mask, &dlr_url, &account, &pid, &alt_dcs, &rpi, &binfo, &priority); } else if (octstr_case_compare(type, text_plain) == 0) { replytext = octstr_duplicate(reply_body); octstr_destroy(reply_body); reply_body = NULL; get_x_kannel_from_headers(reply_headers, &from, &to, &udh, NULL, NULL, &smsc, &mclass, &mwi, &coding, &compress, &validity, &deferred, &dlr_mask, &dlr_url, &account, &pid, &alt_dcs, &rpi, &binfo, &priority); } else if (octstr_case_compare(type, text_xml) == 0) { replytext = octstr_duplicate(reply_body); octstr_destroy(reply_body); reply_body = NULL; get_x_kannel_from_xml(mt_reply, &type, &replytext, reply_headers, &from, &to, &udh, NULL, NULL, &smsc, &mclass, &mwi, &coding, &compress, &validity, &deferred, &dlr_mask, &dlr_url, &account, &pid, &alt_dcs, &rpi, NULL, &charset, &binfo, &priority); } else if (octstr_case_compare(type, octet_stream) == 0) { replytext = octstr_duplicate(reply_body); octstr_destroy(reply_body); octets = 1; reply_body = NULL; get_x_kannel_from_headers(reply_headers, &from, &to, &udh, NULL, NULL, &smsc, &mclass, &mwi, &coding, &compress, &validity, &deferred, &dlr_mask, &dlr_url, &account, &pid, &alt_dcs, &rpi, &binfo, &priority); } else { replytext = octstr_duplicate(reply_couldnotrepresent); } if (charset_processing(charset, replytext, coding) == -1) { replytext = octstr_duplicate(reply_couldnotrepresent); } octstr_destroy(type); } else if (max_http_retries > retries) { id = remember_receiver(msg, trans, method, req_url, req_headers, req_body, retries); list_produce(smsbox_http_requests, id); queued++; goto requeued; } else replytext = octstr_duplicate(reply_couldnotfetch); fill_message(msg, trans, replytext, octets, from, to, udh, mclass, mwi, coding, compress, validity, deferred, dlr_url, dlr_mask, pid, alt_dcs, rpi, smsc, account, charset, binfo, priority); if (final_url == NULL) final_url = octstr_imm(""); if (reply_body == NULL) reply_body = octstr_imm(""); if (msg->sms.sms_type != report_mo) { alog("SMS HTTP-request sender:%s request: '%s' " "url: '%s' reply: %d '%s'", octstr_get_cstr(msg->sms.receiver), (msg->sms.msgdata != NULL) ? octstr_get_cstr(msg->sms.msgdata) : "", octstr_get_cstr(final_url), status, (status == HTTP_OK) ? "<< successful >>" : octstr_get_cstr(reply_body)); }requeued: octstr_destroy(final_url); http_destroy_headers(reply_headers); octstr_destroy(reply_body); octstr_destroy(req_url); http_destroy_headers(req_headers); octstr_destroy(req_body); if (msg->sms.sms_type != report_mo && !queued) { if (send_message(trans, msg) < 0) error(0, "failed to send message to phone"); } msg_destroy(msg); }}/*********************************************************************** * Thread to receive SMS messages from bearerbox and obeying the requests * in them. HTTP requests are started in the background (another thread * will deal with the replies) and other requests are fulfilled directly. *//* * Perform the service requested by the user: translate the request into * a pattern, if it is an URL, start its fetch and return 0, otherwise * return the string in `*result' and return 1. Return -1 for errors. * If we are translating url for ppg dlr, we do not use trans data * structure defined for sms services. This is indicated by trans = NULL. */static int obey_request(Octstr **result, URLTranslation *trans, Msg *msg){ Octstr *pattern, *xml, *tmp; List *request_headers; void *id; struct tm tm; char p[22]; int type; FILE *f; gw_assert(msg != NULL); gw_assert(msg_type(msg) == sms); if (msg->sms.sms_type == report_mo) type = TRANSTYPE_GET_URL; else type = urltrans_type(trans); pattern = urltrans_get_pattern(trans, msg); gw_assert(pattern != NULL); switch (type) { case TRANSTYPE_TEXT: debug("sms", 0, "formatted text answer: <%s>", octstr_get_cstr(pattern)); *result = pattern; alog("SMS request sender:%s request: '%s' fixed answer: '%s'", octstr_get_cstr(msg->sms.receiver), octstr_get_cstr(msg->sms.msgdata), octstr_get_cstr(pattern)); break; case TRANSTYPE_FILE: *result = octstr_read_file(octstr_get_cstr(pattern)); octstr_destroy(pattern); alog("SMS request sender:%s request: '%s' file answer: '%s'", octstr_get_cstr(msg->sms.receiver), octstr_get_cstr(msg->sms.msgdata), octstr_get_cstr(*result)); break; case TRANSTYPE_EXECUTE: debug("sms.exec", 0, "executing sms-service '%s'", octstr_get_cstr(pattern)); if ((f = popen(octstr_get_cstr(pattern), "r")) != NULL) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -