Thu Apr 28 2011 17:15:22

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: 280812 $")
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 /*** DOCUMENTATION
00036    <function name="CALLERID" language="en_US">
00037       <synopsis>
00038          Gets or sets Caller*ID data on the channel.
00039       </synopsis>
00040       <syntax>
00041          <parameter name="datatype" required="true">
00042             <para>The allowable datatypes are:</para>
00043             <enumlist>
00044                <enum name="all" />
00045                <enum name="num" />
00046                <enum name="name" />
00047                <enum name="ANI" />
00048                <enum name="DNID" />
00049                <enum name="RDNIS" />
00050                <enum name="pres" />
00051                <enum name="ton" />
00052             </enumlist>
00053          </parameter>
00054          <parameter name="CID">
00055             <para>Optional Caller*ID</para>
00056          </parameter>
00057       </syntax>
00058       <description>
00059          <para>Gets or sets Caller*ID data on the channel. Uses channel callerid by default or optional
00060          callerid, if specified.</para>
00061       </description>
00062    </function>
00063    <function name="CALLERPRES" language="en_US">
00064       <synopsis>
00065          Gets or sets Caller*ID presentation on the channel.
00066       </synopsis>
00067       <syntax />
00068       <description>
00069          <para>Gets or sets Caller*ID presentation on the channel. The following values
00070          are valid:</para>
00071          <enumlist>
00072             <enum name="allowed_not_screened">
00073                <para>Presentation Allowed, Not Screened.</para>
00074             </enum>
00075             <enum name="allowed_passed_screen">
00076                <para>Presentation Allowed, Passed Screen.</para>
00077             </enum>
00078             <enum name="allowed_failed_screen">
00079                <para>Presentation Allowed, Failed Screen.</para>
00080             </enum>
00081             <enum name="allowed">
00082                <para>Presentation Allowed, Network Number.</para>
00083             </enum>
00084             <enum name="prohib_not_screened">
00085                <para>Presentation Prohibited, Not Screened.</para>
00086             </enum>
00087             <enum name="prohib_passed_screen">
00088                <para>Presentation Prohibited, Passed Screen.</para>
00089             </enum>
00090             <enum name="prohib_failed_screen">
00091                <para>Presentation Prohibited, Failed Screen.</para>
00092             </enum>
00093             <enum name="prohib">
00094                <para>Presentation Prohibited, Network Number.</para>
00095             </enum>
00096             <enum name="unavailable">
00097                <para>Number Unavailable.</para>
00098             </enum>
00099          </enumlist>
00100       </description>
00101    </function>
00102  ***/
00103 
00104 static int callerpres_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00105 {
00106    ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
00107    return 0;
00108 }
00109 
00110 static int callerpres_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00111 {
00112    int pres = ast_parse_caller_presentation(value);
00113    if (pres < 0)
00114       ast_log(LOG_WARNING, "'%s' is not a valid presentation (see 'show function CALLERPRES')\n", value);
00115    else
00116       chan->cid.cid_pres = pres;
00117    return 0;
00118 }
00119 
00120 static int callerid_read(struct ast_channel *chan, const char *cmd, char *data,
00121           char *buf, size_t len)
00122 {
00123    char *opt = data;
00124 
00125    /* Ensure that the buffer is empty */
00126    *buf = 0;
00127 
00128    if (!chan)
00129       return -1;
00130 
00131    if (strchr(opt, ',')) {
00132       char name[80], num[80];
00133 
00134       data = strsep(&opt, ",");
00135       ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
00136 
00137       if (!strncasecmp("all", data, 3)) {
00138          snprintf(buf, len, "\"%s\" <%s>", name, num);
00139       } else if (!strncasecmp("name", data, 4)) {
00140          ast_copy_string(buf, name, len);
00141       } else if (!strncasecmp("num", data, 3)) {
00142          /* also matches "number" */
00143          ast_copy_string(buf, num, len);
00144       } else {
00145          ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00146       }
00147    } else {
00148       ast_channel_lock(chan);
00149 
00150       if (!strncasecmp("all", data, 3)) {
00151          snprintf(buf, len, "\"%s\" <%s>",
00152             S_OR(chan->cid.cid_name, ""),
00153             S_OR(chan->cid.cid_num, ""));
00154       } else if (!strncasecmp("name", data, 4)) {
00155          if (chan->cid.cid_name) {
00156             ast_copy_string(buf, chan->cid.cid_name, len);
00157          }
00158       } else if (!strncasecmp("num", data, 3)) {
00159          /* also matches "number" */
00160          if (chan->cid.cid_num) {
00161             ast_copy_string(buf, chan->cid.cid_num, len);
00162          }
00163       } else if (!strncasecmp("ani", data, 3)) {
00164          if (!strncasecmp(data + 3, "2", 1)) {
00165             snprintf(buf, len, "%d", chan->cid.cid_ani2);
00166          } else if (chan->cid.cid_ani) {
00167             ast_copy_string(buf, chan->cid.cid_ani, len);
00168          }
00169       } else if (!strncasecmp("dnid", data, 4)) {
00170          if (chan->cid.cid_dnid) {
00171             ast_copy_string(buf, chan->cid.cid_dnid, len);
00172          }
00173       } else if (!strncasecmp("rdnis", data, 5)) {
00174          if (chan->cid.cid_rdnis) {
00175             ast_copy_string(buf, chan->cid.cid_rdnis, len);
00176          }
00177       } else if (!strncasecmp("pres", data, 4)) {
00178          ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
00179       } else if (!strncasecmp("ton", data, 3)) {
00180          snprintf(buf, len, "%d", chan->cid.cid_ton);
00181       } else {
00182          ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00183       }
00184 
00185       ast_channel_unlock(chan);
00186    }
00187 
00188    return 0;
00189 }
00190 
00191 static int callerid_write(struct ast_channel *chan, const char *cmd, char *data,
00192            const char *value)
00193 {
00194    int valid = 1;
00195    if (!value || !chan)
00196       return -1;
00197 
00198    value = ast_skip_blanks(value);
00199 
00200    if (!strncasecmp("all", data, 3)) {
00201       char name[256];
00202       char num[256];
00203 
00204       ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
00205       ast_set_callerid(chan, num, name, num);
00206       if (chan->cdr) {
00207          ast_cdr_setcid(chan->cdr, chan);
00208       }
00209    } else if (!strncasecmp("name", data, 4)) {
00210       ast_set_callerid(chan, NULL, value, NULL);
00211       if (chan->cdr) {
00212          ast_cdr_setcid(chan->cdr, chan);
00213       }
00214    } else if (!strncasecmp("num", data, 3)) {
00215       /* also matches "number" */
00216       ast_set_callerid(chan, value, NULL, NULL);
00217       if (chan->cdr) {
00218          ast_cdr_setcid(chan->cdr, chan);
00219       }
00220    } else if (!strncasecmp("ani", data, 3)) {
00221       if (!strncasecmp(data + 3, "2", 1)) {
00222          chan->cid.cid_ani2 = atoi(value);
00223       } else {
00224          ast_set_callerid(chan, NULL, NULL, value);
00225       }
00226       if (chan->cdr) {
00227          ast_cdr_setcid(chan->cdr, chan);
00228       }
00229    } else if (!strncasecmp("dnid", data, 4)) {
00230       ast_channel_lock(chan);
00231       if (chan->cid.cid_dnid) {
00232          ast_free(chan->cid.cid_dnid);
00233       }
00234       chan->cid.cid_dnid = ast_strdup(value);
00235       if (chan->cdr) {
00236          ast_cdr_setcid(chan->cdr, chan);
00237       }
00238       ast_channel_unlock(chan);
00239    } else if (!strncasecmp("rdnis", data, 5)) {
00240       ast_channel_lock(chan);
00241       if (chan->cid.cid_rdnis) {
00242          ast_free(chan->cid.cid_rdnis);
00243       }
00244       chan->cid.cid_rdnis = ast_strdup(value);
00245       if (chan->cdr) {
00246          ast_cdr_setcid(chan->cdr, chan);
00247       }
00248       ast_channel_unlock(chan);
00249    } else if (!strncasecmp("pres", data, 4)) {
00250       int i;
00251       char *val;
00252 
00253       val = ast_strdupa(value);
00254       ast_trim_blanks(val);
00255 
00256       if ((val[0] >= '0') && (val[0] <= '9')) {
00257          i = atoi(val);
00258       } else {
00259          i = ast_parse_caller_presentation(val);
00260       }
00261 
00262       if (i < 0) {
00263          ast_log(LOG_ERROR, "Unknown calling number presentation '%s', value unchanged\n", val);
00264       } else {
00265          chan->cid.cid_pres = i;
00266       }
00267    } else if (!strncasecmp("ton", data, 3)) {
00268       chan->cid.cid_ton = atoi(value);
00269    } else {
00270       ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00271       valid = 0;
00272    }
00273 
00274    if (valid) {
00275       chan->cid.cid_tns = 1;
00276    }
00277 
00278    return 0;
00279 }
00280 
00281 static struct ast_custom_function callerid_function = {
00282    .name = "CALLERID",
00283    .read = callerid_read,
00284    .write = callerid_write,
00285 };
00286 
00287 static struct ast_custom_function callerpres_function = {
00288    .name = "CALLERPRES",
00289    .read = callerpres_read,
00290    .write = callerpres_write,
00291 };
00292 
00293 static int unload_module(void)
00294 {
00295    int res = ast_custom_function_unregister(&callerpres_function);
00296    res |= ast_custom_function_unregister(&callerid_function);
00297    return res;
00298 }
00299 
00300 static int load_module(void)
00301 {
00302    int res = ast_custom_function_register(&callerpres_function);
00303    res |= ast_custom_function_register(&callerid_function);
00304    return res;
00305 }
00306 
00307 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan functions");