00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "asterisk.h"
00025
00026 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 160857 $")
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 if (!chan)
00057 return -1;
00058
00059 if (strchr(opt, ',')) {
00060 char name[80], num[80];
00061
00062 data = strsep(&opt, ",");
00063 ast_callerid_split(opt, name, sizeof(name), num, sizeof(num));
00064
00065 if (!strncasecmp("all", data, 3)) {
00066 snprintf(buf, len, "\"%s\" <%s>", name, num);
00067 } else if (!strncasecmp("name", data, 4)) {
00068 ast_copy_string(buf, name, len);
00069 } else if (!strncasecmp("num", data, 3)) {
00070
00071 ast_copy_string(buf, num, len);
00072 } else {
00073 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00074 }
00075 } else {
00076 ast_channel_lock(chan);
00077
00078 if (!strncasecmp("all", data, 3)) {
00079 snprintf(buf, len, "\"%s\" <%s>",
00080 S_OR(chan->cid.cid_name, ""),
00081 S_OR(chan->cid.cid_num, ""));
00082 } else if (!strncasecmp("name", data, 4)) {
00083 if (chan->cid.cid_name) {
00084 ast_copy_string(buf, chan->cid.cid_name, len);
00085 }
00086 } else if (!strncasecmp("num", data, 3)) {
00087
00088 if (chan->cid.cid_num) {
00089 ast_copy_string(buf, chan->cid.cid_num, len);
00090 }
00091 } else if (!strncasecmp("ani", data, 3)) {
00092 if (!strncasecmp(data + 3, "2", 1)) {
00093 snprintf(buf, len, "%d", chan->cid.cid_ani2);
00094 } else if (chan->cid.cid_ani) {
00095 ast_copy_string(buf, chan->cid.cid_ani, len);
00096 }
00097 } else if (!strncasecmp("dnid", data, 4)) {
00098 if (chan->cid.cid_dnid) {
00099 ast_copy_string(buf, chan->cid.cid_dnid, len);
00100 }
00101 } else if (!strncasecmp("rdnis", data, 5)) {
00102 if (chan->cid.cid_rdnis) {
00103 ast_copy_string(buf, chan->cid.cid_rdnis, len);
00104 }
00105 } else if (!strncasecmp("pres", data, 4)) {
00106 ast_copy_string(buf, ast_named_caller_presentation(chan->cid.cid_pres), len);
00107 } else if (!strncasecmp("ton", data, 3)) {
00108 snprintf(buf, len, "%d", chan->cid.cid_ton);
00109 } else {
00110 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00111 }
00112
00113 ast_channel_unlock(chan);
00114 }
00115
00116 return 0;
00117 }
00118
00119 static int callerid_write(struct ast_channel *chan, const char *cmd, char *data,
00120 const char *value)
00121 {
00122 if (!value || !chan)
00123 return -1;
00124
00125 if (!strncasecmp("all", data, 3)) {
00126 char name[256];
00127 char num[256];
00128
00129 ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
00130 ast_set_callerid(chan, num, name, num);
00131 if (chan->cdr) {
00132 ast_cdr_setcid(chan->cdr, chan);
00133 }
00134 } else if (!strncasecmp("name", data, 4)) {
00135 ast_set_callerid(chan, NULL, value, NULL);
00136 if (chan->cdr) {
00137 ast_cdr_setcid(chan->cdr, chan);
00138 }
00139 } else if (!strncasecmp("num", data, 3)) {
00140
00141 ast_set_callerid(chan, value, NULL, NULL);
00142 if (chan->cdr) {
00143 ast_cdr_setcid(chan->cdr, chan);
00144 }
00145 } else if (!strncasecmp("ani", data, 3)) {
00146 if (!strncasecmp(data + 3, "2", 1)) {
00147 int i = atoi(value);
00148 chan->cid.cid_ani2 = i;
00149 } else
00150 ast_set_callerid(chan, NULL, NULL, value);
00151 if (chan->cdr) {
00152 ast_cdr_setcid(chan->cdr, chan);
00153 }
00154 } else if (!strncasecmp("dnid", data, 4)) {
00155 ast_channel_lock(chan);
00156 if (chan->cid.cid_dnid)
00157 ast_free(chan->cid.cid_dnid);
00158 chan->cid.cid_dnid = ast_strdup(value);
00159 if (chan->cdr) {
00160 ast_cdr_setcid(chan->cdr, chan);
00161 }
00162 ast_channel_unlock(chan);
00163 } else if (!strncasecmp("rdnis", data, 5)) {
00164 ast_channel_lock(chan);
00165 if (chan->cid.cid_rdnis)
00166 ast_free(chan->cid.cid_rdnis);
00167 chan->cid.cid_rdnis = ast_strdup(value);
00168 if (chan->cdr) {
00169 ast_cdr_setcid(chan->cdr, chan);
00170 }
00171 ast_channel_unlock(chan);
00172 } else if (!strncasecmp("pres", data, 4)) {
00173 int i;
00174 char *s, *val;
00175
00176
00177 while ((value[0] == '\t') || (value[0] == ' '))
00178 ++value;
00179
00180 val = ast_strdupa(value);
00181
00182
00183 s = val + strlen(val);
00184 while ((s != val) && ((s[-1] == '\t') || (s[-1] == ' ')))
00185 --s;
00186 *s = '\0';
00187
00188 if ((val[0] >= '0') && (val[0] <= '9'))
00189 i = atoi(val);
00190 else
00191 i = ast_parse_caller_presentation(val);
00192
00193 if (i < 0)
00194 ast_log(LOG_ERROR, "Unknown calling number presentation '%s', value unchanged\n", val);
00195 else
00196 chan->cid.cid_pres = i;
00197 } else if (!strncasecmp("ton", data, 3)) {
00198 int i = atoi(value);
00199 chan->cid.cid_ton = i;
00200 } else {
00201 ast_log(LOG_ERROR, "Unknown callerid data type '%s'.\n", data);
00202 }
00203
00204 return 0;
00205 }
00206
00207 static struct ast_custom_function callerid_function = {
00208 .name = "CALLERID",
00209 .synopsis = "Gets or sets Caller*ID data on the channel.",
00210 .syntax = "CALLERID(datatype[,<optional-CID>])",
00211 .desc =
00212 "Gets or sets Caller*ID data on the channel. The allowable datatypes\n"
00213 "are \"all\", \"name\", \"num\", \"ANI\", \"DNID\", \"RDNIS\", \"pres\",\n"
00214 "and \"ton\".\n"
00215 "Uses channel callerid by default or optional callerid, if specified.\n",
00216 .read = callerid_read,
00217 .write = callerid_write,
00218 };
00219
00220 static struct ast_custom_function callerpres_function = {
00221 .name = "CALLERPRES",
00222 .synopsis = "Gets or sets Caller*ID presentation on the channel.",
00223 .syntax = "CALLERPRES()",
00224 .desc =
00225 "Gets or sets Caller*ID presentation on the channel. The following values\n"
00226 "are valid:\n"
00227 " allowed_not_screened : Presentation Allowed, Not Screened\n"
00228 " allowed_passed_screen : Presentation Allowed, Passed Screen\n"
00229 " allowed_failed_screen : Presentation Allowed, Failed Screen\n"
00230 " allowed : Presentation Allowed, Network Number\n"
00231 " prohib_not_screened : Presentation Prohibited, Not Screened\n"
00232 " prohib_passed_screen : Presentation Prohibited, Passed Screen\n"
00233 " prohib_failed_screen : Presentation Prohibited, Failed Screen\n"
00234 " prohib : Presentation Prohibited, Network Number\n"
00235 " unavailable : Number Unavailable\n",
00236 .read = callerpres_read,
00237 .write = callerpres_write,
00238 };
00239
00240 static int unload_module(void)
00241 {
00242 int res = ast_custom_function_unregister(&callerpres_function);
00243 res |= ast_custom_function_unregister(&callerid_function);
00244 return res;
00245 }
00246
00247 static int load_module(void)
00248 {
00249 int res = ast_custom_function_register(&callerpres_function);
00250 res |= ast_custom_function_register(&callerid_function);
00251 return res;
00252 }
00253
00254 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Caller ID related dialplan functions");