Fri Aug 24 02:22:15 2007

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

Generated on Fri Aug 24 02:22:15 2007 for Asterisk - the Open Source PBX by  doxygen 1.5.1