Fri Jul 24 00:40:56 2009

Asterisk developer's documentation


func_callerid.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999-2006, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Caller ID related dialplan functions
00020  * 
00021  * \ingroup functions
00022  */
00023 
00024 #include "asterisk.h"
00025 
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 160859 $")
00027 
00028 #include "asterisk/module.h"
00029 #include "asterisk/channel.h"
00030 #include "asterisk/pbx.h"
00031 #include "asterisk/utils.h"
00032 #include "asterisk/app.h"
00033 #include "asterisk/callerid.h"
00034 
00035 static int callerpres_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00036 {
00037    ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
00038    return 0;
00039 }
00040 
00041 static int callerpres_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00042 {
00043    int pres = ast_parse_caller_presentation(value);
00044    if (pres < 0)
00045       ast_log(LOG_WARNING, "'%s' is not a valid presentation (see 'show function CALLERPRES')\n", value);
00046    else
00047       chan->cid.cid_pres = pres;
00048    return 0;
00049 }
00050 
00051 static int callerid_read(struct ast_channel *chan, const char *cmd, char *data,
00052           char *buf, size_t len)
00053 {
00054    char *opt = data;
00055 
00056    /* Ensure that the buffer is empty */
00057    *buf = 0;
00058 
00059    if (!chan)
00060       return -1;
00061 
00062    if (strchr(opt, ',')) {
00063       char name[80], num[80];
00064 
00065       data = strsep(&opt, ",");
00066       ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
00067 
00068       if (!strncasecmp("all", data, 3)) {
00069          snprintf(buf, len, "\"%s\" <%s>", name, num);
00070       } else if (!strncasecmp("name", data, 4)) {
00071          ast_copy_string(buf, name, len);
00072       } else if (!strncasecmp("num", data, 3)) {
00073          /* also matches "number" */
00074          ast_copy_string(buf, num, len);
00075       } else {
00076          ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00077       }
00078    } else {
00079       ast_channel_lock(chan);
00080 
00081       if (!strncasecmp("all", data, 3)) {
00082          snprintf(buf, len, "\"%s\" <%s>",
00083             S_OR(chan->cid.cid_name, ""),
00084             S_OR(chan->cid.cid_num, ""));
00085       } else if (!strncasecmp("name", data, 4)) {
00086          if (chan->cid.cid_name) {
00087             ast_copy_string(buf, chan->cid.cid_name, len);
00088          }
00089       } else if (!strncasecmp("num", data, 3)) {
00090          /* also matches "number" */
00091          if (chan->cid.cid_num) {
00092             ast_copy_string(buf, chan->cid.cid_num, len);
00093          }
00094       } else if (!strncasecmp("ani", data, 3)) {
00095          if (!strncasecmp(data + 3, "2", 1)) {
00096             snprintf(buf, len, "%d", chan->cid.cid_ani2);
00097          } else if (chan->cid.cid_ani) {
00098             ast_copy_string(buf, chan->cid.cid_ani, len);
00099          }
00100       } else if (!strncasecmp("dnid", data, 4)) {
00101          if (chan->cid.cid_dnid) {
00102             ast_copy_string(buf, chan->cid.cid_dnid, len);
00103          }
00104       } else if (!strncasecmp("rdnis", data, 5)) {
00105          if (chan->cid.cid_rdnis) {
00106             ast_copy_string(buf, chan->cid.cid_rdnis, len);
00107          }
00108       } else if (!strncasecmp("pres", data, 4)) {
00109          ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
00110       } else if (!strncasecmp("ton", data, 3)) {
00111          snprintf(buf, len, "%d", chan->cid.cid_ton);
00112       } else {
00113          ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00114       }
00115 
00116       ast_channel_unlock(chan);
00117    }
00118 
00119    return 0;
00120 }
00121 
00122 static int callerid_write(struct ast_channel *chan, const char *cmd, char *data,
00123            const char *value)
00124 {
00125    if (!value || !chan)
00126       return -1;
00127 
00128    value = ast_skip_blanks(value);
00129 
00130    if (!strncasecmp("all", data, 3)) {
00131       char name[256];
00132       char num[256];
00133 
00134       ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
00135       ast_set_callerid(chan, num, name, num);
00136       if (chan->cdr) {
00137          ast_cdr_setcid(chan->cdr, chan);
00138       }
00139    } else if (!strncasecmp("name", data, 4)) {
00140       ast_set_callerid(chan, NULL, value, NULL);
00141       if (chan->cdr) {
00142          ast_cdr_setcid(chan->cdr, chan);
00143       }
00144    } else if (!strncasecmp("num", data, 3)) {
00145       /* also matches "number" */
00146       ast_set_callerid(chan, value, NULL, NULL);
00147       if (chan->cdr) {
00148          ast_cdr_setcid(chan->cdr, chan);
00149       }
00150    } else if (!strncasecmp("ani", data, 3)) {
00151       if (!strncasecmp(data + 3, "2", 1)) {
00152          chan->cid.cid_ani2 = atoi(value);
00153       } else {
00154          ast_set_callerid(chan, NULL, NULL, value);
00155       }
00156       if (chan->cdr) {
00157          ast_cdr_setcid(chan->cdr, chan);
00158       }
00159    } else if (!strncasecmp("dnid", data, 4)) {
00160       ast_channel_lock(chan);
00161       if (chan->cid.cid_dnid) {
00162          ast_free(chan->cid.cid_dnid);
00163       }
00164       chan->cid.cid_dnid = ast_strdup(value);
00165       if (chan->cdr) {
00166          ast_cdr_setcid(chan->cdr, chan);
00167       }
00168       ast_channel_unlock(chan);
00169    } else if (!strncasecmp("rdnis", data, 5)) {
00170       ast_channel_lock(chan);
00171       if (chan->cid.cid_rdnis) {
00172          ast_free(chan->cid.cid_rdnis);
00173       }
00174       chan->cid.cid_rdnis = ast_strdup(value);
00175       if (chan->cdr) {
00176          ast_cdr_setcid(chan->cdr, chan);
00177       }
00178       ast_channel_unlock(chan);
00179    } else if (!strncasecmp("pres", data, 4)) {
00180       int i;
00181       char *val;
00182 
00183       val = ast_strdupa(value);
00184       ast_trim_blanks(val);
00185 
00186       if ((val[0] >= '0') && (val[0] <= '9')) {
00187          i = atoi(val);
00188       } else {
00189          i = ast_parse_caller_presentation(val);
00190       }
00191 
00192       if (i < 0) {
00193          ast_log(LOG_ERROR, "Unknown calling number presentation '%s', value unchanged\n", val);
00194       } else {
00195          chan->cid.cid_pres = i;
00196       }
00197    } else if (!strncasecmp("ton", data, 3)) {
00198       chan->cid.cid_ton = atoi(value);
00199    } else {
00200       ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00201    }
00202 
00203    return 0;
00204 }
00205 
00206 static struct ast_custom_function callerid_function = {
00207    .name = "CALLERID",
00208    .synopsis = "Gets or sets Caller*ID data on the channel.",
00209    .syntax = "CALLERID(datatype[,<optional-CID>])",
00210    .desc =
00211       "Gets or sets Caller*ID data on the channel.  The allowable datatypes\n"
00212       "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\", \"pres\",\n"
00213       "and \"ton\".\n"
00214       "Uses channel callerid by default or optional callerid, if specified.\n",
00215    .read = callerid_read,
00216    .write = callerid_write,
00217 };
00218 
00219 static struct ast_custom_function callerpres_function = {
00220    .name = "CALLERPRES",
00221    .synopsis = "Gets or sets Caller*ID presentation on the channel.",
00222    .syntax = "CALLERPRES()",
00223    .desc =
00224 "Gets or sets Caller*ID presentation on the channel.  The following values\n"
00225 "are valid:\n"
00226 "      allowed_not_screened    : Presentation Allowed, Not Screened\n"
00227 "      allowed_passed_screen   : Presentation Allowed, Passed Screen\n" 
00228 "      allowed_failed_screen   : Presentation Allowed, Failed Screen\n" 
00229 "      allowed                 : Presentation Allowed, Network Number\n"
00230 "      prohib_not_screened     : Presentation Prohibited, Not Screened\n" 
00231 "      prohib_passed_screen    : Presentation Prohibited, Passed Screen\n"
00232 "      prohib_failed_screen    : Presentation Prohibited, Failed Screen\n"
00233 "      prohib                  : Presentation Prohibited, Network Number\n"
00234 "      unavailable             : Number Unavailable\n",
00235    .read = callerpres_read,
00236    .write = callerpres_write,
00237 };
00238 
00239 static int unload_module(void)
00240 {
00241    int res = ast_custom_function_unregister(&callerpres_function);
00242    res |= ast_custom_function_unregister(&callerid_function);
00243    return res;
00244 }
00245 
00246 static int load_module(void)
00247 {
00248    int res = ast_custom_function_register(&callerpres_function);
00249    res |= ast_custom_function_register(&callerid_function);
00250    return res;
00251 }
00252 
00253 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan functions");

Generated on Fri Jul 24 00:40:56 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7