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 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 303141 $")
00029
00030 #include "asterisk/_private.h"
00031
00032 #include <pthread.h>
00033 #include <signal.h>
00034 #include <sys/time.h>
00035 #include <sys/signal.h>
00036 #include <netinet/in.h>
00037
00038 #include "asterisk/lock.h"
00039 #include "asterisk/file.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/causes.h"
00043 #include "asterisk/module.h"
00044 #include "asterisk/translate.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/say.h"
00047 #include "asterisk/features.h"
00048 #include "asterisk/musiconhold.h"
00049 #include "asterisk/config.h"
00050 #include "asterisk/cli.h"
00051 #include "asterisk/manager.h"
00052 #include "asterisk/utils.h"
00053 #include "asterisk/adsi.h"
00054 #include "asterisk/devicestate.h"
00055 #include "asterisk/monitor.h"
00056 #include "asterisk/audiohook.h"
00057 #include "asterisk/global_datastores.h"
00058 #include "asterisk/astobj2.h"
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
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 #define DEFAULT_PARK_TIME 45000
00214 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
00215 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000
00216 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
00217 #define DEFAULT_PARKINGLOT "default"
00218 #define DEFAULT_ATXFER_DROP_CALL 0
00219 #define DEFAULT_ATXFER_LOOP_DELAY 10000
00220 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
00221
00222 #define AST_MAX_WATCHERS 256
00223 #define MAX_DIAL_FEATURE_OPTIONS 30
00224
00225 struct feature_group_exten {
00226 AST_LIST_ENTRY(feature_group_exten) entry;
00227 AST_DECLARE_STRING_FIELDS(
00228 AST_STRING_FIELD(exten);
00229 );
00230 struct ast_call_feature *feature;
00231 };
00232
00233 struct feature_group {
00234 AST_LIST_ENTRY(feature_group) entry;
00235 AST_DECLARE_STRING_FIELDS(
00236 AST_STRING_FIELD(gname);
00237 );
00238 AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
00239 };
00240
00241 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
00242
00243 static char *parkedcall = "ParkedCall";
00244
00245 static char pickup_ext[AST_MAX_EXTENSION];
00246
00247
00248
00249
00250 struct parkeduser {
00251 struct ast_channel *chan;
00252 struct timeval start;
00253 int parkingnum;
00254 char parkingexten[AST_MAX_EXTENSION];
00255 char context[AST_MAX_CONTEXT];
00256 char exten[AST_MAX_EXTENSION];
00257 int priority;
00258 int parkingtime;
00259 unsigned int notquiteyet:1;
00260 unsigned int options_specified:1;
00261 char peername[1024];
00262 unsigned char moh_trys;
00263 struct ast_parkinglot *parkinglot;
00264 AST_LIST_ENTRY(parkeduser) list;
00265 };
00266
00267
00268 struct ast_parkinglot {
00269 char name[AST_MAX_CONTEXT];
00270 char parking_con[AST_MAX_EXTENSION];
00271 char parking_con_dial[AST_MAX_EXTENSION];
00272 int parking_start;
00273 int parking_stop;
00274 int parking_offset;
00275 int parkfindnext;
00276 int parkingtime;
00277 char mohclass[MAX_MUSICCLASS];
00278 int parkaddhints;
00279 int parkedcalltransfers;
00280 int parkedcallreparking;
00281 int parkedcallhangup;
00282 int parkedcallrecording;
00283 AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
00284 };
00285
00286
00287 static struct ao2_container *parkinglots;
00288
00289 struct ast_parkinglot *default_parkinglot;
00290 char parking_ext[AST_MAX_EXTENSION];
00291
00292 static char courtesytone[256];
00293 static int parkedplay = 0;
00294 static char xfersound[256];
00295 static char xferfailsound[256];
00296 static char pickupsound[256];
00297 static char pickupfailsound[256];
00298
00299 static int adsipark;
00300
00301 static int transferdigittimeout;
00302 static int featuredigittimeout;
00303 static int comebacktoorigin = 1;
00304
00305 static int atxfernoanswertimeout;
00306 static unsigned int atxferdropcall;
00307 static unsigned int atxferloopdelay;
00308 static unsigned int atxfercallbackretries;
00309
00310 static char *registrar = "features";
00311
00312
00313 static char *parkcall = PARK_APP_NAME;
00314
00315 static struct ast_app *monitor_app = NULL;
00316 static int monitor_ok = 1;
00317
00318 static struct ast_app *mixmonitor_app = NULL;
00319 static int mixmonitor_ok = 1;
00320
00321 static struct ast_app *stopmixmonitor_app = NULL;
00322 static int stopmixmonitor_ok = 1;
00323
00324 static pthread_t parking_thread;
00325 struct ast_dial_features {
00326 struct ast_flags features_caller;
00327 struct ast_flags features_callee;
00328 int is_caller;
00329 };
00330
00331 #if defined(ATXFER_NULL_TECH)
00332 static struct ast_frame *null_read(struct ast_channel *chan)
00333 {
00334
00335 return NULL;
00336 }
00337
00338 static struct ast_frame *null_exception(struct ast_channel *chan)
00339 {
00340
00341 return NULL;
00342 }
00343
00344 static int null_write(struct ast_channel *chan, struct ast_frame *frame)
00345 {
00346
00347 return -1;
00348 }
00349
00350 static int null_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00351 {
00352
00353 return 0;
00354 }
00355
00356 static int null_hangup(struct ast_channel *chan)
00357 {
00358 chan->tech_pvt = NULL;
00359 return 0;
00360 }
00361
00362 static const struct ast_channel_tech null_tech = {
00363 .type = "NULL",
00364 .description = "NULL channel driver for atxfer",
00365 .capabilities = -1,
00366 .read = null_read,
00367 .exception = null_exception,
00368 .write = null_write,
00369 .fixup = null_fixup,
00370 .hangup = null_hangup,
00371 };
00372 #endif
00373
00374 #if defined(ATXFER_NULL_TECH)
00375
00376
00377
00378
00379
00380
00381
00382
00383 static void set_null_chan_tech(struct ast_channel *chan)
00384 {
00385 int idx;
00386
00387 ast_channel_lock(chan);
00388
00389
00390 if (chan->tech->hangup) {
00391 chan->tech->hangup(chan);
00392 }
00393 if (chan->tech_pvt) {
00394 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
00395 chan->name);
00396 ast_free(chan->tech_pvt);
00397 chan->tech_pvt = NULL;
00398 }
00399
00400
00401 chan->tech = &null_tech;
00402 for (idx = 0; idx < AST_MAX_FDS; ++idx) {
00403 switch (idx) {
00404 case AST_ALERT_FD:
00405 case AST_TIMING_FD:
00406 case AST_GENERATOR_FD:
00407
00408 break;
00409 default:
00410 ast_channel_set_fd(chan, idx, -1);
00411 break;
00412 }
00413 }
00414 ast_queue_frame(chan, &ast_null_frame);
00415
00416 ast_channel_unlock(chan);
00417 }
00418 #endif
00419
00420 #if defined(ATXFER_NULL_TECH)
00421
00422
00423
00424
00425
00426
00427
00428
00429 static void set_new_chan_name(struct ast_channel *chan)
00430 {
00431 char *orig_name;
00432 static int seq_num;
00433
00434 ast_channel_lock(chan);
00435
00436 orig_name = ast_strdupa(chan->name);
00437 ast_string_field_build(chan, name, "%s<XFER_%x>", orig_name,
00438 ast_atomic_fetchadd_int(&seq_num, +1));
00439
00440 ast_channel_unlock(chan);
00441 }
00442 #endif
00443
00444 static void *dial_features_duplicate(void *data)
00445 {
00446 struct ast_dial_features *df = data, *df_copy;
00447
00448 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
00449 return NULL;
00450 }
00451
00452 memcpy(df_copy, df, sizeof(*df));
00453
00454 return df_copy;
00455 }
00456
00457 static void dial_features_destroy(void *data)
00458 {
00459 struct ast_dial_features *df = data;
00460 if (df) {
00461 ast_free(df);
00462 }
00463 }
00464
00465 const struct ast_datastore_info dial_features_info = {
00466 .type = "dial-features",
00467 .destroy = dial_features_destroy,
00468 .duplicate = dial_features_duplicate,
00469 };
00470
00471
00472 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
00473 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
00474 static void parkinglot_destroy(void *obj);
00475 int manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, const int nfds, struct pollfd **new_pfds, int *new_nfds, int *fs);
00476 struct ast_parkinglot *find_parkinglot(const char *name);
00477
00478
00479 const char *ast_parking_ext(void)
00480 {
00481 return parking_ext;
00482 }
00483
00484 const char *ast_pickup_ext(void)
00485 {
00486 return pickup_ext;
00487 }
00488
00489 struct ast_bridge_thread_obj
00490 {
00491 struct ast_bridge_config bconfig;
00492 struct ast_channel *chan;
00493 struct ast_channel *peer;
00494 unsigned int return_to_pbx:1;
00495 };
00496
00497 static int parkinglot_hash_cb(const void *obj, const int flags)
00498 {
00499 const struct ast_parkinglot *parkinglot = obj;
00500
00501 return ast_str_case_hash(parkinglot->name);
00502 }
00503
00504 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
00505 {
00506 struct ast_parkinglot *parkinglot = obj, *parkinglot2 = arg;
00507
00508 return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
00509 }
00510
00511
00512
00513
00514
00515 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
00516 {
00517 ast_copy_string(chan->context, context, sizeof(chan->context));
00518 ast_copy_string(chan->exten, ext, sizeof(chan->exten));
00519 chan->priority = pri;
00520 }
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530 static void check_goto_on_transfer(struct ast_channel *chan)
00531 {
00532 struct ast_channel *xferchan;
00533 const char *val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
00534 char *x, *goto_on_transfer;
00535 struct ast_frame *f;
00536
00537 if (ast_strlen_zero(val))
00538 return;
00539
00540 goto_on_transfer = ast_strdupa(val);
00541
00542 if (!(xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "%s", chan->name)))
00543 return;
00544
00545 for (x = goto_on_transfer; x && *x; x++) {
00546 if (*x == '^')
00547 *x = '|';
00548 }
00549
00550 xferchan->readformat = chan->readformat;
00551 xferchan->writeformat = chan->writeformat;
00552 ast_channel_masquerade(xferchan, chan);
00553 ast_parseable_goto(xferchan, goto_on_transfer);
00554 xferchan->_state = AST_STATE_UP;
00555 ast_clear_flag(xferchan, AST_FLAGS_ALL);
00556 xferchan->_softhangup = 0;
00557 if ((f = ast_read(xferchan))) {
00558 ast_frfree(f);
00559 f = NULL;
00560 ast_pbx_start(xferchan);
00561 } else {
00562 ast_hangup(xferchan);
00563 }
00564 }
00565
00566 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
00567 const char *caller_name, struct ast_channel *transferee, const char *type,
00568 int format, void *data, int timeout, int *outstate, const char *cid_num,
00569 const char *cid_name, const char *language);
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 static void *bridge_call_thread(void *data)
00580 {
00581 struct ast_bridge_thread_obj *tobj = data;
00582 int res;
00583
00584 tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00585 tobj->chan->data = tobj->peer->name;
00586 tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00587 tobj->peer->data = tobj->chan->name;
00588
00589 ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
00590
00591 if (tobj->return_to_pbx) {
00592 if (!ast_check_hangup(tobj->peer)) {
00593 ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
00594 res = ast_pbx_start(tobj->peer);
00595 if (res != AST_PBX_SUCCESS)
00596 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
00597 } else
00598 ast_hangup(tobj->peer);
00599 if (!ast_check_hangup(tobj->chan)) {
00600 ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
00601 res = ast_pbx_start(tobj->chan);
00602 if (res != AST_PBX_SUCCESS)
00603 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
00604 } else
00605 ast_hangup(tobj->chan);
00606 } else {
00607 ast_hangup(tobj->chan);
00608 ast_hangup(tobj->peer);
00609 }
00610
00611 ast_free(tobj);
00612
00613 return NULL;
00614 }
00615
00616
00617
00618
00619
00620
00621
00622 static void bridge_call_thread_launch(void *data)
00623 {
00624 pthread_t thread;
00625 pthread_attr_t attr;
00626 struct sched_param sched;
00627
00628 pthread_attr_init(&attr);
00629 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
00630 ast_pthread_create(&thread, &attr, bridge_call_thread, data);
00631 pthread_attr_destroy(&attr);
00632 memset(&sched, 0, sizeof(sched));
00633 pthread_setschedparam(thread, SCHED_RR, &sched);
00634 }
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
00645 {
00646 int res;
00647 int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
00648 char tmp[256];
00649 char *message[5] = {NULL, NULL, NULL, NULL, NULL};
00650
00651 snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
00652 message[0] = tmp;
00653 res = ast_adsi_load_session(chan, NULL, 0, 1);
00654 if (res == -1)
00655 return res;
00656 return ast_adsi_print(chan, message, justify, 1);
00657 }
00658
00659
00660 static const char *findparkinglotname(struct ast_channel *chan)
00661 {
00662 const char *temp, *parkinglot = NULL;
00663
00664
00665 if (!ast_strlen_zero(chan->parkinglot))
00666 parkinglot = chan->parkinglot;
00667
00668
00669
00670 if ((temp = pbx_builtin_getvar_helper(chan, "PARKINGLOT")))
00671 return temp;
00672
00673 return parkinglot;
00674 }
00675
00676
00677 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
00678 {
00679 ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
00680 exten, context, ast_devstate2str(state));
00681
00682 ast_devstate_changed(state, "park:%s@%s", exten, context);
00683 }
00684
00685
00686 static enum ast_device_state metermaidstate(const char *data)
00687 {
00688 char *context;
00689 char *exten;
00690
00691 context = ast_strdupa(data);
00692
00693 exten = strsep(&context, "@");
00694 if (!context)
00695 return AST_DEVICE_INVALID;
00696
00697 ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
00698
00699 if (!ast_exists_extension(NULL, context, exten, 1, NULL))
00700 return AST_DEVICE_NOT_INUSE;
00701
00702 return AST_DEVICE_INUSE;
00703 }
00704
00705
00706 enum ast_park_call_options {
00707
00708 AST_PARK_OPT_RINGING = (1 << 0),
00709
00710
00711 AST_PARK_OPT_RANDOMIZE = (1 << 1),
00712
00713 AST_PARK_OPT_SILENCE = (1 << 2),
00714 };
00715
00716 struct ast_park_call_args {
00717
00718
00719
00720 int timeout;
00721
00722
00723 int *extout;
00724 const char *orig_chan_name;
00725 const char *return_con;
00726 const char *return_ext;
00727 int return_pri;
00728 uint32_t flags;
00729
00730 struct parkeduser *pu;
00731 };
00732
00733 static struct parkeduser *park_space_reserve(struct ast_channel *chan,
00734 struct ast_channel *peer, struct ast_park_call_args *args)
00735 {
00736 struct parkeduser *pu;
00737 int i, parking_space = -1, parking_range;
00738 const char *parkinglotname = NULL;
00739 const char *parkingexten;
00740 struct ast_parkinglot *parkinglot = NULL;
00741
00742 if (peer)
00743 parkinglotname = findparkinglotname(peer);
00744 else
00745 parkinglotname = findparkinglotname(chan);
00746
00747 if (parkinglotname) {
00748 if (option_debug)
00749 ast_log(LOG_DEBUG, "Found chanvar Parkinglot: %s\n", parkinglotname);
00750 parkinglot = find_parkinglot(parkinglotname);
00751 }
00752 if (!parkinglot) {
00753 parkinglot = parkinglot_addref(default_parkinglot);
00754 }
00755
00756 if (option_debug)
00757 ast_log(LOG_DEBUG, "Parkinglot: %s\n", parkinglot->name);
00758
00759
00760 if (!(pu = ast_calloc(1, sizeof(*pu)))) {
00761 parkinglot_unref(parkinglot);
00762 return NULL;
00763 }
00764
00765
00766 AST_LIST_LOCK(&parkinglot->parkings);
00767
00768 ast_channel_lock(chan);
00769 parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGEXTEN"), ""));
00770 ast_channel_unlock(chan);
00771 if (!ast_strlen_zero(parkingexten)) {
00772
00773
00774
00775
00776
00777
00778 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space < 0) {
00779 AST_LIST_UNLOCK(&parkinglot->parkings);
00780 parkinglot_unref(parkinglot);
00781 free(pu);
00782 ast_log(LOG_WARNING, "PARKINGEXTEN does not indicate a valid parking slot: '%s'.\n", parkingexten);
00783 return NULL;
00784 }
00785 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
00786
00787 if (ast_exists_extension(NULL, parkinglot->parking_con, pu->parkingexten, 1, NULL)) {
00788 ast_log(LOG_WARNING, "Requested parking extension already exists: %s@%s\n", parkingexten, parkinglot->parking_con);
00789 AST_LIST_UNLOCK(&parkinglot->parkings);
00790 parkinglot_unref(parkinglot);
00791 ast_free(pu);
00792 return NULL;
00793 }
00794 } else {
00795 int start;
00796 struct parkeduser *cur = NULL;
00797
00798
00799 parking_range = parkinglot->parking_stop - parkinglot->parking_start + 1;
00800
00801 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
00802 start = ast_random() % (parkinglot->parking_stop - parkinglot->parking_start + 1);
00803 } else {
00804 start = parkinglot->parking_start;
00805 }
00806
00807 for (i = start; 1; i++) {
00808 if (i == parkinglot->parking_stop + 1) {
00809 i = parkinglot->parking_start - 1;
00810 break;
00811 }
00812
00813 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
00814 if (cur->parkingnum == i) {
00815 break;
00816 }
00817 }
00818 if (!cur) {
00819 parking_space = i;
00820 break;
00821 }
00822 }
00823
00824 if (i == start - 1 && cur) {
00825 ast_log(LOG_WARNING, "No more parking spaces\n");
00826 ast_free(pu);
00827 AST_LIST_UNLOCK(&parkinglot->parkings);
00828 parkinglot_unref(parkinglot);
00829 return NULL;
00830 }
00831
00832 if (parkinglot->parkfindnext)
00833 parkinglot->parking_offset = parking_space - parkinglot->parking_start + 1;
00834 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
00835 }
00836
00837 pu->notquiteyet = 1;
00838 pu->parkingnum = parking_space;
00839 pu->parkinglot = parkinglot_addref(parkinglot);
00840 AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
00841 parkinglot_unref(parkinglot);
00842
00843 return pu;
00844 }
00845
00846
00847 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
00848 {
00849 struct ast_context *con;
00850 int parkingnum_copy;
00851 struct parkeduser *pu = args->pu;
00852 const char *event_from;
00853
00854 if (pu == NULL)
00855 pu = park_space_reserve(chan, peer, args);
00856 if (pu == NULL)
00857 return 1;
00858
00859 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", pu->parkingnum);
00860
00861 chan->appl = "Parked Call";
00862 chan->data = NULL;
00863
00864 pu->chan = chan;
00865
00866
00867 if (chan != peer) {
00868 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
00869 ast_indicate(pu->chan, AST_CONTROL_RINGING);
00870 } else {
00871 ast_indicate_data(pu->chan, AST_CONTROL_HOLD,
00872 S_OR(pu->parkinglot->mohclass, NULL),
00873 !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
00874 }
00875 }
00876
00877 pu->start = ast_tvnow();
00878 pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->parkingtime;
00879 parkingnum_copy = pu->parkingnum;
00880 if (args->extout)
00881 *(args->extout) = pu->parkingnum;
00882
00883 if (peer) {
00884
00885
00886
00887
00888
00889 if (!strcasecmp(peer->tech->type, "Local")) {
00890 struct ast_channel *tmpchan, *base_peer;
00891 char other_side[AST_CHANNEL_NAME];
00892 char *c;
00893 ast_copy_string(other_side, S_OR(args->orig_chan_name, peer->name), sizeof(other_side));
00894 if ((c = strrchr(other_side, ';'))) {
00895 *++c = '1';
00896 }
00897 if ((tmpchan = ast_get_channel_by_name_locked(other_side))) {
00898 if ((base_peer = ast_bridged_channel(tmpchan))) {
00899 ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
00900 }
00901 ast_channel_unlock(tmpchan);
00902 }
00903 } else {
00904 ast_copy_string(pu->peername, S_OR(args->orig_chan_name, peer->name), sizeof(pu->peername));
00905 }
00906 }
00907
00908
00909
00910
00911 pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
00912
00913
00914
00915
00916
00917 ast_copy_string(pu->context,
00918 S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)),
00919 sizeof(pu->context));
00920 ast_copy_string(pu->exten,
00921 S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)),
00922 sizeof(pu->exten));
00923 pu->priority = args->return_pri ? args->return_pri :
00924 (chan->macropriority ? chan->macropriority : chan->priority);
00925
00926
00927
00928 if (peer != chan)
00929 pu->notquiteyet = 0;
00930
00931
00932 pthread_kill(parking_thread, SIGURG);
00933 ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->context, pu->exten, pu->priority, (pu->parkingtime/1000));
00934
00935 if (peer) {
00936 event_from = peer->name;
00937 } else {
00938 event_from = pbx_builtin_getvar_helper(chan, "BLINDTRANSFER");
00939 }
00940
00941 manager_event(EVENT_FLAG_CALL, "ParkedCall",
00942 "Exten: %s\r\n"
00943 "Channel: %s\r\n"
00944 "Parkinglot: %s\r\n"
00945 "From: %s\r\n"
00946 "Timeout: %ld\r\n"
00947 "CallerIDNum: %s\r\n"
00948 "CallerIDName: %s\r\n"
00949 "Uniqueid: %s\r\n",
00950 pu->parkingexten, pu->chan->name, pu->parkinglot->name, event_from ? event_from : "",
00951 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
00952 S_OR(pu->chan->cid.cid_num, "<unknown>"),
00953 S_OR(pu->chan->cid.cid_name, "<unknown>"),
00954 pu->chan->uniqueid
00955 );
00956
00957 if (peer && adsipark && ast_adsi_available(peer)) {
00958 adsi_announce_park(peer, pu->parkingexten);
00959 ast_adsi_unload_session(peer);
00960 }
00961
00962 con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con, registrar);
00963 if (!con)
00964 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con);
00965 if (con) {
00966 if (!ast_add_extension2(con, 1, pu->parkingexten, 1, NULL, NULL, parkedcall, ast_strdup(pu->parkingexten), ast_free_ptr, registrar))
00967 notify_metermaids(pu->parkingexten, pu->parkinglot->parking_con, AST_DEVICE_INUSE);
00968 }
00969
00970 AST_LIST_UNLOCK(&pu->parkinglot->parkings);
00971
00972
00973 if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE) && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
00974
00975 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
00976
00977 ast_say_digits(peer, pu->parkingnum, "", peer->language);
00978 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
00979 }
00980 if (peer == chan) {
00981
00982 ast_indicate_data(pu->chan, AST_CONTROL_HOLD,
00983 S_OR(pu->parkinglot->mohclass, NULL),
00984 !ast_strlen_zero(pu->parkinglot->mohclass) ? strlen(pu->parkinglot->mohclass) + 1 : 0);
00985 pu->notquiteyet = 0;
00986 pthread_kill(parking_thread, SIGURG);
00987 }
00988 return 0;
00989 }
00990
00991
00992 int ast_park_call(struct ast_channel *chan, struct ast_channel *peer, int timeout, int *extout)
00993 {
00994 struct ast_park_call_args args = {
00995 .timeout = timeout,
00996 .extout = extout,
00997 };
00998
00999 return park_call_full(chan, peer, &args);
01000 }
01001
01002 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout, int play_announcement, struct ast_park_call_args *args)
01003 {
01004 struct ast_channel *chan;
01005 struct ast_frame *f;
01006 int park_status;
01007 struct ast_park_call_args park_args = {0,};
01008
01009 if (!args) {
01010 args = &park_args;
01011 args->timeout = timeout;
01012 args->extout = extout;
01013 }
01014
01015 if ((args->pu = park_space_reserve(rchan, peer, args)) == NULL) {
01016 if (peer)
01017 ast_stream_and_wait(peer, "beeperr", "");
01018 return AST_FEATURE_RETURN_PARKFAILED;
01019 }
01020
01021
01022 if (!(chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten, rchan->context, rchan->amaflags, "Parked/%s",rchan->name))) {
01023 ast_log(LOG_WARNING, "Unable to create parked channel\n");
01024 return -1;
01025 }
01026
01027
01028 chan->readformat = rchan->readformat;
01029 chan->writeformat = rchan->writeformat;
01030 ast_channel_masquerade(chan, rchan);
01031
01032
01033 set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
01034
01035
01036 ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
01037 ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
01038 chan->macropriority = rchan->macropriority;
01039
01040
01041 if ((f = ast_read(chan)))
01042 ast_frfree(f);
01043
01044 if (peer == rchan) {
01045 peer = chan;
01046 }
01047
01048 if (peer && (!play_announcement && args == &park_args)) {
01049 args->orig_chan_name = ast_strdupa(peer->name);
01050 }
01051
01052 park_status = park_call_full(chan, peer, args);
01053 if (park_status == 1) {
01054
01055 ast_hangup(chan);
01056 return -1;
01057 }
01058
01059 return 0;
01060 }
01061
01062
01063 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
01064 {
01065 return masq_park_call(rchan, peer, timeout, extout, 0, NULL);
01066 }
01067
01068 static int masq_park_call_announce_args(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
01069 {
01070 return masq_park_call(rchan, peer, 0, NULL, 1, args);
01071 }
01072
01073 static int masq_park_call_announce(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
01074 {
01075 return masq_park_call(rchan, peer, timeout, extout, 1, NULL);
01076 }
01077
01078 #define FEATURE_SENSE_CHAN (1 << 0)
01079 #define FEATURE_SENSE_PEER (1 << 1)
01080
01081
01082
01083
01084
01085
01086
01087 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
01088 struct ast_channel *peer, struct ast_channel *chan, int sense)
01089 {
01090 if (sense == FEATURE_SENSE_PEER) {
01091 *caller = peer;
01092 *callee = chan;
01093 } else {
01094 *callee = peer;
01095 *caller = chan;
01096 }
01097 }
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01112 {
01113 struct ast_channel *parker;
01114 struct ast_channel *parkee;
01115 int res = 0;
01116
01117 set_peers(&parker, &parkee, peer, chan, sense);
01118
01119
01120
01121
01122
01123
01124
01125
01126 if (chan->_state != AST_STATE_UP)
01127 res = ast_answer(chan);
01128 if (!res)
01129 res = ast_safe_sleep(chan, 1000);
01130
01131 if (!res) {
01132 res = masq_park_call_announce(parkee, parker, 0, NULL);
01133
01134 }
01135
01136 return res;
01137 }
01138
01139
01140
01141
01142 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
01143 {
01144
01145 if (ast_autoservice_start(callee_chan))
01146 return -1;
01147 ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
01148 if (ast_stream_and_wait(caller_chan, audiofile, "")) {
01149 ast_log(LOG_WARNING, "Failed to play automon message!\n");
01150 ast_autoservice_stop(callee_chan);
01151 return -1;
01152 }
01153 if (ast_autoservice_stop(callee_chan))
01154 return -1;
01155
01156 if (ast_autoservice_start(caller_chan))
01157 return -1;
01158 ast_autoservice_ignore(caller_chan, AST_FRAME_DTMF_END);
01159 if (ast_stream_and_wait(callee_chan, audiofile, "")) {
01160 ast_log(LOG_WARNING, "Failed to play automon message !\n");
01161 ast_autoservice_stop(caller_chan);
01162 return -1;
01163 }
01164 if (ast_autoservice_stop(caller_chan))
01165 return -1;
01166 return(0);
01167 }
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01184 {
01185 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
01186 int x = 0;
01187 size_t len;
01188 struct ast_channel *caller_chan, *callee_chan;
01189 const char *automon_message_start = NULL;
01190 const char *automon_message_stop = NULL;
01191
01192 if (!monitor_ok) {
01193 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
01194 return -1;
01195 }
01196
01197 if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
01198 monitor_ok = 0;
01199 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
01200 return -1;
01201 }
01202
01203 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
01204 if (caller_chan) {
01205 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
01206 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
01207 }
01208
01209 if (!ast_strlen_zero(courtesytone)) {
01210 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
01211 return -1;
01212 }
01213 }
01214
01215 if (callee_chan->monitor) {
01216 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
01217 if (!ast_strlen_zero(automon_message_stop)) {
01218 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
01219 }
01220 callee_chan->monitor->stop(callee_chan, 1);
01221 return AST_FEATURE_RETURN_SUCCESS;
01222 }
01223
01224 if (caller_chan && callee_chan) {
01225 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
01226 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
01227 const char *touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
01228
01229 if (!touch_format)
01230 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
01231
01232 if (!touch_monitor)
01233 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
01234
01235 if (!touch_monitor_prefix)
01236 touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
01237
01238 if (touch_monitor) {
01239 len = strlen(touch_monitor) + 50;
01240 args = alloca(len);
01241 touch_filename = alloca(len);
01242 snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
01243 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
01244 } else {
01245 caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
01246 callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
01247 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
01248 args = alloca(len);
01249 touch_filename = alloca(len);
01250 snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
01251 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
01252 }
01253
01254 for(x = 0; x < strlen(args); x++) {
01255 if (args[x] == '/')
01256 args[x] = '-';
01257 }
01258
01259 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
01260
01261 pbx_exec(callee_chan, monitor_app, args);
01262 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
01263 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
01264
01265 if (!ast_strlen_zero(automon_message_start)) {
01266 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
01267 }
01268
01269 return AST_FEATURE_RETURN_SUCCESS;
01270 }
01271
01272 ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
01273 return -1;
01274 }
01275
01276 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01277 {
01278 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
01279 int x = 0;
01280 size_t len;
01281 struct ast_channel *caller_chan, *callee_chan;
01282 const char *mixmonitor_spy_type = "MixMonitor";
01283 int count = 0;
01284
01285 if (!mixmonitor_ok) {
01286 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
01287 return -1;
01288 }
01289
01290 if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
01291 mixmonitor_ok = 0;
01292 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
01293 return -1;
01294 }
01295
01296 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
01297
01298 if (!ast_strlen_zero(courtesytone)) {
01299 if (ast_autoservice_start(callee_chan))
01300 return -1;
01301 ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
01302 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
01303 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
01304 ast_autoservice_stop(callee_chan);
01305 return -1;
01306 }
01307 if (ast_autoservice_stop(callee_chan))
01308 return -1;
01309 }
01310
01311 ast_channel_lock(callee_chan);
01312 count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
01313 ast_channel_unlock(callee_chan);
01314
01315
01316 if (count > 0) {
01317
01318 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
01319
01320
01321 ast_channel_lock(callee_chan);
01322 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
01323 ast_channel_unlock(callee_chan);
01324 if (count > 0) {
01325 if (!stopmixmonitor_ok) {
01326 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
01327 return -1;
01328 }
01329 if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
01330 stopmixmonitor_ok = 0;
01331 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
01332 return -1;
01333 } else {
01334 pbx_exec(callee_chan, stopmixmonitor_app, "");
01335 return AST_FEATURE_RETURN_SUCCESS;
01336 }
01337 }
01338
01339 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");
01340 }
01341
01342 if (caller_chan && callee_chan) {
01343 const char *touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
01344 const char *touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
01345
01346 if (!touch_format)
01347 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
01348
01349 if (!touch_monitor)
01350 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
01351
01352 if (touch_monitor) {
01353 len = strlen(touch_monitor) + 50;
01354 args = alloca(len);
01355 touch_filename = alloca(len);
01356 snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
01357 snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
01358 } else {
01359 caller_chan_id = ast_strdupa(S_OR(caller_chan->cid.cid_num, caller_chan->name));
01360 callee_chan_id = ast_strdupa(S_OR(callee_chan->cid.cid_num, callee_chan->name));
01361 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
01362 args = alloca(len);
01363 touch_filename = alloca(len);
01364 snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
01365 snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
01366 }
01367
01368 for( x = 0; x < strlen(args); x++) {
01369 if (args[x] == '/')
01370 args[x] = '-';
01371 }
01372
01373 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
01374
01375 pbx_exec(callee_chan, mixmonitor_app, args);
01376 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
01377 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
01378 return AST_FEATURE_RETURN_SUCCESS;
01379
01380 }
01381
01382 ast_log(LOG_NOTICE,"Cannot record the call. One or both channels have gone away.\n");
01383 return -1;
01384
01385 }
01386
01387 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01388 {
01389 ast_verb(4, "User hit '%s' to disconnect call.\n", code);
01390 return AST_FEATURE_RETURN_HANGUP;
01391 }
01392
01393 static int finishup(struct ast_channel *chan)
01394 {
01395 ast_indicate(chan, AST_CONTROL_UNHOLD);
01396
01397 return ast_autoservice_stop(chan);
01398 }
01399
01400
01401
01402
01403
01404
01405
01406
01407
01408 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
01409 {
01410 const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
01411 if (ast_strlen_zero(s)) {
01412 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
01413 }
01414 if (ast_strlen_zero(s)) {
01415 s = transferer->macrocontext;
01416 }
01417 if (ast_strlen_zero(s)) {
01418 s = transferer->context;
01419 }
01420 return s;
01421 }
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431
01432
01433
01434
01435
01436
01437 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01438 {
01439 struct ast_channel *transferer;
01440 struct ast_channel *transferee;
01441 const char *transferer_real_context;
01442 char xferto[256];
01443 int res, parkstatus = 0;
01444
01445 set_peers(&transferer, &transferee, peer, chan, sense);
01446 transferer_real_context = real_ctx(transferer, transferee);
01447
01448 ast_autoservice_start(transferee);
01449 ast_autoservice_ignore(transferee, AST_FRAME_DTMF_END);
01450 ast_indicate(transferee, AST_CONTROL_HOLD);
01451
01452 memset(xferto, 0, sizeof(xferto));
01453
01454
01455 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
01456 if (res < 0) {
01457 finishup(transferee);
01458 return -1;
01459 }
01460 if (res > 0)
01461 xferto[0] = (char) res;
01462
01463 ast_stopstream(transferer);
01464 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
01465 if (res < 0) {
01466 finishup(transferee);
01467 return res;
01468 }
01469 if (!strcmp(xferto, ast_parking_ext())) {
01470 res = finishup(transferee);
01471 if (res)
01472 res = -1;
01473 else if (!(parkstatus = masq_park_call_announce(transferee, transferer, 0, NULL))) {
01474
01475
01476
01477
01478 return 0;
01479 } else {
01480 ast_log(LOG_WARNING, "Unable to park call %s, parkstatus = %d\n", transferee->name, parkstatus);
01481 }
01482
01483 } else if (ast_exists_extension(transferee, transferer_real_context, xferto, 1, transferer->cid.cid_num)) {
01484 pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
01485 pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
01486 res=finishup(transferee);
01487 if (!transferer->cdr) {
01488 transferer->cdr=ast_cdr_alloc();
01489 if (transferer->cdr) {
01490 ast_cdr_init(transferer->cdr, transferer);
01491 ast_cdr_start(transferer->cdr);
01492 }
01493 }
01494 if (transferer->cdr) {
01495 struct ast_cdr *swap = transferer->cdr;
01496 ast_log(LOG_DEBUG,"transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
01497 transferer->name, transferee->name, transferer->cdr->lastapp, transferer->cdr->lastdata,
01498 transferer->cdr->channel, transferer->cdr->dstchannel);
01499 ast_log(LOG_DEBUG,"TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
01500 transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel, transferee->cdr->dstchannel);
01501 ast_log(LOG_DEBUG,"transferer_real_context=%s; xferto=%s\n", transferer_real_context, xferto);
01502
01503 transferer->cdr = transferee->cdr;
01504 transferee->cdr = swap;
01505 }
01506 if (!transferee->pbx) {
01507
01508 ast_verb(3, "Transferring %s to '%s' (context %s) priority 1\n"
01509 ,transferee->name, xferto, transferer_real_context);
01510 if (ast_async_goto(transferee, transferer_real_context, xferto, 1))
01511 ast_log(LOG_WARNING, "Async goto failed :-(\n");
01512 } else {
01513
01514 ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT);
01515 ast_log(LOG_DEBUG,"ABOUT TO AST_ASYNC_GOTO, have a pbx... set HANGUP_DONT on chan=%s\n", transferee->name);
01516 set_c_e_p(transferee, transferer_real_context, xferto, 0);
01517 }
01518 check_goto_on_transfer(transferer);
01519 return res;
01520 } else {
01521 ast_verb(3, "Unable to find extension '%s' in context '%s'\n", xferto, transferer_real_context);
01522 }
01523 if (parkstatus != AST_FEATURE_RETURN_PARKFAILED && ast_stream_and_wait(transferer, xferfailsound, AST_DIGIT_ANY) < 0) {
01524 finishup(transferee);
01525 return -1;
01526 }
01527 ast_stopstream(transferer);
01528 res = finishup(transferee);
01529 if (res) {
01530 ast_verb(2, "Hungup during autoservice stop on '%s'\n", transferee->name);
01531 return res;
01532 }
01533 return AST_FEATURE_RETURN_SUCCESS;
01534 }
01535
01536
01537
01538
01539
01540
01541
01542
01543 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
01544 {
01545 if (ast_channel_make_compatible(c, newchan) < 0) {
01546 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
01547 c->name, newchan->name);
01548 ast_hangup(newchan);
01549 return -1;
01550 }
01551 return 0;
01552 }
01553
01554
01555
01556
01557
01558
01559
01560
01561
01562
01563
01564
01565
01566
01567
01568
01569 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
01570 {
01571 struct ast_channel *transferer;
01572 struct ast_channel *transferee;
01573 const char *transferer_real_context;
01574 char xferto[256] = "";
01575 int res;
01576 int outstate=0;
01577 struct ast_channel *newchan;
01578 struct ast_channel *xferchan;
01579 struct ast_bridge_thread_obj *tobj;
01580 struct ast_bridge_config bconfig;
01581 int l;
01582 struct ast_datastore *features_datastore;
01583 struct ast_dial_features *dialfeatures = NULL;
01584 char *transferer_tech;
01585 char *transferer_name;
01586 char *transferer_name_orig;
01587 char *dash;
01588
01589 ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
01590 set_peers(&transferer, &transferee, peer, chan, sense);
01591 transferer_real_context = real_ctx(transferer, transferee);
01592
01593
01594 ast_autoservice_start(transferee);
01595 ast_indicate(transferee, AST_CONTROL_HOLD);
01596
01597
01598 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
01599 if (res < 0) {
01600 finishup(transferee);
01601 return -1;
01602 }
01603 if (res > 0)
01604 xferto[0] = (char) res;
01605
01606
01607 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
01608 if (res < 0) {
01609 finishup(transferee);
01610 return -1;
01611 }
01612 l = strlen(xferto);
01613 if (res == 0) {
01614 if (l) {
01615 ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
01616 xferto, transferer_real_context);
01617 } else {
01618
01619 ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
01620 }
01621 ast_stream_and_wait(transferer, "pbx-invalid", "");
01622 finishup(transferee);
01623 return AST_FEATURE_RETURN_SUCCESS;
01624 }
01625
01626
01627
01628 if (!strcmp(xferto, ast_parking_ext())) {
01629 finishup(transferee);
01630 return builtin_parkcall(chan, peer, config, code, sense, data);
01631 }
01632
01633
01634 snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
01635
01636
01637
01638 if (transferee) {
01639 const char *chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
01640 const char *chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
01641
01642 if (!ast_strlen_zero(chan1_attended_sound)) {
01643 pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
01644 }
01645 if (!ast_strlen_zero(chan2_attended_sound)) {
01646 pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
01647 }
01648 }
01649
01650
01651 transferer_name_orig = ast_strdupa(transferer->name);
01652 transferer_name = ast_strdupa(transferer_name_orig);
01653 transferer_tech = strsep(&transferer_name, "/");
01654 dash = strrchr(transferer_name, '-');
01655 if (dash) {
01656
01657 *dash = '\0';
01658 }
01659
01660
01661 if (ast_autoservice_stop(transferee) < 0) {
01662 ast_indicate(transferee, AST_CONTROL_UNHOLD);
01663 return -1;
01664 }
01665
01666
01667 newchan = feature_request_and_dial(transferer, transferer_name_orig, transferee,
01668 "Local", ast_best_codec(transferer->nativeformats), xferto, atxfernoanswertimeout,
01669 &outstate, transferer->cid.cid_num, transferer->cid.cid_name,
01670 transferer->language);
01671 ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
01672
01673 if (!ast_check_hangup(transferer)) {
01674 int hangup_dont = 0;
01675
01676
01677 ast_debug(1, "Actually doing an attended transfer.\n");
01678
01679
01680 ast_autoservice_start(transferee);
01681
01682 ast_indicate(transferer, -1);
01683 if (!newchan) {
01684
01685 switch (outstate) {
01686 case AST_CONTROL_UNHOLD:
01687 case AST_CONTROL_BUSY:
01688 case AST_CONTROL_CONGESTION:
01689 if (ast_stream_and_wait(transferer, xfersound, "")) {
01690 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01691 }
01692 break;
01693 default:
01694 if (ast_stream_and_wait(transferer, xferfailsound, "")) {
01695 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
01696 }
01697 break;
01698 }
01699 finishup(transferee);
01700 return AST_FEATURE_RETURN_SUCCESS;
01701 }
01702
01703 if (check_compat(transferer, newchan)) {
01704 if (ast_stream_and_wait(transferer, xferfailsound, "")) {
01705 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
01706 }
01707
01708 finishup(transferee);
01709 return AST_FEATURE_RETURN_SUCCESS;
01710 }
01711 memset(&bconfig,0,sizeof(struct ast_bridge_config));
01712 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
01713 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
01714
01715
01716
01717
01718 if (ast_test_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT)) {
01719 hangup_dont = 1;
01720 }
01721
01722 ast_bridge_call(transferer, newchan, &bconfig);
01723 if (hangup_dont) {
01724 ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
01725 }
01726
01727 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
01728 ast_hangup(newchan);
01729 if (ast_stream_and_wait(transferer, xfersound, "")) {
01730 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01731 }
01732 finishup(transferee);
01733 return AST_FEATURE_RETURN_SUCCESS;
01734 }
01735
01736
01737 if (check_compat(transferee, newchan)) {
01738 finishup(transferee);
01739 return -1;
01740 }
01741
01742 ast_indicate(transferee, AST_CONTROL_UNHOLD);
01743 if ((ast_autoservice_stop(transferee) < 0)
01744 || (ast_waitfordigit(transferee, 100) < 0)
01745 || (ast_waitfordigit(newchan, 100) < 0)
01746 || ast_check_hangup(transferee)
01747 || ast_check_hangup(newchan)) {
01748 ast_hangup(newchan);
01749 return -1;
01750 }
01751 } else if (!ast_check_hangup(transferee)) {
01752
01753 ast_debug(1, "Actually doing a blonde transfer.\n");
01754
01755 if (!newchan && !atxferdropcall) {
01756
01757 unsigned int tries = 0;
01758
01759 if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
01760 ast_log(LOG_WARNING,
01761 "Transferer channel name: '%s' cannot be used for callback.\n",
01762 transferer_name_orig);
01763 ast_indicate(transferee, AST_CONTROL_UNHOLD);
01764 return -1;
01765 }
01766
01767 tries = 0;
01768 for (;;) {
01769
01770 ast_debug(1, "We're trying to callback %s/%s\n",
01771 transferer_tech, transferer_name);
01772 newchan = feature_request_and_dial(transferer, transferer_name_orig,
01773 transferee, transferer_tech,
01774 ast_best_codec(transferee->nativeformats), transferer_name,
01775 atxfernoanswertimeout, &outstate, transferee->cid.cid_num,
01776 transferee->cid.cid_name, transferer->language);
01777 ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
01778 !!newchan, outstate);
01779 if (newchan || ast_check_hangup(transferee)) {
01780 break;
01781 }
01782
01783 ++tries;
01784 if (atxfercallbackretries <= tries) {
01785
01786 break;
01787 }
01788
01789 if (atxferloopdelay) {
01790
01791 ast_debug(1, "Sleeping for %d ms before retrying atxfer.\n",
01792 atxferloopdelay);
01793 ast_safe_sleep(transferee, atxferloopdelay);
01794 if (ast_check_hangup(transferee)) {
01795 return -1;
01796 }
01797 }
01798
01799
01800 ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
01801 newchan = feature_request_and_dial(transferer, transferer_name_orig,
01802 transferee, "Local", ast_best_codec(transferee->nativeformats),
01803 xferto, atxfernoanswertimeout, &outstate, transferer->cid.cid_num,
01804 transferer->cid.cid_name, transferer->language);
01805 ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
01806 !!newchan, outstate);
01807 if (newchan || ast_check_hangup(transferee)) {
01808 break;
01809 }
01810 }
01811 }
01812 ast_indicate(transferee, AST_CONTROL_UNHOLD);
01813 if (!newchan) {
01814
01815 return -1;
01816 }
01817
01818
01819 if (ast_check_hangup(newchan)) {
01820 ast_hangup(newchan);
01821 return -1;
01822 }
01823 if (check_compat(transferee, newchan)) {
01824 return -1;
01825 }
01826 } else {
01827
01828
01829
01830
01831 ast_debug(1, "Everyone is hungup.\n");
01832 if (newchan) {
01833 ast_hangup(newchan);
01834 }
01835 return -1;
01836 }
01837
01838
01839
01840 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", 0, "Transfered/%s", transferee->name);
01841 if (!xferchan) {
01842 ast_hangup(newchan);
01843 return -1;
01844 }
01845
01846
01847 xferchan->visible_indication = AST_CONTROL_RINGING;
01848
01849
01850 xferchan->readformat = transferee->readformat;
01851 xferchan->writeformat = transferee->writeformat;
01852
01853 ast_channel_masquerade(xferchan, transferee);
01854 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
01855 xferchan->_state = AST_STATE_UP;
01856 ast_clear_flag(xferchan, AST_FLAGS_ALL);
01857
01858
01859 ast_channel_lock(xferchan);
01860 if (xferchan->masq) {
01861 ast_do_masquerade(xferchan);
01862 }
01863 ast_channel_unlock(xferchan);
01864
01865 newchan->_state = AST_STATE_UP;
01866 ast_clear_flag(newchan, AST_FLAGS_ALL);
01867 tobj = ast_calloc(1, sizeof(*tobj));
01868 if (!tobj) {
01869 ast_hangup(xferchan);
01870 ast_hangup(newchan);
01871 return -1;
01872 }
01873
01874 ast_channel_lock(newchan);
01875 if ((features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL))) {
01876 dialfeatures = features_datastore->data;
01877 }
01878 ast_channel_unlock(newchan);
01879
01880 if (dialfeatures) {
01881
01882
01883 ast_copy_flags(&(config->features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
01884 dialfeatures = NULL;
01885 }
01886
01887 ast_channel_lock(xferchan);
01888 if ((features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL))) {
01889 dialfeatures = features_datastore->data;
01890 }
01891 ast_channel_unlock(xferchan);
01892
01893 if (dialfeatures) {
01894 ast_copy_flags(&(config->features_caller), &(dialfeatures->features_caller), AST_FLAGS_ALL);
01895 }
01896
01897 tobj->chan = newchan;
01898 tobj->peer = xferchan;
01899 tobj->bconfig = *config;
01900
01901 if (tobj->bconfig.end_bridge_callback_data_fixup) {
01902 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
01903 }
01904
01905 if (ast_stream_and_wait(newchan, xfersound, ""))
01906 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
01907 bridge_call_thread_launch(tobj);
01908 return -1;
01909 }
01910
01911
01912 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
01913
01914 AST_RWLOCK_DEFINE_STATIC(features_lock);
01915
01916 static struct ast_call_feature builtin_features[] =
01917 {
01918 { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01919 { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01920 { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01921 { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01922 { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01923 { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
01924 };
01925
01926
01927 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
01928
01929
01930 void ast_register_feature(struct ast_call_feature *feature)
01931 {
01932 if (!feature) {
01933 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
01934 return;
01935 }
01936
01937 AST_RWLIST_WRLOCK(&feature_list);
01938 AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
01939 AST_RWLIST_UNLOCK(&feature_list);
01940
01941 ast_verb(2, "Registered Feature '%s'\n",feature->sname);
01942 }
01943
01944
01945
01946
01947
01948
01949
01950
01951 static struct feature_group *register_group(const char *fgname)
01952 {
01953 struct feature_group *fg;
01954
01955 if (!fgname) {
01956 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
01957 return NULL;
01958 }
01959
01960 if (!(fg = ast_calloc(1, sizeof(*fg)))) {
01961 return NULL;
01962 }
01963
01964 if (ast_string_field_init(fg, 128)) {
01965 ast_free(fg);
01966 return NULL;
01967 }
01968
01969 ast_string_field_set(fg, gname, fgname);
01970
01971 AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
01972
01973 ast_verb(2, "Registered group '%s'\n", fg->gname);
01974
01975 return fg;
01976 }
01977
01978
01979
01980
01981
01982
01983
01984
01985
01986
01987 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
01988 {
01989 struct feature_group_exten *fge;
01990
01991 if (!fg) {
01992 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
01993 return;
01994 }
01995
01996 if (!feature) {
01997 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
01998 return;
01999 }
02000
02001 if (!(fge = ast_calloc(1, sizeof(*fge)))) {
02002 return;
02003 }
02004
02005 if (ast_string_field_init(fge, 128)) {
02006 ast_free(fge);
02007 return;
02008 }
02009
02010 ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
02011
02012 fge->feature = feature;
02013
02014 AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
02015
02016 ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
02017 feature->sname, fg->gname, fge->exten);
02018 }
02019
02020 void ast_unregister_feature(struct ast_call_feature *feature)
02021 {
02022 if (!feature) {
02023 return;
02024 }
02025
02026 AST_RWLIST_WRLOCK(&feature_list);
02027 AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
02028 AST_RWLIST_UNLOCK(&feature_list);
02029
02030 ast_free(feature);
02031 }
02032
02033
02034 static void ast_unregister_features(void)
02035 {
02036 struct ast_call_feature *feature;
02037
02038 AST_RWLIST_WRLOCK(&feature_list);
02039 while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
02040 ast_free(feature);
02041 }
02042 AST_RWLIST_UNLOCK(&feature_list);
02043 }
02044
02045
02046 static struct ast_call_feature *find_dynamic_feature(const char *name)
02047 {
02048 struct ast_call_feature *tmp;
02049
02050 AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
02051 if (!strcasecmp(tmp->sname, name)) {
02052 break;
02053 }
02054 }
02055
02056 return tmp;
02057 }
02058
02059
02060 static void ast_unregister_groups(void)
02061 {
02062 struct feature_group *fg;
02063 struct feature_group_exten *fge;
02064
02065 AST_RWLIST_WRLOCK(&feature_groups);
02066 while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
02067 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
02068 ast_string_field_free_memory(fge);
02069 ast_free(fge);
02070 }
02071
02072 ast_string_field_free_memory(fg);
02073 ast_free(fg);
02074 }
02075 AST_RWLIST_UNLOCK(&feature_groups);
02076 }
02077
02078
02079
02080
02081
02082
02083
02084 static struct feature_group *find_group(const char *name)
02085 {
02086 struct feature_group *fg = NULL;
02087
02088 AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
02089 if (!strcasecmp(fg->gname, name))
02090 break;
02091 }
02092
02093 return fg;
02094 }
02095
02096 void ast_rdlock_call_features(void)
02097 {
02098 ast_rwlock_rdlock(&features_lock);
02099 }
02100
02101 void ast_unlock_call_features(void)
02102 {
02103 ast_rwlock_unlock(&features_lock);
02104 }
02105
02106 struct ast_call_feature *ast_find_call_feature(const char *name)
02107 {
02108 int x;
02109 for (x = 0; x < FEATURES_COUNT; x++) {
02110 if (!strcasecmp(name, builtin_features[x].sname))
02111 return &builtin_features[x];
02112 }
02113 return NULL;
02114 }
02115
02116
02117
02118
02119
02120
02121
02122
02123
02124
02125 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense, void *data)
02126 {
02127 struct ast_app *app;
02128 struct ast_call_feature *feature = data;
02129 struct ast_channel *work, *idle;
02130 int res;
02131
02132 if (!feature) {
02133 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
02134 return -1;
02135 }
02136
02137 if (sense == FEATURE_SENSE_CHAN) {
02138 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
02139 return AST_FEATURE_RETURN_KEEPTRYING;
02140 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
02141 work = chan;
02142 idle = peer;
02143 } else {
02144 work = peer;
02145 idle = chan;
02146 }
02147 } else {
02148 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
02149 return AST_FEATURE_RETURN_KEEPTRYING;
02150 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
02151 work = peer;
02152 idle = chan;
02153 } else {
02154 work = chan;
02155 idle = peer;
02156 }
02157 }
02158
02159 if (!(app = pbx_findapp(feature->app))) {
02160 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
02161 return -2;
02162 }
02163
02164 ast_autoservice_start(idle);
02165 ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
02166
02167 if (!ast_strlen_zero(feature->moh_class))
02168 ast_moh_start(idle, feature->moh_class, NULL);
02169
02170 res = pbx_exec(work, app, feature->app_args);
02171
02172 if (!ast_strlen_zero(feature->moh_class))
02173 ast_moh_stop(idle);
02174
02175 ast_autoservice_stop(idle);
02176
02177 if (res) {
02178 return AST_FEATURE_RETURN_SUCCESSBREAK;
02179 }
02180 return AST_FEATURE_RETURN_SUCCESS;
02181 }
02182
02183 static void unmap_features(void)
02184 {
02185 int x;
02186
02187 ast_rwlock_wrlock(&features_lock);
02188 for (x = 0; x < FEATURES_COUNT; x++)
02189 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
02190 ast_rwlock_unlock(&features_lock);
02191 }
02192
02193 static int remap_feature(const char *name, const char *value)
02194 {
02195 int x, res = -1;
02196
02197 ast_rwlock_wrlock(&features_lock);
02198 for (x = 0; x < FEATURES_COUNT; x++) {
02199 if (strcasecmp(builtin_features[x].sname, name))
02200 continue;
02201
02202 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
02203 res = 0;
02204 break;
02205 }
02206 ast_rwlock_unlock(&features_lock);
02207
02208 return res;
02209 }
02210
02211
02212
02213
02214
02215
02216
02217
02218
02219 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, char *code, int sense)
02220 {
02221 int x;
02222 struct ast_flags features;
02223 struct ast_call_feature *feature = NULL;
02224 struct feature_group *fg = NULL;
02225 struct feature_group_exten *fge;
02226 const char *peer_dynamic_features, *chan_dynamic_features;
02227 char dynamic_features_buf[128];
02228 char *tmp, *tok;
02229 int res = AST_FEATURE_RETURN_PASSDIGITS;
02230 int feature_detected = 0;
02231
02232 if (sense == FEATURE_SENSE_CHAN) {
02233 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
02234 }
02235 else {
02236 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
02237 }
02238
02239 ast_channel_lock(peer);
02240 peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
02241 ast_channel_unlock(peer);
02242
02243 ast_channel_lock(chan);
02244 chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
02245 ast_channel_unlock(chan);
02246
02247 snprintf(dynamic_features_buf, sizeof(dynamic_features_buf), "%s%s%s", S_OR(chan_dynamic_features, ""), chan_dynamic_features && peer_dynamic_features ? "#" : "", S_OR(peer_dynamic_features,""));
02248
02249 ast_debug(3, "Feature interpret: chan=%s, peer=%s, code=%s, sense=%d, features=%d, dynamic=%s\n", chan->name, peer->name, code, sense, features.flags, dynamic_features_buf);
02250
02251 ast_rwlock_rdlock(&features_lock);
02252 for (x = 0; x < FEATURES_COUNT; x++) {
02253 if ((ast_test_flag(&features, builtin_features[x].feature_mask)) &&
02254 !ast_strlen_zero(builtin_features[x].exten)) {
02255
02256 if (!strcmp(builtin_features[x].exten, code)) {
02257 ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
02258 res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
02259 feature_detected = 1;
02260 break;
02261 } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
02262 if (res == AST_FEATURE_RETURN_PASSDIGITS)
02263 res = AST_FEATURE_RETURN_STOREDIGITS;
02264 }
02265 }
02266 }
02267 ast_rwlock_unlock(&features_lock);
02268
02269 if (ast_strlen_zero(dynamic_features_buf) || feature_detected)
02270 return res;
02271
02272 tmp = dynamic_features_buf;
02273
02274 while ((tok = strsep(&tmp, "#"))) {
02275 AST_RWLIST_RDLOCK(&feature_groups);
02276
02277 fg = find_group(tok);
02278
02279 if (fg) {
02280 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
02281 if (!strcmp(fge->exten, code)) {
02282 res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
02283 memcpy(feature, fge->feature, sizeof(feature));
02284 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
02285 AST_RWLIST_UNLOCK(&feature_groups);
02286 break;
02287 }
02288 res = AST_FEATURE_RETURN_PASSDIGITS;
02289 } else if (!strncmp(fge->exten, code, strlen(code))) {
02290 res = AST_FEATURE_RETURN_STOREDIGITS;
02291 }
02292 }
02293 if (fge) {
02294 break;
02295 }
02296 }
02297
02298 AST_RWLIST_UNLOCK(&feature_groups);
02299
02300 AST_RWLIST_RDLOCK(&feature_list);
02301
02302 if (!(feature = find_dynamic_feature(tok))) {
02303 AST_RWLIST_UNLOCK(&feature_list);
02304 continue;
02305 }
02306
02307
02308 if (!strcmp(feature->exten, code)) {
02309 ast_verb(3, " Feature Found: %s exten: %s\n",feature->sname, tok);
02310 res = feature->operation(chan, peer, config, code, sense, feature);
02311 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
02312 AST_RWLIST_UNLOCK(&feature_list);
02313 break;
02314 }
02315 res = AST_FEATURE_RETURN_PASSDIGITS;
02316 } else if (!strncmp(feature->exten, code, strlen(code)))
02317 res = AST_FEATURE_RETURN_STOREDIGITS;
02318
02319 AST_RWLIST_UNLOCK(&feature_list);
02320 }
02321
02322 return res;
02323 }
02324
02325 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
02326 {
02327 int x;
02328
02329 ast_clear_flag(config, AST_FLAGS_ALL);
02330
02331 ast_rwlock_rdlock(&features_lock);
02332 for (x = 0; x < FEATURES_COUNT; x++) {
02333 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
02334 continue;
02335
02336 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
02337 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02338
02339 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
02340 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02341 }
02342 ast_rwlock_unlock(&features_lock);
02343
02344 if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
02345 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
02346
02347 if (dynamic_features) {
02348 char *tmp = ast_strdupa(dynamic_features);
02349 char *tok;
02350 struct ast_call_feature *feature;
02351
02352
02353 while ((tok = strsep(&tmp, "#"))) {
02354 struct feature_group *fg;
02355
02356 AST_RWLIST_RDLOCK(&feature_groups);
02357 AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
02358 struct feature_group_exten *fge;
02359
02360 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
02361 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
02362 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02363 }
02364 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
02365 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02366 }
02367 }
02368 }
02369 AST_RWLIST_UNLOCK(&feature_groups);
02370
02371 AST_RWLIST_RDLOCK(&feature_list);
02372 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
02373 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
02374 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
02375 }
02376 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
02377 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
02378 }
02379 }
02380 AST_RWLIST_UNLOCK(&feature_list);
02381 }
02382 }
02383 }
02384 }
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398
02399
02400
02401
02402
02403
02404
02405
02406
02407
02408
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418
02419
02420
02421 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
02422 const char *caller_name, struct ast_channel *transferee, const char *type,
02423 int format, void *data, int timeout, int *outstate, const char *cid_num,
02424 const char *cid_name, const char *language)
02425 {
02426 int state = 0;
02427 int cause = 0;
02428 int to;
02429 int caller_hungup;
02430 int transferee_hungup;
02431 struct ast_channel *chan;
02432 struct ast_channel *monitor_chans[3];
02433 struct ast_channel *active_channel;
02434 int ready = 0;
02435 struct timeval started;
02436 int x, len = 0;
02437 char *disconnect_code = NULL, *dialed_code = NULL;
02438 struct ast_frame *f;
02439 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
02440
02441 caller_hungup = ast_check_hangup(caller);
02442
02443 if (!(chan = ast_request(type, format, data, &cause))) {
02444 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
02445 switch (cause) {
02446 case AST_CAUSE_BUSY:
02447 state = AST_CONTROL_BUSY;
02448 break;
02449 case AST_CAUSE_CONGESTION:
02450 state = AST_CONTROL_CONGESTION;
02451 break;
02452 default:
02453 state = 0;
02454 break;
02455 }
02456 goto done;
02457 }
02458
02459 ast_set_callerid(chan, cid_num, cid_name, cid_num);
02460 ast_string_field_set(chan, language, language);
02461 ast_channel_inherit_variables(caller, chan);
02462 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
02463
02464 if (ast_call(chan, data, timeout)) {
02465 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
02466 switch (chan->hangupcause) {
02467 case AST_CAUSE_BUSY:
02468 state = AST_CONTROL_BUSY;
02469 break;
02470 case AST_CAUSE_CONGESTION:
02471 state = AST_CONTROL_CONGESTION;
02472 break;
02473 default:
02474 state = 0;
02475 break;
02476 }
02477 goto done;
02478 }
02479
02480
02481 ast_rwlock_rdlock(&features_lock);
02482 for (x = 0; x < FEATURES_COUNT; x++) {
02483 if (strcasecmp(builtin_features[x].sname, "disconnect"))
02484 continue;
02485
02486 disconnect_code = builtin_features[x].exten;
02487 len = strlen(disconnect_code) + 1;
02488 dialed_code = alloca(len);
02489 memset(dialed_code, 0, len);
02490 break;
02491 }
02492 ast_rwlock_unlock(&features_lock);
02493 x = 0;
02494 started = ast_tvnow();
02495 to = timeout;
02496 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
02497
02498 ast_poll_channel_add(caller, chan);
02499
02500 transferee_hungup = 0;
02501 while (!ast_check_hangup(transferee) && (chan->_state != AST_STATE_UP)) {
02502 int num_chans = 0;
02503
02504 monitor_chans[num_chans++] = transferee;
02505 monitor_chans[num_chans++] = chan;
02506 if (!caller_hungup) {
02507 if (ast_check_hangup(caller)) {
02508 caller_hungup = 1;
02509
02510 #if defined(ATXFER_NULL_TECH)
02511
02512 set_new_chan_name(caller);
02513
02514
02515
02516
02517
02518 set_null_chan_tech(caller);
02519 #endif
02520 } else {
02521
02522 monitor_chans[num_chans++] = caller;
02523 }
02524 }
02525
02526
02527 if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
02528 state = AST_CONTROL_UNHOLD;
02529 ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", chan->name);
02530 break;
02531 }
02532
02533 active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
02534 if (!active_channel)
02535 continue;
02536
02537 f = NULL;
02538 if (transferee == active_channel) {
02539 struct ast_frame *dup_f;
02540
02541 f = ast_read(transferee);
02542 if (f == NULL) {
02543 transferee_hungup = 1;
02544 state = 0;
02545 break;
02546 }
02547 if (ast_is_deferrable_frame(f)) {
02548 dup_f = ast_frisolate(f);
02549 if (dup_f) {
02550 if (dup_f == f) {
02551 f = NULL;
02552 }
02553 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
02554 }
02555 }
02556 } else if (chan == active_channel) {
02557 if (!ast_strlen_zero(chan->call_forward)) {
02558 state = 0;
02559 chan = ast_call_forward(caller, chan, NULL, format, NULL, &state);
02560 if (!chan) {
02561 break;
02562 }
02563 continue;
02564 }
02565 f = ast_read(chan);
02566 if (f == NULL) {
02567 switch (chan->hangupcause) {
02568 case AST_CAUSE_BUSY:
02569 state = AST_CONTROL_BUSY;
02570 break;
02571 case AST_CAUSE_CONGESTION:
02572 state = AST_CONTROL_CONGESTION;
02573 break;
02574 default:
02575 state = 0;
02576 break;
02577 }
02578 break;
02579 }
02580
02581 if (f->frametype == AST_FRAME_CONTROL) {
02582 if (f->subclass == AST_CONTROL_RINGING) {
02583 ast_verb(3, "%s is ringing\n", chan->name);
02584 ast_indicate(caller, AST_CONTROL_RINGING);
02585 } else if (f->subclass == AST_CONTROL_BUSY) {
02586 state = f->subclass;
02587 ast_verb(3, "%s is busy\n", chan->name);
02588 ast_indicate(caller, AST_CONTROL_BUSY);
02589 ast_frfree(f);
02590 break;
02591 } else if (f->subclass == AST_CONTROL_CONGESTION) {
02592 state = f->subclass;
02593 ast_verb(3, "%s is congested\n", chan->name);
02594 ast_indicate(caller, AST_CONTROL_CONGESTION);
02595 ast_frfree(f);
02596 break;
02597 } else if (f->subclass == AST_CONTROL_ANSWER) {
02598
02599 state = f->subclass;
02600 ast_frfree(f);
02601 ready=1;
02602 break;
02603 } else if (f->subclass != -1 && f->subclass != AST_CONTROL_PROGRESS) {
02604 ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass);
02605 }
02606
02607 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
02608 ast_write(caller, f);
02609 }
02610 } else if (caller == active_channel) {
02611 f = ast_read(caller);
02612 if (f) {
02613 if (f->frametype == AST_FRAME_DTMF) {
02614 dialed_code[x++] = f->subclass;
02615 dialed_code[x] = '\0';
02616 if (strlen(dialed_code) == len) {
02617 x = 0;
02618 } else if (x && strncmp(dialed_code, disconnect_code, x)) {
02619 x = 0;
02620 dialed_code[x] = '\0';
02621 }
02622 if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
02623
02624 state = AST_CONTROL_UNHOLD;
02625 ast_frfree(f);
02626 break;
02627 }
02628 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
02629 ast_write(chan, f);
02630 }
02631 }
02632 }
02633 if (f)
02634 ast_frfree(f);
02635 }
02636
02637 ast_poll_channel_del(caller, chan);
02638
02639
02640
02641
02642
02643 ast_channel_lock(transferee);
02644 transferee_hungup = (transferee_hungup || ast_check_hangup(transferee));
02645 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
02646 if (!transferee_hungup) {
02647 ast_queue_frame_head(transferee, f);
02648 }
02649 ast_frfree(f);
02650 }
02651 ast_channel_unlock(transferee);
02652
02653 done:
02654 ast_indicate(caller, -1);
02655 if (chan && ready) {
02656 if (chan->_state == AST_STATE_UP)
02657 state = AST_CONTROL_ANSWER;
02658 } else if (chan) {
02659 ast_hangup(chan);
02660 chan = NULL;
02661 }
02662
02663 if (outstate)
02664 *outstate = state;
02665
02666 return chan;
02667 }
02668
02669
02670
02671
02672 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
02673 {
02674 struct ast_cdr *cdr_orig = cdr;
02675 while (cdr) {
02676 if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
02677 return cdr;
02678 cdr = cdr->next;
02679 }
02680 return cdr_orig;
02681 }
02682
02683 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
02684 {
02685 const char *feature;
02686
02687 if (ast_strlen_zero(features)) {
02688 return;
02689 }
02690
02691 for (feature = features; *feature; feature++) {
02692 switch (*feature) {
02693 case 'T' :
02694 case 't' :
02695 ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
02696 break;
02697 case 'K' :
02698 case 'k' :
02699 ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
02700 break;
02701 case 'H' :
02702 case 'h' :
02703 ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
02704 break;
02705 case 'W' :
02706 case 'w' :
02707 ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
02708 break;
02709 default :
02710 ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
02711 }
02712 }
02713 }
02714
02715 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
02716 {
02717 struct ast_datastore *ds_callee_features = NULL, *ds_caller_features = NULL;
02718 struct ast_dial_features *callee_features = NULL, *caller_features = NULL;
02719
02720 ast_channel_lock(caller);
02721 ds_caller_features = ast_channel_datastore_find(caller, &dial_features_info, NULL);
02722 ast_channel_unlock(caller);
02723 if (!ds_caller_features) {
02724 if (!(ds_caller_features = ast_datastore_alloc(&dial_features_info, NULL))) {
02725 ast_log(LOG_WARNING, "Unable to create channel datastore for caller features. Aborting!\n");
02726 return;
02727 }
02728 if (!(caller_features = ast_calloc(1, sizeof(*caller_features)))) {
02729 ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
02730 ast_datastore_free(ds_caller_features);
02731 return;
02732 }
02733 ds_caller_features->inheritance = DATASTORE_INHERIT_FOREVER;
02734 caller_features->is_caller = 1;
02735 ast_copy_flags(&(caller_features->features_callee), &(config->features_callee), AST_FLAGS_ALL);
02736 ast_copy_flags(&(caller_features->features_caller), &(config->features_caller), AST_FLAGS_ALL);
02737 ds_caller_features->data = caller_features;
02738 ast_channel_lock(caller);
02739 ast_channel_datastore_add(caller, ds_caller_features);
02740 ast_channel_unlock(caller);
02741 } else {
02742
02743
02744 return;
02745 }
02746
02747 ast_channel_lock(callee);
02748 ds_callee_features = ast_channel_datastore_find(callee, &dial_features_info, NULL);
02749 ast_channel_unlock(callee);
02750 if (!ds_callee_features) {
02751 if (!(ds_callee_features = ast_datastore_alloc(&dial_features_info, NULL))) {
02752 ast_log(LOG_WARNING, "Unable to create channel datastore for callee features. Aborting!\n");
02753 return;
02754 }
02755 if (!(callee_features = ast_calloc(1, sizeof(*callee_features)))) {
02756 ast_log(LOG_WARNING, "Unable to allocate memory for callee feature flags. Aborting!\n");
02757 ast_datastore_free(ds_callee_features);
02758 return;
02759 }
02760 ds_callee_features->inheritance = DATASTORE_INHERIT_FOREVER;
02761 callee_features->is_caller = 0;
02762 ast_copy_flags(&(callee_features->features_callee), &(config->features_caller), AST_FLAGS_ALL);
02763 ast_copy_flags(&(callee_features->features_caller), &(config->features_callee), AST_FLAGS_ALL);
02764 ds_callee_features->data = callee_features;
02765 ast_channel_lock(callee);
02766 ast_channel_datastore_add(callee, ds_callee_features);
02767 ast_channel_unlock(callee);
02768 }
02769
02770 return;
02771 }
02772
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782 int ast_bridge_call(struct ast_channel *chan,struct ast_channel *peer,struct ast_bridge_config *config)
02783 {
02784
02785
02786 struct ast_frame *f;
02787 struct ast_channel *who;
02788 char chan_featurecode[FEATURE_MAX_LEN + 1]="";
02789 char peer_featurecode[FEATURE_MAX_LEN + 1]="";
02790 char orig_channame[AST_MAX_EXTENSION];
02791 char orig_peername[AST_MAX_EXTENSION];
02792 int res;
02793 int diff;
02794 int hasfeatures=0;
02795 int hadfeatures=0;
02796 int autoloopflag;
02797 struct ast_option_header *aoh;
02798 struct ast_bridge_config backup_config;
02799 struct ast_cdr *bridge_cdr = NULL;
02800 struct ast_cdr *orig_peer_cdr = NULL;
02801 struct ast_cdr *chan_cdr = chan->cdr;
02802 struct ast_cdr *peer_cdr = peer->cdr;
02803 struct ast_cdr *new_chan_cdr = NULL;
02804 struct ast_cdr *new_peer_cdr = NULL;
02805
02806 memset(&backup_config, 0, sizeof(backup_config));
02807
02808 config->start_time = ast_tvnow();
02809
02810 if (chan && peer) {
02811 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
02812 pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
02813 } else if (chan) {
02814 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
02815 }
02816
02817 set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
02818 add_features_datastores(chan, peer, config);
02819
02820
02821
02822
02823 if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
02824 ast_indicate(peer, AST_CONTROL_RINGING);
02825 }
02826
02827 if (monitor_ok) {
02828 const char *monitor_exec;
02829 struct ast_channel *src = NULL;
02830 if (!monitor_app) {
02831 if (!(monitor_app = pbx_findapp("Monitor")))
02832 monitor_ok=0;
02833 }
02834 if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR")))
02835 src = chan;
02836 else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
02837 src = peer;
02838 if (monitor_app && src) {
02839 char *tmp = ast_strdupa(monitor_exec);
02840 pbx_exec(src, monitor_app, tmp);
02841 }
02842 }
02843
02844 set_config_flags(chan, peer, config);
02845 config->firstpass = 1;
02846
02847
02848 if (chan->_state != AST_STATE_UP) {
02849 if (ast_raw_answer(chan, 1)) {
02850 return -1;
02851 }
02852 }
02853
02854 ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
02855 ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
02856 orig_peer_cdr = peer_cdr;
02857
02858 if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
02859
02860 if (chan_cdr) {
02861 ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
02862 ast_cdr_update(chan);
02863 bridge_cdr = ast_cdr_dup(chan_cdr);
02864
02865
02866 bridge_cdr->next = chan_cdr->next;
02867 chan_cdr->next = NULL;
02868 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
02869 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
02870 if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
02871 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
02872 }
02873 ast_cdr_setaccount(peer, chan->accountcode);
02874
02875 } else {
02876
02877 bridge_cdr = ast_cdr_alloc();
02878 ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
02879 ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
02880 ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
02881 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
02882 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
02883 ast_cdr_setcid(bridge_cdr, chan);
02884 bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
02885 bridge_cdr->amaflags = chan->amaflags ? chan->amaflags : ast_default_amaflags;
02886 ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
02887
02888 ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
02889 ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
02890 if (peer_cdr) {
02891 bridge_cdr->start = peer_cdr->start;
02892 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
02893 } else {
02894 ast_cdr_start(bridge_cdr);
02895 }
02896 }
02897 ast_debug(4,"bridge answer set, chan answer set\n");
02898
02899
02900
02901
02902
02903
02904
02905
02906
02907
02908
02909
02910
02911
02912
02913 if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
02914 ast_cdr_setanswer(bridge_cdr, peer_cdr->answer);
02915 ast_cdr_setdisposition(bridge_cdr, peer_cdr->disposition);
02916 if (chan_cdr) {
02917 ast_cdr_setanswer(chan_cdr, peer_cdr->answer);
02918 ast_cdr_setdisposition(chan_cdr, peer_cdr->disposition);
02919 }
02920 } else {
02921 ast_cdr_answer(bridge_cdr);
02922 if (chan_cdr) {
02923 ast_cdr_answer(chan_cdr);
02924 }
02925 }
02926 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
02927 if (chan_cdr) {
02928 ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
02929 }
02930 if (peer_cdr) {
02931 ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
02932 }
02933 }
02934
02935
02936
02937
02938 ast_clear_flag(bridge_cdr, AST_CDR_FLAG_DIALED);
02939 }
02940 for (;;) {
02941 struct ast_channel *other;
02942
02943 res = ast_channel_bridge(chan, peer, config, &f, &who);
02944
02945
02946
02947
02948
02949
02950
02951
02952 if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
02953
02954 diff = ast_tvdiff_ms(ast_tvnow(), config->start_time);
02955 if (res == AST_BRIDGE_RETRY) {
02956
02957
02958
02959 config->feature_timer = -1;
02960 } else {
02961 config->feature_timer -= diff;
02962 }
02963
02964 if (hasfeatures) {
02965
02966
02967
02968 if (backup_config.feature_timer && ((backup_config.feature_timer -= diff) <= 0)) {
02969 ast_debug(1, "Timed out, realtime this time!\n");
02970 config->feature_timer = 0;
02971 who = chan;
02972 if (f)
02973 ast_frfree(f);
02974 f = NULL;
02975 res = 0;
02976 } else if (config->feature_timer <= 0) {
02977
02978
02979 ast_debug(1, "Timed out for feature!\n");
02980 if (!ast_strlen_zero(peer_featurecode)) {
02981 ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
02982 memset(peer_featurecode, 0, sizeof(peer_featurecode));
02983 }
02984 if (!ast_strlen_zero(chan_featurecode)) {
02985 ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
02986 memset(chan_featurecode, 0, sizeof(chan_featurecode));
02987 }
02988 if (f)
02989 ast_frfree(f);
02990 hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
02991 if (!hasfeatures) {
02992
02993 memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
02994 memset(&backup_config, 0, sizeof(backup_config));
02995 }
02996 hadfeatures = hasfeatures;
02997
02998 continue;
02999 } else if (!f) {
03000
03001
03002
03003 continue;
03004 }
03005 } else {
03006 if (config->feature_timer <=0) {
03007
03008 config->feature_timer = 0;
03009 who = chan;
03010 if (f)
03011 ast_frfree(f);
03012 f = NULL;
03013 res = 0;
03014 }
03015 }
03016 }
03017 if (res < 0) {
03018 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer))
03019 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
03020 goto before_you_go;
03021 }
03022
03023 if (!f || (f->frametype == AST_FRAME_CONTROL &&
03024 (f->subclass == AST_CONTROL_HANGUP || f->subclass == AST_CONTROL_BUSY ||
03025 f->subclass == AST_CONTROL_CONGESTION))) {
03026 res = -1;
03027 break;
03028 }
03029
03030 other = (who == chan) ? peer : chan;
03031 if (f->frametype == AST_FRAME_CONTROL) {
03032 switch (f->subclass) {
03033 case AST_CONTROL_RINGING:
03034 case AST_CONTROL_FLASH:
03035 case -1:
03036 ast_indicate(other, f->subclass);
03037 break;
03038 case AST_CONTROL_HOLD:
03039 case AST_CONTROL_UNHOLD:
03040 ast_indicate_data(other, f->subclass, f->data.ptr, f->datalen);
03041 break;
03042 case AST_CONTROL_OPTION:
03043 aoh = f->data.ptr;
03044
03045 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
03046 ast_channel_setoption(other, ntohs(aoh->option), aoh->data,
03047 f->datalen - sizeof(struct ast_option_header), 0);
03048 }
03049 break;
03050 }
03051 } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
03052
03053 } else if (f->frametype == AST_FRAME_DTMF) {
03054 char *featurecode;
03055 int sense;
03056
03057 hadfeatures = hasfeatures;
03058
03059 if (who == chan) {
03060 sense = FEATURE_SENSE_CHAN;
03061 featurecode = chan_featurecode;
03062 } else {
03063 sense = FEATURE_SENSE_PEER;
03064 featurecode = peer_featurecode;
03065 }
03066
03067
03068
03069
03070 featurecode[strlen(featurecode)] = f->subclass;
03071
03072 ast_frfree(f);
03073 f = NULL;
03074 config->feature_timer = backup_config.feature_timer;
03075 res = feature_interpret(chan, peer, config, featurecode, sense);
03076 switch(res) {
03077 case AST_FEATURE_RETURN_PASSDIGITS:
03078 ast_dtmf_stream(other, who, featurecode, 0, 0);
03079
03080 case AST_FEATURE_RETURN_SUCCESS:
03081 memset(featurecode, 0, sizeof(chan_featurecode));
03082 break;
03083 }
03084 if (res >= AST_FEATURE_RETURN_PASSDIGITS) {
03085 res = 0;
03086 } else
03087 break;
03088 hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
03089 if (hadfeatures && !hasfeatures) {
03090
03091 memcpy(config, &backup_config, sizeof(struct ast_bridge_config));
03092 memset(&backup_config, 0, sizeof(struct ast_bridge_config));
03093 } else if (hasfeatures) {
03094 if (!hadfeatures) {
03095
03096 memcpy(&backup_config, config, sizeof(struct ast_bridge_config));
03097
03098 config->play_warning = 0;
03099 ast_clear_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
03100 ast_clear_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
03101 config->warning_freq = 0;
03102 config->warning_sound = NULL;
03103 config->end_sound = NULL;
03104 config->start_sound = NULL;
03105 config->firstpass = 0;
03106 }
03107 config->start_time = ast_tvnow();
03108 config->feature_timer = featuredigittimeout;
03109 ast_debug(1, "Set time limit to %ld ms\n", config->feature_timer);
03110 }
03111 }
03112 if (f)
03113 ast_frfree(f);
03114
03115 }
03116 before_you_go:
03117
03118 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT)) {
03119 ast_clear_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT);
03120 if (bridge_cdr) {
03121 ast_cdr_discard(bridge_cdr);
03122
03123 }
03124 return res;
03125 }
03126
03127 if (config->end_bridge_callback) {
03128 config->end_bridge_callback(config->end_bridge_callback_data);
03129 }
03130
03131
03132
03133
03134
03135 if (!ast_test_flag(&(config->features_caller),AST_FEATURE_NO_H_EXTEN) &&
03136 ast_exists_extension(chan, chan->context, "h", 1, chan->cid.cid_num)) {
03137 struct ast_cdr *swapper = NULL;
03138 char savelastapp[AST_MAX_EXTENSION];
03139 char savelastdata[AST_MAX_EXTENSION];
03140 char save_exten[AST_MAX_EXTENSION];
03141 int save_prio;
03142 int found = 0;
03143 int spawn_error = 0;
03144
03145 autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
03146 ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
03147 if (bridge_cdr && ast_opt_end_cdr_before_h_exten) {
03148 ast_cdr_end(bridge_cdr);
03149 }
03150
03151
03152 ast_channel_lock(chan);
03153 if (bridge_cdr) {
03154 swapper = chan->cdr;
03155 ast_copy_string(savelastapp, bridge_cdr->lastapp, sizeof(bridge_cdr->lastapp));
03156 ast_copy_string(savelastdata, bridge_cdr->lastdata, sizeof(bridge_cdr->lastdata));
03157 chan->cdr = bridge_cdr;
03158 }
03159 ast_copy_string(save_exten, chan->exten, sizeof(save_exten));
03160 save_prio = chan->priority;
03161 ast_copy_string(chan->exten, "h", sizeof(chan->exten));
03162 chan->priority = 1;
03163 ast_channel_unlock(chan);
03164 while ((spawn_error = ast_spawn_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num, &found, 1)) == 0) {
03165 chan->priority++;
03166 }
03167 if (spawn_error && (!ast_exists_extension(chan, chan->context, chan->exten, chan->priority, chan->cid.cid_num) || ast_check_hangup(chan))) {
03168
03169 spawn_error = 0;
03170 }
03171 if (found && spawn_error) {
03172
03173 ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
03174 ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
03175 }
03176
03177 ast_channel_lock(chan);
03178 ast_copy_string(chan->exten, save_exten, sizeof(chan->exten));
03179 chan->priority = save_prio;
03180 if (bridge_cdr) {
03181 if (chan->cdr == bridge_cdr) {
03182 chan->cdr = swapper;
03183 } else {
03184 bridge_cdr = NULL;
03185 }
03186 }
03187 if (!spawn_error) {
03188 ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN);
03189 }
03190 ast_channel_unlock(chan);
03191
03192 if (bridge_cdr) {
03193 ast_copy_string(bridge_cdr->lastapp, savelastapp, sizeof(bridge_cdr->lastapp));
03194 ast_copy_string(bridge_cdr->lastdata, savelastdata, sizeof(bridge_cdr->lastdata));
03195 }
03196 ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
03197 }
03198
03199
03200 new_chan_cdr = pick_unlocked_cdr(chan->cdr);
03201 if (bridge_cdr && new_chan_cdr && ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED))
03202 ast_set_flag(bridge_cdr, AST_CDR_FLAG_POST_DISABLED);
03203
03204
03205 if (bridge_cdr) {
03206 ast_cdr_end(bridge_cdr);
03207 ast_cdr_detach(bridge_cdr);
03208 }
03209
03210
03211
03212
03213
03214
03215
03216
03217
03218
03219
03220
03221
03222
03223
03224
03225
03226
03227
03228
03229
03230
03231
03232
03233
03234 if (new_chan_cdr) {
03235 struct ast_channel *chan_ptr = NULL;
03236
03237 if (strcasecmp(orig_channame, chan->name) != 0) {
03238
03239 chan_ptr = ast_get_channel_by_name_locked(orig_channame);
03240 if (chan_ptr) {
03241 if (!ast_bridged_channel(chan_ptr)) {
03242 struct ast_cdr *cur;
03243 for (cur = chan_ptr->cdr; cur; cur = cur->next) {
03244 if (cur == chan_cdr) {
03245 break;
03246 }
03247 }
03248 if (cur)
03249 ast_cdr_specialized_reset(chan_cdr,0);
03250 }
03251 ast_channel_unlock(chan_ptr);
03252 }
03253
03254 ast_cdr_specialized_reset(new_chan_cdr,0);
03255 } else {
03256 ast_cdr_specialized_reset(chan->cdr,0);
03257 }
03258 }
03259
03260 {
03261 struct ast_channel *chan_ptr = NULL;
03262 new_peer_cdr = pick_unlocked_cdr(peer->cdr);
03263 if (new_chan_cdr && ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED) && new_peer_cdr && !ast_test_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED))
03264 ast_set_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED);
03265 if (strcasecmp(orig_peername, peer->name) != 0) {
03266
03267 chan_ptr = ast_get_channel_by_name_locked(orig_peername);
03268 if (chan_ptr) {
03269 if (!ast_bridged_channel(chan_ptr)) {
03270 struct ast_cdr *cur;
03271 for (cur = chan_ptr->cdr; cur; cur = cur->next) {
03272 if (cur == peer_cdr) {
03273 break;
03274 }
03275 }
03276 if (cur)
03277 ast_cdr_specialized_reset(peer_cdr,0);
03278 }
03279 ast_channel_unlock(chan_ptr);
03280 }
03281
03282 if (new_peer_cdr) {
03283 ast_cdr_specialized_reset(new_peer_cdr, 0);
03284 }
03285 } else {
03286 ast_cdr_specialized_reset(peer->cdr, 0);
03287 }
03288 }
03289
03290 return res;
03291 }
03292
03293
03294 static void post_manager_event(const char *s, struct parkeduser *pu)
03295 {
03296 manager_event(EVENT_FLAG_CALL, s,
03297 "Exten: %s\r\n"
03298 "Channel: %s\r\n"
03299 "Parkinglot: %s\r\n"
03300 "CallerIDNum: %s\r\n"
03301 "CallerIDName: %s\r\n"
03302 "UniqueID: %s\r\n\r\n",
03303 pu->parkingexten,
03304 pu->chan->name,
03305 pu->parkinglot->name,
03306 S_OR(pu->chan->cid.cid_num, "<unknown>"),
03307 S_OR(pu->chan->cid.cid_name, "<unknown>"),
03308 pu->chan->uniqueid
03309 );
03310 }
03311
03312 static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
03313 {
03314 int i = 0;
03315 enum {
03316 OPT_CALLEE_REDIRECT = 't',
03317 OPT_CALLER_REDIRECT = 'T',
03318 OPT_CALLEE_AUTOMON = 'w',
03319 OPT_CALLER_AUTOMON = 'W',
03320 OPT_CALLEE_DISCONNECT = 'h',
03321 OPT_CALLER_DISCONNECT = 'H',
03322 OPT_CALLEE_PARKCALL = 'k',
03323 OPT_CALLER_PARKCALL = 'K',
03324 };
03325
03326 memset(options, 0, len);
03327 if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
03328 options[i++] = OPT_CALLER_REDIRECT;
03329 }
03330 if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
03331 options[i++] = OPT_CALLER_AUTOMON;
03332 }
03333 if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
03334 options[i++] = OPT_CALLER_DISCONNECT;
03335 }
03336 if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
03337 options[i++] = OPT_CALLER_PARKCALL;
03338 }
03339
03340 if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
03341 options[i++] = OPT_CALLEE_REDIRECT;
03342 }
03343 if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
03344 options[i++] = OPT_CALLEE_AUTOMON;
03345 }
03346 if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
03347 options[i++] = OPT_CALLEE_DISCONNECT;
03348 }
03349 if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
03350 options[i++] = OPT_CALLEE_PARKCALL;
03351 }
03352
03353 return options;
03354 }
03355
03356
03357 int manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, const int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
03358 {
03359 struct parkeduser *pu;
03360 int res = 0;
03361 char parkingslot[AST_MAX_EXTENSION];
03362
03363
03364 AST_LIST_LOCK(&curlot->parkings);
03365 AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
03366 struct ast_channel *chan = pu->chan;
03367 int tms;
03368 int x;
03369 struct ast_context *con;
03370
03371 if (pu->notquiteyet) {
03372 continue;
03373 }
03374 tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
03375 if (tms > pu->parkingtime) {
03376
03377 ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
03378
03379 if (pu->peername[0]) {
03380 char *peername = ast_strdupa(pu->peername);
03381 char *dash = strrchr(peername, '-');
03382 char peername_flat[AST_MAX_EXTENSION];
03383 int i;
03384
03385 if (dash) {
03386 *dash = '\0';
03387 }
03388 ast_copy_string(peername_flat, peername, sizeof(peername_flat));
03389 for (i = 0; peername_flat[i] && i < AST_MAX_EXTENSION; i++) {
03390 if (peername_flat[i] == '/') {
03391 peername_flat[i] = '0';
03392 }
03393 }
03394 con = ast_context_find_or_create(NULL, NULL, pu->parkinglot->parking_con_dial, registrar);
03395 if (!con) {
03396 ast_log(LOG_ERROR, "Parking dial context '%s' does not exist and unable to create\n", pu->parkinglot->parking_con_dial);
03397 }
03398 if (con) {
03399 char returnexten[AST_MAX_EXTENSION];
03400 struct ast_datastore *features_datastore;
03401 struct ast_dial_features *dialfeatures = NULL;
03402
03403 ast_channel_lock(chan);
03404
03405 if ((features_datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL)))
03406 dialfeatures = features_datastore->data;
03407
03408 ast_channel_unlock(chan);
03409
03410 if (!strncmp(peername, "Parked/", 7)) {
03411 peername += 7;
03412 }
03413
03414 if (dialfeatures) {
03415 char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
03416 snprintf(returnexten, sizeof(returnexten), "%s,30,%s", peername, callback_dialoptions(&(dialfeatures->features_callee), &(dialfeatures->features_caller), buf, sizeof(buf)));
03417 } else {
03418 ast_log(LOG_WARNING, "Dialfeatures not found on %s, using default!\n", chan->name);
03419 snprintf(returnexten, sizeof(returnexten), "%s,30,t", peername);
03420 }
03421
03422 ast_add_extension2(con, 1, peername_flat, 1, NULL, NULL, "Dial", ast_strdup(returnexten), ast_free_ptr, registrar);
03423 }
03424 if (pu->options_specified == 1) {
03425
03426 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
03427 } else {
03428 if (comebacktoorigin) {
03429 set_c_e_p(chan, pu->parkinglot->parking_con_dial, peername_flat, 1);
03430 } else {
03431 ast_log(LOG_WARNING, "now going to parkedcallstimeout,s,1 | ps is %d\n",pu->parkingnum);
03432 snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
03433 pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
03434 set_c_e_p(chan, "parkedcallstimeout", peername_flat, 1);
03435 }
03436 }
03437 } else {
03438
03439
03440 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
03441 }
03442 post_manager_event("ParkedCallTimeOut", pu);
03443
03444 ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n", pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->chan->context, pu->chan->exten, pu->chan->priority);
03445
03446 if (ast_pbx_start(chan)) {
03447 ast_log(LOG_WARNING, "Unable to restart the PBX for user on '%s', hanging them up...\n", pu->chan->name);
03448 ast_hangup(chan);
03449 }
03450
03451 con = ast_context_find(pu->parkinglot->parking_con);
03452 if (con) {
03453 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03454 ast_log(LOG_WARNING, "Whoa, failed to remove the parking extension!\n");
03455 else
03456 notify_metermaids(pu->parkingexten, curlot->parking_con, AST_DEVICE_NOT_INUSE);
03457 } else
03458 ast_log(LOG_WARNING, "Whoa, no parking context?\n");
03459 AST_LIST_REMOVE_CURRENT(list);
03460 free(pu);
03461 } else {
03462 for (x = 0; x < AST_MAX_FDS; x++) {
03463 struct ast_frame *f;
03464 int y;
03465
03466 if (chan->fds[x] == -1) {
03467 continue;
03468 }
03469
03470 for (y = 0; y < nfds; y++) {
03471 if (pfds[y].fd == chan->fds[x]) {
03472
03473 break;
03474 }
03475 }
03476 if (y == nfds) {
03477
03478 continue;
03479 }
03480
03481 if (!(pfds[y].revents & (POLLIN | POLLERR | POLLPRI))) {
03482
03483 continue;
03484 }
03485
03486 if (pfds[y].revents & POLLERR) {
03487 ast_set_flag(chan, AST_FLAG_EXCEPTION);
03488 } else {
03489 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
03490 }
03491 chan->fdno = x;
03492
03493
03494 f = ast_read(pu->chan);
03495
03496 if (!f || ((f->frametype == AST_FRAME_CONTROL) && (f->subclass == AST_CONTROL_HANGUP))) {
03497 if (f)
03498 ast_frfree(f);
03499 post_manager_event("ParkedCallGiveUp", pu);
03500
03501
03502 ast_verb(2, "%s got tired of being parked\n", chan->name);
03503 ast_hangup(chan);
03504
03505 con = ast_context_find(curlot->parking_con);
03506 if (con) {
03507 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03508 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
03509 else
03510 notify_metermaids(pu->parkingexten, curlot->parking_con, AST_DEVICE_NOT_INUSE);
03511 } else
03512 ast_log(LOG_WARNING, "Whoa, no parking context for parking lot %s?\n", curlot->name);
03513 AST_LIST_REMOVE_CURRENT(list);
03514 free(pu);
03515 break;
03516 } else {
03517
03518 ast_frfree(f);
03519 if (pu->moh_trys < 3 && !chan->generatordata) {
03520 ast_debug(1, "MOH on parked call stopped by outside source. Restarting on channel %s.\n", chan->name);
03521 ast_indicate_data(chan, AST_CONTROL_HOLD,
03522 S_OR(curlot->mohclass, NULL),
03523 (!ast_strlen_zero(curlot->mohclass) ? strlen(curlot->mohclass) + 1 : 0));
03524 pu->moh_trys++;
03525 }
03526 goto std;
03527 }
03528 }
03529 if (x >= AST_MAX_FDS) {
03530 std: for (x = 0; x < AST_MAX_FDS; x++) {
03531 if (chan->fds[x] > -1) {
03532 void *tmp = ast_realloc(*new_pfds, (*new_nfds + 1) * sizeof(struct pollfd));
03533 if (!tmp) {
03534 continue;
03535 }
03536 *new_pfds = tmp;
03537 (*new_pfds)[*new_nfds].fd = chan->fds[x];
03538 (*new_pfds)[*new_nfds].events = POLLIN | POLLERR | POLLPRI;
03539 (*new_pfds)[*new_nfds].revents = 0;
03540 (*new_nfds)++;
03541 }
03542 }
03543
03544 if (tms < *ms || *ms < 0) {
03545 *ms = tms;
03546 }
03547 }
03548 }
03549 }
03550 AST_LIST_TRAVERSE_SAFE_END;
03551 AST_LIST_UNLOCK(&curlot->parkings);
03552
03553 return res;
03554 }
03555
03556
03557
03558
03559
03560
03561
03562
03563
03564 static void *do_parking_thread(void *ignore)
03565 {
03566 struct pollfd *pfds = NULL, *new_pfds = NULL;
03567 int nfds = 0, new_nfds = 0;
03568
03569 for (;;) {
03570 struct ao2_iterator iter;
03571 struct ast_parkinglot *curlot;
03572 int ms = -1;
03573 iter = ao2_iterator_init(parkinglots, 0);
03574
03575 while ((curlot = ao2_iterator_next(&iter))) {
03576 manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
03577 ao2_ref(curlot, -1);
03578 }
03579 ao2_iterator_destroy(&iter);
03580
03581
03582 ast_free(pfds);
03583 pfds = new_pfds;
03584 nfds = new_nfds;
03585 new_pfds = NULL;
03586 new_nfds = 0;
03587
03588
03589 ast_poll(pfds, nfds, ms);
03590 pthread_testcancel();
03591 }
03592
03593 return NULL;
03594 }
03595
03596
03597 struct ast_parkinglot *find_parkinglot(const char *name)
03598 {
03599 struct ast_parkinglot *parkinglot = NULL;
03600 struct ast_parkinglot tmp_parkinglot;
03601
03602 if (ast_strlen_zero(name))
03603 return NULL;
03604
03605 ast_copy_string(tmp_parkinglot.name, name, sizeof(tmp_parkinglot.name));
03606
03607 parkinglot = ao2_find(parkinglots, &tmp_parkinglot, OBJ_POINTER);
03608
03609 if (parkinglot && option_debug)
03610 ast_log(LOG_DEBUG, "Found Parkinglot: %s\n", parkinglot->name);
03611
03612 return parkinglot;
03613 }
03614
03615 AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
03616 AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
03617 AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
03618 AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
03619 END_OPTIONS );
03620
03621
03622 static int park_call_exec(struct ast_channel *chan, void *data)
03623 {
03624
03625
03626
03627 char *orig_chan_name = ast_strdupa(chan->name);
03628 char orig_exten[AST_MAX_EXTENSION];
03629 int orig_priority = chan->priority;
03630
03631
03632
03633 int res = 0;
03634
03635 char *parse = NULL;
03636 AST_DECLARE_APP_ARGS(app_args,
03637 AST_APP_ARG(timeout);
03638 AST_APP_ARG(return_con);
03639 AST_APP_ARG(return_ext);
03640 AST_APP_ARG(return_pri);
03641 AST_APP_ARG(options);
03642 );
03643
03644 parse = ast_strdupa(data);
03645 AST_STANDARD_APP_ARGS(app_args, parse);
03646
03647 ast_copy_string(orig_exten, chan->exten, sizeof(orig_exten));
03648
03649
03650
03651 strcpy(chan->exten, "s");
03652 chan->priority = 1;
03653
03654
03655 if (chan->_state != AST_STATE_UP)
03656 res = ast_answer(chan);
03657
03658
03659 if (!res)
03660 res = ast_safe_sleep(chan, 1000);
03661
03662
03663 if (!res) {
03664 struct ast_park_call_args args = {
03665 .orig_chan_name = orig_chan_name,
03666 };
03667 struct ast_flags flags = { 0 };
03668
03669 if (parse) {
03670 if (!ast_strlen_zero(app_args.timeout)) {
03671 if (sscanf(app_args.timeout, "%30d", &args.timeout) != 1) {
03672 ast_log(LOG_WARNING, "Invalid timeout '%s' provided\n", app_args.timeout);
03673 args.timeout = 0;
03674 }
03675 }
03676 if (!ast_strlen_zero(app_args.return_con)) {
03677 args.return_con = app_args.return_con;
03678 }
03679 if (!ast_strlen_zero(app_args.return_ext)) {
03680 args.return_ext = app_args.return_ext;
03681 }
03682 if (!ast_strlen_zero(app_args.return_pri)) {
03683 if (sscanf(app_args.return_pri, "%30d", &args.return_pri) != 1) {
03684 ast_log(LOG_WARNING, "Invalid priority '%s' specified\n", app_args.return_pri);
03685 args.return_pri = 0;
03686 }
03687 }
03688 }
03689
03690 ast_app_parse_options(park_call_options, &flags, NULL, app_args.options);
03691 args.flags = flags.flags;
03692
03693 res = masq_park_call_announce_args(chan, chan, &args);
03694
03695 if (res == 1) {
03696 ast_copy_string(chan->exten, orig_exten, sizeof(chan->exten));
03697 chan->priority = orig_priority;
03698 res = 0;
03699 } else if (!res) {
03700 res = 1;
03701 }
03702 }
03703
03704 return res;
03705 }
03706
03707
03708 static int park_exec_full(struct ast_channel *chan, void *data, struct ast_parkinglot *parkinglot)
03709 {
03710 int res = 0;
03711 struct ast_channel *peer=NULL;
03712 struct parkeduser *pu;
03713 struct ast_context *con;
03714 int park = 0;
03715 struct ast_bridge_config config;
03716
03717 if (data)
03718 park = atoi((char *)data);
03719
03720 parkinglot = find_parkinglot(findparkinglotname(chan));
03721 if (!parkinglot)
03722 parkinglot = default_parkinglot;
03723
03724 AST_LIST_LOCK(&parkinglot->parkings);
03725 AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot->parkings, pu, list) {
03726 if (!pu->notquiteyet && (!data || pu->parkingnum == park)) {
03727 if (pu->chan->pbx) {
03728 AST_LIST_UNLOCK(&parkinglot->parkings);
03729 return -1;
03730 }
03731 AST_LIST_REMOVE_CURRENT(list);
03732 break;
03733 }
03734 }
03735 AST_LIST_TRAVERSE_SAFE_END;
03736 AST_LIST_UNLOCK(&parkinglot->parkings);
03737
03738 if (pu) {
03739 peer = pu->chan;
03740 con = ast_context_find(parkinglot->parking_con);
03741 if (con) {
03742 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0))
03743 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
03744 else
03745 notify_metermaids(pu->parkingexten, parkinglot->parking_con, AST_DEVICE_NOT_INUSE);
03746 } else
03747 ast_log(LOG_WARNING, "Whoa, no parking context?\n");
03748
03749 manager_event(EVENT_FLAG_CALL, "UnParkedCall",
03750 "Exten: %s\r\n"
03751 "Channel: %s\r\n"
03752 "From: %s\r\n"
03753 "CallerIDNum: %s\r\n"
03754 "CallerIDName: %s\r\n",
03755 pu->parkingexten, pu->chan->name, chan->name,
03756 S_OR(pu->chan->cid.cid_num, "<unknown>"),
03757 S_OR(pu->chan->cid.cid_name, "<unknown>")
03758 );
03759
03760 ast_free(pu);
03761 }
03762
03763 if (chan->_state != AST_STATE_UP)
03764 ast_answer(chan);
03765
03766
03767
03768
03769
03770 if (peer) {
03771 struct ast_datastore *features_datastore;
03772 struct ast_dial_features *dialfeatures = NULL;
03773
03774
03775
03776 if (!ast_strlen_zero(courtesytone)) {
03777 int error = 0;
03778 ast_indicate(peer, AST_CONTROL_UNHOLD);
03779 if (parkedplay == 0) {
03780 error = ast_stream_and_wait(chan, courtesytone, "");
03781 } else if (parkedplay == 1) {
03782 error = ast_stream_and_wait(peer, courtesytone, "");
03783 } else if (parkedplay == 2) {
03784 if (!ast_streamfile(chan, courtesytone, chan->language) &&
03785 !ast_streamfile(peer, courtesytone, chan->language)) {
03786
03787 res = ast_waitstream(chan, "");
03788 if (res >= 0)
03789 res = ast_waitstream(peer, "");
03790 if (res < 0)
03791 error = 1;
03792 }
03793 }
03794 if (error) {
03795 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
03796 ast_hangup(peer);
03797 return -1;
03798 }
03799 } else
03800 ast_indicate(peer, AST_CONTROL_UNHOLD);
03801
03802 res = ast_channel_make_compatible(chan, peer);
03803 if (res < 0) {
03804 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
03805 ast_hangup(peer);
03806 return -1;
03807 }
03808
03809
03810 ast_verb(3, "Channel %s connected to parked call %d\n", chan->name, park);
03811
03812 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
03813 ast_cdr_setdestchan(chan->cdr, peer->name);
03814 memset(&config, 0, sizeof(struct ast_bridge_config));
03815
03816
03817 ast_channel_lock(peer);
03818 if ((features_datastore = ast_channel_datastore_find(peer, &dial_features_info, NULL))) {
03819 dialfeatures = features_datastore->data;
03820 }
03821 ast_channel_unlock(peer);
03822
03823
03824
03825
03826
03827 if (dialfeatures) {
03828 ast_copy_flags(&(config.features_callee), &(dialfeatures->features_caller), AST_FLAGS_ALL);
03829 }
03830
03831 if ((parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
03832 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
03833 }
03834 if ((parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
03835 ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
03836 }
03837 if ((parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
03838 ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
03839 }
03840 if ((parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
03841 ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
03842 }
03843 if ((parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
03844 ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
03845 }
03846 if ((parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
03847 ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
03848 }
03849 if ((parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
03850 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
03851 }
03852 if ((parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
03853 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
03854 }
03855
03856 res = ast_bridge_call(chan, peer, &config);
03857
03858 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
03859 ast_cdr_setdestchan(chan->cdr, peer->name);
03860
03861
03862 ast_hangup(peer);
03863 return -1;
03864 } else {
03865
03866 if (ast_stream_and_wait(chan, "pbx-invalidpark", ""))
03867 ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark", chan->name);
03868 ast_verb(3, "Channel %s tried to talk to nonexistent parked call %d\n", chan->name, park);
03869 res = -1;
03870 }
03871
03872 return -1;
03873 }
03874
03875 static int park_exec(struct ast_channel *chan, void *data)
03876 {
03877 return park_exec_full(chan, data, default_parkinglot);
03878 }
03879
03880
03881
03882 static void parkinglot_unref(struct ast_parkinglot *parkinglot)
03883 {
03884 int refcount = ao2_ref(parkinglot, -1);
03885 if (option_debug > 2)
03886 ast_log(LOG_DEBUG, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount - 1);
03887 }
03888
03889 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
03890 {
03891 int refcount = ao2_ref(parkinglot, +1);
03892 if (option_debug > 2)
03893 ast_log(LOG_DEBUG, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
03894 return parkinglot;
03895 }
03896
03897
03898 static struct ast_parkinglot *create_parkinglot(char *name)
03899 {
03900 struct ast_parkinglot *newlot = (struct ast_parkinglot *) NULL;
03901
03902 if (!name)
03903 return NULL;
03904
03905 newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
03906 if (!newlot)
03907 return NULL;
03908
03909 ast_copy_string(newlot->name, name, sizeof(newlot->name));
03910 AST_LIST_HEAD_INIT(&newlot->parkings);
03911
03912 return newlot;
03913 }
03914
03915
03916 static void parkinglot_destroy(void *obj)
03917 {
03918 struct ast_parkinglot *ruin = obj;
03919 struct ast_context *con;
03920 con = ast_context_find(ruin->parking_con);
03921 if (con)
03922 ast_context_destroy(con, registrar);
03923 ao2_unlink(parkinglots, ruin);
03924 }
03925
03926
03927 static struct ast_parkinglot *build_parkinglot(char *name, struct ast_variable *var)
03928 {
03929 struct ast_parkinglot *parkinglot;
03930 struct ast_context *con = NULL;
03931
03932 struct ast_variable *confvar = var;
03933 int error = 0;
03934 int start = 0, end = 0;
03935 int oldparkinglot = 0;
03936
03937 parkinglot = find_parkinglot(name);
03938 if (parkinglot)
03939 oldparkinglot = 1;
03940 else
03941 parkinglot = create_parkinglot(name);
03942
03943 if (!parkinglot)
03944 return NULL;
03945
03946 ao2_lock(parkinglot);
03947
03948 if (option_debug)
03949 ast_log(LOG_DEBUG, "Building parking lot %s\n", name);
03950
03951
03952 while(confvar) {
03953 if (!strcasecmp(confvar->name, "context")) {
03954 ast_copy_string(parkinglot->parking_con, confvar->value, sizeof(parkinglot->parking_con));
03955 } else if (!strcasecmp(confvar->name, "parkingtime")) {
03956 if ((sscanf(confvar->value, "%30d", &parkinglot->parkingtime) != 1) || (parkinglot->parkingtime < 1)) {
03957 ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", confvar->value);
03958 parkinglot->parkingtime = DEFAULT_PARK_TIME;
03959 } else
03960 parkinglot->parkingtime = parkinglot->parkingtime * 1000;
03961 } else if (!strcasecmp(confvar->name, "parkpos")) {
03962 if (sscanf(confvar->value, "%30d-%30d", &start, &end) != 2) {
03963 ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of parking.conf\n", confvar->lineno);
03964 error = 1;
03965 } else {
03966 parkinglot->parking_start = start;
03967 parkinglot->parking_stop = end;
03968 }
03969 } else if (!strcasecmp(confvar->name, "findslot")) {
03970 parkinglot->parkfindnext = (!strcasecmp(confvar->value, "next"));
03971 } else if (!strcasecmp(confvar->name, "parkedcalltransfers") ||
03972 !strcasecmp(confvar->name, "parkedcallreparking") ||
03973 !strcasecmp(confvar->name, "parkedcallhangup") ||
03974 !strcasecmp(confvar->name, "parkedcallrecording")) {
03975 if (!strcasecmp(confvar->value, "both")) {
03976 parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
03977 } else if (!strcasecmp(confvar->value, "caller")) {
03978 parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
03979 } else if (!strcasecmp(confvar->value, "callee")) {
03980 parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
03981 }
03982 }
03983 confvar = confvar->next;
03984 }
03985
03986 if (parkinglot->parkingtime == 0) {
03987 parkinglot->parkingtime = DEFAULT_PARK_TIME;
03988 }
03989
03990 if (!var) {
03991 ast_copy_string(parkinglot->parking_con, "parkedcalls", sizeof(parkinglot->parking_con));
03992 ast_copy_string(parkinglot->parking_con_dial, "park-dial", sizeof(parkinglot->parking_con_dial));
03993 ast_copy_string(parkinglot->mohclass, "default", sizeof(parkinglot->mohclass));
03994 }
03995
03996
03997 if (ast_strlen_zero(parkinglot->parking_con)) {
03998 ast_log(LOG_WARNING, "Parking lot %s lacks context\n", name);
03999 error = 1;
04000 }
04001
04002
04003 if (!error && !(con = ast_context_find_or_create(NULL, NULL, parkinglot->parking_con, registrar))) {
04004 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", parkinglot->parking_con);
04005 error = 1;
04006 }
04007
04008
04009 if (!error && !oldparkinglot) {
04010 if (!ast_strlen_zero(ast_parking_ext())) {
04011 if (ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, strdup(""), ast_free_ptr, registrar) == -1)
04012 error = 1;
04013 }
04014 }
04015
04016 ao2_unlock(parkinglot);
04017
04018 if (error) {
04019 ast_log(LOG_WARNING, "Parking %s not open for business. Configuration error.\n", name);
04020 parkinglot_destroy(parkinglot);
04021 return NULL;
04022 }
04023 if (option_debug)
04024 ast_log(LOG_DEBUG, "Parking %s now open for business. (start exten %d end %d)\n", name, start, end);
04025
04026
04027
04028 if (!oldparkinglot) {
04029 ao2_link(parkinglots, parkinglot);
04030 }
04031 parkinglot_unref(parkinglot);
04032
04033 return parkinglot;
04034 }
04035
04036
04037
04038
04039
04040
04041
04042
04043 static void park_add_hints(char *context, int start, int stop)
04044 {
04045 int numext;
04046 char device[AST_MAX_EXTENSION];
04047 char exten[10];
04048
04049 for (numext = start; numext <= stop; numext++) {
04050 snprintf(exten, sizeof(exten), "%d", numext);
04051 snprintf(device, sizeof(device), "park:%s@%s", exten, context);
04052 ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
04053 }
04054 }
04055
04056 static int load_config(void)
04057 {
04058 int start = 0, end = 0;
04059 int res;
04060 int i;
04061 struct ast_context *con = NULL;
04062 struct ast_config *cfg = NULL;
04063 struct ast_variable *var = NULL;
04064 struct feature_group *fg = NULL;
04065 struct ast_flags config_flags = { 0 };
04066 char old_parking_ext[AST_MAX_EXTENSION];
04067 char old_parking_con[AST_MAX_EXTENSION] = "";
04068 char *ctg;
04069 static const char *categories[] = {
04070
04071
04072
04073 "general",
04074 "featuremap",
04075 "applicationmap"
04076 };
04077
04078 if (default_parkinglot) {
04079 strcpy(old_parking_con, default_parkinglot->parking_con);
04080 strcpy(old_parking_ext, parking_ext);
04081 } else {
04082 default_parkinglot = build_parkinglot(DEFAULT_PARKINGLOT, NULL);
04083 if (default_parkinglot) {
04084 ao2_lock(default_parkinglot);
04085 default_parkinglot->parking_start = 701;
04086 default_parkinglot->parking_stop = 750;
04087 default_parkinglot->parking_offset = 0;
04088 default_parkinglot->parkfindnext = 0;
04089 default_parkinglot->parkingtime = DEFAULT_PARK_TIME;
04090 ao2_unlock(default_parkinglot);
04091 }
04092 }
04093 if (default_parkinglot) {
04094 if (option_debug)
04095 ast_log(LOG_DEBUG, "Configuration of default parkinglot done.\n");
04096 } else {
04097 ast_log(LOG_ERROR, "Configuration of default parkinglot failed.\n");
04098 return -1;
04099 }
04100
04101
04102
04103 strcpy(parking_ext, "700");
04104 strcpy(pickup_ext, "*8");
04105 courtesytone[0] = '\0';
04106 strcpy(xfersound, "beep");
04107 strcpy(xferfailsound, "pbx-invalid");
04108 pickupsound[0] = '\0';
04109 pickupfailsound[0] = '\0';
04110 adsipark = 0;
04111 comebacktoorigin = 1;
04112
04113 default_parkinglot->parkaddhints = 0;
04114 default_parkinglot->parkedcalltransfers = 0;
04115 default_parkinglot->parkedcallreparking = 0;
04116 default_parkinglot->parkedcallrecording = 0;
04117 default_parkinglot->parkedcallhangup = 0;
04118
04119 transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
04120 featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
04121 atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
04122 atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
04123 atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
04124 atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
04125
04126 cfg = ast_config_load2("features.conf", "features", config_flags);
04127 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID) {
04128 ast_log(LOG_WARNING,"Could not load features.conf\n");
04129 return 0;
04130 }
04131 for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
04132 if (!strcasecmp(var->name, "parkext")) {
04133 ast_copy_string(parking_ext, var->value, sizeof(parking_ext));
04134 } else if (!strcasecmp(var->name, "context")) {
04135 ast_copy_string(default_parkinglot->parking_con, var->value, sizeof(default_parkinglot->parking_con));
04136 } else if (!strcasecmp(var->name, "parkingtime")) {
04137 if ((sscanf(var->value, "%30d", &default_parkinglot->parkingtime) != 1) || (default_parkinglot->parkingtime < 1)) {
04138 ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
04139 default_parkinglot->parkingtime = DEFAULT_PARK_TIME;
04140 } else
04141 default_parkinglot->parkingtime = default_parkinglot->parkingtime * 1000;
04142 } else if (!strcasecmp(var->name, "parkpos")) {
04143 if (sscanf(var->value, "%30d-%30d", &start, &end) != 2) {
04144 ast_log(LOG_WARNING, "Format for parking positions is a-b, where a and b are numbers at line %d of features.conf\n", var->lineno);
04145 } else if (default_parkinglot) {
04146 default_parkinglot->parking_start = start;
04147 default_parkinglot->parking_stop = end;
04148 } else {
04149 ast_log(LOG_WARNING, "No default parking lot!\n");
04150 }
04151 } else if (!strcasecmp(var->name, "findslot")) {
04152 default_parkinglot->parkfindnext = (!strcasecmp(var->value, "next"));
04153 } else if (!strcasecmp(var->name, "parkinghints")) {
04154 default_parkinglot->parkaddhints = ast_true(var->value);
04155 } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
04156 if (!strcasecmp(var->value, "both"))
04157 default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYBOTH;
04158 else if (!strcasecmp(var->value, "caller"))
04159 default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLER;
04160 else if (!strcasecmp(var->value, "callee"))
04161 default_parkinglot->parkedcalltransfers = AST_FEATURE_FLAG_BYCALLEE;
04162 } else if (!strcasecmp(var->name, "parkedcallreparking")) {
04163 if (!strcasecmp(var->value, "both"))
04164 default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYBOTH;
04165 else if (!strcasecmp(var->value, "caller"))
04166 default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYCALLER;
04167 else if (!strcasecmp(var->value, "callee"))
04168 default_parkinglot->parkedcallreparking = AST_FEATURE_FLAG_BYCALLEE;
04169 } else if (!strcasecmp(var->name, "parkedcallhangup")) {
04170 if (!strcasecmp(var->value, "both"))
04171 default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYBOTH;
04172 else if (!strcasecmp(var->value, "caller"))
04173 default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYCALLER;
04174 else if (!strcasecmp(var->value, "callee"))
04175 default_parkinglot->parkedcallhangup = AST_FEATURE_FLAG_BYCALLEE;
04176 } else if (!strcasecmp(var->name, "parkedcallrecording")) {
04177 if (!strcasecmp(var->value, "both"))
04178 default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYBOTH;
04179 else if (!strcasecmp(var->value, "caller"))
04180 default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYCALLER;
04181 else if (!strcasecmp(var->value, "callee"))
04182 default_parkinglot->parkedcallrecording = AST_FEATURE_FLAG_BYCALLEE;
04183 } else if (!strcasecmp(var->name, "adsipark")) {
04184 adsipark = ast_true(var->value);
04185 } else if (!strcasecmp(var->name, "transferdigittimeout")) {
04186 if ((sscanf(var->value, "%30d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
04187 ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
04188 transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
04189 } else
04190 transferdigittimeout = transferdigittimeout * 1000;
04191 } else if (!strcasecmp(var->name, "featuredigittimeout")) {
04192 if ((sscanf(var->value, "%30d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
04193 ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
04194 featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
04195 }
04196 } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
04197 if ((sscanf(var->value, "%30d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
04198 ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
04199 atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
04200 } else
04201 atxfernoanswertimeout = atxfernoanswertimeout * 1000;
04202 } else if (!strcasecmp(var->name, "atxferloopdelay")) {
04203 if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
04204 ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
04205 atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
04206 } else
04207 atxferloopdelay *= 1000;
04208 } else if (!strcasecmp(var->name, "atxferdropcall")) {
04209 atxferdropcall = ast_true(var->value);
04210 } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
04211 if ((sscanf(var->value, "%30u", &atxfercallbackretries) != 1)) {
04212 ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
04213 atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
04214 }
04215 } else if (!strcasecmp(var->name, "courtesytone")) {
04216 ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
04217 } else if (!strcasecmp(var->name, "parkedplay")) {
04218 if (!strcasecmp(var->value, "both"))
04219 parkedplay = 2;
04220 else if (!strcasecmp(var->value, "parked"))
04221 parkedplay = 1;
04222 else
04223 parkedplay = 0;
04224 } else if (!strcasecmp(var->name, "xfersound")) {
04225 ast_copy_string(xfersound, var->value, sizeof(xfersound));
04226 } else if (!strcasecmp(var->name, "xferfailsound")) {
04227 ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
04228 } else if (!strcasecmp(var->name, "pickupexten")) {
04229 ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));
04230 } else if (!strcasecmp(var->name, "pickupsound")) {
04231 ast_copy_string(pickupsound, var->value, sizeof(pickupsound));
04232 } else if (!strcasecmp(var->name, "pickupfailsound")) {
04233 ast_copy_string(pickupfailsound, var->value, sizeof(pickupfailsound));
04234 } else if (!strcasecmp(var->name, "comebacktoorigin")) {
04235 comebacktoorigin = ast_true(var->value);
04236 } else if (!strcasecmp(var->name, "parkedmusicclass")) {
04237 ast_copy_string(default_parkinglot->mohclass, var->value, sizeof(default_parkinglot->mohclass));
04238 }
04239 }
04240
04241 unmap_features();
04242 for (var = ast_variable_browse(cfg, "featuremap"); var; var = var->next) {
04243 if (remap_feature(var->name, var->value))
04244 ast_log(LOG_NOTICE, "Unknown feature '%s'\n", var->name);
04245 }
04246
04247
04248 ast_unregister_features();
04249 for (var = ast_variable_browse(cfg, "applicationmap"); var; var = var->next) {
04250 char *tmp_val = ast_strdupa(var->value);
04251 char *activateon;
04252 struct ast_call_feature *feature;
04253 AST_DECLARE_APP_ARGS(args,
04254 AST_APP_ARG(exten);
04255 AST_APP_ARG(activatedby);
04256 AST_APP_ARG(app);
04257 AST_APP_ARG(app_args);
04258 AST_APP_ARG(moh_class);
04259 );
04260
04261 AST_STANDARD_APP_ARGS(args, tmp_val);
04262 if (strchr(args.app, '(')) {
04263
04264 args.moh_class = args.app_args;
04265 args.app_args = strchr(args.app, '(');
04266 *args.app_args++ = '\0';
04267 if (args.app_args[strlen(args.app_args) - 1] == ')') {
04268 args.app_args[strlen(args.app_args) - 1] = '\0';
04269 }
04270 }
04271
04272 activateon = strsep(&args.activatedby, "/");
04273
04274
04275 if (ast_strlen_zero(args.app) || ast_strlen_zero(args.exten) || ast_strlen_zero(activateon) || ast_strlen_zero(var->name)) {
04276 ast_log(LOG_NOTICE, "Please check the feature Mapping Syntax, either extension, name, or app aren't provided %s %s %s %s\n",
04277 args.app, args.exten, activateon, var->name);
04278 continue;
04279 }
04280
04281 AST_RWLIST_RDLOCK(&feature_list);
04282 if ((feature = find_dynamic_feature(var->name))) {
04283 AST_RWLIST_UNLOCK(&feature_list);
04284 ast_log(LOG_WARNING, "Dynamic Feature '%s' specified more than once!\n", var->name);
04285 continue;
04286 }
04287 AST_RWLIST_UNLOCK(&feature_list);
04288
04289 if (!(feature = ast_calloc(1, sizeof(*feature)))) {
04290 continue;
04291 }
04292
04293 ast_copy_string(feature->sname, var->name, FEATURE_SNAME_LEN);
04294 ast_copy_string(feature->app, args.app, FEATURE_APP_LEN);
04295 ast_copy_string(feature->exten, args.exten, FEATURE_EXTEN_LEN);
04296
04297 if (args.app_args) {
04298 ast_copy_string(feature->app_args, args.app_args, FEATURE_APP_ARGS_LEN);
04299 }
04300
04301 if (args.moh_class) {
04302 ast_copy_string(feature->moh_class, args.moh_class, FEATURE_MOH_LEN);
04303 }
04304
04305 ast_copy_string(feature->exten, args.exten, sizeof(feature->exten));
04306 feature->operation = feature_exec_app;
04307 ast_set_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF);
04308
04309
04310 if (!strcasecmp(activateon, "self") || !strcasecmp(activateon, "caller"))
04311 ast_set_flag(feature, AST_FEATURE_FLAG_ONSELF);
04312 else if (!strcasecmp(activateon, "peer") || !strcasecmp(activateon, "callee"))
04313 ast_set_flag(feature, AST_FEATURE_FLAG_ONPEER);
04314 else {
04315 ast_log(LOG_NOTICE, "Invalid 'ActivateOn' specification for feature '%s',"
04316 " must be 'self', or 'peer'\n", var->name);
04317 continue;
04318 }
04319
04320 if (ast_strlen_zero(args.activatedby))
04321 ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
04322 else if (!strcasecmp(args.activatedby, "caller"))
04323 ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLER);
04324 else if (!strcasecmp(args.activatedby, "callee"))
04325 ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLEE);
04326 else if (!strcasecmp(args.activatedby, "both"))
04327 ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
04328 else {
04329 ast_log(LOG_NOTICE, "Invalid 'ActivatedBy' specification for feature '%s',"
04330 " must be 'caller', or 'callee', or 'both'\n", var->name);
04331 continue;
04332 }
04333
04334 ast_register_feature(feature);
04335
04336 ast_verb(2, "Mapping Feature '%s' to app '%s(%s)' with code '%s'\n", var->name, args.app, args.app_args, args.exten);
04337 }
04338
04339 ast_unregister_groups();
04340 AST_RWLIST_WRLOCK(&feature_groups);
04341
04342 ctg = NULL;
04343 while ((ctg = ast_category_browse(cfg, ctg))) {
04344
04345 if (!strncasecmp(ctg, "parkinglot_", strlen("parkinglot_"))) {
04346 ast_debug(2, "Found configuration section %s, assume parking context\n", ctg);
04347 if(!build_parkinglot(ctg, ast_variable_browse(cfg, ctg)))
04348 ast_log(LOG_ERROR, "Could not build parking lot %s. Configuration error.\n", ctg);
04349 else
04350 ast_debug(1, "Configured parking context %s\n", ctg);
04351 continue;
04352 }
04353
04354 for (i = 0; i < ARRAY_LEN(categories); i++) {
04355 if (!strcasecmp(categories[i], ctg))
04356 break;
04357 }
04358
04359 if (i < ARRAY_LEN(categories))
04360 continue;
04361
04362 if (!(fg = register_group(ctg)))
04363 continue;
04364
04365 for (var = ast_variable_browse(cfg, ctg); var; var = var->next) {
04366 struct ast_call_feature *feature;
04367
04368 AST_RWLIST_RDLOCK(&feature_list);
04369 if (!(feature = find_dynamic_feature(var->name)) &&
04370 !(feature = ast_find_call_feature(var->name))) {
04371 AST_RWLIST_UNLOCK(&feature_list);
04372 ast_log(LOG_WARNING, "Feature '%s' was not found.\n", var->name);
04373 continue;
04374 }
04375 AST_RWLIST_UNLOCK(&feature_list);
04376
04377 register_group_feature(fg, var->value, feature);
04378 }
04379 }
04380
04381 AST_RWLIST_UNLOCK(&feature_groups);
04382
04383 ast_config_destroy(cfg);
04384
04385
04386 if (!ast_strlen_zero(old_parking_con) && (con = ast_context_find(old_parking_con))) {
04387 if(ast_context_remove_extension2(con, old_parking_ext, 1, registrar, 0))
04388 notify_metermaids(old_parking_ext, old_parking_con, AST_DEVICE_NOT_INUSE);
04389 ast_debug(1, "Removed old parking extension %s@%s\n", old_parking_ext, old_parking_con);
04390 }
04391
04392 if (!(con = ast_context_find_or_create(NULL, NULL, default_parkinglot->parking_con, registrar))) {
04393 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n", default_parkinglot->parking_con);
04394 return -1;
04395 }
04396 res = ast_add_extension2(con, 1, ast_parking_ext(), 1, NULL, NULL, parkcall, NULL, NULL, registrar);
04397 if (default_parkinglot->parkaddhints)
04398 park_add_hints(default_parkinglot->parking_con, default_parkinglot->parking_start, default_parkinglot->parking_stop);
04399 if (!res)
04400 notify_metermaids(ast_parking_ext(), default_parkinglot->parking_con, AST_DEVICE_INUSE);
04401 return res;
04402
04403 }
04404
04405
04406
04407
04408
04409
04410
04411
04412
04413
04414 static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04415 {
04416 int i;
04417 struct ast_call_feature *feature;
04418 struct ao2_iterator iter;
04419 struct ast_parkinglot *curlot;
04420 #define HFS_FORMAT "%-25s %-7s %-7s\n"
04421
04422 switch (cmd) {
04423
04424 case CLI_INIT:
04425 e->command = "features show";
04426 e->usage =
04427 "Usage: features show\n"
04428 " Lists configured features\n";
04429 return NULL;
04430 case CLI_GENERATE:
04431 return NULL;
04432 }
04433
04434 ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
04435 ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
04436
04437 ast_cli(a->fd, HFS_FORMAT, "Pickup", "*8", ast_pickup_ext());
04438
04439 ast_rwlock_rdlock(&features_lock);
04440 for (i = 0; i < FEATURES_COUNT; i++)
04441 ast_cli(a->fd, HFS_FORMAT, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
04442 ast_rwlock_unlock(&features_lock);
04443
04444 ast_cli(a->fd, "\n");
04445 ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
04446 ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
04447 if (AST_RWLIST_EMPTY(&feature_list)) {
04448 ast_cli(a->fd, "(none)\n");
04449 } else {
04450 AST_RWLIST_RDLOCK(&feature_list);
04451 AST_RWLIST_TRAVERSE(&feature_list, feature, feature_entry) {
04452 ast_cli(a->fd, HFS_FORMAT, feature->sname, "no def", feature->exten);
04453 }
04454 AST_RWLIST_UNLOCK(&feature_list);
04455 }
04456
04457 ast_cli(a->fd, "\nFeature Groups:\n");
04458 ast_cli(a->fd, "---------------\n");
04459 if (AST_RWLIST_EMPTY(&feature_groups)) {
04460 ast_cli(a->fd, "(none)\n");
04461 } else {
04462 struct feature_group *fg;
04463 struct feature_group_exten *fge;
04464
04465 AST_RWLIST_RDLOCK(&feature_groups);
04466 AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
04467 ast_cli(a->fd, "===> Group: %s\n", fg->gname);
04468 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
04469 ast_cli(a->fd, "===> --> %s (%s)\n", fge->feature->sname, fge->exten);
04470 }
04471 }
04472 AST_RWLIST_UNLOCK(&feature_groups);
04473 }
04474
04475 iter = ao2_iterator_init(parkinglots, 0);
04476 while ((curlot = ao2_iterator_next(&iter))) {
04477 ast_cli(a->fd, "\nCall parking (Parking lot: %s)\n", curlot->name);
04478 ast_cli(a->fd, "------------\n");
04479 ast_cli(a->fd,"%-22s: %s\n", "Parking extension", parking_ext);
04480 ast_cli(a->fd,"%-22s: %s\n", "Parking context", curlot->parking_con);
04481 ast_cli(a->fd,"%-22s: %d-%d\n", "Parked call extensions", curlot->parking_start, curlot->parking_stop);
04482 ast_cli(a->fd,"\n");
04483 ao2_ref(curlot, -1);
04484 }
04485 ao2_iterator_destroy(&iter);
04486
04487 return CLI_SUCCESS;
04488 }
04489
04490 int ast_features_reload(void)
04491 {
04492 int res;
04493
04494
04495
04496
04497
04498 res = load_config();
04499
04500
04501 return res;
04502 }
04503
04504 static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04505 {
04506 switch (cmd) {
04507 case CLI_INIT:
04508 e->command = "features reload";
04509 e->usage =
04510 "Usage: features reload\n"
04511 " Reloads configured call features from features.conf\n";
04512 return NULL;
04513 case CLI_GENERATE:
04514 return NULL;
04515 }
04516 ast_features_reload();
04517
04518 return CLI_SUCCESS;
04519 }
04520
04521 static char mandescr_bridge[] =
04522 "Description: Bridge together two channels already in the PBX\n"
04523 "Variables: ( Headers marked with * are required )\n"
04524 " *Channel1: Channel to Bridge to Channel2\n"
04525 " *Channel2: Channel to Bridge to Channel1\n"
04526 " Tone: (Yes|No) Play courtesy tone to Channel 2\n"
04527 "\n";
04528
04529
04530
04531
04532
04533
04534
04535
04536
04537 static void do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
04538 {
04539 ast_moh_stop(chan);
04540 ast_channel_lock(chan);
04541 ast_setstate(tmpchan, chan->_state);
04542 tmpchan->readformat = chan->readformat;
04543 tmpchan->writeformat = chan->writeformat;
04544 ast_channel_masquerade(tmpchan, chan);
04545 ast_channel_lock(tmpchan);
04546 ast_do_masquerade(tmpchan);
04547
04548 ast_explicit_goto(tmpchan, chan->context, chan->exten, chan->priority + 1);
04549 ast_channel_unlock(tmpchan);
04550 ast_channel_unlock(chan);
04551 }
04552
04553
04554
04555
04556
04557
04558
04559
04560
04561
04562
04563
04564
04565
04566
04567 static int action_bridge(struct mansession *s, const struct message *m)
04568 {
04569 const char *channela = astman_get_header(m, "Channel1");
04570 const char *channelb = astman_get_header(m, "Channel2");
04571 const char *playtone = astman_get_header(m, "Tone");
04572 struct ast_channel *chana = NULL, *chanb = NULL;
04573 struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
04574 struct ast_bridge_thread_obj *tobj = NULL;
04575
04576
04577 if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
04578 astman_send_error(s, m, "Missing channel parameter in request");
04579 return 0;
04580 }
04581
04582
04583
04584
04585
04586
04587 chana = ast_get_channel_by_name_prefix_locked(channela, strlen(channela));
04588
04589
04590 if (!chana) {
04591 char buf[256];
04592 snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
04593 astman_send_error(s, m, buf);
04594 return 0;
04595 }
04596
04597
04598 if (chana->_state != AST_STATE_UP)
04599 ast_answer(chana);
04600
04601
04602 if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
04603 NULL, NULL, 0, "Bridge/%s", chana->name))) {
04604 astman_send_error(s, m, "Unable to create temporary channel!");
04605 ast_channel_unlock(chana);
04606 return 1;
04607 }
04608
04609 do_bridge_masquerade(chana, tmpchana);
04610 ast_channel_unlock(chana);
04611 chana = NULL;
04612
04613
04614 chanb = ast_get_channel_by_name_prefix_locked(channelb, strlen(channelb));
04615
04616 if (!chanb) {
04617 char buf[256];
04618 snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
04619 ast_hangup(tmpchana);
04620 astman_send_error(s, m, buf);
04621 return 0;
04622 }
04623
04624
04625 if (chanb->_state != AST_STATE_UP)
04626 ast_answer(chanb);
04627
04628
04629 if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
04630 NULL, NULL, 0, "Bridge/%s", chanb->name))) {
04631 astman_send_error(s, m, "Unable to create temporary channels!");
04632 ast_hangup(tmpchana);
04633 ast_channel_unlock(chanb);
04634 return 1;
04635 }
04636 do_bridge_masquerade(chanb, tmpchanb);
04637 ast_channel_unlock(chanb);
04638 chanb = NULL;
04639
04640
04641 if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
04642 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
04643 astman_send_error(s, m, "Could not make channels compatible for manager bridge");
04644 ast_hangup(tmpchana);
04645 ast_hangup(tmpchanb);
04646 return 1;
04647 }
04648
04649
04650 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
04651 ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
04652 astman_send_error(s, m, "Unable to spawn a new bridge thread");
04653 ast_hangup(tmpchana);
04654 ast_hangup(tmpchanb);
04655 return 1;
04656 }
04657
04658 tobj->chan = tmpchana;
04659 tobj->peer = tmpchanb;
04660 tobj->return_to_pbx = 1;
04661
04662 if (ast_true(playtone)) {
04663 if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language)) {
04664 if (ast_waitstream(tmpchanb, "") < 0)
04665 ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
04666 }
04667 }
04668
04669 bridge_call_thread_launch(tobj);
04670
04671 astman_send_ack(s, m, "Launched bridge thread with success");
04672
04673 return 0;
04674 }
04675
04676
04677
04678
04679
04680
04681
04682
04683
04684
04685
04686
04687 static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
04688 {
04689 struct parkeduser *cur;
04690 int numparked = 0;
04691 struct ao2_iterator iter;
04692 struct ast_parkinglot *curlot;
04693
04694 switch (cmd) {
04695 case CLI_INIT:
04696 e->command = "parkedcalls show";
04697 e->usage =
04698 "Usage: parkedcalls show\n"
04699 " List currently parked calls\n";
04700 return NULL;
04701 case CLI_GENERATE:
04702 return NULL;
04703 }
04704
04705 if (a->argc > e->args)
04706 return CLI_SHOWUSAGE;
04707
04708 ast_cli(a->fd, "%4s %25s (%-15s %-12s %-4s) %-6s \n", "Num", "Channel"
04709 , "Context", "Extension", "Pri", "Timeout");
04710
04711 iter = ao2_iterator_init(parkinglots, 0);
04712 while ((curlot = ao2_iterator_next(&iter))) {
04713 int lotparked = 0;
04714 ast_cli(a->fd, "*** Parking lot: %s\n", curlot->name);
04715
04716 AST_LIST_LOCK(&curlot->parkings);
04717 AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
04718 ast_cli(a->fd, "%-10.10s %25s (%-15s %-12s %-4d) %6lds\n"
04719 ,cur->parkingexten, cur->chan->name, cur->context, cur->exten
04720 ,cur->priority,
04721 (long)(cur->start.tv_sec + (cur->parkingtime/1000) - time(NULL)) );
04722 numparked++;
04723 numparked += lotparked;
04724 }
04725 AST_LIST_UNLOCK(&curlot->parkings);
04726 if (lotparked)
04727 ast_cli(a->fd, " %d parked call%s in parking lot %s\n", lotparked, ESS(lotparked), curlot->name);
04728
04729 ao2_ref(curlot, -1);
04730 }
04731
04732 ast_cli(a->fd, "---\n%d parked call%s in total.\n", numparked, ESS(numparked));
04733
04734 return CLI_SUCCESS;
04735 }
04736
04737 static struct ast_cli_entry cli_features[] = {
04738 AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
04739 AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
04740 AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls"),
04741 };
04742
04743
04744
04745
04746
04747
04748
04749
04750
04751 static int manager_parking_status(struct mansession *s, const struct message *m)
04752 {
04753 struct parkeduser *cur;
04754 const char *id = astman_get_header(m, "ActionID");
04755 char idText[256] = "";
04756 struct ao2_iterator iter;
04757 struct ast_parkinglot *curlot;
04758
04759 if (!ast_strlen_zero(id))
04760 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
04761
04762 astman_send_ack(s, m, "Parked calls will follow");
04763
04764 iter = ao2_iterator_init(parkinglots, 0);
04765 while ((curlot = ao2_iterator_next(&iter))) {
04766
04767 AST_LIST_LOCK(&curlot->parkings);
04768 AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
04769 astman_append(s, "Event: ParkedCall\r\n"
04770 "Exten: %d\r\n"
04771 "Channel: %s\r\n"
04772 "From: %s\r\n"
04773 "Timeout: %ld\r\n"
04774 "CallerIDNum: %s\r\n"
04775 "CallerIDName: %s\r\n"
04776 "%s"
04777 "\r\n",
04778 cur->parkingnum, cur->chan->name, cur->peername,
04779 (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - (long) time(NULL),
04780 S_OR(cur->chan->cid.cid_num, ""),
04781 S_OR(cur->chan->cid.cid_name, ""),
04782 idText);
04783 }
04784 AST_LIST_UNLOCK(&curlot->parkings);
04785 ao2_ref(curlot, -1);
04786 }
04787
04788 astman_append(s,
04789 "Event: ParkedCallsComplete\r\n"
04790 "%s"
04791 "\r\n",idText);
04792
04793
04794 return RESULT_SUCCESS;
04795 }
04796
04797 static char mandescr_park[] =
04798 "Description: Park a channel.\n"
04799 "Variables: (Names marked with * are required)\n"
04800 " *Channel: Channel name to park\n"
04801 " *Channel2: Channel to announce park info to (and return to if timeout)\n"
04802 " Timeout: Number of milliseconds to wait before callback.\n";
04803
04804
04805
04806
04807
04808
04809
04810
04811
04812 static int manager_park(struct mansession *s, const struct message *m)
04813 {
04814 const char *channel = astman_get_header(m, "Channel");
04815 const char *channel2 = astman_get_header(m, "Channel2");
04816 const char *timeout = astman_get_header(m, "Timeout");
04817 char buf[BUFSIZ];
04818 int to = 0;
04819 int res = 0;
04820 int parkExt = 0;
04821 struct ast_channel *ch1, *ch2;
04822
04823 if (ast_strlen_zero(channel)) {
04824 astman_send_error(s, m, "Channel not specified");
04825 return 0;
04826 }
04827
04828 if (ast_strlen_zero(channel2)) {
04829 astman_send_error(s, m, "Channel2 not specified");
04830 return 0;
04831 }
04832
04833 ch1 = ast_get_channel_by_name_locked(channel);
04834 if (!ch1) {
04835 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
04836 astman_send_error(s, m, buf);
04837 return 0;
04838 }
04839
04840 ch2 = ast_get_channel_by_name_locked(channel2);
04841 if (!ch2) {
04842 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
04843 astman_send_error(s, m, buf);
04844 ast_channel_unlock(ch1);
04845 return 0;
04846 }
04847
04848 if (!ast_strlen_zero(timeout)) {
04849 sscanf(timeout, "%30d", &to);
04850 }
04851
04852 res = ast_masq_park_call(ch1, ch2, to, &parkExt);
04853 if (!res) {
04854 ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
04855 astman_send_ack(s, m, "Park successful");
04856 } else {
04857 astman_send_error(s, m, "Park failure");
04858 }
04859
04860 ast_channel_unlock(ch1);
04861 ast_channel_unlock(ch2);
04862
04863 return 0;
04864 }
04865
04866 static int find_channel_by_group(struct ast_channel *c, void *data) {
04867 struct ast_channel *chan = data;
04868
04869 return !c->pbx &&
04870
04871
04872
04873 (c != chan) &&
04874 (chan->pickupgroup & c->callgroup) &&
04875 ((c->_state == AST_STATE_RINGING) || (c->_state == AST_STATE_RING));
04876 }
04877
04878
04879
04880
04881
04882
04883
04884
04885
04886 int ast_pickup_call(struct ast_channel *chan)
04887 {
04888 struct ast_channel *cur = ast_channel_search_locked(find_channel_by_group, chan);
04889
04890 if (cur) {
04891 int res = -1;
04892 ast_debug(1, "Call pickup on chan '%s' by '%s'\n",cur->name, chan->name);
04893 res = ast_answer(chan);
04894 if (res)
04895 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan->name);
04896 res = ast_queue_control(chan, AST_CONTROL_ANSWER);
04897 if (res)
04898 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan->name);
04899 res = ast_channel_masquerade(cur, chan);
04900 if (res)
04901 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan->name, cur->name);
04902 if (!ast_strlen_zero(pickupsound)) {
04903 ast_stream_and_wait(cur, pickupsound, "");
04904 }
04905 ast_channel_unlock(cur);
04906 return res;
04907 } else {
04908 ast_debug(1, "No call pickup possible...\n");
04909 if (!ast_strlen_zero(pickupfailsound)) {
04910 ast_stream_and_wait(chan, pickupfailsound, "");
04911 }
04912 }
04913 return -1;
04914 }
04915
04916 static char *app_bridge = "Bridge";
04917
04918 enum {
04919 BRIDGE_OPT_PLAYTONE = (1 << 0),
04920 };
04921
04922 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
04923 AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE)
04924 END_OPTIONS );
04925
04926
04927
04928
04929
04930
04931
04932
04933
04934
04935 static int bridge_exec(struct ast_channel *chan, void *data)
04936 {
04937 struct ast_channel *current_dest_chan, *final_dest_chan;
04938 char *tmp_data = NULL;
04939 struct ast_flags opts = { 0, };
04940 struct ast_bridge_config bconfig = { { 0, }, };
04941
04942 AST_DECLARE_APP_ARGS(args,
04943 AST_APP_ARG(dest_chan);
04944 AST_APP_ARG(options);
04945 );
04946
04947 if (ast_strlen_zero(data)) {
04948 ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
04949 return -1;
04950 }
04951
04952 tmp_data = ast_strdupa(data);
04953 AST_STANDARD_APP_ARGS(args, tmp_data);
04954 if (!ast_strlen_zero(args.options))
04955 ast_app_parse_options(bridge_exec_options, &opts, NULL, args.options);
04956
04957
04958 if (!strcmp(chan->name, args.dest_chan)) {
04959 ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", chan->name);
04960 manager_event(EVENT_FLAG_CALL, "BridgeExec",
04961 "Response: Failed\r\n"
04962 "Reason: Unable to bridge channel to itself\r\n"
04963 "Channel1: %s\r\n"
04964 "Channel2: %s\r\n",
04965 chan->name, args.dest_chan);
04966 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
04967 return 0;
04968 }
04969
04970
04971 if (!(current_dest_chan = ast_get_channel_by_name_prefix_locked(args.dest_chan,
04972 strlen(args.dest_chan)))) {
04973 ast_log(LOG_WARNING, "Bridge failed because channel %s does not exists or we "
04974 "cannot get its lock\n", args.dest_chan);
04975 manager_event(EVENT_FLAG_CALL, "BridgeExec",
04976 "Response: Failed\r\n"
04977 "Reason: Cannot grab end point\r\n"
04978 "Channel1: %s\r\n"
04979 "Channel2: %s\r\n", chan->name, args.dest_chan);
04980 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
04981 return 0;
04982 }
04983
04984
04985 if (current_dest_chan->_state != AST_STATE_UP)
04986 ast_answer(current_dest_chan);
04987
04988
04989 if (!(final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
04990 NULL, NULL, 0, "Bridge/%s", current_dest_chan->name))) {
04991 ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
04992 manager_event(EVENT_FLAG_CALL, "BridgeExec",
04993 "Response: Failed\r\n"
04994 "Reason: cannot create placeholder\r\n"
04995 "Channel1: %s\r\n"
04996 "Channel2: %s\r\n", chan->name, args.dest_chan);
04997 }
04998 do_bridge_masquerade(current_dest_chan, final_dest_chan);
04999
05000 ast_channel_unlock(current_dest_chan);
05001
05002
05003
05004 if (ast_channel_make_compatible(chan, final_dest_chan) < 0) {
05005 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, final_dest_chan->name);
05006 manager_event(EVENT_FLAG_CALL, "BridgeExec",
05007 "Response: Failed\r\n"
05008 "Reason: Could not make channels compatible for bridge\r\n"
05009 "Channel1: %s\r\n"
05010 "Channel2: %s\r\n", chan->name, final_dest_chan->name);
05011 ast_hangup(final_dest_chan);
05012 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
05013 return 0;
05014 }
05015
05016
05017 manager_event(EVENT_FLAG_CALL, "BridgeExec",
05018 "Response: Success\r\n"
05019 "Channel1: %s\r\n"
05020 "Channel2: %s\r\n", chan->name, final_dest_chan->name);
05021
05022
05023 if (ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE) && !ast_strlen_zero(xfersound)) {
05024 if (!ast_streamfile(final_dest_chan, xfersound, final_dest_chan->language)) {
05025 if (ast_waitstream(final_dest_chan, "") < 0)
05026 ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", final_dest_chan->name);
05027 }
05028 }
05029
05030
05031 ast_bridge_call(chan, final_dest_chan, &bconfig);
05032
05033
05034 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
05035 if (!ast_check_hangup(final_dest_chan)) {
05036 ast_debug(1, "starting new PBX in %s,%s,%d for chan %s\n",
05037 final_dest_chan->context, final_dest_chan->exten,
05038 final_dest_chan->priority, final_dest_chan->name);
05039
05040 if (ast_pbx_start(final_dest_chan) != AST_PBX_SUCCESS) {
05041 ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", final_dest_chan->name);
05042 ast_hangup(final_dest_chan);
05043 } else
05044 ast_debug(1, "SUCCESS continuing PBX on chan %s\n", final_dest_chan->name);
05045 } else {
05046 ast_debug(1, "hangup chan %s since the other endpoint has hung up\n", final_dest_chan->name);
05047 ast_hangup(final_dest_chan);
05048 }
05049
05050 return 0;
05051 }
05052
05053 int ast_features_init(void)
05054 {
05055 int res;
05056
05057 ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
05058
05059 parkinglots = ao2_container_alloc(7, parkinglot_hash_cb, parkinglot_cmp_cb);
05060
05061 if ((res = load_config()))
05062 return res;
05063 ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features));
05064 ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL);
05065 res = ast_register_application2(parkedcall, park_exec, NULL, NULL, NULL);
05066 if (!res)
05067 res = ast_register_application2(parkcall, park_call_exec, NULL, NULL, NULL);
05068 if (!res) {
05069 ast_manager_register("ParkedCalls", 0, manager_parking_status, "List parked calls");
05070 ast_manager_register2("Park", EVENT_FLAG_CALL, manager_park, "Park a channel", mandescr_park);
05071 ast_manager_register2("Bridge", EVENT_FLAG_CALL, action_bridge, "Bridge two channels already in the PBX", mandescr_bridge);
05072 }
05073
05074 res |= ast_devstate_prov_add("Park", metermaidstate);
05075
05076 return res;
05077 }