Asterisk.org: Developer Documentation (28 Oct 2009)

Wed Oct 28 15:47:54 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 - 2005, 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  */
    00022
    00023 #include <stdlib.h>
    00024 #include <stdio.h>
    00025 #include <string.h>
    00026 #include <sys/types.h>
    00027
    00028 #include "asterisk.h"
    00029
    00030 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 61680 $")
    00031
    00032 #ifndef BUILTIN_FUNC
    00033 #include "asterisk/module.h"
    00034 #endif /* BUILTIN_FUNC */
    00035 #include "asterisk/channel.h"
    00036 #include "asterisk/pbx.h"
    00037 #include "asterisk/logger.h"
    00038 #include "asterisk/utils.h"
    00039 #include "asterisk/app.h"
    00040 #include "asterisk/options.h"
    00041 #include "asterisk/callerid.h"
    00042
    00043 static char *callerid_read(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
    00044 {
    00045    if (!chan)
    00046       return "";
    00047
    00048    if (!strncasecmp("all", data, 3)) {
    00049       snprintf(buf, len, "\"%s\" <%s>", chan->cid.cid_name ? chan->cid.cid_name : "", chan->cid.cid_num ? chan->cid.cid_num : "");
    00050    } else if (!strncasecmp("name", data, 4)) {
    00051       if (chan->cid.cid_name) {
    00052          ast_copy_string(buf, chan->cid.cid_name, len);
    00053       }
    00054    } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
    00055       if (chan->cid.cid_num) {
    00056          ast_copy_string(buf, chan->cid.cid_num, len);
    00057       }
    00058    } else if (!strncasecmp("ani", data, 3)) {
    00059       if (chan->cid.cid_ani) {
    00060          ast_copy_string(buf, chan->cid.cid_ani, len);
    00061       }
    00062    } else if (!strncasecmp("dnid", data, 4)) {
    00063       if (chan->cid.cid_dnid) {
    00064          ast_copy_string(buf, chan->cid.cid_dnid, len);
    00065       }
    00066    } else if (!strncasecmp("rdnis", data, 5)) {
    00067       if (chan->cid.cid_rdnis) {
    00068          ast_copy_string(buf, chan->cid.cid_rdnis, len);
    00069       }
    00070    } else {
    00071       ast_log(LOG_ERROR, "Unknown callerid data type.\n");
    00072    }
    00073
    00074    return buf;
    00075 }
    00076
    00077 static void callerid_write(struct ast_channel *chan, char *cmd, char *data, const char *value)
    00078 {
    00079    if (!value || !chan)
    00080                 return;
    00081
    00082    if (!strncasecmp("all", data, 3)) {
    00083       char name[256];
    00084       char num[256];
    00085       if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
    00086          ast_set_callerid(chan, num, name, num);
    00087         } else if (!strncasecmp("name", data, 4)) {
    00088                 ast_set_callerid(chan, NULL, value, NULL);
    00089         } else if (!strncasecmp("num", data, 3) || !strncasecmp("number", data, 6)) {
    00090                 ast_set_callerid(chan, value, NULL, NULL);
    00091         } else if (!strncasecmp("ani", data, 3)) {
    00092                 ast_set_callerid(chan, NULL, NULL, value);
    00093         } else if (!strncasecmp("dnid", data, 4)) {
    00094                 /* do we need to lock chan here? */
    00095                 if (chan->cid.cid_dnid)
    00096                         free(chan->cid.cid_dnid);
    00097                 chan->cid.cid_dnid = ast_strlen_zero(value) ? NULL : strdup(value);
    00098         } else if (!strncasecmp("rdnis", data, 5)) {
    00099                 /* do we need to lock chan here? */
    00100                 if (chan->cid.cid_rdnis)
    00101                         free(chan->cid.cid_rdnis);
    00102                 chan->cid.cid_rdnis = ast_strlen_zero(value) ? NULL : strdup(value);
    00103         } else {
    00104                 ast_log(LOG_ERROR, "Unknown callerid data type.\n");
    00105         }
    00106 }
    00107
    00108 #ifndef BUILTIN_FUNC
    00109 static
    00110 #endif /* BUILTIN_FUNC */
    00111 struct ast_custom_function callerid_function = {
    00112    .name = "CALLERID",
    00113    .synopsis = "Gets or sets Caller*ID data on the channel.",
    00114    .syntax = "CALLERID(datatype)",
    00115    .desc = "Gets or sets Caller*ID data on the channel.  The allowable datatypes\n"
    00116    "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n",
    00117    .read = callerid_read,
    00118    .write = callerid_write,
    00119 };
    00120
    00121 #ifndef BUILTIN_FUNC
    00122 static char *tdesc = "Caller ID related dialplan function";
    00123
    00124 int unload_module(void)
    00125 {
    00126         return ast_custom_function_unregister(&callerid_function);
    00127 }
    00128
    00129 int load_module(void)
    00130 {
    00131         return ast_custom_function_register(&callerid_function);
    00132 }
    00133
    00134 char *description(void)
    00135 {
    00136    return tdesc;
    00137 }
    00138
    00139 int usecount(void)
    00140 {
    00141    return 0;
    00142 }
    00143
    00144 char *key()
    00145 {
    00146    return ASTERISK_GPL_KEY;
    00147 }
    00148 #endif /* BUILTIN_FUNC */
    00149
    00150 /*
    00151 Local Variables:
    00152 mode: C
    00153 c-file-style: "linux"
    00154 indent-tabs-mode: nil
    00155 End:
    00156 */