00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 378781 $")
00033
00034 #include "asterisk/_private.h"
00035
00036 #include <pthread.h>
00037 #include <signal.h>
00038 #include <sys/time.h>
00039 #include <sys/signal.h>
00040 #include <netinet/in.h>
00041
00042 #include "asterisk/lock.h"
00043 #include "asterisk/file.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/pbx.h"
00046 #include "asterisk/causes.h"
00047 #include "asterisk/module.h"
00048 #include "asterisk/translate.h"
00049 #include "asterisk/app.h"
00050 #include "asterisk/say.h"
00051 #include "asterisk/features.h"
00052 #include "asterisk/musiconhold.h"
00053 #include "asterisk/config.h"
00054 #include "asterisk/cli.h"
00055 #include "asterisk/manager.h"
00056 #include "asterisk/utils.h"
00057 #include "asterisk/adsi.h"
00058 #include "asterisk/devicestate.h"
00059 #include "asterisk/monitor.h"
00060 #include "asterisk/audiohook.h"
00061 #include "asterisk/global_datastores.h"
00062 #include "asterisk/astobj2.h"
00063 #include "asterisk/cel.h"
00064 #include "asterisk/test.h"
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
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 #define DEFAULT_PARK_TIME 45000
00384 #define DEFAULT_PARK_EXTENSION "700"
00385 #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000
00386 #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000
00387 #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000
00388 #define DEFAULT_ATXFER_DROP_CALL 0
00389 #define DEFAULT_ATXFER_LOOP_DELAY 10000
00390 #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
00391
00392 #define AST_MAX_WATCHERS 256
00393 #define MAX_DIAL_FEATURE_OPTIONS 30
00394
00395 struct feature_group_exten {
00396 AST_LIST_ENTRY(feature_group_exten) entry;
00397 AST_DECLARE_STRING_FIELDS(
00398 AST_STRING_FIELD(exten);
00399 );
00400 struct ast_call_feature *feature;
00401 };
00402
00403 struct feature_group {
00404 AST_LIST_ENTRY(feature_group) entry;
00405 AST_DECLARE_STRING_FIELDS(
00406 AST_STRING_FIELD(gname);
00407 );
00408 AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
00409 };
00410
00411 static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
00412
00413 typedef enum {
00414 FEATURE_INTERPRET_DETECT,
00415 FEATURE_INTERPRET_DO,
00416 FEATURE_INTERPRET_CHECK,
00417 } feature_interpret_op;
00418
00419 static const char *parkedcall = "ParkedCall";
00420
00421 static char pickup_ext[AST_MAX_EXTENSION];
00422
00423
00424 struct parking_dp_ramp {
00425
00426 AST_LIST_ENTRY(parking_dp_ramp) node;
00427
00428 unsigned int exclusive:1;
00429
00430 char exten[1];
00431 };
00432
00433
00434 AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
00435
00436
00437 struct parking_dp_spaces {
00438
00439 AST_LIST_ENTRY(parking_dp_spaces) node;
00440
00441 int start;
00442
00443 int stop;
00444 };
00445
00446
00447 AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
00448
00449
00450 struct parking_dp_context {
00451
00452 AST_LIST_ENTRY(parking_dp_context) node;
00453
00454 struct parking_dp_ramp_map access_extens;
00455
00456 struct parking_dp_space_map spaces;
00457
00458 struct parking_dp_space_map hints;
00459
00460 char context[1];
00461 };
00462
00463
00464 AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
00465
00466
00467
00468
00469
00470 struct parkeduser {
00471 struct ast_channel *chan;
00472 struct timeval start;
00473 int parkingnum;
00474 char parkingexten[AST_MAX_EXTENSION];
00475 char context[AST_MAX_CONTEXT];
00476 char exten[AST_MAX_EXTENSION];
00477 int priority;
00478 int parkingtime;
00479
00480 enum ast_control_frame_type hold_method;
00481 unsigned int notquiteyet:1;
00482 unsigned int options_specified:1;
00483 char peername[AST_CHANNEL_NAME];
00484 unsigned char moh_trys;
00485
00486 struct ast_parkinglot *parkinglot;
00487 AST_LIST_ENTRY(parkeduser) list;
00488 };
00489
00490
00491 struct parkinglot_cfg {
00492
00493 char mohclass[MAX_MUSICCLASS];
00494
00495 char parkext[AST_MAX_EXTENSION];
00496
00497 char parking_con[AST_MAX_EXTENSION];
00498
00499 int parking_start;
00500
00501 int parking_stop;
00502
00503 int parkingtime;
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 int parkedcalltransfers;
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 int parkedcallreparking;
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 int parkedcallhangup;
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 int parkedcallrecording;
00544
00545
00546 unsigned int parkfindnext:1;
00547
00548 unsigned int parkext_exclusive:1;
00549
00550 unsigned int parkaddhints:1;
00551
00552 unsigned int is_invalid:1;
00553 };
00554
00555
00556 struct ast_parkinglot {
00557
00558 char name[AST_MAX_CONTEXT];
00559
00560 struct parkinglot_cfg cfg;
00561
00562
00563 int next_parking_space;
00564
00565
00566 unsigned int the_mark:1;
00567
00568 unsigned int disabled:1;
00569
00570
00571 AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
00572 };
00573
00574
00575 static struct ao2_container *parkinglots;
00576
00577
00578
00579
00580
00581
00582 static struct ast_parkinglot *default_parkinglot;
00583
00584
00585 static int force_reload_load;
00586
00587 static int parkedplay = 0;
00588 static int parkeddynamic = 0;
00589 static char courtesytone[256];
00590 static char xfersound[256];
00591 static char xferfailsound[256];
00592 static char pickupsound[256];
00593 static char pickupfailsound[256];
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603 static char parking_con_dial[] = "park-dial";
00604
00605
00606 AST_MUTEX_DEFINE_STATIC(features_reload_lock);
00607
00608 static int adsipark;
00609
00610 static int transferdigittimeout;
00611 static int featuredigittimeout;
00612 static int comebacktoorigin = 1;
00613
00614 static int atxfernoanswertimeout;
00615 static unsigned int atxferdropcall;
00616 static unsigned int atxferloopdelay;
00617 static unsigned int atxfercallbackretries;
00618
00619 static char *registrar = "features";
00620
00621
00622 AST_DEFINE_APP_ARGS_TYPE(park_app_args,
00623 AST_APP_ARG(timeout);
00624 AST_APP_ARG(return_con);
00625 AST_APP_ARG(return_ext);
00626 AST_APP_ARG(return_pri);
00627 AST_APP_ARG(options);
00628 AST_APP_ARG(pl_name);
00629 AST_APP_ARG(dummy);
00630 );
00631
00632
00633 static const char *parkcall = "Park";
00634
00635 static struct ast_app *monitor_app = NULL;
00636 static int monitor_ok = 1;
00637
00638 static struct ast_app *mixmonitor_app = NULL;
00639 static int mixmonitor_ok = 1;
00640
00641 static struct ast_app *stopmixmonitor_app = NULL;
00642 static int stopmixmonitor_ok = 1;
00643
00644 static pthread_t parking_thread;
00645 struct ast_dial_features {
00646
00647 struct ast_flags my_features;
00648
00649 struct ast_flags peer_features;
00650 };
00651
00652 #if defined(ATXFER_NULL_TECH)
00653
00654
00655
00656
00657
00658
00659
00660
00661 static void set_kill_chan_tech(struct ast_channel *chan)
00662 {
00663 int idx;
00664
00665 ast_channel_lock(chan);
00666
00667
00668 if (chan->tech->hangup) {
00669 chan->tech->hangup(chan);
00670 }
00671 if (chan->tech_pvt) {
00672 ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
00673 chan->name);
00674 ast_free(chan->tech_pvt);
00675 chan->tech_pvt = NULL;
00676 }
00677
00678
00679 chan->tech = &ast_kill_tech;
00680 for (idx = 0; idx < AST_MAX_FDS; ++idx) {
00681 switch (idx) {
00682 case AST_ALERT_FD:
00683 case AST_TIMING_FD:
00684 case AST_GENERATOR_FD:
00685
00686 break;
00687 default:
00688 ast_channel_set_fd(chan, idx, -1);
00689 break;
00690 }
00691 }
00692 ast_queue_frame(chan, &ast_null_frame);
00693
00694 ast_channel_unlock(chan);
00695 }
00696 #endif
00697
00698 #if defined(ATXFER_NULL_TECH)
00699
00700
00701
00702
00703
00704
00705
00706
00707 static void set_new_chan_name(struct ast_channel *chan)
00708 {
00709 static int seq_num_last;
00710 int seq_num;
00711 int len;
00712 char *chan_name;
00713 char dummy[1];
00714
00715
00716 ast_channel_lock(chan);
00717 seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
00718 len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", chan->name, seq_num) + 1;
00719 chan_name = ast_alloca(len);
00720 snprintf(chan_name, len, "%s<XFER_%x>", chan->name, seq_num);
00721 ast_channel_unlock(chan);
00722
00723 ast_change_name(chan, chan_name);
00724 }
00725 #endif
00726
00727 static void *dial_features_duplicate(void *data)
00728 {
00729 struct ast_dial_features *df = data, *df_copy;
00730
00731 if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
00732 return NULL;
00733 }
00734
00735 memcpy(df_copy, df, sizeof(*df));
00736
00737 return df_copy;
00738 }
00739
00740 static void dial_features_destroy(void *data)
00741 {
00742 struct ast_dial_features *df = data;
00743 if (df) {
00744 ast_free(df);
00745 }
00746 }
00747
00748 static const struct ast_datastore_info dial_features_info = {
00749 .type = "dial-features",
00750 .destroy = dial_features_destroy,
00751 .duplicate = dial_features_duplicate,
00752 };
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764 static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
00765 {
00766 struct ast_datastore *datastore;
00767 struct ast_dial_features *dialfeatures;
00768
00769 ast_channel_lock(chan);
00770 datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
00771 ast_channel_unlock(chan);
00772 if (datastore) {
00773
00774 return 1;
00775 }
00776
00777
00778 datastore = ast_datastore_alloc(&dial_features_info, NULL);
00779 if (!datastore) {
00780 ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
00781 return 0;
00782 }
00783 dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
00784 if (!dialfeatures) {
00785 ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
00786 ast_datastore_free(datastore);
00787 return 0;
00788 }
00789 ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
00790 ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
00791 datastore->inheritance = DATASTORE_INHERIT_FOREVER;
00792 datastore->data = dialfeatures;
00793 ast_channel_lock(chan);
00794 ast_channel_datastore_add(chan, datastore);
00795 ast_channel_unlock(chan);
00796 return 0;
00797 }
00798
00799
00800 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
00801 static void parkinglot_unref(struct ast_parkinglot *parkinglot);
00802 static struct ast_parkinglot *find_parkinglot(const char *name);
00803 static struct ast_parkinglot *create_parkinglot(const char *name);
00804 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
00805 static int parkinglot_activate(struct ast_parkinglot *parkinglot);
00806 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
00820 {
00821 struct ast_exten *exten;
00822 struct pbx_find_info q = { .stacklen = 0 };
00823 const char *app_at_exten;
00824
00825 ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
00826 exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
00827 E_MATCH);
00828 if (!exten) {
00829 return NULL;
00830 }
00831
00832 app_at_exten = ast_get_extension_app(exten);
00833 if (!app_at_exten || strcasecmp(parkcall, app_at_exten)) {
00834 return NULL;
00835 }
00836
00837 return exten;
00838 }
00839
00840 int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
00841 {
00842 return get_parking_exten(exten_str, chan, context) ? 1 : 0;
00843 }
00844
00845 const char *ast_pickup_ext(void)
00846 {
00847 return pickup_ext;
00848 }
00849
00850 struct ast_bridge_thread_obj
00851 {
00852 struct ast_bridge_config bconfig;
00853 struct ast_channel *chan;
00854 struct ast_channel *peer;
00855 unsigned int return_to_pbx:1;
00856 };
00857
00858 static int parkinglot_hash_cb(const void *obj, const int flags)
00859 {
00860 const struct ast_parkinglot *parkinglot = obj;
00861
00862 return ast_str_case_hash(parkinglot->name);
00863 }
00864
00865 static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
00866 {
00867 struct ast_parkinglot *parkinglot = obj;
00868 struct ast_parkinglot *parkinglot2 = arg;
00869
00870 return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
00871 }
00872
00873
00874
00875
00876
00877 static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
00878 {
00879 ast_copy_string(chan->context, context, sizeof(chan->context));
00880 ast_copy_string(chan->exten, ext, sizeof(chan->exten));
00881 chan->priority = pri;
00882 }
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892 static void check_goto_on_transfer(struct ast_channel *chan)
00893 {
00894 struct ast_channel *xferchan;
00895 const char *val;
00896 char *goto_on_transfer;
00897 char *x;
00898
00899 ast_channel_lock(chan);
00900 val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
00901 if (ast_strlen_zero(val)) {
00902 ast_channel_unlock(chan);
00903 return;
00904 }
00905 goto_on_transfer = ast_strdupa(val);
00906 ast_channel_unlock(chan);
00907
00908 ast_debug(1, "Attempting GOTO_ON_BLINDXFR=%s for %s.\n", val, chan->name);
00909
00910 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", chan->linkedid, 0,
00911 "%s", chan->name);
00912 if (!xferchan) {
00913 return;
00914 }
00915
00916
00917 xferchan->readformat = chan->readformat;
00918 xferchan->writeformat = chan->writeformat;
00919
00920 if (ast_channel_masquerade(xferchan, chan)) {
00921
00922 ast_hangup(xferchan);
00923 return;
00924 }
00925
00926 for (x = goto_on_transfer; *x; ++x) {
00927 if (*x == '^') {
00928 *x = ',';
00929 }
00930 }
00931 ast_parseable_goto(xferchan, goto_on_transfer);
00932 xferchan->_state = AST_STATE_UP;
00933 ast_clear_flag(xferchan, AST_FLAGS_ALL);
00934 ast_channel_clear_softhangup(xferchan, AST_SOFTHANGUP_ALL);
00935
00936 ast_do_masquerade(xferchan);
00937 if (ast_pbx_start(xferchan)) {
00938
00939 ast_hangup(xferchan);
00940 }
00941 }
00942
00943 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
00944 const char *caller_name, struct ast_channel *requestor,
00945 struct ast_channel *transferee, const char *type, format_t format, void *data,
00946 int timeout, int *outstate, const char *language);
00947
00948 static const struct ast_datastore_info channel_app_data_datastore = {
00949 .type = "Channel appdata datastore",
00950 .destroy = ast_free_ptr,
00951 };
00952
00953 static int set_chan_app_data(struct ast_channel *chan, const char *src_app_data)
00954 {
00955 struct ast_datastore *datastore;
00956 char *dst_app_data;
00957
00958 datastore = ast_datastore_alloc(&channel_app_data_datastore, NULL);
00959 if (!datastore) {
00960 return -1;
00961 }
00962
00963 dst_app_data = ast_malloc(strlen(src_app_data) + 1);
00964 if (!dst_app_data) {
00965 ast_datastore_free(datastore);
00966 return -1;
00967 }
00968
00969 chan->data = strcpy(dst_app_data, src_app_data);
00970 datastore->data = dst_app_data;
00971 ast_channel_datastore_add(chan, datastore);
00972 return 0;
00973 }
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983 static void *bridge_call_thread(void *data)
00984 {
00985 struct ast_bridge_thread_obj *tobj = data;
00986
00987 tobj->chan->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00988 if (set_chan_app_data(tobj->chan, tobj->peer->name)) {
00989 tobj->chan->data = "(Empty)";
00990 }
00991 tobj->peer->appl = !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge";
00992 if (set_chan_app_data(tobj->peer, tobj->chan->name)) {
00993 tobj->peer->data = "(Empty)";
00994 }
00995
00996 ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
00997
00998 if (tobj->return_to_pbx) {
00999 if (!ast_check_hangup(tobj->peer)) {
01000 ast_log(LOG_VERBOSE, "putting peer %s into PBX again\n", tobj->peer->name);
01001 if (ast_pbx_start(tobj->peer)) {
01002 ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", tobj->peer->name);
01003 ast_hangup(tobj->peer);
01004 }
01005 } else {
01006 ast_hangup(tobj->peer);
01007 }
01008 if (!ast_check_hangup(tobj->chan)) {
01009 ast_log(LOG_VERBOSE, "putting chan %s into PBX again\n", tobj->chan->name);
01010 if (ast_pbx_start(tobj->chan)) {
01011 ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", tobj->chan->name);
01012 ast_hangup(tobj->chan);
01013 }
01014 } else {
01015 ast_hangup(tobj->chan);
01016 }
01017 } else {
01018 ast_hangup(tobj->chan);
01019 ast_hangup(tobj->peer);
01020 }
01021
01022 ast_free(tobj);
01023
01024 return NULL;
01025 }
01026
01027
01028
01029
01030
01031
01032
01033 static void bridge_call_thread_launch(struct ast_bridge_thread_obj *data)
01034 {
01035 pthread_t thread;
01036 pthread_attr_t attr;
01037 struct sched_param sched;
01038
01039 pthread_attr_init(&attr);
01040 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
01041 if (ast_pthread_create(&thread, &attr, bridge_call_thread, data)) {
01042 ast_hangup(data->chan);
01043 ast_hangup(data->peer);
01044 ast_log(LOG_ERROR, "Failed to create bridge_call_thread.\n");
01045 }
01046 pthread_attr_destroy(&attr);
01047 memset(&sched, 0, sizeof(sched));
01048 pthread_setschedparam(thread, SCHED_RR, &sched);
01049 }
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059 static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
01060 {
01061 int res;
01062 int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
01063 char tmp[256];
01064 char *message[5] = {NULL, NULL, NULL, NULL, NULL};
01065
01066 snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
01067 message[0] = tmp;
01068 res = ast_adsi_load_session(chan, NULL, 0, 1);
01069 if (res == -1)
01070 return res;
01071 return ast_adsi_print(chan, message, justify, 1);
01072 }
01073
01074
01075
01076
01077
01078 static const char *findparkinglotname(struct ast_channel *chan)
01079 {
01080 const char *name;
01081
01082
01083 name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
01084 if (!name && !ast_strlen_zero(chan->parkinglot)) {
01085
01086 name = chan->parkinglot;
01087 }
01088 return name;
01089 }
01090
01091
01092 static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
01093 {
01094 ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
01095 exten, context, ast_devstate2str(state));
01096
01097 ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%s@%s", exten, context);
01098 }
01099
01100
01101 static enum ast_device_state metermaidstate(const char *data)
01102 {
01103 char *context;
01104 char *exten;
01105
01106 context = ast_strdupa(data);
01107
01108 exten = strsep(&context, "@");
01109 if (!context)
01110 return AST_DEVICE_INVALID;
01111
01112 ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
01113
01114 if (!ast_exists_extension(NULL, context, exten, 1, NULL))
01115 return AST_DEVICE_NOT_INUSE;
01116
01117 return AST_DEVICE_INUSE;
01118 }
01119
01120
01121 enum ast_park_call_options {
01122
01123 AST_PARK_OPT_RINGING = (1 << 0),
01124
01125
01126 AST_PARK_OPT_RANDOMIZE = (1 << 1),
01127
01128 AST_PARK_OPT_SILENCE = (1 << 2),
01129 };
01130
01131
01132 struct ast_park_call_args {
01133
01134
01135
01136 int timeout;
01137
01138
01139 int *extout;
01140 const char *orig_chan_name;
01141 const char *return_con;
01142 const char *return_ext;
01143 int return_pri;
01144 uint32_t flags;
01145
01146 struct parkeduser *pu;
01147
01148 struct ast_parkinglot *parkinglot;
01149 };
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161 static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
01162 {
01163 const char *dyn_context;
01164 const char *dyn_exten;
01165 const char *dyn_range;
01166 const char *template_name;
01167 struct ast_parkinglot *template_parkinglot = NULL;
01168 struct ast_parkinglot *parkinglot;
01169 int dyn_start;
01170 int dyn_end;
01171
01172 ast_channel_lock(chan);
01173 template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
01174 dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
01175 dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
01176 dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
01177 ast_channel_unlock(chan);
01178
01179 if (!ast_strlen_zero(template_name)) {
01180 template_parkinglot = find_parkinglot(template_name);
01181 if (!template_parkinglot) {
01182 ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
01183 template_name);
01184 } else if (template_parkinglot->cfg.is_invalid) {
01185 ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
01186 template_name);
01187 parkinglot_unref(template_parkinglot);
01188 template_parkinglot = NULL;
01189 }
01190 }
01191 if (!template_parkinglot) {
01192 template_parkinglot = parkinglot_addref(default_parkinglot);
01193 ast_debug(1, "Using default parking lot for template\n");
01194 }
01195
01196 parkinglot = copy_parkinglot(name, template_parkinglot);
01197 if (!parkinglot) {
01198 ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
01199 } else {
01200
01201 if (!ast_strlen_zero(dyn_context)) {
01202 ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
01203 sizeof(parkinglot->cfg.parking_con));
01204 }
01205 if (!ast_strlen_zero(dyn_exten)) {
01206 ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
01207 sizeof(parkinglot->cfg.parkext));
01208 }
01209 if (!ast_strlen_zero(dyn_range)) {
01210 if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
01211 ast_log(LOG_WARNING,
01212 "Format for parking positions is a-b, where a and b are numbers\n");
01213 } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
01214 ast_log(LOG_WARNING,
01215 "Format for parking positions is a-b, where a <= b\n");
01216 } else {
01217 parkinglot->cfg.parking_start = dyn_start;
01218 parkinglot->cfg.parking_stop = dyn_end;
01219 }
01220 }
01221
01222
01223
01224
01225
01226
01227
01228
01229 if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
01230 if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
01231 && parkinglot->cfg.parkext_exclusive) {
01232 ast_log(LOG_WARNING,
01233 "Parking lot '%s' conflicts with template parking lot '%s'!\n"
01234 "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
01235 parkinglot->name, template_parkinglot->name);
01236 }
01237 if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
01238 && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
01239 || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
01240 && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
01241 || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
01242 && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
01243 ast_log(LOG_WARNING,
01244 "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
01245 "Change PARKINGDYNPOS.\n",
01246 parkinglot->name, template_parkinglot->name);
01247 }
01248 }
01249
01250 parkinglot_activate(parkinglot);
01251 ao2_link(parkinglots, parkinglot);
01252 }
01253 parkinglot_unref(template_parkinglot);
01254
01255 return parkinglot;
01256 }
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268 static void park_space_abort(struct parkeduser *pu)
01269 {
01270 struct ast_parkinglot *parkinglot;
01271
01272 parkinglot = pu->parkinglot;
01273
01274
01275 --parkinglot->next_parking_space;
01276
01277 AST_LIST_REMOVE(&parkinglot->parkings, pu, list);
01278
01279 AST_LIST_UNLOCK(&parkinglot->parkings);
01280 parkinglot_unref(parkinglot);
01281 ast_free(pu);
01282 }
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295 static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
01296 {
01297 struct parkeduser *pu;
01298 int i;
01299 int parking_space = -1;
01300 const char *parkinglotname;
01301 const char *parkingexten;
01302 struct parkeduser *cur;
01303 struct ast_parkinglot *parkinglot = NULL;
01304
01305 if (args->parkinglot) {
01306 parkinglot = parkinglot_addref(args->parkinglot);
01307 parkinglotname = parkinglot->name;
01308 } else {
01309 if (parker) {
01310 parkinglotname = findparkinglotname(parker);
01311 } else {
01312 parkinglotname = findparkinglotname(park_me);
01313 }
01314 if (!ast_strlen_zero(parkinglotname)) {
01315 parkinglot = find_parkinglot(parkinglotname);
01316 } else {
01317
01318 ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
01319 parkinglot = parkinglot_addref(default_parkinglot);
01320 }
01321 }
01322
01323
01324 if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
01325 parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
01326 }
01327
01328 if (!parkinglot) {
01329 ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", park_me->name);
01330 return NULL;
01331 }
01332
01333 ast_debug(1, "Parking lot: %s\n", parkinglot->name);
01334 if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
01335 ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
01336 parkinglot->name);
01337 parkinglot_unref(parkinglot);
01338 return NULL;
01339 }
01340
01341
01342 if (!(pu = ast_calloc(1, sizeof(*pu)))) {
01343 parkinglot_unref(parkinglot);
01344 return NULL;
01345 }
01346
01347
01348 AST_LIST_LOCK(&parkinglot->parkings);
01349
01350
01351 parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
01352 if (!ast_strlen_zero(parkingexten)) {
01353
01354
01355
01356
01357
01358
01359
01360 if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
01361 ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
01362 parkingexten);
01363 AST_LIST_UNLOCK(&parkinglot->parkings);
01364 parkinglot_unref(parkinglot);
01365 ast_free(pu);
01366 return NULL;
01367 }
01368
01369 if (parking_space < parkinglot->cfg.parking_start
01370 || parkinglot->cfg.parking_stop < parking_space) {
01371
01372
01373
01374
01375
01376 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
01377 parking_space, parkinglot->name, parkinglot->cfg.parking_start,
01378 parkinglot->cfg.parking_stop);
01379 AST_LIST_UNLOCK(&parkinglot->parkings);
01380 parkinglot_unref(parkinglot);
01381 ast_free(pu);
01382 return NULL;
01383 }
01384
01385
01386 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
01387 if (cur->parkingnum == parking_space) {
01388 ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
01389 parking_space, parkinglot->name);
01390 AST_LIST_UNLOCK(&parkinglot->parkings);
01391 parkinglot_unref(parkinglot);
01392 ast_free(pu);
01393 return NULL;
01394 }
01395 }
01396 } else {
01397
01398 int start;
01399 int start_checked = 0;
01400
01401
01402 if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
01403 start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
01404 start += parkinglot->cfg.parking_start;
01405 } else if (parkinglot->cfg.parkfindnext
01406 && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
01407 && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
01408
01409 start = parkinglot->next_parking_space;
01410 } else {
01411
01412 start = parkinglot->cfg.parking_start;
01413 }
01414
01415
01416 for (i = start; ; i++) {
01417
01418 if (i == parkinglot->cfg.parking_stop + 1) {
01419 i = parkinglot->cfg.parking_start;
01420 }
01421
01422 if (i == start) {
01423
01424 if (start_checked) {
01425 break;
01426 } else {
01427 start_checked = 1;
01428 }
01429 }
01430
01431
01432 AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
01433 if (cur->parkingnum == i) {
01434 break;
01435 }
01436 }
01437 if (!cur) {
01438
01439 parking_space = i;
01440 break;
01441 }
01442 }
01443 if (parking_space == -1) {
01444
01445 ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
01446 AST_LIST_UNLOCK(&parkinglot->parkings);
01447 parkinglot_unref(parkinglot);
01448 ast_free(pu);
01449 return NULL;
01450 }
01451 }
01452
01453
01454 parkinglot->next_parking_space = parking_space + 1;
01455
01456 snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
01457 pu->notquiteyet = 1;
01458 pu->parkingnum = parking_space;
01459 pu->parkinglot = parkinglot;
01460 AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
01461
01462 return pu;
01463 }
01464
01465
01466 static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
01467 {
01468 struct parkeduser *pu = args->pu;
01469 const char *event_from;
01470 char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
01471
01472 if (pu == NULL) {
01473 args->pu = pu = park_space_reserve(chan, peer, args);
01474 if (pu == NULL) {
01475 return -1;
01476 }
01477 }
01478
01479 chan->appl = "Parked Call";
01480 chan->data = NULL;
01481
01482 pu->chan = chan;
01483
01484
01485 if (chan != peer) {
01486 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
01487 pu->hold_method = AST_CONTROL_RINGING;
01488 ast_indicate(chan, AST_CONTROL_RINGING);
01489 } else {
01490 pu->hold_method = AST_CONTROL_HOLD;
01491 ast_indicate_data(chan, AST_CONTROL_HOLD,
01492 S_OR(pu->parkinglot->cfg.mohclass, NULL),
01493 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
01494 }
01495 }
01496
01497 pu->start = ast_tvnow();
01498 pu->parkingtime = (args->timeout > 0) ? args->timeout : pu->parkinglot->cfg.parkingtime;
01499 if (args->extout)
01500 *(args->extout) = pu->parkingnum;
01501
01502 if (peer) {
01503 event_from = S_OR(args->orig_chan_name, peer->name);
01504
01505
01506
01507
01508
01509
01510
01511
01512
01513
01514 if (!strcasecmp(peer->tech->type, "Local")) {
01515 struct ast_channel *tmpchan, *base_peer;
01516 char other_side[AST_CHANNEL_NAME];
01517 char *c;
01518
01519 ast_copy_string(other_side, event_from, sizeof(other_side));
01520 if ((c = strrchr(other_side, ';'))) {
01521 *++c = '1';
01522 }
01523 if ((tmpchan = ast_channel_get_by_name(other_side))) {
01524 ast_channel_lock(tmpchan);
01525 if ((base_peer = ast_bridged_channel(tmpchan))) {
01526 ast_copy_string(pu->peername, base_peer->name, sizeof(pu->peername));
01527 }
01528 ast_channel_unlock(tmpchan);
01529 tmpchan = ast_channel_unref(tmpchan);
01530 }
01531 } else {
01532 ast_copy_string(pu->peername, event_from, sizeof(pu->peername));
01533 }
01534 } else {
01535 event_from = S_OR(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"), chan->name);
01536 }
01537
01538
01539
01540
01541
01542 pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
01543
01544
01545
01546
01547
01548
01549
01550 ast_copy_string(pu->context,
01551 S_OR(args->return_con, S_OR(chan->macrocontext, chan->context)),
01552 sizeof(pu->context));
01553 ast_copy_string(pu->exten,
01554 S_OR(args->return_ext, S_OR(chan->macroexten, chan->exten)),
01555 sizeof(pu->exten));
01556 pu->priority = args->return_pri ? args->return_pri :
01557 (chan->macropriority ? chan->macropriority : chan->priority);
01558
01559
01560
01561
01562
01563
01564 if (peer != chan) {
01565 pu->notquiteyet = 0;
01566 }
01567
01568
01569 pthread_kill(parking_thread, SIGURG);
01570 ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %d seconds\n",
01571 chan->name, pu->parkingnum, pu->parkinglot->name,
01572 pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
01573
01574 ast_cel_report_event(chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
01575
01576 ast_manager_event(chan, EVENT_FLAG_CALL, "ParkedCall",
01577 "Exten: %s\r\n"
01578 "Channel: %s\r\n"
01579 "Parkinglot: %s\r\n"
01580 "From: %s\r\n"
01581 "Timeout: %ld\r\n"
01582 "CallerIDNum: %s\r\n"
01583 "CallerIDName: %s\r\n"
01584 "ConnectedLineNum: %s\r\n"
01585 "ConnectedLineName: %s\r\n"
01586 "Uniqueid: %s\r\n",
01587 pu->parkingexten, chan->name, pu->parkinglot->name, event_from,
01588 (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
01589 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, "<unknown>"),
01590 S_COR(chan->caller.id.name.valid, chan->caller.id.name.str, "<unknown>"),
01591 S_COR(chan->connected.id.number.valid, chan->connected.id.number.str, "<unknown>"),
01592 S_COR(chan->connected.id.name.valid, chan->connected.id.name.str, "<unknown>"),
01593 chan->uniqueid
01594 );
01595 ast_debug(4, "peer->name: %s\n", peer ? peer->name : "-No peer-");
01596 ast_debug(4, "args->orig_chan_name: %s\n", args->orig_chan_name ? args->orig_chan_name : "-none-");
01597 ast_debug(4, "pu->peername: %s\n", pu->peername);
01598 ast_debug(4, "AMI ParkedCall Channel: %s\n", chan->name);
01599 ast_debug(4, "AMI ParkedCall From: %s\n", event_from);
01600
01601 if (peer && adsipark && ast_adsi_available(peer)) {
01602 adsi_announce_park(peer, pu->parkingexten);
01603 ast_adsi_unload_session(peer);
01604 }
01605
01606 snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
01607 pu->parkinglot->name);
01608 if (ast_add_extension(pu->parkinglot->cfg.parking_con, 1, pu->parkingexten, 1,
01609 NULL, NULL, parkedcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
01610 ast_log(LOG_ERROR, "Could not create parked call exten: %s@%s\n",
01611 pu->parkingexten, pu->parkinglot->cfg.parking_con);
01612 } else {
01613 notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con, AST_DEVICE_INUSE);
01614 }
01615
01616 AST_LIST_UNLOCK(&pu->parkinglot->parkings);
01617
01618
01619 if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
01620 && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(peer->name, args->orig_chan_name))) {
01621
01622
01623
01624
01625
01626 ast_set_flag(peer, AST_FLAG_MASQ_NOSTREAM);
01627
01628 ast_say_digits(peer, pu->parkingnum, "", peer->language);
01629 ast_clear_flag(peer, AST_FLAG_MASQ_NOSTREAM);
01630 }
01631 if (peer == chan) {
01632
01633 if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
01634 pu->hold_method = AST_CONTROL_RINGING;
01635 ast_indicate(chan, AST_CONTROL_RINGING);
01636 } else {
01637 pu->hold_method = AST_CONTROL_HOLD;
01638 ast_indicate_data(chan, AST_CONTROL_HOLD,
01639 S_OR(pu->parkinglot->cfg.mohclass, NULL),
01640 !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
01641 }
01642 pu->notquiteyet = 0;
01643 pthread_kill(parking_thread, SIGURG);
01644 }
01645 return 0;
01646 }
01647
01648 int ast_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
01649 {
01650 int res;
01651 char *parse;
01652 const char *app_data;
01653 struct ast_exten *exten;
01654 struct park_app_args app_args;
01655 struct ast_park_call_args args = {
01656 .timeout = timeout,
01657 .extout = extout,
01658 };
01659
01660 if (!park_exten || !park_context) {
01661 return park_call_full(park_me, parker, &args);
01662 }
01663
01664
01665
01666
01667
01668 if (parker && parker != park_me) {
01669 ast_autoservice_start(park_me);
01670 }
01671 exten = get_parking_exten(park_exten, parker, park_context);
01672 if (exten) {
01673 app_data = ast_get_extension_app_data(exten);
01674 if (!app_data) {
01675 app_data = "";
01676 }
01677 parse = ast_strdupa(app_data);
01678 AST_STANDARD_APP_ARGS(app_args, parse);
01679
01680 if (!ast_strlen_zero(app_args.pl_name)) {
01681
01682 args.parkinglot = find_parkinglot(app_args.pl_name);
01683 if (!args.parkinglot && parkeddynamic) {
01684 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
01685 }
01686 }
01687 }
01688 if (parker && parker != park_me) {
01689 ast_autoservice_stop(park_me);
01690 }
01691
01692 res = park_call_full(park_me, parker, &args);
01693 if (args.parkinglot) {
01694 parkinglot_unref(args.parkinglot);
01695 }
01696 return res;
01697 }
01698
01699 int ast_park_call(struct ast_channel *park_me, struct ast_channel *parker, int timeout, const char *park_exten, int *extout)
01700 {
01701 struct ast_park_call_args args = {
01702 .timeout = timeout,
01703 .extout = extout,
01704 };
01705
01706 return park_call_full(park_me, parker, &args);
01707 }
01708
01709
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719 static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
01720 {
01721 struct ast_channel *chan;
01722
01723
01724 chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, rchan->accountcode, rchan->exten,
01725 rchan->context, rchan->linkedid, rchan->amaflags, "Parked/%s", rchan->name);
01726 if (!chan) {
01727 ast_log(LOG_WARNING, "Unable to create parked channel\n");
01728 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01729 if (peer == rchan) {
01730
01731 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01732 } else if (peer) {
01733
01734 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01735 }
01736 }
01737 return -1;
01738 }
01739
01740 args->pu = park_space_reserve(rchan, peer, args);
01741 if (!args->pu) {
01742 ast_hangup(chan);
01743 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01744 if (peer == rchan) {
01745
01746 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01747 } else if (peer) {
01748
01749 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01750 }
01751 }
01752 return -1;
01753 }
01754
01755
01756 chan->readformat = rchan->readformat;
01757 chan->writeformat = rchan->writeformat;
01758
01759 if (ast_channel_masquerade(chan, rchan)) {
01760 park_space_abort(args->pu);
01761 args->pu = NULL;
01762 ast_hangup(chan);
01763 if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
01764 if (peer == rchan) {
01765
01766 ast_stream_and_wait(peer, "pbx-parkingfailed", "");
01767 } else if (peer) {
01768
01769 play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
01770 }
01771 }
01772 return -1;
01773 }
01774
01775
01776 set_c_e_p(chan, rchan->context, rchan->exten, rchan->priority);
01777
01778
01779 ast_copy_string(chan->macrocontext,rchan->macrocontext,sizeof(chan->macrocontext));
01780 ast_copy_string(chan->macroexten,rchan->macroexten,sizeof(chan->macroexten));
01781 chan->macropriority = rchan->macropriority;
01782
01783
01784 ast_do_masquerade(chan);
01785
01786 if (peer == rchan) {
01787 peer = chan;
01788 }
01789
01790
01791 park_call_full(chan, peer, args);
01792
01793 return 0;
01794 }
01795
01796 int ast_masq_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
01797 {
01798 int res;
01799 char *parse;
01800 const char *app_data;
01801 struct ast_exten *exten;
01802 struct park_app_args app_args;
01803 struct ast_park_call_args args = {
01804 .timeout = timeout,
01805 .extout = extout,
01806 };
01807
01808 if (parker) {
01809 args.orig_chan_name = ast_strdupa(parker->name);
01810 }
01811 if (!park_exten || !park_context) {
01812 return masq_park_call(park_me, parker, &args);
01813 }
01814
01815
01816
01817
01818
01819 if (parker && parker != park_me) {
01820 ast_autoservice_start(park_me);
01821 }
01822 exten = get_parking_exten(park_exten, parker, park_context);
01823 if (exten) {
01824 app_data = ast_get_extension_app_data(exten);
01825 if (!app_data) {
01826 app_data = "";
01827 }
01828 parse = ast_strdupa(app_data);
01829 AST_STANDARD_APP_ARGS(app_args, parse);
01830
01831 if (!ast_strlen_zero(app_args.pl_name)) {
01832
01833 args.parkinglot = find_parkinglot(app_args.pl_name);
01834 if (!args.parkinglot && parkeddynamic) {
01835 args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
01836 }
01837 }
01838 }
01839 if (parker && parker != park_me) {
01840 ast_autoservice_stop(park_me);
01841 }
01842
01843 res = masq_park_call(park_me, parker, &args);
01844 if (args.parkinglot) {
01845 parkinglot_unref(args.parkinglot);
01846 }
01847 return res;
01848 }
01849
01850 int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
01851 {
01852 struct ast_park_call_args args = {
01853 .timeout = timeout,
01854 .extout = extout,
01855 };
01856
01857 if (peer) {
01858 args.orig_chan_name = ast_strdupa(peer->name);
01859 }
01860 return masq_park_call(rchan, peer, &args);
01861 }
01862
01863 static int finishup(struct ast_channel *chan)
01864 {
01865 ast_indicate(chan, AST_CONTROL_UNHOLD);
01866
01867 return ast_autoservice_stop(chan);
01868 }
01869
01870
01871
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883
01884 static int xfer_park_call_helper(struct ast_channel *park_me, struct ast_channel *parker, struct ast_exten *park_exten)
01885 {
01886 char *parse;
01887 const char *app_data;
01888 const char *pl_name;
01889 struct ast_park_call_args args = { 0, };
01890 struct park_app_args app_args;
01891 int res;
01892
01893 app_data = ast_get_extension_app_data(park_exten);
01894 if (!app_data) {
01895 app_data = "";
01896 }
01897 parse = ast_strdupa(app_data);
01898 AST_STANDARD_APP_ARGS(app_args, parse);
01899
01900
01901 if (!ast_strlen_zero(app_args.pl_name)) {
01902 pl_name = app_args.pl_name;
01903 } else {
01904 pl_name = findparkinglotname(parker);
01905 }
01906 if (ast_strlen_zero(pl_name)) {
01907
01908 args.parkinglot = parkinglot_addref(default_parkinglot);
01909 } else {
01910 args.parkinglot = find_parkinglot(pl_name);
01911 if (!args.parkinglot && parkeddynamic) {
01912 args.parkinglot = create_dynamic_parkinglot(pl_name, park_me);
01913 }
01914 }
01915
01916 if (args.parkinglot) {
01917
01918 res = finishup(park_me);
01919 if (res) {
01920
01921 parkinglot_unref(args.parkinglot);
01922 return -1;
01923 }
01924 res = masq_park_call(park_me, parker, &args);
01925 parkinglot_unref(args.parkinglot);
01926 } else {
01927
01928 if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
01929 ast_stream_and_wait(parker, "pbx-parkingfailed", "");
01930 }
01931 finishup(park_me);
01932 res = -1;
01933 }
01934
01935 return res ? AST_FEATURE_RETURN_SUCCESS : -1;
01936 }
01937
01938
01939
01940
01941
01942
01943
01944 static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
01945 struct ast_channel *peer, struct ast_channel *chan, int sense)
01946 {
01947 if (sense == FEATURE_SENSE_PEER) {
01948 *caller = peer;
01949 *callee = chan;
01950 } else {
01951 *callee = peer;
01952 *caller = chan;
01953 }
01954 }
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969 static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
01970 {
01971 struct ast_channel *parker;
01972 struct ast_channel *parkee;
01973 struct ast_park_call_args args = { 0, };
01974
01975
01976
01977
01978
01979
01980
01981
01982
01983
01984
01985 if (chan->_state != AST_STATE_UP) {
01986
01987
01988
01989
01990 if (ast_answer(chan)) {
01991 return -1;
01992 }
01993
01994
01995 if (ast_safe_sleep(chan, 1000)) {
01996 return -1;
01997 }
01998 }
01999
02000
02001 set_peers(&parker, &parkee, peer, chan, sense);
02002 return masq_park_call(parkee, parker, &args) ? AST_FEATURE_RETURN_SUCCESS : -1;
02003 }
02004
02005
02006
02007
02008
02009
02010
02011
02012
02013
02014
02015
02016
02017 static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
02018 {
02019
02020 if (ast_autoservice_start(other)) {
02021 return -1;
02022 }
02023 ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
02024 ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
02025 if (ast_stream_and_wait(play_to, audiofile, "")) {
02026 ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
02027 ast_autoservice_stop(other);
02028 return -1;
02029 }
02030 if (ast_autoservice_stop(other)) {
02031 return -1;
02032 }
02033 return 0;
02034 }
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047
02048
02049
02050
02051
02052 static int play_message_to_chans(struct ast_channel *left, struct ast_channel *right, int which, const char *msg, const char *audiofile)
02053 {
02054
02055 if (which <= 0 && play_message_on_chan(left, right, msg, audiofile)) {
02056 return -1;
02057 }
02058
02059
02060 if (which >= 0 && play_message_on_chan(right, left, msg, audiofile)) {
02061 return -1;
02062 }
02063
02064 return 0;
02065 }
02066
02067
02068
02069
02070
02071 static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
02072 {
02073 return play_message_to_chans(caller_chan, callee_chan, 0, "automon message",
02074 audiofile);
02075 }
02076
02077
02078
02079
02080
02081
02082
02083
02084
02085
02086
02087
02088
02089
02090
02091 static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02092 {
02093 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
02094 int x = 0;
02095 size_t len;
02096 struct ast_channel *caller_chan, *callee_chan;
02097 const char *automon_message_start = NULL;
02098 const char *automon_message_stop = NULL;
02099 const char *touch_format = NULL;
02100 const char *touch_monitor = NULL;
02101 const char *touch_monitor_prefix = NULL;
02102
02103 if (!monitor_ok) {
02104 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
02105 return -1;
02106 }
02107
02108 if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
02109 monitor_ok = 0;
02110 ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
02111 return -1;
02112 }
02113
02114 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
02115
02116
02117 automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
02118 automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
02119
02120 if (!ast_strlen_zero(courtesytone)) {
02121 if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
02122 return -1;
02123 }
02124 }
02125
02126 if (callee_chan->monitor) {
02127 ast_verb(4, "User hit '%s' to stop recording call.\n", code);
02128 if (!ast_strlen_zero(automon_message_stop)) {
02129 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
02130 }
02131 callee_chan->monitor->stop(callee_chan, 1);
02132 return AST_FEATURE_RETURN_SUCCESS;
02133 }
02134
02135 touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
02136 touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
02137 touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
02138
02139 if (!touch_format)
02140 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
02141
02142 if (!touch_monitor)
02143 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
02144
02145 if (!touch_monitor_prefix)
02146 touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
02147
02148 if (touch_monitor) {
02149 len = strlen(touch_monitor) + 50;
02150 args = ast_alloca(len);
02151 touch_filename = ast_alloca(len);
02152 snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
02153 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
02154 } else {
02155 caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
02156 caller_chan->caller.id.number.str, caller_chan->name));
02157 callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
02158 callee_chan->caller.id.number.str, callee_chan->name));
02159 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
02160 args = ast_alloca(len);
02161 touch_filename = ast_alloca(len);
02162 snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
02163 snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
02164 }
02165
02166 for(x = 0; x < strlen(args); x++) {
02167 if (args[x] == '/')
02168 args[x] = '-';
02169 }
02170
02171 ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
02172
02173 pbx_exec(callee_chan, monitor_app, args);
02174 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
02175 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
02176
02177 if (!ast_strlen_zero(automon_message_start)) {
02178 play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
02179 }
02180
02181 return AST_FEATURE_RETURN_SUCCESS;
02182 }
02183
02184 static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02185 {
02186 char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
02187 int x = 0;
02188 size_t len;
02189 struct ast_channel *caller_chan, *callee_chan;
02190 const char *mixmonitor_spy_type = "MixMonitor";
02191 const char *touch_format;
02192 const char *touch_monitor;
02193 int count = 0;
02194
02195 if (!mixmonitor_ok) {
02196 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
02197 return -1;
02198 }
02199
02200 if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
02201 mixmonitor_ok = 0;
02202 ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
02203 return -1;
02204 }
02205
02206 set_peers(&caller_chan, &callee_chan, peer, chan, sense);
02207
02208 if (!ast_strlen_zero(courtesytone)) {
02209 if (ast_autoservice_start(callee_chan))
02210 return -1;
02211 ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
02212 if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
02213 ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
02214 ast_autoservice_stop(callee_chan);
02215 return -1;
02216 }
02217 if (ast_autoservice_stop(callee_chan))
02218 return -1;
02219 }
02220
02221 ast_channel_lock(callee_chan);
02222 count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
02223 ast_channel_unlock(callee_chan);
02224
02225
02226 if (count > 0) {
02227 ast_verb(3, "User hit '%s' to stop recording call.\n", code);
02228
02229
02230 ast_channel_lock(callee_chan);
02231 count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
02232 ast_channel_unlock(callee_chan);
02233 if (count > 0) {
02234 if (!stopmixmonitor_ok) {
02235 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
02236 return -1;
02237 }
02238 if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
02239 stopmixmonitor_ok = 0;
02240 ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
02241 return -1;
02242 } else {
02243 pbx_exec(callee_chan, stopmixmonitor_app, "");
02244 return AST_FEATURE_RETURN_SUCCESS;
02245 }
02246 }
02247
02248 ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");
02249 }
02250
02251 touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
02252 touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
02253
02254 if (!touch_format)
02255 touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
02256
02257 if (!touch_monitor)
02258 touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
02259
02260 if (touch_monitor) {
02261 len = strlen(touch_monitor) + 50;
02262 args = ast_alloca(len);
02263 touch_filename = ast_alloca(len);
02264 snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
02265 snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
02266 } else {
02267 caller_chan_id = ast_strdupa(S_COR(caller_chan->caller.id.number.valid,
02268 caller_chan->caller.id.number.str, caller_chan->name));
02269 callee_chan_id = ast_strdupa(S_COR(callee_chan->caller.id.number.valid,
02270 callee_chan->caller.id.number.str, callee_chan->name));
02271 len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
02272 args = ast_alloca(len);
02273 touch_filename = ast_alloca(len);
02274 snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
02275 snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
02276 }
02277
02278 for( x = 0; x < strlen(args); x++) {
02279 if (args[x] == '/')
02280 args[x] = '-';
02281 }
02282
02283 ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
02284
02285 pbx_exec(callee_chan, mixmonitor_app, args);
02286 pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
02287 pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
02288 return AST_FEATURE_RETURN_SUCCESS;
02289 }
02290
02291 static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02292 {
02293 ast_verb(4, "User hit '%s' to disconnect call.\n", code);
02294 return AST_FEATURE_RETURN_HANGUP;
02295 }
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305 static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
02306 {
02307 const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
02308 if (ast_strlen_zero(s)) {
02309 s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
02310 }
02311 if (ast_strlen_zero(s)) {
02312 s = transferer->macrocontext;
02313 }
02314 if (ast_strlen_zero(s)) {
02315 s = transferer->context;
02316 }
02317 return s;
02318 }
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334 static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02335 {
02336 struct ast_channel *transferer;
02337 struct ast_channel *transferee;
02338 struct ast_exten *park_exten;
02339 const char *transferer_real_context;
02340 char xferto[256] = "";
02341 int res;
02342
02343 ast_debug(1, "Executing Blind Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
02344 set_peers(&transferer, &transferee, peer, chan, sense);
02345 transferer_real_context = real_ctx(transferer, transferee);
02346
02347
02348 ast_autoservice_start(transferee);
02349 ast_indicate(transferee, AST_CONTROL_HOLD);
02350
02351
02352 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
02353 if (res < 0) {
02354 finishup(transferee);
02355 return -1;
02356 }
02357 if (res > 0) {
02358 xferto[0] = (char) res;
02359 }
02360
02361 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
02362 if (res < 0) {
02363 finishup(transferee);
02364 return -1;
02365 }
02366 if (res == 0) {
02367 if (xferto[0]) {
02368 ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
02369 xferto, transferer_real_context);
02370 } else {
02371
02372 ast_log(LOG_WARNING, "No digits dialed.\n");
02373 }
02374 ast_stream_and_wait(transferer, "pbx-invalid", "");
02375 finishup(transferee);
02376 return AST_FEATURE_RETURN_SUCCESS;
02377 }
02378
02379 park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
02380 if (park_exten) {
02381
02382 return xfer_park_call_helper(transferee, transferer, park_exten);
02383 }
02384
02385
02386 ast_verb(3, "Blind transferring %s to '%s' (context %s) priority 1\n",
02387 transferee->name, xferto, transferer_real_context);
02388 ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
02389 pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", transferee->name);
02390 pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", transferer->name);
02391 finishup(transferee);
02392 ast_channel_lock(transferer);
02393 if (!transferer->cdr) {
02394 transferer->cdr = ast_cdr_alloc();
02395 if (transferer->cdr) {
02396 ast_cdr_init(transferer->cdr, transferer);
02397 ast_cdr_start(transferer->cdr);
02398 }
02399 }
02400 ast_channel_unlock(transferer);
02401 if (transferer->cdr) {
02402 struct ast_cdr *swap = transferer->cdr;
02403
02404 ast_debug(1,
02405 "transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
02406 transferer->name, transferee->name, transferer->cdr->lastapp,
02407 transferer->cdr->lastdata, transferer->cdr->channel,
02408 transferer->cdr->dstchannel);
02409 ast_debug(1, "TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
02410 transferee->cdr->lastapp, transferee->cdr->lastdata, transferee->cdr->channel,
02411 transferee->cdr->dstchannel);
02412 ast_debug(1, "transferer_real_context=%s; xferto=%s\n",
02413 transferer_real_context, xferto);
02414
02415 transferer->cdr = transferee->cdr;
02416 transferee->cdr = swap;
02417 }
02418 if (!transferee->pbx) {
02419
02420 ast_debug(1, "About to ast_async_goto %s.\n", transferee->name);
02421 if (ast_async_goto(transferee, transferer_real_context, xferto, 1)) {
02422 ast_log(LOG_WARNING, "Async goto failed :-(\n");
02423 }
02424
02425
02426 res = -1;
02427 } else {
02428
02429 ast_debug(1, "About to explicit goto %s, it has a PBX.\n", transferee->name);
02430 ast_set_flag(transferee, AST_FLAG_BRIDGE_HANGUP_DONT);
02431 set_c_e_p(transferee, transferer_real_context, xferto, 0);
02432
02433
02434
02435
02436
02437 res = AST_FEATURE_RETURN_SUCCESSBREAK;
02438 }
02439 check_goto_on_transfer(transferer);
02440 return res;
02441 }
02442
02443
02444
02445
02446
02447
02448
02449
02450 static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
02451 {
02452 if (ast_channel_make_compatible(c, newchan) < 0) {
02453 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
02454 c->name, newchan->name);
02455 ast_hangup(newchan);
02456 return -1;
02457 }
02458 return 0;
02459 }
02460
02461
02462
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474 static void atxfer_fail_cleanup(struct ast_channel *transferee, struct ast_channel *transferer, struct ast_party_connected_line *connected_line)
02475 {
02476 finishup(transferee);
02477
02478
02479
02480
02481
02482
02483
02484 if (ast_channel_connected_line_macro(transferee, transferer, connected_line, 1, 0)) {
02485 ast_channel_update_connected_line(transferer, connected_line, NULL);
02486 }
02487 ast_party_connected_line_free(connected_line);
02488 }
02489
02490
02491
02492
02493
02494
02495
02496
02497
02498
02499
02500
02501
02502
02503
02504
02505 static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
02506 {
02507 struct ast_channel *transferer;
02508 struct ast_channel *transferee;
02509 struct ast_exten *park_exten;
02510 const char *chan1_attended_sound;
02511 const char *chan2_attended_sound;
02512 const char *transferer_real_context;
02513 char xferto[256] = "";
02514 int res;
02515 int outstate=0;
02516 struct ast_channel *newchan;
02517 struct ast_channel *xferchan;
02518 struct ast_bridge_thread_obj *tobj;
02519 struct ast_bridge_config bconfig;
02520 int l;
02521 struct ast_party_connected_line connected_line;
02522 struct ast_datastore *features_datastore;
02523 struct ast_dial_features *dialfeatures;
02524 char *transferer_tech;
02525 char *transferer_name;
02526 char *transferer_name_orig;
02527 char *dash;
02528
02529 ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", chan->name, peer->name, sense);
02530 set_peers(&transferer, &transferee, peer, chan, sense);
02531 transferer_real_context = real_ctx(transferer, transferee);
02532
02533
02534 ast_autoservice_start(transferee);
02535 ast_indicate(transferee, AST_CONTROL_HOLD);
02536
02537
02538 res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
02539 if (res < 0) {
02540 finishup(transferee);
02541 return -1;
02542 }
02543 if (res > 0) {
02544 xferto[0] = (char) res;
02545 }
02546
02547
02548 res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
02549 if (res < 0) {
02550 finishup(transferee);
02551 return -1;
02552 }
02553 l = strlen(xferto);
02554 if (res == 0) {
02555 if (l) {
02556 ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
02557 xferto, transferer_real_context);
02558 } else {
02559
02560 ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
02561 }
02562 ast_stream_and_wait(transferer, "pbx-invalid", "");
02563 finishup(transferee);
02564 return AST_FEATURE_RETURN_SUCCESS;
02565 }
02566
02567 park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
02568 if (park_exten) {
02569
02570 return xfer_park_call_helper(transferee, transferer, park_exten);
02571 }
02572
02573
02574
02575
02576
02577
02578
02579
02580 snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
02581
02582
02583
02584 chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
02585 chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
02586 if (!ast_strlen_zero(chan1_attended_sound)) {
02587 pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
02588 }
02589 if (!ast_strlen_zero(chan2_attended_sound)) {
02590 pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
02591 }
02592
02593
02594 transferer_name_orig = ast_strdupa(transferer->name);
02595 transferer_name = ast_strdupa(transferer_name_orig);
02596 transferer_tech = strsep(&transferer_name, "/");
02597 dash = strrchr(transferer_name, '-');
02598 if (dash) {
02599
02600 *dash = '\0';
02601 }
02602
02603
02604 if (ast_autoservice_stop(transferee) < 0) {
02605 ast_indicate(transferee, AST_CONTROL_UNHOLD);
02606 return -1;
02607 }
02608
02609
02610 ast_party_connected_line_init(&connected_line);
02611 ast_channel_lock(transferer);
02612 ast_party_connected_line_copy(&connected_line, &transferer->connected);
02613 ast_channel_unlock(transferer);
02614 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02615
02616
02617 newchan = feature_request_and_dial(transferer, transferer_name_orig, transferer,
02618 transferee, "Local", ast_best_codec(transferer->nativeformats), xferto,
02619 atxfernoanswertimeout, &outstate, transferer->language);
02620 ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
02621
02622 if (!ast_check_hangup(transferer)) {
02623 int hangup_dont = 0;
02624
02625
02626 ast_debug(1, "Actually doing an attended transfer.\n");
02627
02628
02629 ast_autoservice_start(transferee);
02630
02631 ast_indicate(transferer, -1);
02632 if (!newchan) {
02633
02634 switch (outstate) {
02635 case AST_CONTROL_UNHOLD:
02636 case AST_CONTROL_BUSY:
02637 case AST_CONTROL_CONGESTION:
02638 if (ast_stream_and_wait(transferer, xfersound, "")) {
02639 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02640 }
02641 break;
02642 default:
02643 if (ast_stream_and_wait(transferer, xferfailsound, "")) {
02644 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
02645 }
02646 break;
02647 }
02648 atxfer_fail_cleanup(transferee, transferer, &connected_line);
02649 return AST_FEATURE_RETURN_SUCCESS;
02650 }
02651
02652 if (check_compat(transferer, newchan)) {
02653 if (ast_stream_and_wait(transferer, xferfailsound, "")) {
02654 ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
02655 }
02656 atxfer_fail_cleanup(transferee, transferer, &connected_line);
02657 return AST_FEATURE_RETURN_SUCCESS;
02658 }
02659 memset(&bconfig,0,sizeof(struct ast_bridge_config));
02660 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
02661 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
02662
02663
02664
02665
02666
02667
02668 if (ast_test_flag(transferer, AST_FLAG_BRIDGE_HANGUP_DONT)) {
02669 hangup_dont = 1;
02670 }
02671
02672
02673
02674
02675
02676 ast_set_flag(transferer, AST_FLAG_BRIDGE_HANGUP_DONT);
02677
02678
02679
02680
02681
02682 ast_bridge_call(transferer, newchan, &bconfig);
02683
02684 if (hangup_dont) {
02685
02686 ast_set_flag(transferer, AST_FLAG_BRIDGE_HANGUP_DONT);
02687 }
02688
02689 if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
02690 ast_hangup(newchan);
02691 if (ast_stream_and_wait(transferer, xfersound, "")) {
02692 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02693 }
02694 atxfer_fail_cleanup(transferee, transferer, &connected_line);
02695 return AST_FEATURE_RETURN_SUCCESS;
02696 }
02697
02698
02699 if (check_compat(transferee, newchan)) {
02700 finishup(transferee);
02701 ast_party_connected_line_free(&connected_line);
02702 return -1;
02703 }
02704
02705 ast_indicate(transferee, AST_CONTROL_UNHOLD);
02706 if ((ast_autoservice_stop(transferee) < 0)
02707 || (ast_waitfordigit(transferee, 100) < 0)
02708 || (ast_waitfordigit(newchan, 100) < 0)
02709 || ast_check_hangup(transferee)
02710 || ast_check_hangup(newchan)) {
02711 ast_hangup(newchan);
02712 ast_party_connected_line_free(&connected_line);
02713 return -1;
02714 }
02715 } else if (!ast_check_hangup(transferee)) {
02716
02717 ast_debug(1, "Actually doing a blonde transfer.\n");
02718
02719 if (!newchan && !atxferdropcall) {
02720
02721 unsigned int tries = 0;
02722
02723 if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
02724 ast_log(LOG_WARNING,
02725 "Transferer channel name: '%s' cannot be used for callback.\n",
02726 transferer_name_orig);
02727 ast_indicate(transferee, AST_CONTROL_UNHOLD);
02728 ast_party_connected_line_free(&connected_line);
02729 return -1;
02730 }
02731
02732 tries = 0;
02733 for (;;) {
02734
02735 ast_debug(1, "We're trying to callback %s/%s\n",
02736 transferer_tech, transferer_name);
02737 newchan = feature_request_and_dial(transferer, transferer_name_orig,
02738 transferee, transferee, transferer_tech,
02739 ast_best_codec(transferee->nativeformats), transferer_name,
02740 atxfernoanswertimeout, &outstate, transferer->language);
02741 ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
02742 !!newchan, outstate);
02743 if (newchan) {
02744
02745
02746
02747
02748
02749 ast_channel_lock(transferer);
02750 features_datastore = ast_channel_datastore_find(transferer,
02751 &dial_features_info, NULL);
02752 if (features_datastore && (dialfeatures = features_datastore->data)) {
02753 struct ast_flags my_features = { 0 };
02754 struct ast_flags peer_features = { 0 };
02755
02756 ast_copy_flags(&my_features, &dialfeatures->my_features,
02757 AST_FLAGS_ALL);
02758 ast_copy_flags(&peer_features, &dialfeatures->peer_features,
02759 AST_FLAGS_ALL);
02760 ast_channel_unlock(transferer);
02761 add_features_datastore(newchan, &my_features, &peer_features);
02762 } else {
02763 ast_channel_unlock(transferer);
02764 }
02765 break;
02766 }
02767 if (ast_check_hangup(transferee)) {
02768 break;
02769 }
02770
02771 ++tries;
02772 if (atxfercallbackretries <= tries) {
02773
02774 break;
02775 }
02776
02777 if (atxferloopdelay) {
02778
02779 ast_debug(1, "Sleeping for %d ms before retrying atxfer.\n",
02780 atxferloopdelay);
02781 ast_safe_sleep(transferee, atxferloopdelay);
02782 if (ast_check_hangup(transferee)) {
02783 ast_party_connected_line_free(&connected_line);
02784 return -1;
02785 }
02786 }
02787
02788
02789 ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
02790 newchan = feature_request_and_dial(transferer, transferer_name_orig,
02791 transferer, transferee, "Local",
02792 ast_best_codec(transferee->nativeformats), xferto,
02793 atxfernoanswertimeout, &outstate, transferer->language);
02794 ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
02795 !!newchan, outstate);
02796 if (newchan || ast_check_hangup(transferee)) {
02797 break;
02798 }
02799 }
02800 }
02801 ast_indicate(transferee, AST_CONTROL_UNHOLD);
02802 if (!newchan) {
02803
02804 ast_party_connected_line_free(&connected_line);
02805 return -1;
02806 }
02807
02808
02809 if (ast_check_hangup(newchan)) {
02810 ast_hangup(newchan);
02811 ast_party_connected_line_free(&connected_line);
02812 return -1;
02813 }
02814 if (check_compat(transferee, newchan)) {
02815 ast_party_connected_line_free(&connected_line);
02816 return -1;
02817 }
02818 } else {
02819
02820
02821
02822
02823 ast_debug(1, "Everyone is hungup.\n");
02824 if (newchan) {
02825 ast_hangup(newchan);
02826 }
02827 ast_party_connected_line_free(&connected_line);
02828 return -1;
02829 }
02830
02831
02832 ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
02833
02834 xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", transferee->linkedid, 0, "Transfered/%s", transferee->name);
02835 if (!xferchan) {
02836 ast_hangup(newchan);
02837 ast_party_connected_line_free(&connected_line);
02838 return -1;
02839 }
02840
02841
02842 xferchan->visible_indication = AST_CONTROL_RINGING;
02843
02844
02845 xferchan->readformat = transferee->readformat;
02846 xferchan->writeformat = transferee->writeformat;
02847
02848 if (ast_channel_masquerade(xferchan, transferee)) {
02849 ast_hangup(xferchan);
02850 ast_hangup(newchan);
02851 ast_party_connected_line_free(&connected_line);
02852 return -1;
02853 }
02854 ast_explicit_goto(xferchan, transferee->context, transferee->exten, transferee->priority);
02855 xferchan->_state = AST_STATE_UP;
02856 ast_clear_flag(xferchan, AST_FLAGS_ALL);
02857
02858
02859 ast_do_masquerade(xferchan);
02860
02861 newchan->_state = AST_STATE_UP;
02862 ast_clear_flag(newchan, AST_FLAGS_ALL);
02863 tobj = ast_calloc(1, sizeof(*tobj));
02864 if (!tobj) {
02865 ast_hangup(xferchan);
02866 ast_hangup(newchan);
02867 ast_party_connected_line_free(&connected_line);
02868 return -1;
02869 }
02870
02871 tobj->chan = newchan;
02872 tobj->peer = xferchan;
02873 tobj->bconfig = *config;
02874
02875 ast_channel_lock(newchan);
02876 features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL);
02877 if (features_datastore && (dialfeatures = features_datastore->data)) {
02878 ast_copy_flags(&tobj->bconfig.features_callee, &dialfeatures->my_features,
02879 AST_FLAGS_ALL);
02880 }
02881 ast_channel_unlock(newchan);
02882
02883 ast_channel_lock(xferchan);
02884 features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL);
02885 if (features_datastore && (dialfeatures = features_datastore->data)) {
02886 ast_copy_flags(&tobj->bconfig.features_caller, &dialfeatures->my_features,
02887 AST_FLAGS_ALL);
02888 }
02889 ast_channel_unlock(xferchan);
02890
02891 if (tobj->bconfig.end_bridge_callback_data_fixup) {
02892 tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
02893 }
02894
02895
02896
02897
02898
02899
02900
02901
02902
02903
02904
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925 ast_channel_lock(transferer);
02926
02927
02928
02929
02930 ast_party_connected_line_copy(&connected_line, &transferer->connected);
02931 ast_channel_unlock(transferer);
02932 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02933 if (ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
02934 ast_channel_update_connected_line(xferchan, &connected_line, NULL);
02935 }
02936
02937
02938 ast_channel_lock(xferchan);
02939 ast_connected_line_copy_from_caller(&connected_line, &xferchan->caller);
02940 ast_channel_unlock(xferchan);
02941 connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
02942 if (ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
02943 ast_channel_update_connected_line(newchan, &connected_line, NULL);
02944 }
02945
02946 if (ast_stream_and_wait(newchan, xfersound, ""))
02947 ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
02948 bridge_call_thread_launch(tobj);
02949
02950 ast_party_connected_line_free(&connected_line);
02951 return -1;
02952 }
02953
02954
02955 #define FEATURES_COUNT ARRAY_LEN(builtin_features)
02956
02957 AST_RWLOCK_DEFINE_STATIC(features_lock);
02958
02959 static struct ast_call_feature builtin_features[] = {
02960 { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02961 { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02962 { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02963 { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02964 { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02965 { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
02966 };
02967
02968
02969 static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
02970
02971
02972 void ast_register_feature(struct ast_call_feature *feature)
02973 {
02974 if (!feature) {
02975 ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
02976 return;
02977 }
02978
02979 AST_RWLIST_WRLOCK(&feature_list);
02980 AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
02981 AST_RWLIST_UNLOCK(&feature_list);
02982
02983 ast_verb(2, "Registered Feature '%s'\n",feature->sname);
02984 }
02985
02986
02987
02988
02989
02990
02991
02992
02993 static struct feature_group *register_group(const char *fgname)
02994 {
02995 struct feature_group *fg;
02996
02997 if (!fgname) {
02998 ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
02999 return NULL;
03000 }
03001
03002 if (!(fg = ast_calloc_with_stringfields(1, struct feature_group, 128))) {
03003 return NULL;
03004 }
03005
03006 ast_string_field_set(fg, gname, fgname);
03007
03008 AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
03009
03010 ast_verb(2, "Registered group '%s'\n", fg->gname);
03011
03012 return fg;
03013 }
03014
03015
03016
03017
03018
03019
03020
03021
03022
03023
03024 static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
03025 {
03026 struct feature_group_exten *fge;
03027
03028 if (!fg) {
03029 ast_log(LOG_NOTICE, "You didn't pass a group!\n");
03030 return;
03031 }
03032
03033 if (!feature) {
03034 ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
03035 return;
03036 }
03037
03038 if (!(fge = ast_calloc_with_stringfields(1, struct feature_group_exten, 128))) {
03039 return;
03040 }
03041
03042 ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
03043
03044 fge->feature = feature;
03045
03046 AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
03047
03048 ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
03049 feature->sname, fg->gname, fge->exten);
03050 }
03051
03052 void ast_unregister_feature(struct ast_call_feature *feature)
03053 {
03054 if (!feature) {
03055 return;
03056 }
03057
03058 AST_RWLIST_WRLOCK(&feature_list);
03059 AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
03060 AST_RWLIST_UNLOCK(&feature_list);
03061
03062 ast_free(feature);
03063 }
03064
03065
03066 static void ast_unregister_features(void)
03067 {
03068 struct ast_call_feature *feature;
03069
03070 AST_RWLIST_WRLOCK(&feature_list);
03071 while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
03072 ast_free(feature);
03073 }
03074 AST_RWLIST_UNLOCK(&feature_list);
03075 }
03076
03077
03078 static struct ast_call_feature *find_dynamic_feature(const char *name)
03079 {
03080 struct ast_call_feature *tmp;
03081
03082 AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
03083 if (!strcasecmp(tmp->sname, name)) {
03084 break;
03085 }
03086 }
03087
03088 return tmp;
03089 }
03090
03091
03092 static void ast_unregister_groups(void)
03093 {
03094 struct feature_group *fg;
03095 struct feature_group_exten *fge;
03096
03097 AST_RWLIST_WRLOCK(&feature_groups);
03098 while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
03099 while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
03100 ast_string_field_free_memory(fge);
03101 ast_free(fge);
03102 }
03103
03104 ast_string_field_free_memory(fg);
03105 ast_free(fg);
03106 }
03107 AST_RWLIST_UNLOCK(&feature_groups);
03108 }
03109
03110
03111
03112
03113
03114
03115
03116 static struct feature_group *find_group(const char *name)
03117 {
03118 struct feature_group *fg = NULL;
03119
03120 AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
03121 if (!strcasecmp(fg->gname, name))
03122 break;
03123 }
03124
03125 return fg;
03126 }
03127
03128 void ast_rdlock_call_features(void)
03129 {
03130 ast_rwlock_rdlock(&features_lock);
03131 }
03132
03133 void ast_unlock_call_features(void)
03134 {
03135 ast_rwlock_unlock(&features_lock);
03136 }
03137
03138 struct ast_call_feature *ast_find_call_feature(const char *name)
03139 {
03140 int x;
03141 for (x = 0; x < FEATURES_COUNT; x++) {
03142 if (!strcasecmp(name, builtin_features[x].sname))
03143 return &builtin_features[x];
03144 }
03145 return NULL;
03146 }
03147
03148
03149
03150
03151
03152
03153
03154
03155
03156
03157 static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
03158 {
03159 struct ast_app *app;
03160 struct ast_call_feature *feature = data;
03161 struct ast_channel *work, *idle;
03162 int res;
03163
03164 if (!feature) {
03165 ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
03166 return -1;
03167 }
03168
03169 if (sense == FEATURE_SENSE_CHAN) {
03170 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
03171 return AST_FEATURE_RETURN_KEEPTRYING;
03172 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
03173 work = chan;
03174 idle = peer;
03175 } else {
03176 work = peer;
03177 idle = chan;
03178 }
03179 } else {
03180 if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
03181 return AST_FEATURE_RETURN_KEEPTRYING;
03182 if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
03183 work = peer;
03184 idle = chan;
03185 } else {
03186 work = chan;
03187 idle = peer;
03188 }
03189 }
03190
03191 if (!(app = pbx_findapp(feature->app))) {
03192 ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
03193 return -2;
03194 }
03195
03196 ast_autoservice_start(idle);
03197 ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
03198
03199 pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", idle->name);
03200 pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", work->name);
03201 pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
03202 pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
03203
03204 if (!ast_strlen_zero(feature->moh_class))
03205 ast_moh_start(idle, feature->moh_class, NULL);
03206
03207 res = pbx_exec(work, app, feature->app_args);
03208
03209 if (!ast_strlen_zero(feature->moh_class))
03210 ast_moh_stop(idle);
03211
03212 ast_autoservice_stop(idle);
03213
03214 if (res) {
03215 return AST_FEATURE_RETURN_SUCCESSBREAK;
03216 }
03217 return AST_FEATURE_RETURN_SUCCESS;
03218 }
03219
03220 static void unmap_features(void)
03221 {
03222 int x;
03223
03224 ast_rwlock_wrlock(&features_lock);
03225 for (x = 0; x < FEATURES_COUNT; x++)
03226 strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
03227 ast_rwlock_unlock(&features_lock);
03228 }
03229
03230 static int remap_feature(const char *name, const char *value)
03231 {
03232 int x, res = -1;
03233
03234 ast_rwlock_wrlock(&features_lock);
03235 for (x = 0; x < FEATURES_COUNT; x++) {
03236 if (strcasecmp(builtin_features[x].sname, name))
03237 continue;
03238
03239 ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
03240 res = 0;
03241 break;
03242 }
03243 ast_rwlock_unlock(&features_lock);
03244
03245 return res;
03246 }
03247
03248
03249
03250
03251
03252
03253
03254
03255
03256
03257
03258 static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
03259 struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
03260 struct ast_flags *features, feature_interpret_op operation, struct ast_call_feature *feature)
03261 {
03262 int x;
03263 struct feature_group *fg = NULL;
03264 struct feature_group_exten *fge;
03265 struct ast_call_feature *tmpfeature;
03266 char *tmp, *tok;
03267 int res = AST_FEATURE_RETURN_PASSDIGITS;
03268 int feature_detected = 0;
03269
03270 if (!(peer && chan && config) && operation == FEATURE_INTERPRET_DO) {
03271 return -1;
03272 }
03273
03274 ast_rwlock_rdlock(&features_lock);
03275 for (x = 0; x < FEATURES_COUNT; x++) {
03276 if ((ast_test_flag(features, builtin_features[x].feature_mask)) &&
03277 !ast_strlen_zero(builtin_features[x].exten)) {
03278
03279 if (!strcmp(builtin_features[x].exten, code)) {
03280 ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, builtin_features[x].exten);
03281 if (operation == FEATURE_INTERPRET_CHECK) {
03282 res = AST_FEATURE_RETURN_SUCCESS;
03283 } else if (operation == FEATURE_INTERPRET_DO) {
03284 res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
03285 }
03286 if (feature) {
03287 memcpy(feature, &builtin_features[x], sizeof(*feature));
03288 }
03289 feature_detected = 1;
03290 break;
03291 } else if (!strncmp(builtin_features[x].exten, code, strlen(code))) {
03292 if (res == AST_FEATURE_RETURN_PASSDIGITS) {
03293 res = AST_FEATURE_RETURN_STOREDIGITS;
03294 }
03295 }
03296 }
03297 }
03298 ast_rwlock_unlock(&features_lock);
03299
03300 if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
03301 return res;
03302 }
03303
03304 tmp = dynamic_features_buf;
03305
03306 while ((tok = strsep(&tmp, "#"))) {
03307 AST_RWLIST_RDLOCK(&feature_groups);
03308
03309 fg = find_group(tok);
03310
03311 if (fg) {
03312 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
03313 if (!strcmp(fge->exten, code)) {
03314 if (operation) {
03315 res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
03316 }
03317 if (feature) {
03318 memcpy(feature, fge->feature, sizeof(*feature));
03319 }
03320 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
03321 AST_RWLIST_UNLOCK(&feature_groups);
03322 break;
03323 }
03324 res = AST_FEATURE_RETURN_PASSDIGITS;
03325 } else if (!strncmp(fge->exten, code, strlen(code))) {
03326 res = AST_FEATURE_RETURN_STOREDIGITS;
03327 }
03328 }
03329 if (fge) {
03330 break;
03331 }
03332 }
03333
03334 AST_RWLIST_UNLOCK(&feature_groups);
03335
03336 AST_RWLIST_RDLOCK(&feature_list);
03337
03338 if (!(tmpfeature = find_dynamic_feature(tok))) {
03339 AST_RWLIST_UNLOCK(&feature_list);
03340 continue;
03341 }
03342
03343
03344 if (!strcmp(tmpfeature->exten, code)) {
03345 ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
03346 if (operation == FEATURE_INTERPRET_CHECK) {
03347 res = AST_FEATURE_RETURN_SUCCESS;
03348 } else if (operation == FEATURE_INTERPRET_DO) {
03349 res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
03350 }
03351 if (feature) {
03352 memcpy(feature, tmpfeature, sizeof(*feature));
03353 }
03354 if (res != AST_FEATURE_RETURN_KEEPTRYING) {
03355 AST_RWLIST_UNLOCK(&feature_list);
03356 break;
03357 }
03358 res = AST_FEATURE_RETURN_PASSDIGITS;
03359 } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
03360 res = AST_FEATURE_RETURN_STOREDIGITS;
03361
03362 AST_RWLIST_UNLOCK(&feature_list);
03363 }
03364
03365 return res;
03366 }
03367
03368
03369
03370
03371
03372
03373
03374
03375 static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
03376
03377 char dynamic_features_buf[128];
03378 const char *peer_dynamic_features, *chan_dynamic_features;
03379 struct ast_flags features;
03380 struct ast_call_feature feature;
03381 if (sense == FEATURE_SENSE_CHAN) {
03382
03383 ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
03384 }
03385 else {
03386
03387 ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
03388 }
03389
03390 ast_channel_lock(peer);
03391 peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
03392 ast_channel_unlock(peer);
03393
03394 ast_channel_lock(chan);
03395 chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
03396 ast_channel_unlock(chan);
03397
03398 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,""));
03399
03400 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);
03401
03402 return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, FEATURE_INTERPRET_DO, &feature);
03403 }
03404
03405
03406 int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
03407
03408 return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, FEATURE_INTERPRET_DETECT, feature);
03409 }
03410
03411
03412 static int feature_check(struct ast_channel *chan, struct ast_flags *features, char *code) {
03413 char *chan_dynamic_features;
03414 ast_channel_lock(chan);
03415 chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
03416 ast_channel_unlock(chan);
03417
03418 return feature_interpret_helper(chan, NULL, NULL, code, 0, chan_dynamic_features, features, FEATURE_INTERPRET_CHECK, NULL);
03419 }
03420
03421 static void set_config_flags(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
03422 {
03423 int x;
03424
03425 ast_clear_flag(config, AST_FLAGS_ALL);
03426
03427 ast_rwlock_rdlock(&features_lock);
03428 for (x = 0; x < FEATURES_COUNT; x++) {
03429 if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
03430 continue;
03431
03432 if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
03433 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03434
03435 if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
03436 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03437 }
03438 ast_rwlock_unlock(&features_lock);
03439
03440 if (chan && peer && !(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
03441 const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
03442
03443 if (dynamic_features) {
03444 char *tmp = ast_strdupa(dynamic_features);
03445 char *tok;
03446 struct ast_call_feature *feature;
03447
03448
03449 while ((tok = strsep(&tmp, "#"))) {
03450 struct feature_group *fg;
03451
03452 AST_RWLIST_RDLOCK(&feature_groups);
03453 AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
03454 struct feature_group_exten *fge;
03455
03456 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
03457 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
03458 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03459 }
03460 if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
03461 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03462 }
03463 }
03464 }
03465 AST_RWLIST_UNLOCK(&feature_groups);
03466
03467 AST_RWLIST_RDLOCK(&feature_list);
03468 if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
03469 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
03470 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
03471 }
03472 if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
03473 ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
03474 }
03475 }
03476 AST_RWLIST_UNLOCK(&feature_list);
03477 }
03478 }
03479 }
03480 }
03481
03482
03483
03484
03485
03486
03487
03488
03489
03490
03491
03492
03493
03494
03495
03496
03497
03498
03499
03500
03501
03502
03503
03504
03505
03506
03507
03508
03509
03510
03511
03512
03513
03514
03515
03516 static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
03517 const char *caller_name, struct ast_channel *requestor,
03518 struct ast_channel *transferee, const char *type, format_t format, void *data,
03519 int timeout, int *outstate, const char *language)
03520 {
03521 int state = 0;
03522 int cause = 0;
03523 int to;
03524 int caller_hungup;
03525 int transferee_hungup;
03526 struct ast_channel *chan;
03527 struct ast_channel *monitor_chans[3];
03528 struct ast_channel *active_channel;
03529 int res;
03530 int ready = 0;
03531 struct timeval started;
03532 int x, len = 0;
03533 char *disconnect_code = NULL, *dialed_code = NULL;
03534 struct ast_frame *f;
03535 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
03536
03537 caller_hungup = ast_check_hangup(caller);
03538
03539 if (!(chan = ast_request(type, format, requestor, data, &cause))) {
03540 ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, (char *)data);
03541 switch (cause) {
03542 case AST_CAUSE_BUSY:
03543 state = AST_CONTROL_BUSY;
03544 break;
03545 case AST_CAUSE_CONGESTION:
03546 state = AST_CONTROL_CONGESTION;
03547 break;
03548 default:
03549 state = 0;
03550 break;
03551 }
03552 goto done;
03553 }
03554
03555 ast_string_field_set(chan, language, language);
03556 ast_channel_inherit_variables(caller, chan);
03557 pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
03558
03559 ast_channel_lock(chan);
03560 ast_connected_line_copy_from_caller(&chan->connected, &requestor->caller);
03561 ast_channel_unlock(chan);
03562
03563 if (ast_call(chan, data, timeout)) {
03564 ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, (char *)data);
03565 switch (chan->hangupcause) {
03566 case AST_CAUSE_BUSY:
03567 state = AST_CONTROL_BUSY;
03568 break;
03569 case AST_CAUSE_CONGESTION:
03570 state = AST_CONTROL_CONGESTION;
03571 break;
03572 default:
03573 state = 0;
03574 break;
03575 }
03576 goto done;
03577 }
03578
03579
03580 ast_rwlock_rdlock(&features_lock);
03581 for (x = 0; x < FEATURES_COUNT; x++) {
03582 if (strcasecmp(builtin_features[x].sname, "disconnect"))
03583 continue;
03584
03585 disconnect_code = builtin_features[x].exten;
03586 len = strlen(disconnect_code) + 1;
03587 dialed_code = ast_alloca(len);
03588 memset(dialed_code, 0, len);
03589 break;
03590 }
03591 ast_rwlock_unlock(&features_lock);
03592 x = 0;
03593 started = ast_tvnow();
03594 to = timeout;
03595 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
03596
03597 ast_poll_channel_add(caller, chan);
03598
03599 transferee_hungup = 0;
03600 while (!ast_check_hangup(transferee) && (chan->_state != AST_STATE_UP)) {
03601 int num_chans = 0;
03602
03603 monitor_chans[num_chans++] = transferee;
03604 monitor_chans[num_chans++] = chan;
03605 if (!caller_hungup) {
03606 if (ast_check_hangup(caller)) {
03607 caller_hungup = 1;
03608
03609 #if defined(ATXFER_NULL_TECH)
03610
03611 set_new_chan_name(caller);
03612
03613
03614
03615
03616
03617 set_kill_chan_tech(caller);
03618 #endif
03619 } else {
03620
03621 monitor_chans[num_chans++] = caller;
03622 }
03623 }
03624
03625
03626 if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
03627 state = AST_CONTROL_UNHOLD;
03628 ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", chan->name);
03629 break;
03630 }
03631
03632 active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
03633 if (!active_channel)
03634 continue;
03635
03636 f = NULL;
03637 if (transferee == active_channel) {
03638 struct ast_frame *dup_f;
03639
03640 f = ast_read(transferee);
03641 if (f == NULL) {
03642 transferee_hungup = 1;
03643 state = 0;
03644 break;
03645 }
03646 if (ast_is_deferrable_frame(f)) {
03647 dup_f = ast_frisolate(f);
03648 if (dup_f) {
03649 if (dup_f == f) {
03650 f = NULL;
03651 }
03652 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
03653 }
03654 }
03655 } else if (chan == active_channel) {
03656 if (!ast_strlen_zero(chan->call_forward)) {
03657 state = 0;
03658 ast_autoservice_start(transferee);
03659 chan = ast_call_forward(caller, chan, NULL, format, NULL, &state);
03660 ast_autoservice_stop(transferee);
03661 if (!chan) {
03662 break;
03663 }
03664 continue;
03665 }
03666 f = ast_read(chan);
03667 if (f == NULL) {
03668 switch (chan->hangupcause) {
03669 case AST_CAUSE_BUSY:
03670 state = AST_CONTROL_BUSY;
03671 break;
03672 case AST_CAUSE_CONGESTION:
03673 state = AST_CONTROL_CONGESTION;
03674 break;
03675 default:
03676 state = 0;
03677 break;
03678 }
03679 break;
03680 }
03681
03682 if (f->frametype == AST_FRAME_CONTROL) {
03683 if (f->subclass.integer == AST_CONTROL_RINGING) {
03684 ast_verb(3, "%s is ringing\n", chan->name);
03685 ast_indicate(caller, AST_CONTROL_RINGING);
03686 } else if (f->subclass.integer == AST_CONTROL_BUSY) {
03687 state = f->subclass.integer;
03688 ast_verb(3, "%s is busy\n", chan->name);
03689 ast_indicate(caller, AST_CONTROL_BUSY);
03690 ast_frfree(f);
03691 break;
03692 } else if (f->subclass.integer == AST_CONTROL_INCOMPLETE) {
03693 ast_verb(3, "%s dialed incomplete extension %s; ignoring\n", chan->name, chan->exten);
03694 } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
03695 state = f->subclass.integer;
03696 ast_verb(3, "%s is congested\n", chan->name);
03697 ast_indicate(caller, AST_CONTROL_CONGESTION);
03698 ast_frfree(f);
03699 break;
03700 } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
03701
03702 state = f->subclass.integer;
03703 ast_frfree(f);
03704 ready=1;
03705 break;
03706 } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
03707 if (caller_hungup) {
03708 struct ast_party_connected_line connected;
03709
03710
03711 ast_party_connected_line_set_init(&connected, &caller->connected);
03712 res = ast_connected_line_parse_data(f->data.ptr, f->datalen,
03713 &connected);
03714 if (!res) {
03715 ast_channel_set_connected_line(caller, &connected, NULL);
03716 }
03717 ast_party_connected_line_free(&connected);
03718 } else {
03719 ast_autoservice_start(transferee);
03720 if (ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
03721 ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE,
03722 f->data.ptr, f->datalen);
03723 }
03724 ast_autoservice_stop(transferee);
03725 }
03726 } else if (f->subclass.integer == AST_CONTROL_REDIRECTING) {
03727 if (!caller_hungup) {
03728 ast_autoservice_start(transferee);
03729 if (ast_channel_redirecting_macro(chan, caller, f, 1, 1)) {
03730 ast_indicate_data(caller, AST_CONTROL_REDIRECTING,
03731 f->data.ptr, f->datalen);
03732 }
03733 ast_autoservice_stop(transferee);
03734 }
03735 } else if (f->subclass.integer != -1
03736 && f->subclass.integer != AST_CONTROL_PROGRESS
03737 && f->subclass.integer != AST_CONTROL_PROCEEDING) {
03738 ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
03739 }
03740
03741 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
03742 ast_write(caller, f);
03743 }
03744 } else if (caller == active_channel) {
03745 f = ast_read(caller);
03746 if (f) {
03747 if (f->frametype == AST_FRAME_DTMF) {
03748 dialed_code[x++] = f->subclass.integer;
03749 dialed_code[x] = '\0';
03750 if (strlen(dialed_code) == len) {
03751 x = 0;
03752 } else if (x && strncmp(dialed_code, disconnect_code, x)) {
03753 x = 0;
03754 dialed_code[x] = '\0';
03755 }
03756 if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
03757
03758 state = AST_CONTROL_UNHOLD;
03759 ast_frfree(f);
03760 break;
03761 }
03762 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
03763 ast_write(chan, f);
03764 }
03765 }
03766 }
03767 if (f)
03768 ast_frfree(f);
03769 }
03770
03771 ast_poll_channel_del(caller, chan);
03772
03773
03774
03775
03776
03777 ast_channel_lock(transferee);
03778 transferee_hungup = (transferee_hungup || ast_check_hangup(transferee));
03779 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
03780 if (!transferee_hungup) {
03781 ast_queue_frame_head(transferee, f);
03782 }
03783 ast_frfree(f);
03784 }
03785 ast_channel_unlock(transferee);
03786
03787 done:
03788 ast_indicate(caller, -1);
03789 if (chan && (ready || chan->_state == AST_STATE_UP)) {
03790 state = AST_CONTROL_ANSWER;
03791 } else if (chan) {
03792 ast_hangup(chan);
03793 chan = NULL;
03794 }
03795
03796 if (outstate)
03797 *outstate = state;
03798
03799 return chan;
03800 }
03801
03802 void ast_channel_log(char *title, struct ast_channel *chan);
03803
03804 void ast_channel_log(char *title, struct ast_channel *chan)
03805 {
03806 ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long)chan);
03807 ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
03808 chan->name, chan->appl, chan->data, chan->context, chan->exten, chan->priority);
03809 ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
03810 chan->accountcode, chan->dialcontext, chan->amaflags, chan->macrocontext, chan->macroexten, chan->macropriority);
03811 ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; _bridge: %p; uniqueID: %s; linkedID:%s\n",
03812 chan->masq, chan->masqr,
03813 chan->_bridge, chan->uniqueid, chan->linkedid);
03814 if (chan->masqr)
03815 ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
03816 chan->masqr->name, chan->masqr->cdr);
03817 if (chan->_bridge)
03818 ast_log(LOG_NOTICE, "CHAN: Bridged to %s\n", chan->_bridge->name);
03819
03820 ast_log(LOG_NOTICE, "===== done ====\n");
03821 }
03822
03823
03824
03825
03826 static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
03827 {
03828 struct ast_cdr *cdr_orig = cdr;
03829 while (cdr) {
03830 if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
03831 return cdr;
03832 cdr = cdr->next;
03833 }
03834 return cdr_orig;
03835 }
03836
03837 static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
03838 {
03839 const char *feature;
03840
03841 if (ast_strlen_zero(features)) {
03842 return;
03843 }
03844
03845 for (feature = features; *feature; feature++) {
03846 switch (*feature) {
03847 case 'T' :
03848 case 't' :
03849 ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
03850 break;
03851 case 'K' :
03852 case 'k' :
03853 ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
03854 break;
03855 case 'H' :
03856 case 'h' :
03857 ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
03858 break;
03859 case 'W' :
03860 case 'w' :
03861 ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
03862 break;
03863 default :
03864 ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
03865 }
03866 }
03867 }
03868
03869 static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
03870 {
03871 if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
03872
03873
03874
03875
03876
03877 return;
03878 }
03879
03880 add_features_datastore(callee, &config->features_callee, &config->features_caller);
03881 }
03882
03883 static void clear_dialed_interfaces(struct ast_channel *chan)
03884 {
03885 struct ast_datastore *di_datastore;
03886
03887 ast_channel_lock(chan);
03888 if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
03889 if (option_debug) {
03890 ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", chan->name);
03891 }
03892 if (!ast_channel_datastore_remove(chan, di_datastore)) {
03893 ast_datastore_free(di_datastore);
03894 }
03895 }
03896 ast_channel_unlock(chan);
03897 }
03898
03899 void ast_bridge_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
03900 {
03901 int dead;
03902 long duration;
03903
03904 ast_channel_lock(chan);
03905 dead = ast_test_flag(chan, AST_FLAG_ZOMBIE) || ast_check_hangup(chan);
03906 ast_channel_unlock(chan);
03907 if (dead) {
03908 return;
03909 }
03910
03911 duration = ast_tvdiff_ms(ast_tvnow(), start);
03912 ast_senddigit_end(chan, digit, duration);
03913 ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n",
03914 digit, chan->name, why, duration);
03915 }
03916
03917
03918
03919
03920
03921
03922
03923
03924
03925
03926
03927
03928
03929 int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
03930 {
03931
03932
03933 struct ast_frame *f;
03934 struct ast_channel *who;
03935 char chan_featurecode[FEATURE_MAX_LEN + 1]="";
03936 char peer_featurecode[FEATURE_MAX_LEN + 1]="";
03937 char orig_channame[AST_CHANNEL_NAME];
03938 char orig_peername[AST_CHANNEL_NAME];
03939 int res;
03940 int diff;
03941 int hasfeatures=0;
03942 int hadfeatures=0;
03943 int autoloopflag;
03944 int sendingdtmfdigit = 0;
03945 int we_disabled_peer_cdr = 0;
03946 struct ast_option_header *aoh;
03947 struct ast_cdr *bridge_cdr = NULL;
03948 struct ast_cdr *chan_cdr = chan->cdr;
03949 struct ast_cdr *peer_cdr = peer->cdr;
03950 struct ast_cdr *new_chan_cdr = NULL;
03951 struct ast_cdr *new_peer_cdr = NULL;
03952 struct ast_silence_generator *silgen = NULL;
03953 const char *h_context;
03954
03955 pbx_builtin_setvar_helper(chan, "BRIDGEPEER", peer->name);
03956 pbx_builtin_setvar_helper(peer, "BRIDGEPEER", chan->name);
03957
03958
03959 pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
03960 pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", NULL);
03961
03962 set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
03963 add_features_datastores(chan, peer, config);
03964
03965
03966
03967
03968 if (chan->_state == AST_STATE_RINGING && peer->visible_indication != AST_CONTROL_RINGING) {
03969 ast_indicate(peer, AST_CONTROL_RINGING);
03970 }
03971
03972 if (monitor_ok) {
03973 const char *monitor_exec;
03974 struct ast_channel *src = NULL;
03975 if (!monitor_app) {
03976 if (!(monitor_app = pbx_findapp("Monitor")))
03977 monitor_ok=0;
03978 }
03979 if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR")))
03980 src = chan;
03981 else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
03982 src = peer;
03983 if (monitor_app && src) {
03984 char *tmp = ast_strdupa(monitor_exec);
03985 pbx_exec(src, monitor_app, tmp);
03986 }
03987 }
03988
03989 set_config_flags(chan, peer, config);
03990
03991
03992 if (chan->_state != AST_STATE_UP) {
03993 if (ast_raw_answer(chan, 1)) {
03994 return -1;
03995 }
03996 }
03997
03998 #ifdef FOR_DEBUG
03999
04000 ast_channel_log("Pre-bridge CHAN Channel info", chan);
04001 ast_channel_log("Pre-bridge PEER Channel info", peer);
04002 #endif
04003
04004 ast_channel_set_linkgroup(chan,peer);
04005
04006
04007 if (chan->cdr && peer->cdr && !ast_strlen_zero(peer->cdr->userfield)) {
04008 char tmp[256];
04009
04010 ast_channel_lock(chan);
04011 if (!ast_strlen_zero(chan->cdr->userfield)) {
04012 snprintf(tmp, sizeof(tmp), "%s;%s", chan->cdr->userfield, peer->cdr->userfield);
04013 ast_cdr_appenduserfield(chan, tmp);
04014 } else {
04015 ast_cdr_setuserfield(chan, peer->cdr->userfield);
04016 }
04017 ast_channel_unlock(chan);
04018
04019 ast_set_flag(peer->cdr, AST_CDR_FLAG_POST_DISABLED);
04020 we_disabled_peer_cdr = 1;
04021 }
04022 ast_copy_string(orig_channame,chan->name,sizeof(orig_channame));
04023 ast_copy_string(orig_peername,peer->name,sizeof(orig_peername));
04024
04025 if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
04026 ast_channel_lock_both(chan, peer);
04027 if (chan_cdr) {
04028 ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
04029 ast_cdr_update(chan);
04030 bridge_cdr = ast_cdr_dup_unique_swap(chan_cdr);
04031
04032
04033 bridge_cdr->next = chan_cdr->next;
04034 chan_cdr->next = NULL;
04035 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
04036 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
04037 if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
04038 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
04039 }
04040 ast_cdr_setaccount(peer, chan->accountcode);
04041 } else {
04042
04043 bridge_cdr = ast_cdr_alloc();
04044 ast_copy_string(bridge_cdr->channel, chan->name, sizeof(bridge_cdr->channel));
04045 ast_copy_string(bridge_cdr->dstchannel, peer->name, sizeof(bridge_cdr->dstchannel));
04046 ast_copy_string(bridge_cdr->uniqueid, chan->uniqueid, sizeof(bridge_cdr->uniqueid));
04047 ast_copy_string(bridge_cdr->lastapp, S_OR(chan->appl, ""), sizeof(bridge_cdr->lastapp));
04048 ast_copy_string(bridge_cdr->lastdata, S_OR(chan->data, ""), sizeof(bridge_cdr->lastdata));
04049 ast_cdr_setcid(bridge_cdr, chan);
04050 bridge_cdr->disposition = (chan->_state == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
04051 bridge_cdr->amaflags = chan->amaflags ? chan->amaflags : ast_default_amaflags;
04052 ast_copy_string(bridge_cdr->accountcode, chan->accountcode, sizeof(bridge_cdr->accountcode));
04053
04054 ast_copy_string(bridge_cdr->dst, chan->exten, sizeof(bridge_cdr->dst));
04055 ast_copy_string(bridge_cdr->dcontext, chan->context, sizeof(bridge_cdr->dcontext));
04056 if (peer_cdr) {
04057 bridge_cdr->start = peer_cdr->start;
04058 ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
04059 } else {
04060 ast_cdr_start(bridge_cdr);
04061 }
04062 }
04063 ast_channel_unlock(chan);
04064 ast_channel_unlock(peer);
04065
04066 ast_debug(4,"bridge answer set, chan answer set\n");
04067
04068
04069
04070
04071
04072
04073
04074
04075
04076
04077
04078
04079
04080
04081
04082 if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
04083 ast_cdr_setanswer(bridge_cdr, peer_cdr->answer);
04084 ast_cdr_setdisposition(bridge_cdr, peer_cdr->disposition);
04085 if (chan_cdr) {
04086 ast_cdr_setanswer(chan_cdr, peer_cdr->answer);
04087 ast_cdr_setdisposition(chan_cdr, peer_cdr->disposition);
04088 }
04089 } else {
04090 ast_cdr_answer(bridge_cdr);
04091 if (chan_cdr) {
04092 ast_cdr_answer(chan_cdr);
04093 }
04094 }
04095 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
04096 if (chan_cdr) {
04097 ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
04098 }
04099 if (peer_cdr) {
04100 ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
04101 }
04102 }
04103
04104
04105
04106
04107 ast_clear_flag(bridge_cdr, AST_CDR_FLAG_DIALED);
04108 }
04109 ast_cel_report_event(chan, AST_CEL_BRIDGE_START, NULL, NULL, peer);
04110
04111
04112
04113
04114 clear_dialed_interfaces(chan);
04115 clear_dialed_interfaces(peer);
04116
04117 for (;;) {
04118 struct ast_channel *other;
04119
04120 res = ast_channel_bridge(chan, peer, config, &f, &who);
04121
04122 if (ast_test_flag(chan, AST_FLAG_ZOMBIE)
04123 || ast_test_flag(peer, AST_FLAG_ZOMBIE)) {
04124
04125 res = -1;
04126 if (f) {
04127 ast_frfree(f);
04128 }
04129 goto before_you_go;
04130 }
04131
04132
04133
04134
04135
04136
04137
04138
04139 if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
04140
04141 diff = ast_tvdiff_ms(ast_tvnow(), config->feature_start_time);
04142 if (res == AST_BRIDGE_RETRY) {
04143
04144
04145
04146 config->feature_timer = -1;
04147 } else {
04148 config->feature_timer -= diff;
04149 }
04150
04151 if (hasfeatures) {
04152 if (config->feature_timer <= 0) {
04153
04154
04155 ast_debug(1, "Timed out for feature!\n");
04156 if (!ast_strlen_zero(peer_featurecode)) {
04157 ast_dtmf_stream(chan, peer, peer_featurecode, 0, 0);
04158 memset(peer_featurecode, 0, sizeof(peer_featurecode));
04159 }
04160 if (!ast_strlen_zero(chan_featurecode)) {
04161 ast_dtmf_stream(peer, chan, chan_featurecode, 0, 0);
04162 memset(chan_featurecode, 0, sizeof(chan_featurecode));
04163 }
04164 if (f)
04165 ast_frfree(f);
04166 hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
04167 if (!hasfeatures) {
04168
04169 config->feature_timer = 0;
04170 }
04171 hadfeatures = hasfeatures;
04172
04173 continue;
04174 } else if (!f) {
04175
04176
04177
04178 continue;
04179 }
04180 } else {
04181 if (config->feature_timer <=0) {
04182
04183 config->feature_timer = 0;
04184 who = chan;
04185 if (f)
04186 ast_frfree(f);
04187 f = NULL;
04188 res = 0;
04189 }
04190 }
04191 }
04192 if (res < 0) {
04193 if (!ast_test_flag(chan, AST_FLAG_ZOMBIE) && !ast_test_flag(peer, AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer)) {
04194 ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", chan->name, peer->name);
04195 }
04196 goto before_you_go;
04197 }
04198
04199 if (!f || (f->frametype == AST_FRAME_CONTROL &&
04200 (f->subclass.integer == AST_CONTROL_HANGUP || f->subclass.integer == AST_CONTROL_BUSY ||
04201 f->subclass.integer == AST_CONTROL_CONGESTION))) {
04202
04203
04204
04205
04206
04207
04208 ast_channel_lock(chan);
04209 if (chan->_softhangup & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE)) {
04210 ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
04211 }
04212 ast_channel_unlock(chan);
04213 res = -1;
04214 break;
04215 }
04216
04217 other = (who == chan) ? peer : chan;
04218 if (f->frametype == AST_FRAME_CONTROL) {
04219 switch (f->subclass.integer) {
04220 case AST_CONTROL_RINGING:
04221 case AST_CONTROL_FLASH:
04222 case -1:
04223 ast_indicate(other, f->subclass.integer);
04224 break;
04225 case AST_CONTROL_CONNECTED_LINE:
04226 if (!ast_channel_connected_line_macro(who, other, f, who != chan, 1)) {
04227 break;
04228 }
04229 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04230 break;
04231 case AST_CONTROL_REDIRECTING:
04232 if (!ast_channel_redirecting_macro(who, other, f, who != chan, 1)) {
04233 break;
04234 }
04235 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04236 break;
04237 case AST_CONTROL_AOC:
04238 case AST_CONTROL_HOLD:
04239 case AST_CONTROL_UNHOLD:
04240 ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
04241 break;
04242 case AST_CONTROL_OPTION:
04243 aoh = f->data.ptr;
04244
04245
04246
04247
04248 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
04249 switch (ntohs(aoh->option)) {
04250 case AST_OPTION_TONE_VERIFY:
04251 case AST_OPTION_TDD:
04252 case AST_OPTION_RELAXDTMF:
04253 case AST_OPTION_AUDIO_MODE:
04254 case AST_OPTION_DIGIT_DETECT:
04255 case AST_OPTION_FAX_DETECT:
04256 ast_channel_setoption(other, ntohs(aoh->option), aoh->data,
04257 f->datalen - sizeof(struct ast_option_header), 0);
04258 }
04259 }
04260 break;
04261 }
04262 } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
04263 struct ast_flags *cfg;
04264 char dtmfcode[2] = { f->subclass.integer, };
04265 size_t featurelen;
04266
04267 if (who == chan) {
04268 featurelen = strlen(chan_featurecode);
04269 cfg = &(config->features_caller);
04270 } else {
04271 featurelen = strlen(peer_featurecode);
04272 cfg = &(config->features_callee);
04273 }
04274
04275
04276
04277 if (featurelen == 0
04278 && feature_check(chan, cfg, &dtmfcode[0]) == AST_FEATURE_RETURN_PASSDIGITS) {
04279 if (option_debug > 3) {
04280 ast_log(LOG_DEBUG, "Passing DTMF through, since it is not a feature code\n");
04281 }
04282 ast_write(other, f);
04283 sendingdtmfdigit = 1;
04284 } else {
04285
04286
04287
04288 if (!silgen && ast_opt_transmit_silence) {
04289 silgen = ast_channel_start_silence_generator(other);
04290 }
04291 if (option_debug > 3) {
04292 ast_log(LOG_DEBUG, "Not passing DTMF through, since it may be a feature code\n");
04293 }
04294 }
04295 } else if (f->frametype == AST_FRAME_DTMF_END) {
04296 char *featurecode;
04297 int sense;
04298
04299 hadfeatures = hasfeatures;
04300
04301 if (who == chan) {
04302 sense = FEATURE_SENSE_CHAN;
04303 featurecode = chan_featurecode;
04304 } else {
04305 sense = FEATURE_SENSE_PEER;
04306 featurecode = peer_featurecode;
04307 }
04308
04309 if (sendingdtmfdigit == 1) {
04310
04311
04312 ast_write(other, f);
04313 sendingdtmfdigit = 0;
04314 } else {
04315
04316
04317
04318
04319 featurecode[strlen(featurecode)] = f->subclass.integer;
04320
04321 ast_frfree(f);
04322 f = NULL;
04323 if (silgen) {
04324 ast_channel_stop_silence_generator(other, silgen);
04325 silgen = NULL;
04326 }
04327 config->feature_timer = 0;
04328 res = feature_interpret(chan, peer, config, featurecode, sense);
04329 switch(res) {
04330 case AST_FEATURE_RETURN_PASSDIGITS:
04331 ast_dtmf_stream(other, who, featurecode, 0, 0);
04332
04333 case AST_FEATURE_RETURN_SUCCESS:
04334 memset(featurecode, 0, sizeof(chan_featurecode));
04335 break;
04336 }
04337 if (res >= AST_FEATURE_RETURN_PASSDIGITS) {
04338 res = 0;
04339 } else {
04340 break;
04341 }
04342 hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
04343 if (hadfeatures && !hasfeatures) {
04344
04345 config->feature_timer = 0;
04346 } else if (hasfeatures) {
04347 if (config->timelimit) {
04348
04349 ast_set_flag(config, AST_FEATURE_WARNING_ACTIVE);
04350 }
04351 config->feature_start_time = ast_tvnow();
04352 config->feature_timer = featuredigittimeout;
04353 ast_debug(1, "Set feature timer to %ld ms\n", config->feature_timer);
04354 }
04355 }
04356 }
04357 if (f)
04358 ast_frfree(f);
04359 }
04360 ast_cel_report_event(chan, AST_CEL_BRIDGE_END, NULL, NULL, peer);
04361
04362 before_you_go:
04363 if (chan->sending_dtmf_digit) {
04364 ast_bridge_end_dtmf(chan, chan->sending_dtmf_digit, chan->sending_dtmf_tv,
04365 "bridge end");
04366 }
04367 if (peer->sending_dtmf_digit) {
04368 ast_bridge_end_dtmf(peer, peer->sending_dtmf_digit, peer->sending_dtmf_tv,
04369 "bridge end");
04370 }
04371
04372
04373 if (silgen) {
04374 ast_channel_stop_silence_generator(who == chan ? peer : chan, silgen);
04375 silgen = NULL;
04376 }
04377
04378 if (ast_test_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT)) {
04379 ast_clear_flag(chan,AST_FLAG_BRIDGE_HANGUP_DONT);
04380 if (bridge_cdr) {
04381 ast_cdr_discard(bridge_cdr);
04382
04383 }
04384 return res;
04385 }
04386
04387 if (config->end_bridge_callback) {
04388 config->end_bridge_callback(config->end_bridge_callback_data);
04389 }
04390
04391
04392
04393
04394
04395 if (ast_test_flag(&config->features_caller, AST_FEATURE_NO_H_EXTEN)) {
04396 h_context = NULL;
04397 } else if (ast_exists_extension(chan, chan->context, "h", 1,
04398 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
04399 h_context = chan->context;
04400 } else if (!ast_strlen_zero(chan->macrocontext)
04401 && ast_exists_extension(chan, chan->macrocontext, "h", 1,
04402 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL))) {
04403 h_context = chan->macrocontext;
04404 } else {
04405 h_context = NULL;
04406 }
04407 if (h_context) {
04408 struct ast_cdr *swapper = NULL;
04409 char savelastapp[AST_MAX_EXTENSION];
04410 char savelastdata[AST_MAX_EXTENSION];
04411 char save_context[AST_MAX_CONTEXT];
04412 char save_exten[AST_MAX_EXTENSION];
04413 int save_prio;
04414 int found = 0;
04415 int spawn_error = 0;
04416
04417
04418
04419
04420
04421 ast_softhangup(chan, AST_SOFTHANGUP_APPUNLOAD);
04422
04423 autoloopflag = ast_test_flag(chan, AST_FLAG_IN_AUTOLOOP);
04424 ast_set_flag(chan, AST_FLAG_IN_AUTOLOOP);
04425 if (bridge_cdr && ast_opt_end_cdr_before_h_exten) {
04426 ast_cdr_end(bridge_cdr);
04427 }
04428
04429
04430
04431 ast_channel_lock(chan);
04432 if (bridge_cdr) {
04433 swapper = chan->cdr;
04434 ast_copy_string(savelastapp, bridge_cdr->lastapp, sizeof(bridge_cdr->lastapp));
04435 ast_copy_string(savelastdata, bridge_cdr->lastdata, sizeof(bridge_cdr->lastdata));
04436 chan->cdr = bridge_cdr;
04437 }
04438 ast_copy_string(save_context, chan->context, sizeof(save_context));
04439 ast_copy_string(save_exten, chan->exten, sizeof(save_exten));
04440 save_prio = chan->priority;
04441 if (h_context != chan->context) {
04442 ast_copy_string(chan->context, h_context, sizeof(chan->context));
04443 }
04444 ast_copy_string(chan->exten, "h", sizeof(chan->exten));
04445 chan->priority = 1;
04446 ast_channel_unlock(chan);
04447
04448 while ((spawn_error = ast_spawn_extension(chan, chan->context, chan->exten,
04449 chan->priority,
04450 S_COR(chan->caller.id.number.valid, chan->caller.id.number.str, NULL),
04451 &found, 1)) == 0) {
04452 chan->priority++;
04453 }
04454 if (found && spawn_error) {
04455
04456 ast_debug(1, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
04457 ast_verb(2, "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", chan->context, chan->exten, chan->priority, chan->name);
04458 }
04459
04460
04461 ast_channel_lock(chan);
04462 ast_copy_string(chan->context, save_context, sizeof(chan->context));
04463 ast_copy_string(chan->exten, save_exten, sizeof(chan->exten));
04464 chan->priority = save_prio;
04465 if (bridge_cdr) {
04466 if (chan->cdr == bridge_cdr) {
04467 chan->cdr = swapper;
04468 } else {
04469 bridge_cdr = NULL;
04470 }
04471 }
04472
04473 ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_RUN);
04474 ast_channel_unlock(chan);
04475
04476
04477 if (bridge_cdr) {
04478 ast_copy_string(bridge_cdr->lastapp, savelastapp, sizeof(bridge_cdr->lastapp));
04479 ast_copy_string(bridge_cdr->lastdata, savelastdata, sizeof(bridge_cdr->lastdata));
04480 }
04481 ast_set2_flag(chan, autoloopflag, AST_FLAG_IN_AUTOLOOP);
04482 }
04483
04484
04485 new_chan_cdr = pick_unlocked_cdr(chan->cdr);
04486
04487
04488
04489 if (new_chan_cdr && bridge_cdr && !h_context) {
04490 ast_cdr_copy_vars(bridge_cdr, new_chan_cdr);
04491 ast_copy_string(bridge_cdr->userfield, new_chan_cdr->userfield, sizeof(bridge_cdr->userfield));
04492 bridge_cdr->amaflags = new_chan_cdr->amaflags;
04493 ast_copy_string(bridge_cdr->accountcode, new_chan_cdr->accountcode, sizeof(bridge_cdr->accountcode));
04494 if (ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED)) {
04495 ast_set_flag(bridge_cdr, AST_CDR_FLAG_POST_DISABLED);
04496 }
04497 }
04498
04499
04500 if (bridge_cdr) {
04501 ast_cdr_end(bridge_cdr);
04502 ast_cdr_detach(bridge_cdr);
04503 }
04504
04505
04506
04507
04508
04509
04510
04511
04512
04513
04514
04515
04516
04517
04518
04519
04520
04521
04522
04523
04524
04525
04526
04527
04528
04529 if (new_chan_cdr) {
04530 struct ast_channel *chan_ptr = NULL;
04531
04532 if (strcasecmp(orig_channame, chan->name) != 0) {
04533
04534 if ((chan_ptr = ast_channel_get_by_name(orig_channame))) {
04535 ast_channel_lock(chan_ptr);
04536 if (!ast_bridged_channel(chan_ptr)) {
04537 struct ast_cdr *cur;
04538 for (cur = chan_ptr->cdr; cur; cur = cur->next) {
04539 if (cur == chan_cdr) {
04540 break;
04541 }
04542 }
04543 if (cur) {
04544 ast_cdr_specialized_reset(chan_cdr, 0);
04545 }
04546 }
04547 ast_channel_unlock(chan_ptr);
04548 chan_ptr = ast_channel_unref(chan_ptr);
04549 }
04550
04551 ast_cdr_specialized_reset(new_chan_cdr, 0);
04552 } else {
04553 ast_cdr_specialized_reset(chan->cdr, 0);
04554 }
04555 }
04556
04557 {
04558 struct ast_channel *chan_ptr = NULL;
04559 new_peer_cdr = pick_unlocked_cdr(peer->cdr);
04560 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))
04561 ast_set_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED);
04562 if (strcasecmp(orig_peername, peer->name) != 0) {
04563
04564 if ((chan_ptr = ast_channel_get_by_name(orig_peername))) {
04565 ast_channel_lock(chan_ptr);
04566 if (!ast_bridged_channel(chan_ptr)) {
04567 struct ast_cdr *cur;
04568 for (cur = chan_ptr->cdr; cur; cur = cur->next) {
04569 if (cur == peer_cdr) {
04570 break;
04571 }
04572 }
04573 if (cur) {
04574 ast_cdr_specialized_reset(peer_cdr, 0);
04575 }
04576 }
04577 ast_channel_unlock(chan_ptr);
04578 chan_ptr = ast_channel_unref(chan_ptr);
04579 }
04580
04581 if (new_peer_cdr) {
04582 ast_cdr_specialized_reset(new_peer_cdr, 0);
04583 }
04584 } else {
04585 if (we_disabled_peer_cdr) {
04586 ast_clear_flag(peer->cdr, AST_CDR_FLAG_POST_DISABLED);
04587 }
04588 ast_cdr_specialized_reset(peer->cdr, 0);
04589 }
04590 }
04591
04592 return res;
04593 }
04594
04595
04596 static void post_manager_event(const char *s, struct parkeduser *pu)
04597 {
04598 manager_event(EVENT_FLAG_CALL, s,
04599 "Exten: %s\r\n"
04600 "Channel: %s\r\n"
04601 "Parkinglot: %s\r\n"
04602 "CallerIDNum: %s\r\n"
04603 "CallerIDName: %s\r\n"
04604 "ConnectedLineNum: %s\r\n"
04605 "ConnectedLineName: %s\r\n"
04606 "UniqueID: %s\r\n",
04607 pu->parkingexten,
04608 pu->chan->name,
04609 pu->parkinglot->name,
04610 S_COR(pu->chan->caller.id.number.valid, pu->chan->caller.id.number.str, "<unknown>"),
04611 S_COR(pu->chan->caller.id.name.valid, pu->chan->caller.id.name.str, "<unknown>"),
04612 S_COR(pu->chan->connected.id.number.valid, pu->chan->connected.id.number.str, "<unknown>"),
04613 S_COR(pu->chan->connected.id.name.valid, pu->chan->connected.id.name.str, "<unknown>"),
04614 pu->chan->uniqueid
04615 );
04616 }
04617
04618 static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
04619 {
04620 int i = 0;
04621 enum {
04622 OPT_CALLEE_REDIRECT = 't',
04623 OPT_CALLER_REDIRECT = 'T',
04624 OPT_CALLEE_AUTOMON = 'w',
04625 OPT_CALLER_AUTOMON = 'W',
04626 OPT_CALLEE_DISCONNECT = 'h',
04627 OPT_CALLER_DISCONNECT = 'H',
04628 OPT_CALLEE_PARKCALL = 'k',
04629 OPT_CALLER_PARKCALL = 'K',
04630 };
04631
04632 memset(options, 0, len);
04633 if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
04634 options[i++] = OPT_CALLER_REDIRECT;
04635 }
04636 if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
04637 options[i++] = OPT_CALLER_AUTOMON;
04638 }
04639 if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
04640 options[i++] = OPT_CALLER_DISCONNECT;
04641 }
04642 if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
04643 options[i++] = OPT_CALLER_PARKCALL;
04644 }
04645
04646 if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
04647 options[i++] = OPT_CALLEE_REDIRECT;
04648 }
04649 if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
04650 options[i++] = OPT_CALLEE_AUTOMON;
04651 }
04652 if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
04653 options[i++] = OPT_CALLEE_DISCONNECT;
04654 }
04655 if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
04656 options[i++] = OPT_CALLEE_PARKCALL;
04657 }
04658
04659 return options;
04660 }
04661
04662
04663
04664
04665
04666
04667
04668
04669
04670 static int manage_parked_call(struct parkeduser *pu, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
04671 {
04672 struct ast_channel *chan = pu->chan;
04673 int tms;
04674 int x;
04675
04676 tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
04677 if (tms > pu->parkingtime) {
04678
04679
04680
04681
04682 switch (pu->hold_method) {
04683 case AST_CONTROL_HOLD:
04684 ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
04685 break;
04686 case AST_CONTROL_RINGING:
04687 ast_indicate(pu->chan, -1);
04688 break;
04689 default:
04690 break;
04691 }
04692 pu->hold_method = 0;
04693
04694
04695 if (pu->peername[0]) {
04696 char *peername;
04697 char *dash;
04698 char *peername_flat;
04699 int i;
04700
04701 peername = ast_strdupa(pu->peername);
04702 dash = strrchr(peername, '-');
04703 if (dash) {
04704 *dash = '\0';
04705 }
04706
04707 peername_flat = ast_strdupa(peername);
04708 for (i = 0; peername_flat[i]; i++) {
04709 if (peername_flat[i] == '/') {
04710 peername_flat[i] = '_';
04711 }
04712 }
04713
04714 if (!ast_context_find_or_create(NULL, NULL, parking_con_dial, registrar)) {
04715 ast_log(LOG_ERROR,
04716 "Parking dial context '%s' does not exist and unable to create\n",
04717 parking_con_dial);
04718 } else {
04719 char returnexten[AST_MAX_EXTENSION];
04720 struct ast_datastore *features_datastore;
04721 struct ast_dial_features *dialfeatures;
04722
04723 if (!strncmp(peername, "Parked/", 7)) {
04724 peername += 7;
04725 }
04726
04727 ast_channel_lock(chan);
04728 features_datastore = ast_channel_datastore_find(chan, &dial_features_info,
04729 NULL);
04730 if (features_datastore && (dialfeatures = features_datastore->data)) {
04731 char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
04732
04733 snprintf(returnexten, sizeof(returnexten), "%s,30,%s", peername,
04734 callback_dialoptions(&dialfeatures->peer_features,
04735 &dialfeatures->my_features, buf, sizeof(buf)));
04736 } else {
04737 ast_log(LOG_NOTICE, "Dial features not found on %s, using default!\n",
04738 chan->name);
04739 snprintf(returnexten, sizeof(returnexten), "%s,30,t", peername);
04740 }
04741 ast_channel_unlock(chan);
04742
04743 if (ast_add_extension(parking_con_dial, 1, peername_flat, 1, NULL, NULL,
04744 "Dial", ast_strdup(returnexten), ast_free_ptr, registrar)) {
04745 ast_log(LOG_ERROR,
04746 "Could not create parking return dial exten: %s@%s\n",
04747 peername_flat, parking_con_dial);
04748 }
04749 }
04750 if (pu->options_specified) {
04751
04752
04753
04754
04755 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
04756 } else if (comebacktoorigin) {
04757 set_c_e_p(chan, parking_con_dial, peername_flat, 1);
04758 } else {
04759 char parkingslot[AST_MAX_EXTENSION];
04760
04761 snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
04762 pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
04763 set_c_e_p(chan, "parkedcallstimeout", peername_flat, 1);
04764 }
04765 } else {
04766
04767
04768
04769
04770
04771 set_c_e_p(chan, pu->context, pu->exten, pu->priority);
04772 }
04773 post_manager_event("ParkedCallTimeOut", pu);
04774 ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallTimeOut", NULL);
04775
04776 ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n",
04777 pu->chan->name, pu->parkingnum, pu->parkinglot->name, pu->chan->context,
04778 pu->chan->exten, pu->chan->priority);
04779
04780
04781 if (ast_pbx_start(chan)) {
04782 ast_log(LOG_WARNING,
04783 "Unable to restart the PBX for user on '%s', hanging them up...\n",
04784 pu->chan->name);
04785 ast_hangup(chan);
04786 }
04787
04788
04789 return 1;
04790 }
04791
04792
04793 if (pfds) {
04794 for (x = 0; x < AST_MAX_FDS; x++) {
04795 struct ast_frame *f;
04796 int y;
04797
04798 if (chan->fds[x] == -1) {
04799 continue;
04800 }
04801
04802 for (y = 0; y < nfds; y++) {
04803 if (pfds[y].fd == chan->fds[x]) {
04804
04805 break;
04806 }
04807 }
04808 if (y == nfds) {
04809
04810 continue;
04811 }
04812
04813 if (!(pfds[y].revents & (POLLIN | POLLERR | POLLPRI))) {
04814
04815 continue;
04816 }
04817
04818 if (pfds[y].revents & POLLPRI) {
04819 ast_set_flag(chan, AST_FLAG_EXCEPTION);
04820 } else {
04821 ast_clear_flag(chan, AST_FLAG_EXCEPTION);
04822 }
04823 chan->fdno = x;
04824
04825
04826 f = ast_read(pu->chan);
04827
04828 if (!f || (f->frametype == AST_FRAME_CONTROL
04829 && f->subclass.integer == AST_CONTROL_HANGUP)) {
04830 if (f) {
04831 ast_frfree(f);
04832 }
04833 post_manager_event("ParkedCallGiveUp", pu);
04834 ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallGiveUp",
04835 NULL);
04836
04837
04838 ast_verb(2, "%s got tired of being parked\n", chan->name);
04839 ast_hangup(chan);
04840
04841
04842 return 1;
04843 } else {
04844
04845 ast_frfree(f);
04846 if (pu->hold_method == AST_CONTROL_HOLD
04847 && pu->moh_trys < 3
04848 && !chan->generatordata) {
04849 ast_debug(1,
04850 "MOH on parked call stopped by outside source. Restarting on channel %s.\n",
04851 chan->name);
04852 ast_indicate_data(chan, AST_CONTROL_HOLD,
04853 S_OR(pu->parkinglot->cfg.mohclass, NULL),
04854 (!ast_strlen_zero(pu->parkinglot->cfg.mohclass)
04855 ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0));
04856 pu->moh_trys++;
04857 }
04858 break;
04859 }
04860 }
04861 }
04862
04863
04864 for (x = 0; x < AST_MAX_FDS; x++) {
04865 if (chan->fds[x] > -1) {
04866 void *tmp = ast_realloc(*new_pfds,
04867 (*new_nfds + 1) * sizeof(struct pollfd));
04868
04869 if (!tmp) {
04870 continue;
04871 }
04872 *new_pfds = tmp;
04873 (*new_pfds)[*new_nfds].fd = chan->fds[x];
04874 (*new_pfds)[*new_nfds].events = POLLIN | POLLERR | POLLPRI;
04875 (*new_pfds)[*new_nfds].revents = 0;
04876 (*new_nfds)++;
04877 }
04878 }
04879
04880 if (tms < *ms || *ms < 0) {
04881 *ms = tms;
04882 }
04883
04884
04885 return 0;
04886 }
04887
04888
04889 static void manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
04890 {
04891 struct parkeduser *pu;
04892 struct ast_context *con;
04893
04894
04895 AST_LIST_LOCK(&curlot->parkings);
04896 AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
04897 if (pu->notquiteyet) {
04898 continue;
04899 }
04900 if (manage_parked_call(pu, pfds, nfds, new_pfds, new_nfds, ms)) {
04901
04902 con = ast_context_find(pu->parkinglot->cfg.parking_con);
04903 if (con) {
04904 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
04905 ast_log(LOG_WARNING,
04906 "Whoa, failed to remove the parking extension %s@%s!\n",
04907 pu->parkingexten, pu->parkinglot->cfg.parking_con);
04908 }
04909 notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con,
04910 AST_DEVICE_NOT_INUSE);
04911 } else {
04912 ast_log(LOG_WARNING,
04913 "Whoa, parking lot '%s' context '%s' does not exist.\n",
04914 pu->parkinglot->name, pu->parkinglot->cfg.parking_con);
04915 }
04916 AST_LIST_REMOVE_CURRENT(list);
04917 parkinglot_unref(pu->parkinglot);
04918 ast_free(pu);
04919 }
04920 }
04921 AST_LIST_TRAVERSE_SAFE_END;
04922 AST_LIST_UNLOCK(&curlot->parkings);
04923 }
04924
04925
04926
04927
04928
04929
04930
04931
04932
04933 static void *do_parking_thread(void *ignore)
04934 {
04935 struct pollfd *pfds = NULL, *new_pfds = NULL;
04936 int nfds = 0, new_nfds = 0;
04937
04938 for (;;) {
04939 struct ao2_iterator iter;
04940 struct ast_parkinglot *curlot;
04941 int ms = -1;
04942
04943 iter = ao2_iterator_init(parkinglots, 0);
04944 while ((curlot = ao2_iterator_next(&iter))) {
04945 manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
04946 ao2_ref(curlot, -1);
04947 }
04948 ao2_iterator_destroy(&iter);
04949
04950
04951 ast_free(pfds);
04952 pfds = new_pfds;
04953 nfds = new_nfds;
04954 new_pfds = NULL;
04955 new_nfds = 0;
04956
04957
04958 ast_poll(pfds, nfds, ms);
04959 pthread_testcancel();
04960 }
04961
04962 return NULL;
04963 }
04964
04965
04966 static struct ast_parkinglot *find_parkinglot(const char *name)
04967 {
04968 struct ast_parkinglot *parkinglot;
04969
04970 if (ast_strlen_zero(name)) {
04971 return NULL;
04972 }
04973
04974 parkinglot = ao2_find(parkinglots, (void *) name, 0);
04975 if (parkinglot) {
04976 ast_debug(1, "Found Parking lot: %s\n", parkinglot->name);
04977 }
04978
04979 return parkinglot;
04980 }
04981
04982
04983 static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot)
04984 {
04985 struct ast_parkinglot *copylot;
04986
04987 if ((copylot = find_parkinglot(name))) {
04988 ao2_ref(copylot, -1);
04989 return NULL;
04990 }
04991
04992 copylot = create_parkinglot(name);
04993 if (!copylot) {
04994 return NULL;
04995 }
04996
04997 ast_debug(1, "Building parking lot %s\n", name);
04998
04999
05000 copylot->cfg = parkinglot->cfg;
05001
05002 return copylot;
05003 }
05004
05005 AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
05006 AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
05007 AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
05008 AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
05009 END_OPTIONS );
05010
05011
05012 static int park_call_exec(struct ast_channel *chan, const char *data)
05013 {
05014 struct ast_park_call_args args = { 0, };
05015 struct ast_flags flags = { 0 };
05016 char orig_exten[AST_MAX_EXTENSION];
05017 int orig_priority;
05018 int res;
05019 const char *pl_name;
05020 char *parse;
05021 struct park_app_args app_args;
05022
05023
05024
05025
05026
05027
05028
05029
05030
05031
05032
05033 args.orig_chan_name = ast_strdupa(S_OR(
05034 pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"), chan->name));
05035
05036
05037 if (chan->_state != AST_STATE_UP) {
05038 if (ast_answer(chan)) {
05039 return -1;
05040 }
05041
05042
05043 if (ast_safe_sleep(chan, 1000)) {
05044 return -1;
05045 }
05046 }
05047
05048
05049 parse = ast_strdupa(data);
05050 AST_STANDARD_APP_ARGS(app_args, parse);
05051
05052 if (!ast_strlen_zero(app_args.timeout)) {
05053 if (sscanf(app_args.timeout, "%30d", &args.timeout) != 1) {
05054 ast_log(LOG_WARNING, "Invalid timeout '%s' provided\n", app_args.timeout);
05055 args.timeout = 0;
05056 }
05057 }
05058 if (!ast_strlen_zero(app_args.return_con)) {
05059 args.return_con = app_args.return_con;
05060 }
05061 if (!ast_strlen_zero(app_args.return_ext)) {
05062 args.return_ext = app_args.return_ext;
05063 }
05064 if (!ast_strlen_zero(app_args.return_pri)) {
05065 if (sscanf(app_args.return_pri, "%30d", &args.return_pri) != 1) {
05066 ast_log(LOG_WARNING, "Invalid priority '%s' specified\n", app_args.return_pri);
05067 args.return_pri = 0;
05068 }
05069 }
05070
05071 ast_app_parse_options(park_call_options, &flags, NULL, app_args.options);
05072 args.flags = flags.flags;
05073
05074
05075
05076
05077
05078 ast_copy_string(orig_exten, chan->exten, sizeof(orig_exten));
05079 orig_priority = chan->priority;
05080 strcpy(chan->exten, "s");
05081 chan->priority = 1;
05082
05083
05084 if (!ast_strlen_zero(app_args.pl_name)) {
05085 pl_name = app_args.pl_name;
05086 } else {
05087 pl_name = findparkinglotname(chan);
05088 }
05089 if (ast_strlen_zero(pl_name)) {
05090
05091 args.parkinglot = parkinglot_addref(default_parkinglot);
05092 } else {
05093 args.parkinglot = find_parkinglot(pl_name);
05094 if (!args.parkinglot && parkeddynamic) {
05095 args.parkinglot = create_dynamic_parkinglot(pl_name, chan);
05096 }
05097 }
05098 if (args.parkinglot) {
05099 res = masq_park_call(chan, chan, &args);
05100 parkinglot_unref(args.parkinglot);
05101 } else {
05102
05103 if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
05104 ast_stream_and_wait(chan, "pbx-parkingfailed", "");
05105 }
05106 res = -1;
05107 }
05108 if (res) {
05109
05110 ast_copy_string(chan->exten, orig_exten, sizeof(chan->exten));
05111 chan->priority = orig_priority;
05112 res = 0;
05113 } else {
05114
05115 res = -1;
05116 }
05117
05118 return res;
05119 }
05120
05121
05122 static int parked_call_exec(struct ast_channel *chan, const char *data)
05123 {
05124 int res;
05125 struct ast_channel *peer = NULL;
05126 struct parkeduser *pu;
05127 struct ast_context *con;
05128 char *parse;
05129 const char *pl_name;
05130 int park = 0;
05131 struct ast_bridge_config config;
05132 struct ast_parkinglot *parkinglot;
05133 AST_DECLARE_APP_ARGS(app_args,
05134 AST_APP_ARG(pl_space);
05135 AST_APP_ARG(pl_name);
05136 AST_APP_ARG(dummy);
05137 );
05138
05139 parse = ast_strdupa(data);
05140 AST_STANDARD_APP_ARGS(app_args, parse);
05141
05142 if (!ast_strlen_zero(app_args.pl_space)) {
05143 if (sscanf(app_args.pl_space, "%30u", &park) != 1) {
05144 ast_log(LOG_WARNING, "Specified parking extension not a number: %s\n",
05145 app_args.pl_space);
05146 park = -1;
05147 }
05148 }
05149
05150 if (!ast_strlen_zero(app_args.pl_name)) {
05151 pl_name = app_args.pl_name;
05152 } else {
05153 pl_name = findparkinglotname(chan);
05154 }
05155 if (ast_strlen_zero(pl_name)) {
05156
05157 parkinglot = parkinglot_addref(default_parkinglot);
05158 } else {
05159 parkinglot = find_parkinglot(pl_name);
05160 if (!parkinglot) {
05161
05162 if (chan->_state != AST_STATE_UP) {
05163 ast_answer(chan);
05164 }
05165 if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
05166 ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n",
05167 "pbx-invalidpark", chan->name);
05168 }
05169 ast_log(LOG_WARNING,
05170 "Channel %s tried to retrieve parked call from unknown parking lot '%s'\n",
05171 chan->name, pl_name);
05172 return -1;
05173 }
05174 }
05175
05176 AST_LIST_LOCK(&parkinglot->parkings);
05177 AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot->parkings, pu, list) {
05178 if ((ast_strlen_zero(app_args.pl_space) || pu->parkingnum == park)
05179 && !pu->notquiteyet && !pu->chan->pbx) {
05180
05181 AST_LIST_REMOVE_CURRENT(list);
05182 break;
05183 }
05184 }
05185 AST_LIST_TRAVERSE_SAFE_END;
05186 if (pu) {
05187
05188 peer = pu->chan;
05189 con = ast_context_find(parkinglot->cfg.parking_con);
05190 if (con) {
05191 if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
05192 ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
05193 } else {
05194 notify_metermaids(pu->parkingexten, parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
05195 }
05196 } else {
05197 ast_log(LOG_WARNING, "Whoa, no parking context?\n");
05198 }
05199
05200 ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "UnParkedCall", chan);
05201 ast_manager_event(pu->chan, EVENT_FLAG_CALL, "UnParkedCall",
05202 "Exten: %s\r\n"
05203 "Channel: %s\r\n"
05204 "Parkinglot: %s\r\n"
05205 "From: %s\r\n"
05206 "CallerIDNum: %s\r\n"
05207 "CallerIDName: %s\r\n"
05208 "ConnectedLineNum: %s\r\n"
05209 "ConnectedLineName: %s\r\n"
05210 "Uniqueid: %s\r\n",
05211 pu->parkingexten, pu->chan->name, pu->parkinglot->name, chan->name,
05212 S_COR(pu->chan->caller.id.number.valid, pu->chan->caller.id.number.str, "<unknown>"),
05213 S_COR(pu->chan->caller.id.name.valid, pu->chan->caller.id.name.str, "<unknown>"),
05214 S_COR(pu->chan->connected.id.number.valid, pu->chan->connected.id.number.str, "<unknown>"),
05215 S_COR(pu->chan->connected.id.name.valid, pu->chan->connected.id.name.str, "<unknown>"),
05216 pu->chan->uniqueid
05217 );
05218
05219
05220 switch (pu->hold_method) {
05221 case AST_CONTROL_HOLD:
05222 ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
05223 break;
05224 case AST_CONTROL_RINGING:
05225 ast_indicate(pu->chan, -1);
05226 break;
05227 default:
05228 break;
05229 }
05230 pu->hold_method = 0;
05231
05232 parkinglot_unref(pu->parkinglot);
05233 ast_free(pu);
05234 }
05235 AST_LIST_UNLOCK(&parkinglot->parkings);
05236
05237 if (peer) {
05238
05239 struct ast_party_connected_line connected;
05240
05241 ast_party_connected_line_init(&connected);
05242
05243
05244 ast_channel_lock(chan);
05245 ast_connected_line_copy_from_caller(&connected, &chan->caller);
05246 ast_channel_unlock(chan);
05247 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
05248 if (ast_channel_connected_line_macro(chan, peer, &connected, 0, 0)) {
05249 ast_channel_update_connected_line(peer, &connected, NULL);
05250 }
05251
05252
05253
05254
05255
05256
05257
05258
05259 ast_channel_lock(peer);
05260 ast_connected_line_copy_from_caller(&connected, &peer->caller);
05261 ast_channel_unlock(peer);
05262 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
05263 if (ast_channel_connected_line_macro(peer, chan, &connected, 1, 0)) {
05264 ast_channel_update_connected_line(chan, &connected, NULL);
05265 }
05266
05267 ast_party_connected_line_free(&connected);
05268 }
05269
05270
05271 if (chan->_state != AST_STATE_UP) {
05272 ast_answer(chan);
05273 }
05274
05275 if (peer) {
05276 struct ast_datastore *features_datastore;
05277 struct ast_dial_features *dialfeatures;
05278
05279
05280 if (!ast_strlen_zero(courtesytone)) {
05281 static const char msg[] = "courtesy tone";
05282
05283 switch (parkedplay) {
05284 case 0:
05285 res = play_message_to_chans(chan, peer, -1, msg, courtesytone);
05286 break;
05287 case 1:
05288 res = play_message_to_chans(chan, peer, 1, msg, courtesytone);
05289 break;
05290 case 2:
05291 res = play_message_to_chans(chan, peer, 0, msg, courtesytone);
05292 break;
05293 default:
05294 res = 0;
05295 break;
05296 }
05297 if (res) {
05298 ast_hangup(peer);
05299 parkinglot_unref(parkinglot);
05300 return -1;
05301 }
05302 }
05303
05304 res = ast_channel_make_compatible(chan, peer);
05305 if (res < 0) {
05306 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, peer->name);
05307 ast_hangup(peer);
05308 parkinglot_unref(parkinglot);
05309 return -1;
05310 }
05311
05312
05313 ast_verb(3, "Channel %s connected to parked call %d\n", chan->name, park);
05314
05315 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
05316 ast_cdr_setdestchan(chan->cdr, peer->name);
05317 memset(&config, 0, sizeof(struct ast_bridge_config));
05318
05319
05320 ast_channel_lock(peer);
05321 features_datastore = ast_channel_datastore_find(peer, &dial_features_info, NULL);
05322 if (features_datastore && (dialfeatures = features_datastore->data)) {
05323 ast_copy_flags(&config.features_callee, &dialfeatures->my_features,
05324 AST_FLAGS_ALL);
05325 }
05326 ast_channel_unlock(peer);
05327
05328 if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
05329 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
05330 }
05331 if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
05332 ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
05333 }
05334 if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
05335 ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
05336 }
05337 if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
05338 ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
05339 }
05340 if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
05341 ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
05342 }
05343 if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
05344 ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
05345 }
05346 if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
05347 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
05348 }
05349 if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
05350 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
05351 }
05352
05353 res = ast_bridge_call(chan, peer, &config);
05354
05355 pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", peer->name);
05356 ast_cdr_setdestchan(chan->cdr, peer->name);
05357
05358
05359 ast_hangup(peer);
05360 } else {
05361 if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
05362 ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark",
05363 chan->name);
05364 }
05365 ast_verb(3, "Channel %s tried to retrieve nonexistent parked call %d\n",
05366 chan->name, park);
05367 res = -1;
05368 }
05369
05370 parkinglot_unref(parkinglot);
05371 return res;
05372 }
05373
05374
05375
05376
05377 static void parkinglot_unref(struct ast_parkinglot *parkinglot)
05378 {
05379 ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name,
05380 ao2_ref(parkinglot, 0) - 1);
05381 ao2_ref(parkinglot, -1);
05382 }
05383
05384 static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
05385 {
05386 int refcount;
05387
05388 refcount = ao2_ref(parkinglot, +1);
05389 ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
05390 return parkinglot;
05391 }
05392
05393
05394 static void parkinglot_destroy(void *obj)
05395 {
05396 struct ast_parkinglot *doomed = obj;
05397
05398
05399
05400
05401
05402
05403 ast_assert(AST_LIST_EMPTY(&doomed->parkings));
05404 AST_LIST_HEAD_DESTROY(&doomed->parkings);
05405 }
05406
05407
05408 static struct ast_parkinglot *create_parkinglot(const char *name)
05409 {
05410 struct ast_parkinglot *newlot;
05411
05412 if (ast_strlen_zero(name)) {
05413 return NULL;
05414 }
05415
05416 newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
05417 if (!newlot)
05418 return NULL;
05419
05420 ast_copy_string(newlot->name, name, sizeof(newlot->name));
05421 newlot->cfg.is_invalid = 1;
05422 AST_LIST_HEAD_INIT(&newlot->parkings);
05423
05424 return newlot;
05425 }
05426
05427
05428
05429
05430
05431
05432
05433 static void park_add_hints(const char *context, int start, int stop)
05434 {
05435 int numext;
05436 char device[AST_MAX_EXTENSION];
05437 char exten[10];
05438
05439 for (numext = start; numext <= stop; numext++) {
05440 snprintf(exten, sizeof(exten), "%d", numext);
05441 snprintf(device, sizeof(device), "park:%s@%s", exten, context);
05442 ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
05443 }
05444 }
05445
05446
05447 static const struct parkinglot_cfg parkinglot_cfg_default_default = {
05448 .mohclass = "default",
05449 .parkext = DEFAULT_PARK_EXTENSION,
05450 .parking_con = "parkedcalls",
05451 .parking_start = 701,
05452 .parking_stop = 750,
05453 .parkingtime = DEFAULT_PARK_TIME,
05454 };
05455
05456
05457 static const struct parkinglot_cfg parkinglot_cfg_default = {
05458 .parkext = DEFAULT_PARK_EXTENSION,
05459 .parkingtime = DEFAULT_PARK_TIME,
05460 };
05461
05462
05463
05464
05465
05466
05467
05468
05469
05470
05471
05472 static void parkinglot_feature_flag_cfg(const char *pl_name, int *param, struct ast_variable *var)
05473 {
05474 ast_debug(1, "Setting parking lot %s %s to %s\n", pl_name, var->name, var->value);
05475 if (!strcasecmp(var->value, "both")) {
05476 *param = AST_FEATURE_FLAG_BYBOTH;
05477 } else if (!strcasecmp(var->value, "caller")) {
05478 *param = AST_FEATURE_FLAG_BYCALLER;
05479 } else if (!strcasecmp(var->value, "callee")) {
05480 *param = AST_FEATURE_FLAG_BYCALLEE;
05481 }
05482 }
05483
05484
05485
05486
05487
05488
05489
05490
05491
05492
05493
05494
05495 static int parkinglot_config_read(const char *pl_name, struct parkinglot_cfg *cfg, struct ast_variable *var)
05496 {
05497 int error = 0;
05498
05499 while (var) {
05500 if (!strcasecmp(var->name, "context")) {
05501 ast_copy_string(cfg->parking_con, var->value, sizeof(cfg->parking_con));
05502 } else if (!strcasecmp(var->name, "parkext")) {
05503 ast_copy_string(cfg->parkext, var->value, sizeof(cfg->parkext));
05504 } else if (!strcasecmp(var->name, "parkext_exclusive")) {
05505 cfg->parkext_exclusive = ast_true(var->value);
05506 } else if (!strcasecmp(var->name, "parkinghints")) {
05507 cfg->parkaddhints = ast_true(var->value);
05508 } else if (!strcasecmp(var->name, "parkedmusicclass")) {
05509 ast_copy_string(cfg->mohclass, var->value, sizeof(cfg->mohclass));
05510 } else if (!strcasecmp(var->name, "parkingtime")) {
05511 int parkingtime = 0;
05512
05513 if ((sscanf(var->value, "%30d", &parkingtime) != 1) || parkingtime < 1) {
05514 ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
05515 error = -1;
05516 } else {
05517 cfg->parkingtime = parkingtime * 1000;
05518 }
05519 } else if (!strcasecmp(var->name, "parkpos")) {
05520 int start = 0;
05521 int end = 0;
05522
05523 if (sscanf(var->value, "%30d-%30d", &start, &end) != 2) {
05524 ast_log(LOG_WARNING,
05525 "Format for parking positions is a-b, where a and b are numbers at line %d of %s\n",
05526 var->lineno, var->file);
05527 error = -1;
05528 } else if (end < start || start <= 0 || end <= 0) {
05529 ast_log(LOG_WARNING, "Parking range is invalid. Must be a <= b, at line %d of %s\n",
05530 var->lineno, var->file);
05531 error = -1;
05532 } else {
05533 cfg->parking_start = start;
05534 cfg->parking_stop = end;
05535 }
05536 } else if (!strcasecmp(var->name, "findslot")) {
05537 cfg->parkfindnext = (!strcasecmp(var->value, "next"));
05538 } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
05539 parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcalltransfers, var);
05540 } else if (!strcasecmp(var->name, "parkedcallreparking")) {
05541 parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallreparking, var);
05542 } else if (!strcasecmp(var->name, "parkedcallhangup")) {
05543 parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallhangup, var);
05544 } else if (!strcasecmp(var->name, "parkedcallrecording")) {
05545 parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallrecording, var);
05546 }
05547 var = var->next;
05548 }
05549
05550
05551 if (ast_strlen_zero(cfg->parking_con)) {
05552 ast_log(LOG_WARNING, "Parking lot %s needs context\n", pl_name);
05553 error = -1;
05554 }
05555 if (ast_strlen_zero(cfg->parkext)) {
05556 ast_log(LOG_WARNING, "Parking lot %s needs parkext\n", pl_name);
05557 error = -1;
05558 }
05559 if (!cfg->parking_start) {
05560 ast_log(LOG_WARNING, "Parking lot %s needs parkpos\n", pl_name);
05561 error = -1;
05562 }
05563 if (error) {
05564 cfg->is_invalid = 1;
05565 }
05566
05567 return error;
05568 }
05569
05570
05571
05572
05573
05574
05575
05576
05577
05578
05579
05580
05581
05582
05583 static int parkinglot_activate(struct ast_parkinglot *parkinglot)
05584 {
05585 int disabled = 0;
05586 char app_data[5 + AST_MAX_CONTEXT];
05587
05588
05589 if (parkinglot->cfg.parkext_exclusive) {
05590
05591 snprintf(app_data, sizeof(app_data), ",,,,,%s", parkinglot->name);
05592 } else {
05593
05594 app_data[0] = '\0';
05595 }
05596
05597
05598 if (!ast_context_find_or_create(NULL, NULL, parkinglot->cfg.parking_con, registrar)) {
05599 ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n",
05600 parkinglot->cfg.parking_con);
05601 disabled = 1;
05602
05603
05604 } else if (ast_add_extension(parkinglot->cfg.parking_con, 1, parkinglot->cfg.parkext,
05605 1, NULL, NULL, parkcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
05606 ast_log(LOG_ERROR, "Could not create parking lot %s access exten %s@%s\n",
05607 parkinglot->name, parkinglot->cfg.parkext, parkinglot->cfg.parking_con);
05608 disabled = 1;
05609 } else {
05610
05611 if (parkinglot->cfg.parkaddhints) {
05612 park_add_hints(parkinglot->cfg.parking_con, parkinglot->cfg.parking_start,
05613 parkinglot->cfg.parking_stop);
05614 }
05615
05616
05617
05618
05619
05620
05621
05622
05623 notify_metermaids(parkinglot->cfg.parkext, parkinglot->cfg.parking_con,
05624 AST_DEVICE_INUSE);
05625 }
05626
05627 parkinglot->disabled = disabled;
05628 return disabled ? -1 : 0;
05629 }
05630
05631
05632 static struct ast_parkinglot *build_parkinglot(const char *pl_name, struct ast_variable *var)
05633 {
05634 struct ast_parkinglot *parkinglot;
05635 const struct parkinglot_cfg *cfg_defaults;
05636 struct parkinglot_cfg new_cfg;
05637 int cfg_error;
05638 int oldparkinglot = 0;
05639
05640 parkinglot = find_parkinglot(pl_name);
05641 if (parkinglot) {
05642 oldparkinglot = 1;
05643 } else {
05644 parkinglot = create_parkinglot(pl_name);
05645 if (!parkinglot) {
05646 return NULL;
05647 }
05648 }
05649 if (!strcmp(parkinglot->name, DEFAULT_PARKINGLOT)) {
05650 cfg_defaults = &parkinglot_cfg_default_default;
05651 } else {
05652 cfg_defaults = &parkinglot_cfg_default;
05653 }
05654 new_cfg = *cfg_defaults;
05655
05656 ast_debug(1, "Building parking lot %s\n", parkinglot->name);
05657
05658 ao2_lock(parkinglot);
05659
05660
05661 cfg_error = parkinglot_config_read(parkinglot->name, &new_cfg, var);
05662 if (oldparkinglot) {
05663 if (cfg_error) {
05664
05665 ast_log(LOG_WARNING, "Changes to parking lot %s are discarded.\n",
05666 parkinglot->name);
05667 cfg_error = 0;
05668 } else if (!AST_LIST_EMPTY(&parkinglot->parkings)
05669 && memcmp(&new_cfg, &parkinglot->cfg, sizeof(parkinglot->cfg))) {
05670
05671 ast_log(LOG_WARNING,
05672 "Parking lot %s has parked calls. Parking lot changes discarded.\n",
05673 parkinglot->name);
05674 force_reload_load = 1;
05675 } else {
05676
05677 parkinglot->cfg = new_cfg;
05678 }
05679 } else {
05680
05681 parkinglot->cfg = new_cfg;
05682 }
05683 parkinglot->the_mark = 0;
05684
05685 ao2_unlock(parkinglot);
05686
05687 if (cfg_error) {
05688
05689 ast_log(LOG_WARNING, "New parking lot %s is discarded.\n", parkinglot->name);
05690 parkinglot_unref(parkinglot);
05691 return NULL;
05692 }
05693
05694
05695 if (!oldparkinglot) {
05696 ao2_link(parkinglots, parkinglot);
05697 }
05698 parkinglot_unref(parkinglot);
05699
05700 return parkinglot;
05701 }
05702
05703
05704
05705
05706
05707
05708
05709
05710
05711 static void process_applicationmap_line(struct ast_variable *var)
05712 {
05713 char *tmp_val = ast_strdupa(var->value);
05714 char *activateon, *new_syn;
05715 struct ast_call_feature *feature;
05716 AST_DECLARE_APP_ARGS(args,
05717 AST_APP_ARG(exten);
05718 AST_APP_ARG(activatedby);
05719 AST_APP_ARG(app);
05720 AST_APP_ARG(app_args);
05721 AST_APP_ARG(moh_class);
05722 );
05723
05724 AST_STANDARD_APP_ARGS(args, tmp_val);
05725 if ((new_syn = strchr(args.app, '('))) {
05726
05727 args.moh_class = args.app_args;
05728 args.app_args = new_syn;
05729 *args.app_args++ = '\0';
05730 if (args.app_args[strlen(args.app_args) - 1] == ')') {
05731 args.app_args[strlen(args.app_args) - 1] = '\0';
05732 }
05733 }
05734
05735 activateon = strsep(&args.activatedby, "/");
05736
05737
05738 if (ast_strlen_zero(args.app)
05739 || ast_strlen_zero(args.exten)
05740 || ast_strlen_zero(activateon)
05741 || ast_strlen_zero(var->name)) {
05742 ast_log(LOG_NOTICE,
05743 "Please check the feature Mapping Syntax, either extension, name, or app aren't provided %s %s %s %s\n",
05744 args.app, args.exten, activateon, var->name);
05745 return;
05746 }
05747
05748 AST_RWLIST_RDLOCK(&feature_list);
05749 if (find_dynamic_feature(var->name)) {
05750 AST_RWLIST_UNLOCK(&feature_list);
05751 ast_log(LOG_WARNING, "Dynamic Feature '%s' specified more than once!\n",
05752 var->name);
05753 return;
05754 }
05755 AST_RWLIST_UNLOCK(&feature_list);
05756
05757 if (!(feature = ast_calloc(1, sizeof(*feature)))) {
05758 return;
05759 }
05760
05761 ast_copy_string(feature->sname, var->name, FEATURE_SNAME_LEN);
05762 ast_copy_string(feature->app, args.app, FEATURE_APP_LEN);
05763 ast_copy_string(feature->exten, args.exten, FEATURE_EXTEN_LEN);
05764
05765 if (args.app_args) {
05766 ast_copy_string(feature->app_args, args.app_args, FEATURE_APP_ARGS_LEN);
05767 }
05768
05769 if (args.moh_class) {
05770 ast_copy_string(feature->moh_class, args.moh_class, FEATURE_MOH_LEN);
05771 }
05772
05773 ast_copy_string(feature->exten, args.exten, sizeof(feature->exten));
05774 feature->operation = feature_exec_app;
05775 ast_set_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF);
05776
05777
05778 if (!strcasecmp(activateon, "self") || !strcasecmp(activateon, "caller")) {
05779 ast_set_flag(feature, AST_FEATURE_FLAG_ONSELF);
05780 } else if (!strcasecmp(activateon, "peer") || !strcasecmp(activateon, "callee")) {
05781 ast_set_flag(feature, AST_FEATURE_FLAG_ONPEER);
05782 } else {
05783 ast_log(LOG_NOTICE, "Invalid 'ActivateOn' specification for feature '%s',"
05784 " must be 'self', or 'peer'\n", var->name);
05785 ast_free(feature);
05786 return;
05787 }
05788
05789 if (ast_strlen_zero(args.activatedby)) {
05790 ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
05791 } else if (!strcasecmp(args.activatedby, "caller")) {
05792 ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLER);
05793 } else if (!strcasecmp(args.activatedby, "callee")) {
05794 ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLEE);
05795 } else if (!strcasecmp(args.activatedby, "both")) {
05796 ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
05797 } else {
05798 ast_log(LOG_NOTICE, "Invalid 'ActivatedBy' specification for feature '%s',"
05799 " must be 'caller', or 'callee', or 'both'\n", var->name);
05800 ast_free(feature);
05801 return;
05802 }
05803
05804 ast_register_feature(feature);
05805
05806 ast_verb(2, "Mapping Feature '%s' to app '%s(%s)' with code '%s'\n",
05807 var->name, args.app, args.app_args, args.exten);
05808 }
05809
05810 static int process_config(struct ast_config *cfg)
05811 {
05812 int i;
05813 struct ast_variable *var = NULL;
05814 struct feature_group *fg = NULL;
05815 char *ctg;
05816 static const char * const categories[] = {
05817
05818
05819
05820 "general",
05821 "featuremap",
05822 "applicationmap"
05823 };
05824
05825
05826 featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
05827
05828
05829 strcpy(pickup_ext, "*8");
05830 pickupsound[0] = '\0';
05831 pickupfailsound[0] = '\0';
05832
05833
05834 strcpy(xfersound, "beep");
05835 strcpy(xferfailsound, "beeperr");
05836 transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
05837 atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
05838 atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
05839 atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
05840 atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
05841
05842
05843 comebacktoorigin = 1;
05844 courtesytone[0] = '\0';
05845 parkedplay = 0;
05846 adsipark = 0;
05847 parkeddynamic = 0;
05848
05849 var = ast_variable_browse(cfg, "general");
05850 build_parkinglot(DEFAULT_PARKINGLOT, var);
05851 for (; var; var = var->next) {
05852 if (!strcasecmp(var->name, "parkeddynamic")) {
05853 parkeddynamic = ast_true(var->value);
05854 } else if (!strcasecmp(var->name, "adsipark")) {
05855 adsipark = ast_true(var->value);
05856 } else if (!strcasecmp(var->name, "transferdigittimeout")) {
05857 if ((sscanf(var->value, "%30d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
05858 ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
05859 transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
05860 } else {
05861 transferdigittimeout = transferdigittimeout * 1000;
05862 }
05863 } else if (!strcasecmp(var->name, "featuredigittimeout")) {
05864 if ((sscanf(var->value, "%30d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
05865 ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
05866 featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
05867 }
05868 } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
05869 if ((sscanf(var->value, "%30d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
05870 ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
05871 atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
05872 } else {
05873 atxfernoanswertimeout = atxfernoanswertimeout * 1000;
05874 }
05875 } else if (!strcasecmp(var->name, "atxferloopdelay")) {
05876 if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
05877 ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
05878 atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
05879 } else {
05880 atxferloopdelay *= 1000;
05881 }
05882 } else if (!strcasecmp(var->name, "atxferdropcall")) {
05883 atxferdropcall = ast_true(var->value);
05884 } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
05885 if ((sscanf(var->value, "%30u", &atxfercallbackretries) != 1)) {
05886 ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
05887 atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
05888 }
05889 } else if (!strcasecmp(var->name, "courtesytone")) {
05890 ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
05891 } else if (!strcasecmp(var->name, "parkedplay")) {
05892 if (!strcasecmp(var->value, "both")) {
05893 parkedplay = 2;
05894 } else if (!strcasecmp(var->value, "parked")) {
05895 parkedplay = 1;
05896 } else {
05897 parkedplay = 0;
05898 }
05899 } else if (!strcasecmp(var->name, "xfersound")) {
05900 ast_copy_string(xfersound, var->value, sizeof(xfersound));
05901 } else if (!strcasecmp(var->name, "xferfailsound")) {
05902 ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
05903 } else if (!strcasecmp(var->name, "pickupexten")) {
05904 ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));
05905 } else if (!strcasecmp(var->name, "pickupsound")) {
05906 ast_copy_string(pickupsound, var->value, sizeof(pickupsound));
05907 } else if (!strcasecmp(var->name, "pickupfailsound")) {
05908 ast_copy_string(pickupfailsound, var->value, sizeof(pickupfailsound));
05909 } else if (!strcasecmp(var->name, "comebacktoorigin")) {
05910 comebacktoorigin = ast_true(var->value);
05911 }
05912 }
05913
05914 unmap_features();
05915 for (var = ast_variable_browse(cfg, "featuremap"); var; var = var->next) {
05916 if (remap_feature(var->name, var->value)) {
05917 ast_log(LOG_NOTICE, "Unknown feature '%s'\n", var->name);
05918 }
05919 }
05920
05921
05922 ast_unregister_features();
05923 for (var = ast_variable_browse(cfg, "applicationmap"); var; var = var->next) {
05924 process_applicationmap_line(var);
05925 }
05926
05927 ast_unregister_groups();
05928 AST_RWLIST_WRLOCK(&feature_groups);
05929
05930 ctg = NULL;
05931 while ((ctg = ast_category_browse(cfg, ctg))) {
05932
05933 if (!strncasecmp(ctg, "parkinglot_", strlen("parkinglot_"))) {
05934 ast_debug(2, "Found configuration section %s, assume parking context\n", ctg);
05935 if (!build_parkinglot(ctg, ast_variable_browse(cfg, ctg))) {
05936 ast_log(LOG_ERROR, "Could not build parking lot %s. Configuration error.\n", ctg);
05937 } else {
05938 ast_debug(1, "Configured parking context %s\n", ctg);
05939 }
05940 continue;
05941 }
05942
05943
05944 for (i = 0; i < ARRAY_LEN(categories); i++) {
05945 if (!strcasecmp(categories[i], ctg)) {
05946 break;
05947 }
05948 }
05949 if (i < ARRAY_LEN(categories)) {
05950 continue;
05951 }
05952
05953 if (!(fg = register_group(ctg))) {
05954 continue;
05955 }
05956
05957 for (var = ast_variable_browse(cfg, ctg); var; var = var->next) {
05958 struct ast_call_feature *feature;
05959
05960 AST_RWLIST_RDLOCK(&feature_list);
05961 if (!(feature = find_dynamic_feature(var->name)) &&
05962 !(feature = ast_find_call_feature(var->name))) {
05963 AST_RWLIST_UNLOCK(&feature_list);
05964 ast_log(LOG_WARNING, "Feature '%s' was not found.\n", var->name);
05965 continue;
05966 }
05967 AST_RWLIST_UNLOCK(&feature_list);
05968
05969 register_group_feature(fg, var->value, feature);
05970 }
05971 }
05972
05973 AST_RWLIST_UNLOCK(&feature_groups);
05974
05975 return 0;
05976 }
05977
05978
05979
05980
05981
05982
05983
05984
05985
05986 static void destroy_dialplan_usage_context(struct parking_dp_context *doomed)
05987 {
05988 struct parking_dp_ramp *ramp;
05989 struct parking_dp_spaces *spaces;
05990
05991 while ((ramp = AST_LIST_REMOVE_HEAD(&doomed->access_extens, node))) {
05992 ast_free(ramp);
05993 }
05994 while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->spaces, node))) {
05995 ast_free(spaces);
05996 }
05997 while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->hints, node))) {
05998 ast_free(spaces);
05999 }
06000 ast_free(doomed);
06001 }
06002
06003
06004
06005
06006
06007
06008
06009
06010
06011 static void destroy_dialplan_usage_map(struct parking_dp_map *doomed)
06012 {
06013 struct parking_dp_context *item;
06014
06015 while ((item = AST_LIST_REMOVE_HEAD(doomed, node))) {
06016 destroy_dialplan_usage_context(item);
06017 }
06018 }
06019
06020
06021
06022
06023
06024
06025
06026
06027
06028
06029
06030 static struct parking_dp_ramp *build_dialplan_useage_ramp(const char *exten, int exclusive)
06031 {
06032 struct parking_dp_ramp *ramp_node;
06033
06034 ramp_node = ast_calloc(1, sizeof(*ramp_node) + strlen(exten));
06035 if (!ramp_node) {
06036 return NULL;
06037 }
06038 ramp_node->exclusive = exclusive;
06039 strcpy(ramp_node->exten, exten);
06040 return ramp_node;
06041 }
06042
06043
06044
06045
06046
06047
06048
06049
06050
06051
06052
06053
06054
06055
06056 static int usage_context_add_ramp(struct parking_dp_ramp_map *ramp_map, const char *exten, int exclusive, struct ast_parkinglot *lot, int complain)
06057 {
06058 struct parking_dp_ramp *cur_ramp;
06059 struct parking_dp_ramp *new_ramp;
06060 int cmp;
06061
06062
06063 if (exclusive) {
06064 exclusive = 1;
06065 }
06066
06067 AST_LIST_TRAVERSE_SAFE_BEGIN(ramp_map, cur_ramp, node) {
06068 cmp = strcmp(exten, cur_ramp->exten);
06069 if (cmp > 0) {
06070
06071 continue;
06072 }
06073 if (cmp == 0) {
06074
06075 if (complain && (cur_ramp->exclusive || exclusive)) {
06076 ast_log(LOG_WARNING,
06077 "Parking lot '%s' parkext %s@%s used by another parking lot.\n",
06078 lot->name, exten, lot->cfg.parking_con);
06079 }
06080 return 0;
06081 }
06082
06083 new_ramp = build_dialplan_useage_ramp(exten, exclusive);
06084 if (!new_ramp) {
06085 return -1;
06086 }
06087 AST_LIST_INSERT_BEFORE_CURRENT(new_ramp, node);
06088 return 0;
06089 }
06090 AST_LIST_TRAVERSE_SAFE_END;
06091
06092
06093 new_ramp = build_dialplan_useage_ramp(exten, exclusive);
06094 if (!new_ramp) {
06095 return -1;
06096 }
06097 AST_LIST_INSERT_TAIL(ramp_map, new_ramp, node);
06098 return 0;
06099 }
06100
06101
06102
06103
06104
06105
06106
06107
06108
06109
06110
06111 static struct parking_dp_spaces *build_dialplan_useage_spaces(int start, int stop)
06112 {
06113 struct parking_dp_spaces *spaces_node;
06114
06115 spaces_node = ast_calloc(1, sizeof(*spaces_node));
06116 if (!spaces_node) {
06117 return NULL;
06118 }
06119 spaces_node->start = start;
06120 spaces_node->stop = stop;
06121 return spaces_node;
06122 }
06123
06124
06125
06126
06127
06128
06129
06130
06131
06132
06133
06134
06135
06136
06137 static int usage_context_add_spaces(struct parking_dp_space_map *space_map, int start, int stop, struct ast_parkinglot *lot, int complain)
06138 {
06139 struct parking_dp_spaces *cur_node;
06140 struct parking_dp_spaces *expand_node;
06141 struct parking_dp_spaces *new_node;
06142
06143 expand_node = NULL;
06144 AST_LIST_TRAVERSE_SAFE_BEGIN(space_map, cur_node, node) {
06145
06146 if (expand_node) {
06147
06148 if (expand_node->stop + 1 < cur_node->start) {
06149
06150 return 0;
06151 }
06152
06153 if (complain
06154 && ((cur_node->start <= start && start <= cur_node->stop)
06155 || (cur_node->start <= stop && stop <= cur_node->stop)
06156 || (start < cur_node->start && cur_node->stop < stop))) {
06157
06158 complain = 0;
06159 ast_log(LOG_WARNING,
06160 "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
06161 lot->name, start, stop, lot->cfg.parking_con);
06162 }
06163
06164
06165 if (expand_node->stop < cur_node->stop) {
06166 expand_node->stop = cur_node->stop;
06167 }
06168 AST_LIST_REMOVE_CURRENT(node);
06169 ast_free(cur_node);
06170 continue;
06171 }
06172
06173 if (cur_node->stop + 1 < start) {
06174
06175 continue;
06176 }
06177 if (stop + 1 < cur_node->start) {
06178
06179 new_node = build_dialplan_useage_spaces(start, stop);
06180 if (!new_node) {
06181 return -1;
06182 }
06183 AST_LIST_INSERT_BEFORE_CURRENT(new_node, node);
06184 return 0;
06185 }
06186
06187 if (complain
06188 && ((cur_node->start <= start && start <= cur_node->stop)
06189 || (cur_node->start <= stop && stop <= cur_node->stop)
06190 || (start < cur_node->start && cur_node->stop < stop))) {
06191
06192 complain = 0;
06193 ast_log(LOG_WARNING,
06194 "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
06195 lot->name, start, stop, lot->cfg.parking_con);
06196 }
06197
06198
06199 if (start < cur_node->start) {
06200
06201 cur_node->start = start;
06202 }
06203 if (stop <= cur_node->stop) {
06204
06205 return 0;
06206 }
06207 cur_node->stop = stop;
06208 expand_node = cur_node;
06209 }
06210 AST_LIST_TRAVERSE_SAFE_END;
06211
06212 if (expand_node) {
06213
06214
06215
06216
06217 return 0;
06218 }
06219
06220
06221 new_node = build_dialplan_useage_spaces(start, stop);
06222 if (!new_node) {
06223 return -1;
06224 }
06225 AST_LIST_INSERT_TAIL(space_map, new_node, node);
06226 return 0;
06227 }
06228
06229
06230
06231
06232
06233
06234
06235
06236
06237
06238
06239
06240 static int dialplan_usage_add_parkinglot_data(struct parking_dp_context *ctx_node, struct ast_parkinglot *lot, int complain)
06241 {
06242 if (usage_context_add_ramp(&ctx_node->access_extens, lot->cfg.parkext,
06243 lot->cfg.parkext_exclusive, lot, complain)) {
06244 return -1;
06245 }
06246 if (usage_context_add_spaces(&ctx_node->spaces, lot->cfg.parking_start,
06247 lot->cfg.parking_stop, lot, complain)) {
06248 return -1;
06249 }
06250 if (lot->cfg.parkaddhints
06251 && usage_context_add_spaces(&ctx_node->hints, lot->cfg.parking_start,
06252 lot->cfg.parking_stop, lot, 0)) {
06253 return -1;
06254 }
06255 return 0;
06256 }
06257
06258
06259
06260
06261
06262
06263
06264
06265
06266
06267 static struct parking_dp_context *build_dialplan_useage_context(struct ast_parkinglot *lot)
06268 {
06269 struct parking_dp_context *ctx_node;
06270
06271 ctx_node = ast_calloc(1, sizeof(*ctx_node) + strlen(lot->cfg.parking_con));
06272 if (!ctx_node) {
06273 return NULL;
06274 }
06275 if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 0)) {
06276 destroy_dialplan_usage_context(ctx_node);
06277 return NULL;
06278 }
06279 strcpy(ctx_node->context, lot->cfg.parking_con);
06280 return ctx_node;
06281 }
06282
06283
06284
06285
06286
06287
06288
06289
06290
06291
06292
06293
06294 static int dialplan_usage_add_parkinglot(struct parking_dp_map *usage_map, struct ast_parkinglot *lot, int complain)
06295 {
06296 struct parking_dp_context *cur_ctx;
06297 struct parking_dp_context *new_ctx;
06298 int cmp;
06299
06300 AST_LIST_TRAVERSE_SAFE_BEGIN(usage_map, cur_ctx, node) {
06301 cmp = strcmp(lot->cfg.parking_con, cur_ctx->context);
06302 if (cmp > 0) {
06303
06304 continue;
06305 }
06306 if (cmp == 0) {
06307
06308 return dialplan_usage_add_parkinglot_data(cur_ctx, lot, complain);
06309 }
06310
06311 new_ctx = build_dialplan_useage_context(lot);
06312 if (!new_ctx) {
06313 return -1;
06314 }
06315 AST_LIST_INSERT_BEFORE_CURRENT(new_ctx, node);
06316 return 0;
06317 }
06318 AST_LIST_TRAVERSE_SAFE_END;
06319
06320
06321 new_ctx = build_dialplan_useage_context(lot);
06322 if (!new_ctx) {
06323 return -1;
06324 }
06325 AST_LIST_INSERT_TAIL(usage_map, new_ctx, node);
06326 return 0;
06327 }
06328
06329
06330
06331
06332
06333
06334
06335
06336
06337
06338
06339 static int build_dialplan_useage_map(struct parking_dp_map *usage_map, int complain)
06340 {
06341 int status = 0;
06342 struct ao2_iterator iter;
06343 struct ast_parkinglot *curlot;
06344
06345
06346 iter = ao2_iterator_init(parkinglots, 0);
06347 for (; (curlot = ao2_iterator_next(&iter)); ao2_ref(curlot, -1)) {
06348
06349 if (dialplan_usage_add_parkinglot(usage_map, curlot, complain)) {
06350 ao2_ref(curlot, -1);
06351 status = -1;
06352 break;
06353 }
06354 }
06355 ao2_iterator_destroy(&iter);
06356
06357 return status;
06358 }
06359
06360
06361
06362
06363
06364
06365
06366
06367
06368
06369
06370 static void remove_exten_if_exist(const char *context, const char *exten, int priority)
06371 {
06372 struct pbx_find_info q = { .stacklen = 0 };
06373
06374 if (pbx_find_extension(NULL, NULL, &q, context, exten, priority, NULL, NULL,
06375 E_MATCH)) {
06376 ast_debug(1, "Removing unneeded parking lot exten: %s@%s priority:%d\n",
06377 context, exten, priority);
06378 ast_context_remove_extension(context, exten, priority, registrar);
06379 }
06380 }
06381
06382
06383
06384
06385
06386
06387
06388
06389
06390
06391
06392
06393
06394
06395
06396 static void remove_dead_ramp_usage(const char *context, struct parking_dp_ramp_map *old_ramps, struct parking_dp_ramp_map *new_ramps)
06397 {
06398 struct parking_dp_ramp *old_ramp;
06399 struct parking_dp_ramp *new_ramp;
06400 int cmp;
06401
06402 old_ramp = AST_LIST_FIRST(old_ramps);
06403 new_ramp = AST_LIST_FIRST(new_ramps);
06404
06405 while (new_ramp) {
06406 if (!old_ramp) {
06407
06408 return;
06409 }
06410 cmp = strcmp(old_ramp->exten, new_ramp->exten);
06411 if (cmp < 0) {
06412
06413 remove_exten_if_exist(context, old_ramp->exten, 1);
06414 old_ramp = AST_LIST_NEXT(old_ramp, node);
06415 continue;
06416 }
06417 if (cmp == 0) {
06418
06419 old_ramp = AST_LIST_NEXT(old_ramp, node);
06420 } else {
06421
06422 }
06423 new_ramp = AST_LIST_NEXT(new_ramp, node);
06424 }
06425
06426
06427 for (; old_ramp; old_ramp = AST_LIST_NEXT(old_ramp, node)) {
06428 remove_exten_if_exist(context, old_ramp->exten, 1);
06429 }
06430 }
06431
06432
06433
06434
06435
06436
06437
06438
06439
06440
06441 static void destroy_space(const char *context, int space)
06442 {
06443 char exten[AST_MAX_EXTENSION];
06444
06445
06446 snprintf(exten, sizeof(exten), "%d", space);
06447 remove_exten_if_exist(context, exten, PRIORITY_HINT);
06448 remove_exten_if_exist(context, exten, 1);
06449 }
06450
06451
06452
06453
06454
06455
06456
06457
06458
06459
06460
06461
06462
06463
06464
06465
06466 static void remove_dead_spaces_usage(const char *context,
06467 struct parking_dp_space_map *old_spaces, struct parking_dp_space_map *new_spaces,
06468 void (*destroy_space)(const char *context, int space))
06469 {
06470 struct parking_dp_spaces *old_range;
06471 struct parking_dp_spaces *new_range;
06472 int space;
06473 int stop;
06474
06475 old_range = AST_LIST_FIRST(old_spaces);
06476 new_range = AST_LIST_FIRST(new_spaces);
06477 space = -1;
06478
06479 while (old_range) {
06480 if (space < old_range->start) {
06481 space = old_range->start;
06482 }
06483 if (new_range) {
06484 if (space < new_range->start) {
06485
06486 if (old_range->stop < new_range->start) {
06487
06488 stop = old_range->stop;
06489 old_range = AST_LIST_NEXT(old_range, node);
06490 } else {
06491
06492 stop = new_range->start - 1;
06493 }
06494 } else if ( space <= new_range->stop) {
06495
06496 if (old_range->stop <= new_range->stop) {
06497
06498 old_range = AST_LIST_NEXT(old_range, node);
06499 } else {
06500
06501 space = new_range->stop + 1;
06502 new_range = AST_LIST_NEXT(new_range, node);
06503 }
06504 continue;
06505 } else {
06506
06507 new_range = AST_LIST_NEXT(new_range, node);
06508 continue;
06509 }
06510 } else {
06511
06512 stop = old_range->stop;
06513 old_range = AST_LIST_NEXT(old_range, node);
06514 }
06515
06516
06517 for (; space <= stop; ++space) {
06518 destroy_space(context, space);
06519 }
06520 }
06521 }
06522
06523
06524
06525
06526
06527
06528
06529
06530
06531
06532
06533
06534
06535
06536
06537 static void remove_dead_context_usage(const char *context, struct parking_dp_context *old_ctx, struct parking_dp_context *new_ctx)
06538 {
06539 remove_dead_ramp_usage(context, &old_ctx->access_extens, &new_ctx->access_extens);
06540 remove_dead_spaces_usage(context, &old_ctx->spaces, &new_ctx->spaces, destroy_space);
06541 #if 0
06542
06543 remove_dead_spaces_usage(context, &old_ctx->hints, &new_ctx->hints, destroy_space_hint);
06544 #endif
06545 }
06546
06547
06548
06549
06550
06551
06552
06553
06554
06555
06556
06557
06558
06559
06560 static void remove_dead_dialplan_useage(struct parking_dp_map *old_map, struct parking_dp_map *new_map)
06561 {
06562 struct parking_dp_context *old_ctx;
06563 struct parking_dp_context *new_ctx;
06564 struct ast_context *con;
06565 int cmp;
06566
06567 old_ctx = AST_LIST_FIRST(old_map);
06568 new_ctx = AST_LIST_FIRST(new_map);
06569
06570 while (new_ctx) {
06571 if (!old_ctx) {
06572
06573 return;
06574 }
06575 cmp = strcmp(old_ctx->context, new_ctx->context);
06576 if (cmp < 0) {
06577
06578 con = ast_context_find(old_ctx->context);
06579 if (con) {
06580 ast_context_destroy(con, registrar);
06581 }
06582 old_ctx = AST_LIST_NEXT(old_ctx, node);
06583 continue;
06584 }
06585 if (cmp == 0) {
06586
06587 remove_dead_context_usage(old_ctx->context, old_ctx, new_ctx);
06588 old_ctx = AST_LIST_NEXT(old_ctx, node);
06589 } else {
06590
06591 }
06592 new_ctx = AST_LIST_NEXT(new_ctx, node);
06593 }
06594
06595
06596 for (; old_ctx; old_ctx = AST_LIST_NEXT(old_ctx, node)) {
06597 con = ast_context_find(old_ctx->context);
06598 if (con) {
06599 ast_context_destroy(con, registrar);
06600 }
06601 }
06602 }
06603
06604 static int parkinglot_markall_cb(void *obj, void *arg, int flags)
06605 {
06606 struct ast_parkinglot *parkinglot = obj;
06607
06608 parkinglot->the_mark = 1;
06609 return 0;
06610 }
06611
06612 static int parkinglot_is_marked_cb(void *obj, void *arg, int flags)
06613 {
06614 struct ast_parkinglot *parkinglot = obj;
06615
06616 if (parkinglot->the_mark) {
06617 if (AST_LIST_EMPTY(&parkinglot->parkings)) {
06618
06619 return CMP_MATCH;
06620 }
06621
06622 ast_log(LOG_WARNING,
06623 "Parking lot %s has parked calls. Could not remove.\n",
06624 parkinglot->name);
06625 parkinglot->disabled = 1;
06626 force_reload_load = 1;
06627 }
06628
06629 return 0;
06630 }
06631
06632 static int parkinglot_activate_cb(void *obj, void *arg, int flags)
06633 {
06634 struct ast_parkinglot *parkinglot = obj;
06635
06636 if (parkinglot->the_mark) {
06637
06638
06639
06640
06641 return 0;
06642 }
06643
06644 if (parkinglot_activate(parkinglot)) {
06645
06646
06647
06648
06649 force_reload_load = 1;
06650 ast_log(LOG_WARNING, "Parking lot %s not open for business.\n", parkinglot->name);
06651 } else {
06652 ast_debug(1, "Parking lot %s now open for business. (parkpos %d-%d)\n",
06653 parkinglot->name, parkinglot->cfg.parking_start,
06654 parkinglot->cfg.parking_stop);
06655 }
06656
06657 return 0;
06658 }
06659
06660 static int load_config(int reload)
06661 {
06662 struct ast_flags config_flags = {
06663 reload && !force_reload_load ? CONFIG_FLAG_FILEUNCHANGED : 0 };
06664 struct ast_config *cfg;
06665 struct parking_dp_map old_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
06666 struct parking_dp_map new_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
06667
06668
06669 force_reload_load = 0;
06670
06671 if (!default_parkinglot) {
06672
06673 default_parkinglot = build_parkinglot(DEFAULT_PARKINGLOT, NULL);
06674 if (!default_parkinglot) {
06675 ast_log(LOG_ERROR, "Configuration of default default parking lot failed.\n");
06676 return -1;
06677 }
06678 ast_debug(1, "Configuration of default default parking lot done.\n");
06679 }
06680
06681 cfg = ast_config_load2("features.conf", "features", config_flags);
06682 if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
06683
06684 ast_debug(1, "features.conf did not change.\n");
06685 return 0;
06686 }
06687 if (cfg == CONFIG_STATUS_FILEMISSING
06688 || cfg == CONFIG_STATUS_FILEINVALID) {
06689 ast_log(LOG_WARNING, "Could not load features.conf\n");
06690 return 0;
06691 }
06692
06693
06694 if (build_dialplan_useage_map(&old_usage_map, 0)) {
06695 destroy_dialplan_usage_map(&old_usage_map);
06696
06697
06698 force_reload_load = 1;
06699 return -1;
06700 }
06701
06702 ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_markall_cb, NULL,
06703 "callback to mark all parking lots");
06704 process_config(cfg);
06705 ast_config_destroy(cfg);
06706 ao2_t_callback(parkinglots, OBJ_NODATA | OBJ_UNLINK, parkinglot_is_marked_cb, NULL,
06707 "callback to remove marked parking lots");
06708
06709
06710 if (build_dialplan_useage_map(&new_usage_map, 1)) {
06711
06712
06713
06714
06715
06716 destroy_dialplan_usage_map(&old_usage_map);
06717 destroy_dialplan_usage_map(&new_usage_map);
06718 return -1;
06719 }
06720
06721
06722 remove_dead_dialplan_useage(&old_usage_map, &new_usage_map);
06723
06724 destroy_dialplan_usage_map(&old_usage_map);
06725 destroy_dialplan_usage_map(&new_usage_map);
06726
06727 ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_activate_cb, NULL,
06728 "callback to activate all parking lots");
06729
06730 return 0;
06731 }
06732
06733
06734
06735
06736
06737
06738
06739
06740
06741
06742 static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06743 {
06744 int i;
06745 struct ast_call_feature *feature;
06746 struct ao2_iterator iter;
06747 struct ast_parkinglot *curlot;
06748 #define HFS_FORMAT "%-25s %-7s %-7s\n"
06749
06750 switch (cmd) {
06751
06752 case CLI_INIT:
06753 e->command = "features show";
06754 e->usage =
06755 "Usage: features show\n"
06756 " Lists configured features\n";
06757 return NULL;
06758 case CLI_GENERATE:
06759 return NULL;
06760 }
06761
06762 ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
06763 ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
06764
06765 ast_cli(a->fd, HFS_FORMAT, "Pickup", "*8", ast_pickup_ext());
06766
06767 ast_rwlock_rdlock(&features_lock);
06768 for (i = 0; i < FEATURES_COUNT; i++)
06769 ast_cli(a->fd, HFS_FORMAT, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
06770 ast_rwlock_unlock(&features_lock);
06771
06772 ast_cli(a->fd, "\n");
06773 ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
06774 ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
06775 if (AST_RWLIST_EMPTY(&feature_list)) {
06776 ast_cli(a->fd, "(none)\n");
06777 } else {
06778 AST_RWLIST_RDLOCK(&feature_list);
06779 AST_RWLIST_TRAVERSE(&feature_list, feature, feature_entry) {
06780 ast_cli(a->fd, HFS_FORMAT, feature->sname, "no def", feature->exten);
06781 }
06782 AST_RWLIST_UNLOCK(&feature_list);
06783 }
06784
06785 ast_cli(a->fd, "\nFeature Groups:\n");
06786 ast_cli(a->fd, "---------------\n");
06787 if (AST_RWLIST_EMPTY(&feature_groups)) {
06788 ast_cli(a->fd, "(none)\n");
06789 } else {
06790 struct feature_group *fg;
06791 struct feature_group_exten *fge;
06792
06793 AST_RWLIST_RDLOCK(&feature_groups);
06794 AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
06795 ast_cli(a->fd, "===> Group: %s\n", fg->gname);
06796 AST_LIST_TRAVERSE(&fg->features, fge, entry) {
06797 ast_cli(a->fd, "===> --> %s (%s)\n", fge->feature->sname, fge->exten);
06798 }
06799 }
06800 AST_RWLIST_UNLOCK(&feature_groups);
06801 }
06802
06803 iter = ao2_iterator_init(parkinglots, 0);
06804 while ((curlot = ao2_iterator_next(&iter))) {
06805 ast_cli(a->fd, "\nCall parking (Parking lot: %s)\n", curlot->name);
06806 ast_cli(a->fd, "------------\n");
06807 ast_cli(a->fd,"%-22s: %s\n", "Parking extension", curlot->cfg.parkext);
06808 ast_cli(a->fd,"%-22s: %s\n", "Parking context", curlot->cfg.parking_con);
06809 ast_cli(a->fd,"%-22s: %d-%d\n", "Parked call extensions",
06810 curlot->cfg.parking_start, curlot->cfg.parking_stop);
06811 ast_cli(a->fd,"%-22s: %d ms\n", "Parkingtime", curlot->cfg.parkingtime);
06812 ast_cli(a->fd,"%-22s: %s\n", "MusicOnHold class", curlot->cfg.mohclass);
06813 ast_cli(a->fd,"%-22s: %s\n", "Enabled", AST_CLI_YESNO(!curlot->disabled));
06814 ast_cli(a->fd,"\n");
06815 ao2_ref(curlot, -1);
06816 }
06817 ao2_iterator_destroy(&iter);
06818
06819 return CLI_SUCCESS;
06820 }
06821
06822 int ast_features_reload(void)
06823 {
06824 struct ast_context *con;
06825 int res;
06826
06827 ast_mutex_lock(&features_reload_lock);
06828
06829
06830
06831
06832
06833
06834
06835
06836
06837 con = ast_context_find(parking_con_dial);
06838 if (con) {
06839 ast_context_destroy(con, registrar);
06840 }
06841
06842 res = load_config(1);
06843 ast_mutex_unlock(&features_reload_lock);
06844
06845 return res;
06846 }
06847
06848 static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
06849 {
06850 switch (cmd) {
06851 case CLI_INIT:
06852 e->command = "features reload";
06853 e->usage =
06854 "Usage: features reload\n"
06855 " Reloads configured call features from features.conf\n";
06856 return NULL;
06857 case CLI_GENERATE:
06858 return NULL;
06859 }
06860 ast_features_reload();
06861
06862 return CLI_SUCCESS;
06863 }
06864
06865
06866
06867
06868
06869
06870
06871
06872
06873
06874
06875
06876 static int do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
06877 {
06878 const char *context;
06879 const char *exten;
06880 int priority;
06881
06882 ast_moh_stop(chan);
06883 ast_channel_lock_both(chan, tmpchan);
06884 context = ast_strdupa(chan->context);
06885 exten = ast_strdupa(chan->exten);
06886 priority = chan->priority;
06887 ast_setstate(tmpchan, chan->_state);
06888 tmpchan->readformat = chan->readformat;
06889 tmpchan->writeformat = chan->writeformat;
06890 ast_channel_unlock(chan);
06891 ast_channel_unlock(tmpchan);
06892
06893
06894 if (ast_channel_masquerade(tmpchan, chan)) {
06895 return -1;
06896 }
06897 ast_do_masquerade(tmpchan);
06898
06899
06900 ast_explicit_goto(tmpchan, context, exten, priority + 1);
06901
06902 return 0;
06903 }
06904
06905
06906
06907
06908
06909
06910
06911
06912
06913
06914
06915
06916
06917
06918 static int action_bridge(struct mansession *s, const struct message *m)
06919 {
06920 const char *channela = astman_get_header(m, "Channel1");
06921 const char *channelb = astman_get_header(m, "Channel2");
06922 const char *playtone = astman_get_header(m, "Tone");
06923 struct ast_channel *chana = NULL, *chanb = NULL, *chans[2];
06924 struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
06925 struct ast_bridge_thread_obj *tobj = NULL;
06926 char buf[256];
06927
06928
06929 if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
06930 astman_send_error(s, m, "Missing channel parameter in request");
06931 return 0;
06932 }
06933
06934
06935 chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
06936 if (!chana) {
06937 snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
06938 astman_send_error(s, m, buf);
06939 return 0;
06940 }
06941
06942
06943 if (chana->_state != AST_STATE_UP)
06944 ast_answer(chana);
06945
06946
06947 if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
06948 NULL, NULL, chana->linkedid, 0, "Bridge/%s", chana->name))) {
06949 astman_send_error(s, m, "Unable to create temporary channel!");
06950 chana = ast_channel_unref(chana);
06951 return 0;
06952 }
06953
06954 if (do_bridge_masquerade(chana, tmpchana)) {
06955 snprintf(buf, sizeof(buf), "Unable to masquerade channel %s!", channela);
06956 astman_send_error(s, m, buf);
06957 ast_hangup(tmpchana);
06958 chana = ast_channel_unref(chana);
06959 return 0;
06960 }
06961
06962 chana = ast_channel_unref(chana);
06963
06964
06965 chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
06966 if (!chanb) {
06967 snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
06968 astman_send_error(s, m, buf);
06969 ast_hangup(tmpchana);
06970 return 0;
06971 }
06972
06973
06974 if (chanb->_state != AST_STATE_UP)
06975 ast_answer(chanb);
06976
06977
06978 if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
06979 NULL, NULL, chanb->linkedid, 0, "Bridge/%s", chanb->name))) {
06980 astman_send_error(s, m, "Unable to create temporary channels!");
06981 ast_hangup(tmpchana);
06982 chanb = ast_channel_unref(chanb);
06983 return 0;
06984 }
06985
06986 if (do_bridge_masquerade(chanb, tmpchanb)) {
06987 snprintf(buf, sizeof(buf), "Unable to masquerade channel %s!", channelb);
06988 astman_send_error(s, m, buf);
06989 ast_hangup(tmpchana);
06990 ast_hangup(tmpchanb);
06991 chanb = ast_channel_unref(chanb);
06992 return 0;
06993 }
06994
06995 chanb = ast_channel_unref(chanb);
06996
06997
06998 if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
06999 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", tmpchana->name, tmpchanb->name);
07000 astman_send_error(s, m, "Could not make channels compatible for manager bridge");
07001 ast_hangup(tmpchana);
07002 ast_hangup(tmpchanb);
07003 return 0;
07004 }
07005
07006
07007 if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
07008 ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", tmpchana->name, tmpchanb->name, strerror(errno));
07009 astman_send_error(s, m, "Unable to spawn a new bridge thread");
07010 ast_hangup(tmpchana);
07011 ast_hangup(tmpchanb);
07012 return 0;
07013 }
07014
07015 tobj->chan = tmpchana;
07016 tobj->peer = tmpchanb;
07017 tobj->return_to_pbx = 1;
07018
07019 if (ast_true(playtone)) {
07020 if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, tmpchanb->language)) {
07021 if (ast_waitstream(tmpchanb, "") < 0)
07022 ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", tmpchanb->name);
07023 }
07024 }
07025
07026 chans[0] = tmpchana;
07027 chans[1] = tmpchanb;
07028
07029 ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeAction", 2, chans,
07030 "Response: Success\r\n"
07031 "Channel1: %s\r\n"
07032 "Channel2: %s\r\n", tmpchana->name, tmpchanb->name);
07033
07034 bridge_call_thread_launch(tobj);
07035
07036 astman_send_ack(s, m, "Launched bridge thread with success");
07037
07038 return 0;
07039 }
07040
07041
07042
07043
07044
07045
07046
07047
07048
07049
07050
07051
07052 static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
07053 {
07054 struct parkeduser *cur;
07055 int numparked = 0;
07056 struct ao2_iterator iter;
07057 struct ast_parkinglot *curlot;
07058
07059 switch (cmd) {
07060 case CLI_INIT:
07061 e->command = "parkedcalls show";
07062 e->usage =
07063 "Usage: parkedcalls show\n"
07064 " List currently parked calls\n";
07065 return NULL;
07066 case CLI_GENERATE:
07067 return NULL;
07068 }
07069
07070 if (a->argc > e->args)
07071 return CLI_SHOWUSAGE;
07072
07073 ast_cli(a->fd, "%-10s %-25s (%-15s %-12s %4s) %s\n", "Num", "Channel",
07074 "Context", "Extension", "Pri", "Timeout");
07075
07076 iter = ao2_iterator_init(parkinglots, 0);
07077 while ((curlot = ao2_iterator_next(&iter))) {
07078 int lotparked = 0;
07079
07080
07081 ast_cli(a->fd, "*** Parking lot: %s (%d)\n", curlot->name,
07082 ao2_ref(curlot, 0) - 2 - (curlot == default_parkinglot));
07083
07084 AST_LIST_LOCK(&curlot->parkings);
07085 AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
07086 ast_cli(a->fd, "%-10.10s %-25s (%-15s %-12s %4d) %6lds\n",
07087 cur->parkingexten, cur->chan->name, cur->context, cur->exten,
07088 cur->priority,
07089 (long) (cur->start.tv_sec + (cur->parkingtime / 1000) - time(NULL)));
07090 ++lotparked;
07091 }
07092 AST_LIST_UNLOCK(&curlot->parkings);
07093 if (lotparked) {
07094 numparked += lotparked;
07095 ast_cli(a->fd, " %d parked call%s in parking lot %s\n", lotparked,
07096 ESS(lotparked), curlot->name);
07097 }
07098
07099 ao2_ref(curlot, -1);
07100 }
07101 ao2_iterator_destroy(&iter);
07102
07103 ast_cli(a->fd, "---\n%d parked call%s in total.\n", numparked, ESS(numparked));
07104
07105 return CLI_SUCCESS;
07106 }
07107
07108 static struct ast_cli_entry cli_features[] = {
07109 AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
07110 AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
07111 AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls"),
07112 };
07113
07114
07115
07116
07117
07118
07119
07120
07121
07122 static int manager_parking_status(struct mansession *s, const struct message *m)
07123 {
07124 struct parkeduser *cur;
07125 const char *id = astman_get_header(m, "ActionID");
07126 char idText[256] = "";
07127 struct ao2_iterator iter;
07128 struct ast_parkinglot *curlot;
07129 int numparked = 0;
07130
07131 if (!ast_strlen_zero(id))
07132 snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
07133
07134 astman_send_ack(s, m, "Parked calls will follow");
07135
07136 iter = ao2_iterator_init(parkinglots, 0);
07137 while ((curlot = ao2_iterator_next(&iter))) {
07138 AST_LIST_LOCK(&curlot->parkings);
07139 AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
07140 astman_append(s, "Event: ParkedCall\r\n"
07141 "Parkinglot: %s\r\n"
07142 "Exten: %d\r\n"
07143 "Channel: %s\r\n"
07144 "From: %s\r\n"
07145 "Timeout: %ld\r\n"
07146 "CallerIDNum: %s\r\n"
07147 "CallerIDName: %s\r\n"
07148 "ConnectedLineNum: %s\r\n"
07149 "ConnectedLineName: %s\r\n"
07150 "%s"
07151 "\r\n",
07152 curlot->name,
07153 cur->parkingnum, cur->chan->name, cur->peername,
07154 (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - (long) time(NULL),
07155 S_COR(cur->chan->caller.id.number.valid, cur->chan->caller.id.number.str, ""),
07156 S_COR(cur->chan->caller.id.name.valid, cur->chan->caller.id.name.str, ""),
07157 S_COR(cur->chan->connected.id.number.valid, cur->chan->connected.id.number.str, ""),
07158 S_COR(cur->chan->connected.id.name.valid, cur->chan->connected.id.name.str, ""),
07159 idText);
07160 ++numparked;
07161 }
07162 AST_LIST_UNLOCK(&curlot->parkings);
07163 ao2_ref(curlot, -1);
07164 }
07165 ao2_iterator_destroy(&iter);
07166
07167 astman_append(s,
07168 "Event: ParkedCallsComplete\r\n"
07169 "Total: %d\r\n"
07170 "%s"
07171 "\r\n",
07172 numparked, idText);
07173
07174 return RESULT_SUCCESS;
07175 }
07176
07177
07178
07179
07180
07181
07182
07183
07184
07185
07186
07187
07188 static int manager_park(struct mansession *s, const struct message *m)
07189 {
07190 const char *channel = astman_get_header(m, "Channel");
07191 const char *channel2 = astman_get_header(m, "Channel2");
07192 const char *timeout = astman_get_header(m, "Timeout");
07193 const char *parkinglotname = astman_get_header(m, "Parkinglot");
07194 char buf[BUFSIZ];
07195 int res = 0;
07196 struct ast_channel *ch1, *ch2;
07197 struct ast_park_call_args args = {
07198
07199
07200
07201
07202
07203
07204
07205
07206
07207
07208
07209
07210
07211
07212
07213
07214
07215
07216
07217 .flags = AST_PARK_OPT_SILENCE,
07218 };
07219
07220 if (ast_strlen_zero(channel)) {
07221 astman_send_error(s, m, "Channel not specified");
07222 return 0;
07223 }
07224
07225 if (ast_strlen_zero(channel2)) {
07226 astman_send_error(s, m, "Channel2 not specified");
07227 return 0;
07228 }
07229
07230 if (!ast_strlen_zero(timeout)) {
07231 if (sscanf(timeout, "%30d", &args.timeout) != 1) {
07232 astman_send_error(s, m, "Invalid timeout value.");
07233 return 0;
07234 }
07235 }
07236
07237 if (!(ch1 = ast_channel_get_by_name(channel))) {
07238 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
07239 astman_send_error(s, m, buf);
07240 return 0;
07241 }
07242
07243 if (!(ch2 = ast_channel_get_by_name(channel2))) {
07244 snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
07245 astman_send_error(s, m, buf);
07246 ast_channel_unref(ch1);
07247 return 0;
07248 }
07249
07250 if (!ast_strlen_zero(parkinglotname)) {
07251 args.parkinglot = find_parkinglot(parkinglotname);
07252 }
07253
07254 res = masq_park_call(ch1, ch2, &args);
07255 if (!res) {
07256 ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
07257 astman_send_ack(s, m, "Park successful");
07258 } else {
07259 astman_send_error(s, m, "Park failure");
07260 }
07261
07262 if (args.parkinglot) {
07263 parkinglot_unref(args.parkinglot);
07264 }
07265 ch1 = ast_channel_unref(ch1);
07266 ch2 = ast_channel_unref(ch2);
07267
07268 return 0;
07269 }
07270
07271
07272
07273
07274
07275
07276
07277 static const struct ast_datastore_info pickup_active = {
07278 .type = "pickup-active",
07279 };
07280
07281 int ast_can_pickup(struct ast_channel *chan)
07282 {
07283 if (!chan->pbx && !chan->masq && !ast_test_flag(chan, AST_FLAG_ZOMBIE)
07284 && (chan->_state == AST_STATE_RINGING
07285 || chan->_state == AST_STATE_RING
07286
07287
07288
07289
07290
07291
07292 || chan->_state == AST_STATE_DOWN)
07293 && !ast_channel_datastore_find(chan, &pickup_active, NULL)) {
07294 return 1;
07295 }
07296 return 0;
07297 }
07298
07299 static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
07300 {
07301 struct ast_channel *target = obj;
07302 struct ast_channel *chan = data;
07303
07304 ast_channel_lock(target);
07305 if (chan != target && (chan->pickupgroup & target->callgroup)
07306 && ast_can_pickup(target)) {
07307
07308 return CMP_MATCH | CMP_STOP;
07309 }
07310 ast_channel_unlock(target);
07311
07312 return 0;
07313 }
07314
07315
07316
07317
07318
07319
07320
07321
07322
07323 int ast_pickup_call(struct ast_channel *chan)
07324 {
07325 struct ast_channel *target;
07326 int res = -1;
07327 ast_debug(1, "pickup attempt by %s\n", chan->name);
07328
07329
07330 target = ast_channel_callback(find_channel_by_group, NULL, chan, 0);
07331 if (target) {
07332 ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", target->name, chan->name);
07333
07334 res = ast_do_pickup(chan, target);
07335 ast_channel_unlock(target);
07336 if (!res) {
07337 if (!ast_strlen_zero(pickupsound)) {
07338 pbx_builtin_setvar_helper(target, "BRIDGE_PLAY_SOUND", pickupsound);
07339 }
07340 } else {
07341 ast_log(LOG_WARNING, "pickup %s failed by %s\n", target->name, chan->name);
07342 }
07343 target = ast_channel_unref(target);
07344 }
07345
07346 if (res < 0) {
07347 ast_debug(1, "No call pickup possible... for %s\n", chan->name);
07348 if (!ast_strlen_zero(pickupfailsound)) {
07349 ast_answer(chan);
07350 ast_stream_and_wait(chan, pickupfailsound, "");
07351 }
07352 }
07353
07354 return res;
07355 }
07356
07357 int ast_do_pickup(struct ast_channel *chan, struct ast_channel *target)
07358 {
07359 struct ast_party_connected_line connected_caller;
07360 struct ast_channel *chans[2] = { chan, target };
07361 struct ast_datastore *ds_pickup;
07362 const char *chan_name;
07363 const char *target_name;
07364 int res = -1;
07365
07366 target_name = ast_strdupa(target->name);
07367 ast_debug(1, "Call pickup on '%s' by '%s'\n", target_name, chan->name);
07368
07369
07370 ds_pickup = ast_datastore_alloc(&pickup_active, NULL);
07371 if (!ds_pickup) {
07372 ast_log(LOG_WARNING,
07373 "Unable to create channel datastore on '%s' for call pickup\n", target_name);
07374 return -1;
07375 }
07376 ast_channel_datastore_add(target, ds_pickup);
07377
07378 ast_party_connected_line_init(&connected_caller);
07379 ast_party_connected_line_copy(&connected_caller, &target->connected);
07380 ast_channel_unlock(target);
07381 connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
07382 if (ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
07383 ast_channel_update_connected_line(chan, &connected_caller, NULL);
07384 }
07385 ast_party_connected_line_free(&connected_caller);
07386
07387 ast_channel_lock(chan);
07388 chan_name = ast_strdupa(chan->name);
07389 ast_connected_line_copy_from_caller(&connected_caller, &chan->caller);
07390 ast_channel_unlock(chan);
07391 connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
07392
07393 ast_cel_report_event(target, AST_CEL_PICKUP, NULL, NULL, chan);
07394
07395 if (ast_answer(chan)) {
07396 ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan_name);
07397 goto pickup_failed;
07398 }
07399
07400 if (ast_queue_control(chan, AST_CONTROL_ANSWER)) {
07401 ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan_name);
07402 goto pickup_failed;
07403 }
07404
07405 ast_channel_queue_connected_line_update(chan, &connected_caller, NULL);
07406
07407
07408 ast_set_flag(chan, AST_FLAG_ANSWERED_ELSEWHERE);
07409
07410 if (ast_channel_masquerade(target, chan)) {
07411 ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan_name,
07412 target_name);
07413 goto pickup_failed;
07414 }
07415
07416
07417 ast_manager_event_multichan(EVENT_FLAG_CALL, "Pickup", 2, chans,
07418 "Channel: %s\r\n"
07419 "TargetChannel: %s\r\n",
07420 chan_name, target_name);
07421
07422
07423 ast_do_masquerade(target);
07424 res = 0;
07425
07426 pickup_failed:
07427 ast_channel_lock(target);
07428 if (!ast_channel_datastore_remove(target, ds_pickup)) {
07429 ast_datastore_free(ds_pickup);
07430 }
07431 ast_party_connected_line_free(&connected_caller);
07432
07433 return res;
07434 }
07435
07436 static char *app_bridge = "Bridge";
07437
07438 enum {
07439 BRIDGE_OPT_PLAYTONE = (1 << 0),
07440 OPT_CALLEE_HANGUP = (1 << 1),
07441 OPT_CALLER_HANGUP = (1 << 2),
07442 OPT_DURATION_LIMIT = (1 << 3),
07443 OPT_DURATION_STOP = (1 << 4),
07444 OPT_CALLEE_TRANSFER = (1 << 5),
07445 OPT_CALLER_TRANSFER = (1 << 6),
07446 OPT_CALLEE_MONITOR = (1 << 7),
07447 OPT_CALLER_MONITOR = (1 << 8),
07448 OPT_CALLEE_PARK = (1 << 9),
07449 OPT_CALLER_PARK = (1 << 10),
07450 OPT_CALLEE_KILL = (1 << 11),
07451 };
07452
07453 enum {
07454 OPT_ARG_DURATION_LIMIT = 0,
07455 OPT_ARG_DURATION_STOP,
07456
07457 OPT_ARG_ARRAY_SIZE,
07458 };
07459
07460 AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
07461 AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
07462 AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
07463 AST_APP_OPTION('H', OPT_CALLER_HANGUP),
07464 AST_APP_OPTION('k', OPT_CALLEE_PARK),
07465 AST_APP_OPTION('K', OPT_CALLER_PARK),
07466 AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
07467 AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
07468 AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
07469 AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
07470 AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
07471 AST_APP_OPTION('W', OPT_CALLER_MONITOR),
07472 AST_APP_OPTION('x', OPT_CALLEE_KILL),
07473 END_OPTIONS );
07474
07475 int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
07476 char *parse, struct timeval *calldurationlimit)
07477 {
07478 char *stringp = ast_strdupa(parse);
07479 char *limit_str, *warning_str, *warnfreq_str;
07480 const char *var;
07481 int play_to_caller = 0, play_to_callee = 0;
07482 int delta;
07483
07484 limit_str = strsep(&stringp, ":");
07485 warning_str = strsep(&stringp, ":");
07486 warnfreq_str = strsep(&stringp, ":");
07487
07488 config->timelimit = atol(limit_str);
07489 if (warning_str)
07490 config->play_warning = atol(warning_str);
07491 if (warnfreq_str)
07492 config->warning_freq = atol(warnfreq_str);
07493
07494 if (!config->timelimit) {
07495 ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
07496 config->timelimit = config->play_warning = config->warning_freq = 0;
07497 config->warning_sound = NULL;
07498 return -1;
07499 } else if ( (delta = config->play_warning - config->timelimit) > 0) {
07500 int w = config->warning_freq;
07501
07502
07503
07504
07505
07506
07507
07508
07509
07510
07511
07512
07513
07514
07515
07516 if (w == 0) {
07517 config->play_warning = 0;
07518 } else {
07519 config->play_warning -= w * ( 1 + (delta-1)/w );
07520 if (config->play_warning < 1)
07521 config->play_warning = config->warning_freq = 0;
07522 }
07523 }
07524
07525 ast_channel_lock(chan);
07526
07527 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
07528 play_to_caller = var ? ast_true(var) : 1;
07529
07530 var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
07531 play_to_callee = var ? ast_true(var) : 0;
07532
07533 if (!play_to_caller && !play_to_callee)
07534 play_to_caller = 1;
07535
07536 var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
07537 config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
07538
07539
07540
07541
07542
07543
07544
07545 var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
07546 config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
07547
07548 var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
07549 config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
07550
07551 ast_channel_unlock(chan);
07552
07553
07554 calldurationlimit->tv_sec = 0;
07555 calldurationlimit->tv_usec = 0;
07556
07557
07558 if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
07559 calldurationlimit->tv_sec = config->timelimit / 1000;
07560 calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
07561 ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
07562 calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
07563 config->timelimit = play_to_caller = play_to_callee =
07564 config->play_warning = config->warning_freq = 0;
07565 } else {
07566 ast_verb(4, "Limit Data for this call:\n");
07567 ast_verb(4, "timelimit = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
07568 ast_verb(4, "play_warning = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
07569 ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
07570 ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
07571 ast_verb(4, "warning_freq = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
07572 ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
07573 ast_verb(4, "warning_sound = %s\n", config->warning_sound);
07574 ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
07575 }
07576 if (play_to_caller)
07577 ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
07578 if (play_to_callee)
07579 ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
07580 return 0;
07581 }
07582
07583
07584
07585
07586
07587
07588
07589
07590
07591
07592
07593 static int bridge_exec(struct ast_channel *chan, const char *data)
07594 {
07595 struct ast_channel *current_dest_chan, *final_dest_chan, *chans[2];
07596 char *tmp_data = NULL;
07597 struct ast_flags opts = { 0, };
07598 struct ast_bridge_config bconfig = { { 0, }, };
07599 char *opt_args[OPT_ARG_ARRAY_SIZE];
07600 struct timeval calldurationlimit = { 0, };
07601
07602 AST_DECLARE_APP_ARGS(args,
07603 AST_APP_ARG(dest_chan);
07604 AST_APP_ARG(options);
07605 );
07606
07607 if (ast_strlen_zero(data)) {
07608 ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
07609 return -1;
07610 }
07611
07612 tmp_data = ast_strdupa(data);
07613 AST_STANDARD_APP_ARGS(args, tmp_data);
07614 if (!ast_strlen_zero(args.options))
07615 ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
07616
07617
07618 if (!strcmp(chan->name, args.dest_chan)) {
07619 ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", chan->name);
07620 ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07621 "Response: Failed\r\n"
07622 "Reason: Unable to bridge channel to itself\r\n"
07623 "Channel1: %s\r\n"
07624 "Channel2: %s\r\n",
07625 chan->name, args.dest_chan);
07626 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
07627 return 0;
07628 }
07629
07630
07631 if (!(current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
07632 strlen(args.dest_chan)))) {
07633 ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
07634 args.dest_chan);
07635 ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07636 "Response: Failed\r\n"
07637 "Reason: Channel2 does not exist\r\n"
07638 "Channel1: %s\r\n"
07639 "Channel2: %s\r\n", chan->name, args.dest_chan);
07640 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
07641 return 0;
07642 }
07643
07644
07645 if (!(final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
07646 NULL, NULL, current_dest_chan->linkedid, 0, "Bridge/%s", current_dest_chan->name))) {
07647 ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
07648 ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07649 "Response: Failed\r\n"
07650 "Reason: Cannot create placeholder channel\r\n"
07651 "Channel1: %s\r\n"
07652 "Channel2: %s\r\n", chan->name, args.dest_chan);
07653 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
07654 ast_channel_unref(current_dest_chan);
07655 return 0;
07656 }
07657
07658 if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
07659 && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
07660 && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
07661 ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07662 "Response: Failed\r\n"
07663 "Reason: Cannot setup bridge time limit\r\n"
07664 "Channel1: %s\r\n"
07665 "Channel2: %s\r\n", chan->name, args.dest_chan);
07666 ast_hangup(final_dest_chan);
07667 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
07668 current_dest_chan = ast_channel_unref(current_dest_chan);
07669 goto done;
07670 }
07671
07672 if (do_bridge_masquerade(current_dest_chan, final_dest_chan)) {
07673 ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
07674 "Response: Failed\r\n"
07675 "Reason: Cannot masquerade channels\r\n"
07676 "Channel1: %s\r\n"
07677 "Channel2: %s\r\n", chan->name, args.dest_chan);
07678 ast_hangup(final_dest_chan);
07679 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
07680 current_dest_chan = ast_channel_unref(current_dest_chan);
07681 goto done;
07682 }
07683
07684
07685 if (final_dest_chan->_state != AST_STATE_UP) {
07686 ast_answer(final_dest_chan);
07687 }
07688
07689 chans[0] = current_dest_chan;
07690 chans[1] = final_dest_chan;
07691
07692
07693
07694 if (ast_channel_make_compatible(chan, final_dest_chan) < 0) {
07695 ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", chan->name, final_dest_chan->name);
07696 ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
07697 "Response: Failed\r\n"
07698 "Reason: Could not make channels compatible for bridge\r\n"
07699 "Channel1: %s\r\n"
07700 "Channel2: %s\r\n", chan->name, final_dest_chan->name);
07701
07702
07703 ast_hangup(final_dest_chan);
07704
07705 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
07706 current_dest_chan = ast_channel_unref(current_dest_chan);
07707 goto done;
07708 }
07709
07710
07711 ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
07712 "Response: Success\r\n"
07713 "Channel1: %s\r\n"
07714 "Channel2: %s\r\n", chan->name, final_dest_chan->name);
07715
07716 current_dest_chan = ast_channel_unref(current_dest_chan);
07717
07718
07719 if (ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE) && !ast_strlen_zero(xfersound)) {
07720 if (!ast_streamfile(final_dest_chan, xfersound, final_dest_chan->language)) {
07721 if (ast_waitstream(final_dest_chan, "") < 0)
07722 ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", final_dest_chan->name);
07723 }
07724 }
07725
07726 if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
07727 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
07728 if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
07729 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
07730 if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
07731 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
07732 if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
07733 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
07734 if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
07735 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
07736 if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
07737 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
07738 if (ast_test_flag(&opts, OPT_CALLEE_PARK))
07739 ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
07740 if (ast_test_flag(&opts, OPT_CALLER_PARK))
07741 ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
07742
07743
07744
07745
07746
07747 ast_set_flag(chan, AST_FLAG_BRIDGE_HANGUP_DONT);
07748 ast_bridge_call(chan, final_dest_chan, &bconfig);
07749
07750
07751 pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
07752
07753
07754 if (!ast_check_hangup(final_dest_chan)) {
07755 if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
07756 ast_debug(1, "starting new PBX in %s,%s,%d for chan %s\n",
07757 final_dest_chan->context, final_dest_chan->exten,
07758 final_dest_chan->priority, final_dest_chan->name);
07759
07760 if (ast_pbx_start(final_dest_chan)) {
07761 ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", final_dest_chan->name);
07762 ast_hangup(final_dest_chan);
07763 } else {
07764 ast_debug(1, "SUCCESS continuing PBX on chan %s\n", final_dest_chan->name);
07765 }
07766 } else {
07767 ast_hangup(final_dest_chan);
07768 }
07769 } else {
07770 ast_debug(1, "chan %s was hungup\n", final_dest_chan->name);
07771 ast_hangup(final_dest_chan);
07772 }
07773 done:
07774 ast_free((char *) bconfig.warning_sound);
07775 ast_free((char *) bconfig.end_sound);
07776 ast_free((char *) bconfig.start_sound);
07777
07778 return 0;
07779 }
07780
07781 #if defined(TEST_FRAMEWORK)
07782
07783
07784
07785
07786
07787
07788
07789
07790
07791 static void create_spaces_str(struct ast_str **str, struct parking_dp_space_map *spaces)
07792 {
07793 const char *comma;
07794 struct parking_dp_spaces *cur;
07795
07796 ast_str_reset(*str);
07797 comma = "";
07798 AST_LIST_TRAVERSE(spaces, cur, node) {
07799 if (cur->start == cur->stop) {
07800 ast_str_append(str, 0, "%s%d", comma, cur->start);
07801 } else {
07802 ast_str_append(str, 0, "%s%d-%d", comma, cur->start, cur->stop);
07803 }
07804 comma = ",";
07805 }
07806 }
07807 #endif
07808
07809 #if defined(TEST_FRAMEWORK)
07810
07811
07812
07813
07814
07815
07816
07817
07818
07819
07820
07821
07822 static int check_spaces(struct ast_test *test, struct parking_dp_space_map *spaces, const char *expected, const char *what)
07823 {
07824 int cmp;
07825 struct ast_str *str = ast_str_alloca(1024);
07826
07827 create_spaces_str(&str, spaces);
07828 cmp = strcmp(expected, ast_str_buffer(str));
07829 if (cmp) {
07830 ast_test_status_update(test,
07831 "Unexpected parking space map for %s. Expect:'%s' Got:'%s'\n",
07832 what, expected, ast_str_buffer(str));
07833 }
07834 return cmp;
07835 }
07836 #endif
07837
07838 #if defined(TEST_FRAMEWORK)
07839
07840
07841
07842
07843
07844
07845
07846
07847
07848 static void test_add_dead_space(const char *context, int space)
07849 {
07850 struct parking_dp_space_map *dead_spaces = (struct parking_dp_space_map *) context;
07851
07852 usage_context_add_spaces(dead_spaces, space, space, NULL, 0);
07853 }
07854 #endif
07855
07856 #if defined(TEST_FRAMEWORK)
07857 struct test_map {
07858 const char *ramp;
07859 int start;
07860 int stop;
07861 const char *expect;
07862 };
07863
07864
07865
07866
07867
07868
07869
07870
07871
07872
07873
07874
07875
07876
07877 static struct parking_dp_context *test_build_maps(struct ast_test *test,
07878 struct ast_parkinglot *lot, const char *table_name, const struct test_map *table,
07879 size_t num_entries)
07880 {
07881 struct parking_dp_context *ctx_node;
07882 int cur_index = 0;
07883 char what[40];
07884
07885 snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
07886 ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
07887 lot->cfg.parking_start = table->start;
07888 lot->cfg.parking_stop = table->stop;
07889 ctx_node = build_dialplan_useage_context(lot);
07890 if (!ctx_node) {
07891 ast_test_status_update(test, "Failed to create parking lot context map for %s\n",
07892 what);
07893 return NULL;
07894 }
07895 if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
07896 destroy_dialplan_usage_context(ctx_node);
07897 return NULL;
07898 }
07899 while (--num_entries) {
07900 ++cur_index;
07901 ++table;
07902 snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
07903 ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
07904 lot->cfg.parking_start = table->start;
07905 lot->cfg.parking_stop = table->stop;
07906 if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 1)) {
07907 ast_test_status_update(test, "Failed to add parking lot data for %s\n", what);
07908 destroy_dialplan_usage_context(ctx_node);
07909 return NULL;
07910 }
07911 if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
07912 destroy_dialplan_usage_context(ctx_node);
07913 return NULL;
07914 }
07915 }
07916 return ctx_node;
07917 }
07918
07919 static const struct test_map test_old_ctx[] = {
07920
07921 { "702", 14, 15, "14-15" },
07922 { "700", 10, 11, "10-11,14-15" },
07923 { "701", 18, 19, "10-11,14-15,18-19" },
07924 { "703", 12, 13, "10-15,18-19" },
07925 { "704", 16, 17, "10-19" },
07926
07927
07928 { "704", 9, 19, "9-19" },
07929 { "704", 9, 20, "9-20" },
07930 { "704", 8, 21, "8-21" },
07931
07932
07933 { "705", 23, 25, "8-21,23-25" },
07934 { "706", 28, 31, "8-21,23-25,28-31" },
07935 { "707", 33, 34, "8-21,23-25,28-31,33-34" },
07936 { "708", 38, 40, "8-21,23-25,28-31,33-34,38-40" },
07937 { "709", 42, 43, "8-21,23-25,28-31,33-34,38-40,42-43" },
07938 };
07939
07940 static const struct test_map test_new_ctx[] = {
07941 { "702", 4, 5, "4-5" },
07942 { "704", 24, 26, "4-5,24-26" },
07943 { "709", 29, 30, "4-5,24-26,29-30" },
07944 { "710", 32, 35, "4-5,24-26,29-30,32-35" },
07945 { "711", 37, 39, "4-5,24-26,29-30,32-35,37-39" },
07946 };
07947 #endif
07948
07949 #if defined(TEST_FRAMEWORK)
07950
07951
07952
07953
07954
07955
07956
07957
07958
07959 static int test_dialplan_usage_map(struct ast_test *test)
07960 {
07961 struct parking_dp_context *old_ctx;
07962 struct parking_dp_context *new_ctx;
07963 struct ast_parkinglot *lot;
07964 struct parking_dp_spaces *spaces;
07965 struct parking_dp_space_map dead_spaces = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
07966 int res;
07967
07968 ast_test_status_update(test, "Test parking dialplan usage map code\n");
07969
07970 lot = create_parkinglot("test_lot");
07971 if (!lot) {
07972 return -1;
07973 }
07974 ast_copy_string(lot->cfg.parking_con, "test-ctx", sizeof(lot->cfg.parking_con));
07975 lot->cfg.parkext_exclusive = 1;
07976
07977 ast_test_status_update(test,
07978 "Build old_ctx map\n");
07979 ast_log(LOG_NOTICE, "6 Ramp and space conflict warnings are expected.\n");
07980 old_ctx = test_build_maps(test, lot, "test_old_ctx", test_old_ctx,
07981 ARRAY_LEN(test_old_ctx));
07982 if (!old_ctx) {
07983 ao2_ref(lot, -1);
07984 return -1;
07985 }
07986
07987 ast_test_status_update(test, "Build new_ctx map\n");
07988 new_ctx = test_build_maps(test, lot, "test_new_ctx", test_new_ctx,
07989 ARRAY_LEN(test_new_ctx));
07990 if (!new_ctx) {
07991 res = -1;
07992 goto fail_old_ctx;
07993 }
07994
07995 ast_test_status_update(test, "Test removing dead parking spaces\n");
07996 remove_dead_spaces_usage((void *) &dead_spaces, &old_ctx->spaces,
07997 &new_ctx->spaces, test_add_dead_space);
07998 if (check_spaces(test, &dead_spaces, "8-21,23,28,31,40,42-43", "dead_spaces")) {
07999 res = -1;
08000 goto fail_dead_spaces;
08001 }
08002
08003 res = 0;
08004
08005 fail_dead_spaces:
08006 while ((spaces = AST_LIST_REMOVE_HEAD(&dead_spaces, node))) {
08007 ast_free(spaces);
08008 }
08009 destroy_dialplan_usage_context(new_ctx);
08010
08011 fail_old_ctx:
08012 destroy_dialplan_usage_context(old_ctx);
08013 ao2_ref(lot, -1);
08014 return res;
08015 }
08016 #endif
08017
08018 #if defined(TEST_FRAMEWORK)
08019 static int fake_fixup(struct ast_channel *clonechan, struct ast_channel *original)
08020 {
08021 return 0;
08022 }
08023 #endif
08024
08025 #if defined(TEST_FRAMEWORK)
08026 static struct ast_channel *create_test_channel(const struct ast_channel_tech *fake_tech)
08027 {
08028 struct ast_channel *test_channel1;
08029
08030 if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
08031 NULL, NULL, 0, 0, "TestChannel1"))) {
08032 ast_log(LOG_WARNING, "Whoa, test channel creation failed.\n");
08033 return NULL;
08034 }
08035
08036
08037 test_channel1->nativeformats = AST_FORMAT_GSM;
08038 test_channel1->writeformat = AST_FORMAT_GSM;
08039 test_channel1->rawwriteformat = AST_FORMAT_GSM;
08040 test_channel1->readformat = AST_FORMAT_GSM;
08041 test_channel1->rawreadformat = AST_FORMAT_GSM;
08042 test_channel1->tech = fake_tech;
08043
08044 return test_channel1;
08045 }
08046 #endif
08047
08048 #if defined(TEST_FRAMEWORK)
08049 static int unpark_test_channel(struct ast_channel *toremove, struct ast_park_call_args *args)
08050 {
08051 struct ast_context *con;
08052 struct parkeduser *pu_toremove;
08053 int res = 0;
08054
08055 args->pu->notquiteyet = 1;
08056
08057 AST_LIST_LOCK(&args->pu->parkinglot->parkings);
08058 AST_LIST_TRAVERSE_SAFE_BEGIN(&args->pu->parkinglot->parkings, pu_toremove, list) {
08059 if (pu_toremove == args->pu) {
08060 AST_LIST_REMOVE_CURRENT(list);
08061 break;
08062 }
08063 }
08064 AST_LIST_TRAVERSE_SAFE_END;
08065 AST_LIST_UNLOCK(&args->pu->parkinglot->parkings);
08066
08067 if (!pu_toremove) {
08068 ast_log(LOG_WARNING, "Whoa, could not find parking test call!\n");
08069 return -1;
08070 }
08071
08072 con = ast_context_find(args->pu->parkinglot->cfg.parking_con);
08073 if (con) {
08074 if (ast_context_remove_extension2(con, args->pu->parkingexten, 1, NULL, 0)) {
08075 ast_log(LOG_WARNING, "Whoa, failed to remove the parking extension!\n");
08076 res = -1;
08077 } else {
08078 notify_metermaids(args->pu->parkingexten,
08079 pu_toremove->parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
08080 }
08081 } else {
08082 ast_log(LOG_WARNING, "Whoa, no parking context?\n");
08083 res = -1;
08084 }
08085
08086 parkinglot_unref(pu_toremove->parkinglot);
08087 ast_free(pu_toremove);
08088 args->pu = NULL;
08089
08090 if (!res && toremove) {
08091 ast_hangup(toremove);
08092 }
08093 return res;
08094 }
08095 #endif
08096
08097 #if defined(TEST_FRAMEWORK)
08098 AST_TEST_DEFINE(features_test)
08099 {
08100 struct ast_channel *test_channel1 = NULL;
08101 struct ast_channel *parked_chan = NULL;
08102 struct ast_parkinglot *dynlot;
08103 struct ast_park_call_args args = {
08104 .timeout = DEFAULT_PARK_TIME,
08105 };
08106
08107 int res = 0;
08108
08109 static const struct ast_channel_tech fake_tech = {
08110 .fixup = fake_fixup,
08111 };
08112
08113 static const char unique_lot_1[] = "myuniquetestparkinglot314";
08114 static const char unique_lot_2[] = "myuniquetestparkinglot3141592654";
08115 static const char unique_context_1[] = "myuniquetestcontext314";
08116 static const char unique_context_2[] = "myuniquetestcontext3141592654";
08117 static const char parkinglot_parkext[] = "750";
08118 static const char parkinglot_range[] = "751-760";
08119
08120 switch (cmd) {
08121 case TEST_INIT:
08122 info->name = "features_test";
08123 info->category = "/main/features/";
08124 info->summary = "Features unit test";
08125 info->description =
08126 "Tests whether parking respects PARKINGLOT settings";
08127 return AST_TEST_NOT_RUN;
08128 case TEST_EXECUTE:
08129 break;
08130 }
08131
08132 if (test_dialplan_usage_map(test)) {
08133 res = -1;
08134 goto exit_features_test;
08135 }
08136
08137
08138 parkeddynamic = 1;
08139
08140 ast_test_status_update(test, "Test parking functionality with defaults\n");
08141 if (!(test_channel1 = create_test_channel(&fake_tech))) {
08142 res = -1;
08143 goto exit_features_test;
08144 }
08145 if (park_call_full(test_channel1, NULL, &args)) {
08146 res = -1;
08147 goto exit_features_test;
08148 }
08149 if (unpark_test_channel(test_channel1, &args)) {
08150 res = -1;
08151 goto exit_features_test;
08152 }
08153
08154
08155 ast_test_status_update(test, "Check that certain parking options are respected\n");
08156 if (!(test_channel1 = create_test_channel(&fake_tech))) {
08157 res = -1;
08158 goto exit_features_test;
08159 }
08160 pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_1);
08161 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_1);
08162 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
08163 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
08164 if (park_call_full(test_channel1, NULL, &args)) {
08165 res = -1;
08166 goto exit_features_test;
08167 }
08168
08169 dynlot = args.pu->parkinglot;
08170 if (args.pu->parkingnum != 751
08171 || strcmp(dynlot->name, unique_lot_1)
08172 || strcmp(dynlot->cfg.parking_con, unique_context_1)
08173 || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
08174 || dynlot->cfg.parking_start != 751
08175 || dynlot->cfg.parking_stop != 760) {
08176 ast_test_status_update(test, "Parking settings were not respected\n");
08177 ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
08178 ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
08179 ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
08180 ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
08181 dynlot->cfg.parking_stop);
08182 ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
08183 if (!unpark_test_channel(test_channel1, &args)) {
08184 test_channel1 = NULL;
08185 }
08186 res = -1;
08187 goto exit_features_test;
08188 } else {
08189 ast_test_status_update(test, "Parking settings for non-masquerading park verified\n");
08190 }
08191 if (unpark_test_channel(test_channel1, &args)) {
08192 res = -1;
08193 goto exit_features_test;
08194 }
08195
08196
08197 ast_test_status_update(test, "Check #2 that certain parking options are respected\n");
08198 if (!(test_channel1 = create_test_channel(&fake_tech))) {
08199 res = -1;
08200 goto exit_features_test;
08201 }
08202 pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_2);
08203 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_2);
08204 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
08205 pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
08206 if (masq_park_call(test_channel1, NULL, &args)) {
08207 res = -1;
08208 goto exit_features_test;
08209 }
08210
08211 ast_hangup(test_channel1);
08212 test_channel1 = NULL;
08213
08214 dynlot = args.pu->parkinglot;
08215 if (args.pu->parkingnum != 751
08216 || strcmp(dynlot->name, unique_lot_2)
08217 || strcmp(dynlot->cfg.parking_con, unique_context_2)
08218 || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
08219 || dynlot->cfg.parking_start != 751
08220 || dynlot->cfg.parking_stop != 760) {
08221 ast_test_status_update(test, "Parking settings were not respected\n");
08222 ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
08223 ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
08224 ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
08225 ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
08226 dynlot->cfg.parking_stop);
08227 ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
08228 res = -1;
08229 } else {
08230 ast_test_status_update(test, "Parking settings for masquerading park verified\n");
08231 }
08232
08233
08234 parked_chan = ast_channel_get_by_name("TestChannel1");
08235 if (unpark_test_channel(parked_chan, &args)) {
08236 if (parked_chan) {
08237 ast_hangup(parked_chan);
08238 }
08239 res = -1;
08240 }
08241
08242
08243 exit_features_test:
08244
08245 if (test_channel1) {
08246 ast_hangup(test_channel1);
08247 }
08248
08249 force_reload_load = 1;
08250 ast_features_reload();
08251 return res ? AST_TEST_FAIL : AST_TEST_PASS;
08252 }
08253 #endif
08254
08255
08256 static void features_shutdown(void)
08257 {
08258 ast_devstate_prov_del("Park");
08259 ast_manager_unregister("Bridge");
08260 ast_manager_unregister("Park");
08261 ast_manager_unregister("Parkinglots");
08262 ast_manager_unregister("ParkedCalls");
08263 ast_unregister_application(parkcall);
08264 ast_unregister_application(parkedcall);
08265 ast_unregister_application(app_bridge);
08266
08267 pthread_cancel(parking_thread);
08268 ao2_ref(parkinglots, -1);
08269 }
08270
08271 int ast_features_init(void)
08272 {
08273 int res;
08274
08275 parkinglots = ao2_container_alloc(7, parkinglot_hash_cb, parkinglot_cmp_cb);
08276 if (!parkinglots) {
08277 return -1;
08278 }
08279
08280 res = load_config(0);
08281 if (res) {
08282 return res;
08283 }
08284 ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features));
08285 if (ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL)) {
08286 return -1;
08287 }
08288 ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
08289 res = ast_register_application2(parkedcall, parked_call_exec, NULL, NULL, NULL);
08290 if (!res)
08291 res = ast_register_application2(parkcall, park_call_exec, NULL, NULL, NULL);
08292 if (!res) {
08293 ast_manager_register_xml("ParkedCalls", 0, manager_parking_status);
08294 ast_manager_register_xml("Park", EVENT_FLAG_CALL, manager_park);
08295 ast_manager_register_xml("Bridge", EVENT_FLAG_CALL, action_bridge);
08296 }
08297
08298 res |= ast_devstate_prov_add("Park", metermaidstate);
08299 #if defined(TEST_FRAMEWORK)
08300 res |= AST_TEST_REGISTER(features_test);
08301 #endif
08302
08303 ast_register_atexit(features_shutdown);
08304
08305 return res;
08306 }