00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "asterisk.h"
00024
00025 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 146799 $")
00026
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <sys/types.h>
00031
00032 #include "asterisk/module.h"
00033 #include "asterisk/channel.h"
00034 #include "asterisk/pbx.h"
00035 #include "asterisk/logger.h"
00036 #include "asterisk/utils.h"
00037 #include "asterisk/app.h"
00038 #include "asterisk/options.h"
00039 #include "asterisk/callerid.h"
00040
00041 static int callerid_read(struct ast_channel *chan, char *cmd, char *data,
00042 char *buf, size_t len)
00043 {
00044 int res = -1;
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 res = 0;
00059 } else if (!strncasecmp("name", data, 4)) {
00060 ast_copy_string(buf, name, len);
00061 res = 0;
00062 } else if (!strncasecmp("num", data, 3) ||
00063 !strncasecmp("number", data, 6)) {
00064
00065 ast_copy_string(buf, num, len);
00066 res = 0;
00067 } else {
00068 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00069 }
00070 } else {
00071 ast_channel_lock(chan);
00072
00073 if (!strncasecmp("all", data, 3)) {
00074 snprintf(buf, len, "\"%s\" <%s>",
00075 S_OR(chan->cid.cid_name, ""),
00076 S_OR(chan->cid.cid_num, ""));
00077 res = 0;
00078 } else if (!strncasecmp("name", data, 4)) {
00079 if (chan->cid.cid_name) {
00080 ast_copy_string(buf, chan->cid.cid_name, len);
00081 res = 0;
00082 }
00083 } else if (!strncasecmp("num", data, 3)
00084 || !strncasecmp("number", data, 6)) {
00085 if (chan->cid.cid_num) {
00086 ast_copy_string(buf, chan->cid.cid_num, len);
00087 res = 0;
00088 }
00089 } else if (!strncasecmp("ani", data, 3)) {
00090 if (chan->cid.cid_ani) {
00091 ast_copy_string(buf, chan->cid.cid_ani, len);
00092 res = 0;
00093 }
00094 } else if (!strncasecmp("dnid", data, 4)) {
00095 if (chan->cid.cid_dnid) {
00096 ast_copy_string(buf, chan->cid.cid_dnid, len);
00097 res = 0;
00098 }
00099 } else if (!strncasecmp("rdnis", data, 5)) {
00100 if (chan->cid.cid_rdnis) {
00101 ast_copy_string(buf, chan->cid.cid_rdnis, len);
00102 res = 0;
00103 }
00104 } else {
00105 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00106 }
00107
00108 ast_channel_unlock(chan);
00109 }
00110
00111 return res;
00112 }
00113
00114 static int callerid_write(struct ast_channel *chan, char *cmd, char *data,
00115 const char *value)
00116 {
00117 if (!value || !chan)
00118 return -1;
00119
00120 if (!strncasecmp("all", data, 3)) {
00121 char name[256];
00122 char num[256];
00123
00124 if (!ast_callerid_split(value, name, sizeof(name), num, sizeof(num)))
00125 ast_set_callerid(chan, num, name, num);
00126 } else if (!strncasecmp("name", data, 4)) {
00127 ast_set_callerid(chan, NULL, value, NULL);
00128 } else if (!strncasecmp("num", data, 3) ||
00129 !strncasecmp("number", data, 6)) {
00130 ast_set_callerid(chan, value, NULL, NULL);
00131 } else if (!strncasecmp("ani", data, 3)) {
00132 ast_set_callerid(chan, NULL, NULL, value);
00133 } else if (!strncasecmp("dnid", data, 4)) {
00134 ast_channel_lock(chan);
00135 if (chan->cid.cid_dnid)
00136 free(chan->cid.cid_dnid);
00137 chan->cid.cid_dnid = ast_strdup(value);
00138 ast_channel_unlock(chan);
00139 } else if (!strncasecmp("rdnis", data, 5)) {
00140 ast_channel_lock(chan);
00141 if (chan->cid.cid_rdnis)
00142 free(chan->cid.cid_rdnis);
00143 chan->cid.cid_rdnis = ast_strdup(value);
00144 ast_channel_unlock(chan);
00145 } else {
00146 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00147 }
00148
00149 return 0;
00150 }
00151
00152 static struct ast_custom_function callerid_function = {
00153 .name = "CALLERID",
00154 .synopsis = "Gets or sets Caller*ID data on the channel.",
00155 .syntax = "CALLERID(datatype[,<optional-CID>])",
00156 .desc =
00157 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
00158 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\".\n"
00159 "Uses channel callerid by default or optional callerid, if specified.\n",
00160 .read = callerid_read,
00161 .write = callerid_write,
00162 };
00163
00164 static int unload_module(void)
00165 {
00166 return ast_custom_function_unregister(&callerid_function);
00167 }
00168
00169 static int load_module(void)
00170 {
00171 return ast_custom_function_register(&callerid_function);
00172 }
00173
00174 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan function");