00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "asterisk.h"
00032
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 263541 $")
00034
00035
00036
00037
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <sys/types.h>
00042
00043 #include "asterisk/module.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/logger.h"
00047 #include "asterisk/utils.h"
00048 #include "asterisk/app.h"
00049 #include "asterisk/options.h"
00050 #include "asterisk/callerid.h"
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 enum ID_FIELD_STATUS {
00111 ID_FIELD_VALID,
00112 ID_FIELD_INVALID,
00113 ID_FIELD_UNKNOWN
00114 };
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 static enum ID_FIELD_STATUS redirecting_id_read(char *buf, size_t len, char *data, const struct ast_party_id *id)
00132 {
00133 enum ID_FIELD_STATUS status;
00134
00135 status = ID_FIELD_VALID;
00136
00137 if (!strncasecmp("all", data, 3)) {
00138 snprintf(buf, len, "\"%s\" <%s>",
00139 S_OR(id->name, ""),
00140 S_OR(id->number, ""));
00141 } else if (!strncasecmp("name", data, 4)) {
00142 if (id->name) {
00143 ast_copy_string(buf, id->name, len);
00144 }
00145 } else if (!strncasecmp("num", data, 3)) {
00146 if (id->number) {
00147 ast_copy_string(buf, id->number, len);
00148 }
00149 } else if (!strncasecmp("tag", data, 3)) {
00150 if (id->tag) {
00151 ast_copy_string(buf, id->tag, len);
00152 }
00153 } else if (!strncasecmp("ton", data, 3)) {
00154 snprintf(buf, len, "%d", id->number_type);
00155 } else if (!strncasecmp("pres", data, 4)) {
00156 ast_copy_string(buf, ast_named_caller_presentation(id->number_presentation), len);
00157 } else {
00158 status = ID_FIELD_UNKNOWN;
00159 }
00160
00161 return status;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 static int redirecting_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
00182 {
00183
00184 *buf = 0;
00185
00186 if (!chan)
00187 return -1;
00188
00189 ast_channel_lock(chan);
00190
00191 if (!strncasecmp("from-", data, 5)) {
00192 switch (redirecting_id_read(buf, len, data + 5, &chan->redirecting.from)) {
00193 case ID_FIELD_VALID:
00194 case ID_FIELD_INVALID:
00195 break;
00196
00197 default:
00198 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00199 break;
00200 }
00201 } else if (!strncasecmp("to-", data, 3)) {
00202 switch (redirecting_id_read(buf, len, data + 3, &chan->redirecting.to)) {
00203 case ID_FIELD_VALID:
00204 case ID_FIELD_INVALID:
00205 break;
00206
00207 default:
00208 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00209 break;
00210 }
00211 } else if (!strncasecmp("pres", data, 4)) {
00212 ast_copy_string(buf, ast_named_caller_presentation(chan->redirecting.from.number_presentation), len);
00213 } else if (!strncasecmp("reason", data, 6)) {
00214 ast_copy_string(buf, ast_redirecting_reason_name(chan->redirecting.reason), len);
00215 } else if (!strncasecmp("count", data, 5)) {
00216 snprintf(buf, len, "%d", chan->redirecting.count);
00217 } else {
00218 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00219 }
00220
00221 ast_channel_unlock(chan);
00222
00223 return 0;
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 static enum ID_FIELD_STATUS redirecting_id_write(struct ast_party_id *id, char *data, const char *value)
00243 {
00244 char *val;
00245 enum ID_FIELD_STATUS status;
00246
00247 status = ID_FIELD_VALID;
00248
00249 if (!strncasecmp("all", data, 3)) {
00250 char name[256];
00251 char num[256];
00252
00253 ast_callerid_split(value, name, sizeof(name), num, sizeof(num));
00254 if (!(id->name = ast_strdup(name))) {
00255 return ID_FIELD_INVALID;
00256 }
00257 if (!(id->number = ast_strdup(num))) {
00258 return ID_FIELD_INVALID;
00259 }
00260 } else if (!strncasecmp("name", data, 4)) {
00261 id->name = ast_strdup(value);
00262 ast_trim_blanks(id->name);
00263 } else if (!strncasecmp("num", data, 3)) {
00264 id->number = ast_strdup(value);
00265 ast_trim_blanks(id->number);
00266 } else if (!strncasecmp("tag", data, 3)) {
00267 id->tag = ast_strdup(value);
00268 ast_trim_blanks(id->tag);
00269 } else if (!strncasecmp("ton", data, 3)) {
00270 val = ast_strdupa(value);
00271 ast_trim_blanks(val);
00272
00273 if (('0' <= val[0]) && (val[0] <= '9')) {
00274 id->number_type = atoi(val);
00275 } else {
00276 ast_log(LOG_ERROR, "Unknown redirecting type of number '%s', value unchanged\n", val);
00277 status = ID_FIELD_INVALID;
00278 }
00279 } else if (!strncasecmp("pres", data, 4)) {
00280 int pres;
00281
00282 val = ast_strdupa(value);
00283 ast_trim_blanks(val);
00284
00285 if (('0' <= val[0]) && (val[0] <= '9')) {
00286 pres = atoi(val);
00287 } else {
00288 pres = ast_parse_caller_presentation(val);
00289 }
00290
00291 if (pres < 0) {
00292 ast_log(LOG_ERROR, "Unknown redirecting number presentation '%s', value unchanged\n", val);
00293 status = ID_FIELD_INVALID;
00294 } else {
00295 id->number_presentation = pres;
00296 }
00297 } else {
00298 status = ID_FIELD_UNKNOWN;
00299 }
00300
00301 return status;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 static int redirecting_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
00321 {
00322 struct ast_party_redirecting redirecting;
00323 char *val;
00324 char *option;
00325 void (*set_it)(struct ast_channel *chan, const struct ast_party_redirecting *redirecting);
00326
00327 if (!value || !chan) {
00328 return -1;
00329 }
00330
00331
00332 option = strchr(data, ',');
00333 if (option) {
00334 option = ast_skip_blanks(option + 1);
00335 switch (*option) {
00336 case 'i':
00337 set_it = ast_channel_set_redirecting;
00338 break;
00339
00340 default:
00341 ast_log(LOG_ERROR, "Unknown redirecting option '%s'.\n", option);
00342 return 0;
00343 }
00344 }
00345 else {
00346 set_it = ast_channel_update_redirecting;
00347 }
00348
00349 ast_channel_lock(chan);
00350 ast_party_redirecting_set_init(&redirecting, &chan->redirecting);
00351 ast_channel_unlock(chan);
00352
00353 value = ast_skip_blanks(value);
00354
00355 if (!strncasecmp("from-", data, 5)) {
00356 switch (redirecting_id_write(&redirecting.from, data + 5, value)) {
00357 case ID_FIELD_VALID:
00358 set_it(chan, &redirecting);
00359 break;
00360
00361 case ID_FIELD_INVALID:
00362 break;
00363
00364 default:
00365 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00366 break;
00367 }
00368 ast_party_redirecting_free(&redirecting);
00369 } else if (!strncasecmp("to-", data, 3)) {
00370 switch (redirecting_id_write(&redirecting.to, data + 3, value)) {
00371 case ID_FIELD_VALID:
00372 set_it(chan, &redirecting);
00373 break;
00374
00375 case ID_FIELD_INVALID:
00376 break;
00377
00378 default:
00379 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00380 break;
00381 }
00382 ast_party_redirecting_free(&redirecting);
00383 } else if (!strncasecmp("pres", data, 4)) {
00384 int pres;
00385
00386 val = ast_strdupa(value);
00387 ast_trim_blanks(val);
00388
00389 if (('0' <= val[0]) && (val[0] <= '9')) {
00390 pres = atoi(val);
00391 } else {
00392 pres = ast_parse_caller_presentation(val);
00393 }
00394
00395 if (pres < 0) {
00396 ast_log(LOG_ERROR, "Unknown redirecting number presentation '%s', value unchanged\n", val);
00397 } else {
00398 redirecting.from.number_presentation = pres;
00399 redirecting.to.number_presentation = pres;
00400 set_it(chan, &redirecting);
00401 }
00402 } else if (!strncasecmp("reason", data, 6)) {
00403 int reason;
00404
00405 val = ast_strdupa(value);
00406 ast_trim_blanks(val);
00407
00408 if (('0' <= val[0]) && (val[0] <= '9')) {
00409 reason = atoi(val);
00410 } else {
00411 reason = ast_redirecting_reason_parse(val);
00412 }
00413
00414 if (reason < 0) {
00415 ast_log(LOG_ERROR, "Unknown redirecting reason '%s', value unchanged\n", val);
00416 } else {
00417 redirecting.reason = reason;
00418 set_it(chan, &redirecting);
00419 }
00420 } else if (!strncasecmp("count", data, 5)) {
00421 val = ast_strdupa(value);
00422 ast_trim_blanks(val);
00423
00424 if (('0' <= val[0]) && (val[0] <= '9')) {
00425 redirecting.count = atoi(val);
00426 set_it(chan, &redirecting);
00427 } else {
00428 ast_log(LOG_ERROR, "Unknown redirecting count '%s', value unchanged\n", val);
00429 }
00430 } else {
00431 ast_log(LOG_ERROR, "Unknown redirecting data type '%s'.\n", data);
00432 }
00433
00434 return 0;
00435 }
00436
00437
00438
00439
00440 static struct ast_custom_function redirecting_function = {
00441 .name = "REDIRECTING",
00442 .read = redirecting_read,
00443 .write = redirecting_write,
00444 };
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 static int unload_module(void)
00458 {
00459 return ast_custom_function_unregister(&redirecting_function);
00460 }
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 static int load_module(void)
00474 {
00475 return ast_custom_function_register(&redirecting_function)
00476 ? AST_MODULE_LOAD_DECLINE
00477 : AST_MODULE_LOAD_SUCCESS;
00478 }
00479
00480
00481
00482
00483 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Redirecting data dialplan function");
00484
00485
00486
00487