00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "asterisk.h"
00037
00038 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
00039
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include <string.h>
00043 #include <unistd.h>
00044 #include <errno.h>
00045 #include <sys/ioctl.h>
00046 #include <sys/stat.h>
00047 #include <sys/types.h>
00048
00049 #include "asterisk/lock.h"
00050 #include "asterisk/file.h"
00051 #include "asterisk/logger.h"
00052 #include "asterisk/channel.h"
00053 #include "asterisk/pbx.h"
00054 #include "asterisk/module.h"
00055 #include "asterisk/config.h"
00056 #include "asterisk/app.h"
00057 #include "asterisk/dsp.h"
00058 #include "asterisk/musiconhold.h"
00059 #include "asterisk/manager.h"
00060 #include "asterisk/options.h"
00061 #include "asterisk/cli.h"
00062 #include "asterisk/say.h"
00063 #include "asterisk/utils.h"
00064 #include "asterisk/translate.h"
00065 #include "asterisk/ulaw.h"
00066 #include "asterisk/astobj.h"
00067 #include "asterisk/astobj2.h"
00068 #include "asterisk/devicestate.h"
00069 #include "asterisk/dial.h"
00070 #include "asterisk/causes.h"
00071
00072 #include "asterisk/dahdi_compat.h"
00073
00074 #include "enter.h"
00075 #include "leave.h"
00076
00077 #define CONFIG_FILE_NAME "meetme.conf"
00078 #define SLA_CONFIG_FILE "sla.conf"
00079
00080
00081 #define DEFAULT_AUDIO_BUFFERS 32
00082
00083 enum {
00084 ADMINFLAG_MUTED = (1 << 1),
00085 ADMINFLAG_SELFMUTED = (1 << 2),
00086 ADMINFLAG_KICKME = (1 << 3)
00087 };
00088
00089 #define MEETME_DELAYDETECTTALK 300
00090 #define MEETME_DELAYDETECTENDTALK 1000
00091
00092 #define AST_FRAME_BITS 32
00093
00094 enum volume_action {
00095 VOL_UP,
00096 VOL_DOWN
00097 };
00098
00099 enum entrance_sound {
00100 ENTER,
00101 LEAVE
00102 };
00103
00104 enum recording_state {
00105 MEETME_RECORD_OFF,
00106 MEETME_RECORD_STARTED,
00107 MEETME_RECORD_ACTIVE,
00108 MEETME_RECORD_TERMINATE
00109 };
00110
00111 #define CONF_SIZE 320
00112
00113 enum {
00114
00115 CONFFLAG_ADMIN = (1 << 0),
00116
00117 CONFFLAG_MONITOR = (1 << 1),
00118
00119 CONFFLAG_POUNDEXIT = (1 << 2),
00120
00121 CONFFLAG_STARMENU = (1 << 3),
00122
00123 CONFFLAG_TALKER = (1 << 4),
00124
00125 CONFFLAG_QUIET = (1 << 5),
00126
00127
00128 CONFFLAG_ANNOUNCEUSERCOUNT = (1 << 6),
00129
00130 CONFFLAG_AGI = (1 << 7),
00131
00132 CONFFLAG_MOH = (1 << 8),
00133
00134 CONFFLAG_MARKEDEXIT = (1 << 9),
00135
00136 CONFFLAG_WAITMARKED = (1 << 10),
00137
00138 CONFFLAG_EXIT_CONTEXT = (1 << 11),
00139
00140 CONFFLAG_MARKEDUSER = (1 << 12),
00141
00142 CONFFLAG_INTROUSER = (1 << 13),
00143
00144 CONFFLAG_RECORDCONF = (1<< 14),
00145
00146 CONFFLAG_MONITORTALKER = (1 << 15),
00147 CONFFLAG_DYNAMIC = (1 << 16),
00148 CONFFLAG_DYNAMICPIN = (1 << 17),
00149 CONFFLAG_EMPTY = (1 << 18),
00150 CONFFLAG_EMPTYNOPIN = (1 << 19),
00151 CONFFLAG_ALWAYSPROMPT = (1 << 20),
00152
00153 CONFFLAG_OPTIMIZETALKER = (1 << 21),
00154
00155
00156 CONFFLAG_NOONLYPERSON = (1 << 22),
00157
00158
00159 CONFFLAG_INTROUSERNOREVIEW = (1 << 23),
00160
00161 CONFFLAG_STARTMUTED = (1 << 24),
00162
00163 CONFFLAG_PASS_DTMF = (1 << 25),
00164
00165 CONFFLAG_SLA_STATION = (1 << 26),
00166
00167 CONFFLAG_SLA_TRUNK = (1 << 27),
00168
00169 CONFFLAG_NO_AUDIO_UNTIL_UP = (1 << 28),
00170 };
00171
00172 enum {
00173 OPT_ARG_WAITMARKED = 0,
00174 OPT_ARG_ARRAY_SIZE = 1,
00175 };
00176
00177 AST_APP_OPTIONS(meetme_opts, BEGIN_OPTIONS
00178 AST_APP_OPTION('A', CONFFLAG_MARKEDUSER ),
00179 AST_APP_OPTION('a', CONFFLAG_ADMIN ),
00180 AST_APP_OPTION('b', CONFFLAG_AGI ),
00181 AST_APP_OPTION('c', CONFFLAG_ANNOUNCEUSERCOUNT ),
00182 AST_APP_OPTION('D', CONFFLAG_DYNAMICPIN ),
00183 AST_APP_OPTION('d', CONFFLAG_DYNAMIC ),
00184 AST_APP_OPTION('E', CONFFLAG_EMPTYNOPIN ),
00185 AST_APP_OPTION('e', CONFFLAG_EMPTY ),
00186 AST_APP_OPTION('F', CONFFLAG_PASS_DTMF ),
00187 AST_APP_OPTION('i', CONFFLAG_INTROUSER ),
00188 AST_APP_OPTION('I', CONFFLAG_INTROUSERNOREVIEW ),
00189 AST_APP_OPTION('M', CONFFLAG_MOH ),
00190 AST_APP_OPTION('m', CONFFLAG_STARTMUTED ),
00191 AST_APP_OPTION('o', CONFFLAG_OPTIMIZETALKER ),
00192 AST_APP_OPTION('P', CONFFLAG_ALWAYSPROMPT ),
00193 AST_APP_OPTION('p', CONFFLAG_POUNDEXIT ),
00194 AST_APP_OPTION('q', CONFFLAG_QUIET ),
00195 AST_APP_OPTION('r', CONFFLAG_RECORDCONF ),
00196 AST_APP_OPTION('s', CONFFLAG_STARMENU ),
00197 AST_APP_OPTION('T', CONFFLAG_MONITORTALKER ),
00198 AST_APP_OPTION('l', CONFFLAG_MONITOR ),
00199 AST_APP_OPTION('t', CONFFLAG_TALKER ),
00200 AST_APP_OPTION_ARG('w', CONFFLAG_WAITMARKED, OPT_ARG_WAITMARKED ),
00201 AST_APP_OPTION('X', CONFFLAG_EXIT_CONTEXT ),
00202 AST_APP_OPTION('x', CONFFLAG_MARKEDEXIT ),
00203 AST_APP_OPTION('1', CONFFLAG_NOONLYPERSON ),
00204 END_OPTIONS );
00205
00206 static const char *app = "MeetMe";
00207 static const char *app2 = "MeetMeCount";
00208 static const char *app3 = "MeetMeAdmin";
00209 static const char *slastation_app = "SLAStation";
00210 static const char *slatrunk_app = "SLATrunk";
00211
00212 static const char *synopsis = "MeetMe conference bridge";
00213 static const char *synopsis2 = "MeetMe participant count";
00214 static const char *synopsis3 = "MeetMe conference Administration";
00215 static const char *slastation_synopsis = "Shared Line Appearance Station";
00216 static const char *slatrunk_synopsis = "Shared Line Appearance Trunk";
00217
00218 static const char *descrip =
00219 " MeetMe([confno][,[options][,pin]]): Enters the user into a specified MeetMe\n"
00220 "conference. If the conference number is omitted, the user will be prompted\n"
00221 "to enter one. User can exit the conference by hangup, or if the 'p' option\n"
00222 "is specified, by pressing '#'.\n"
00223 "Please note: The DAHDI kernel modules and at least one hardware driver (or dahdi_dummy)\n"
00224 " must be present for conferencing to operate properly. In addition, the chan_dahdi\n"
00225 " channel driver must be loaded for the 'i' and 'r' options to operate at all.\n\n"
00226 "The option string may contain zero or more of the following characters:\n"
00227 " 'a' -- set admin mode\n"
00228 " 'A' -- set marked mode\n"
00229 " 'b' -- run AGI script specified in ${MEETME_AGI_BACKGROUND}\n"
00230 " Default: conf-background.agi (Note: This does not work with\n"
00231 " non-DAHDI channels in the same conference)\n"
00232 " 'c' -- announce user(s) count on joining a conference\n"
00233 " 'd' -- dynamically add conference\n"
00234 " 'D' -- dynamically add conference, prompting for a PIN\n"
00235 " 'e' -- select an empty conference\n"
00236 " 'E' -- select an empty pinless conference\n"
00237 " 'F' -- Pass DTMF through the conference.\n"
00238 " 'i' -- announce user join/leave with review\n"
00239 " 'I' -- announce user join/leave without review\n"
00240 " 'l' -- set listen only mode (Listen only, no talking)\n"
00241 " 'm' -- set initially muted\n"
00242 " 'M' -- enable music on hold when the conference has a single caller\n"
00243 " 'o' -- set talker optimization - treats talkers who aren't speaking as\n"
00244 " being muted, meaning (a) No encode is done on transmission and\n"
00245 " (b) Received audio that is not registered as talking is omitted\n"
00246 " causing no buildup in background noise. Note that this option\n"
00247 " will be removed in 1.6 and enabled by default.\n"
00248 " 'p' -- allow user to exit the conference by pressing '#'\n"
00249 " 'P' -- always prompt for the pin even if it is specified\n"
00250 " 'q' -- quiet mode (don't play enter/leave sounds)\n"
00251 " 'r' -- Record conference (records as ${MEETME_RECORDINGFILE}\n"
00252 " using format ${MEETME_RECORDINGFORMAT}). Default filename is\n"
00253 " meetme-conf-rec-${CONFNO}-${UNIQUEID} and the default format is\n"
00254 " wav.\n"
00255 " 's' -- Present menu (user or admin) when '*' is received ('send' to menu)\n"
00256 " 't' -- set talk only mode. (Talk only, no listening)\n"
00257 " 'T' -- set talker detection (sent to manager interface and meetme list)\n"
00258 " 'w[(<secs>)]'\n"
00259 " -- wait until the marked user enters the conference\n"
00260 " 'x' -- close the conference when last marked user exits\n"
00261 " 'X' -- allow user to exit the conference by entering a valid single\n"
00262 " digit extension ${MEETME_EXIT_CONTEXT} or the current context\n"
00263 " if that variable is not defined.\n"
00264 " '1' -- do not play message when first person enters\n";
00265
00266 static const char *descrip2 =
00267 " MeetMeCount(confno[|var]): Plays back the number of users in the specified\n"
00268 "MeetMe conference. If var is specified, playback will be skipped and the value\n"
00269 "will be returned in the variable. Upon app completion, MeetMeCount will hangup\n"
00270 "the channel, unless priority n+1 exists, in which case priority progress will\n"
00271 "continue.\n";
00272
00273 static const char *descrip3 =
00274 " MeetMeAdmin(confno,command[,user]): Run admin command for conference\n"
00275 " 'e' -- Eject last user that joined\n"
00276 " 'k' -- Kick one user out of conference\n"
00277 " 'K' -- Kick all users out of conference\n"
00278 " 'l' -- Unlock conference\n"
00279 " 'L' -- Lock conference\n"
00280 " 'm' -- Unmute one user\n"
00281 " 'M' -- Mute one user\n"
00282 " 'n' -- Unmute all users in the conference\n"
00283 " 'N' -- Mute all non-admin users in the conference\n"
00284 " 'r' -- Reset one user's volume settings\n"
00285 " 'R' -- Reset all users volume settings\n"
00286 " 's' -- Lower entire conference speaking volume\n"
00287 " 'S' -- Raise entire conference speaking volume\n"
00288 " 't' -- Lower one user's talk volume\n"
00289 " 'T' -- Raise one user's talk volume\n"
00290 " 'u' -- Lower one user's listen volume\n"
00291 " 'U' -- Raise one user's listen volume\n"
00292 " 'v' -- Lower entire conference listening volume\n"
00293 " 'V' -- Raise entire conference listening volume\n"
00294 "";
00295
00296 static const char *slastation_desc =
00297 " SLAStation(station):\n"
00298 "This application should be executed by an SLA station. The argument depends\n"
00299 "on how the call was initiated. If the phone was just taken off hook, then\n"
00300 "the argument \"station\" should be just the station name. If the call was\n"
00301 "initiated by pressing a line key, then the station name should be preceded\n"
00302 "by an underscore and the trunk name associated with that line button.\n"
00303 "For example: \"station1_line1\"."
00304 " On exit, this application will set the variable SLASTATION_STATUS to\n"
00305 "one of the following values:\n"
00306 " FAILURE | CONGESTION | SUCCESS\n"
00307 "";
00308
00309 static const char *slatrunk_desc =
00310 " SLATrunk(trunk):\n"
00311 "This application should be executed by an SLA trunk on an inbound call.\n"
00312 "The channel calling this application should correspond to the SLA trunk\n"
00313 "with the name \"trunk\" that is being passed as an argument.\n"
00314 " On exit, this application will set the variable SLATRUNK_STATUS to\n"
00315 "one of the following values:\n"
00316 " FAILURE | SUCCESS | UNANSWERED | RINGTIMEOUT\n"
00317 "";
00318
00319 #define MAX_CONFNUM 80
00320 #define MAX_PIN 80
00321
00322 enum announcetypes {
00323 CONF_HASJOIN,
00324 CONF_HASLEFT
00325 };
00326
00327 struct announce_listitem {
00328 AST_LIST_ENTRY(announce_listitem) entry;
00329 char namerecloc[PATH_MAX];
00330 char language[MAX_LANGUAGE];
00331 struct ast_channel *confchan;
00332 int confusers;
00333 enum announcetypes announcetype;
00334 };
00335
00336
00337 struct ast_conference {
00338 ast_mutex_t playlock;
00339 ast_mutex_t listenlock;
00340 char confno[MAX_CONFNUM];
00341 struct ast_channel *chan;
00342 struct ast_channel *lchan;
00343 int fd;
00344 int zapconf;
00345 int users;
00346 int markedusers;
00347 time_t start;
00348 int refcount;
00349 enum recording_state recording:2;
00350 unsigned int isdynamic:1;
00351 unsigned int locked:1;
00352 pthread_t recordthread;
00353 ast_mutex_t recordthreadlock;
00354 pthread_attr_t attr;
00355 const char *recordingfilename;
00356 const char *recordingformat;
00357 char pin[MAX_PIN];
00358 char pinadmin[MAX_PIN];
00359 struct ast_frame *transframe[32];
00360 struct ast_frame *origframe;
00361 struct ast_trans_pvt *transpath[32];
00362 AST_LIST_HEAD_NOLOCK(, ast_conf_user) userlist;
00363 AST_LIST_ENTRY(ast_conference) list;
00364
00365 pthread_t announcethread;
00366 ast_mutex_t announcethreadlock;
00367 unsigned int announcethread_stop:1;
00368 ast_cond_t announcelist_addition;
00369 AST_LIST_HEAD_NOLOCK(, announce_listitem) announcelist;
00370 ast_mutex_t announcelistlock;
00371 };
00372
00373 static AST_LIST_HEAD_STATIC(confs, ast_conference);
00374
00375 static unsigned int conf_map[1024] = {0, };
00376
00377 struct volume {
00378 int desired;
00379 int actual;
00380 };
00381
00382 struct ast_conf_user {
00383 int user_no;
00384 int userflags;
00385 int adminflags;
00386 struct ast_channel *chan;
00387 int talking;
00388 int zapchannel;
00389 char usrvalue[50];
00390 char namerecloc[PATH_MAX];
00391 time_t jointime;
00392 struct volume talk;
00393 struct volume listen;
00394 AST_LIST_ENTRY(ast_conf_user) list;
00395 };
00396
00397 enum sla_which_trunk_refs {
00398 ALL_TRUNK_REFS,
00399 INACTIVE_TRUNK_REFS,
00400 };
00401
00402 enum sla_trunk_state {
00403 SLA_TRUNK_STATE_IDLE,
00404 SLA_TRUNK_STATE_RINGING,
00405 SLA_TRUNK_STATE_UP,
00406 SLA_TRUNK_STATE_ONHOLD,
00407 SLA_TRUNK_STATE_ONHOLD_BYME,
00408 };
00409
00410 enum sla_hold_access {
00411
00412
00413 SLA_HOLD_OPEN,
00414
00415
00416 SLA_HOLD_PRIVATE,
00417 };
00418
00419 struct sla_trunk_ref;
00420
00421 struct sla_station {
00422 AST_RWLIST_ENTRY(sla_station) entry;
00423 AST_DECLARE_STRING_FIELDS(
00424 AST_STRING_FIELD(name);
00425 AST_STRING_FIELD(device);
00426 AST_STRING_FIELD(autocontext);
00427 );
00428 AST_LIST_HEAD_NOLOCK(, sla_trunk_ref) trunks;
00429 struct ast_dial *dial;
00430
00431
00432
00433 unsigned int ring_timeout;
00434
00435
00436
00437 unsigned int ring_delay;
00438
00439
00440 unsigned int hold_access:1;
00441 };
00442
00443 struct sla_station_ref {
00444 AST_LIST_ENTRY(sla_station_ref) entry;
00445 struct sla_station *station;
00446 };
00447
00448 struct sla_trunk {
00449 AST_RWLIST_ENTRY(sla_trunk) entry;
00450 AST_DECLARE_STRING_FIELDS(
00451 AST_STRING_FIELD(name);
00452 AST_STRING_FIELD(device);
00453 AST_STRING_FIELD(autocontext);
00454 );
00455 AST_LIST_HEAD_NOLOCK(, sla_station_ref) stations;
00456
00457 unsigned int num_stations;
00458
00459 unsigned int active_stations;
00460
00461 unsigned int hold_stations;
00462 struct ast_channel *chan;
00463 unsigned int ring_timeout;
00464
00465
00466 unsigned int barge_disabled:1;
00467
00468
00469 unsigned int hold_access:1;
00470
00471
00472 unsigned int on_hold:1;
00473 };
00474
00475 struct sla_trunk_ref {
00476 AST_LIST_ENTRY(sla_trunk_ref) entry;
00477 struct sla_trunk *trunk;
00478 enum sla_trunk_state state;
00479 struct ast_channel *chan;
00480
00481
00482
00483 unsigned int ring_timeout;
00484
00485
00486
00487 unsigned int ring_delay;
00488 };
00489
00490 static AST_RWLIST_HEAD_STATIC(sla_stations, sla_station);
00491 static AST_RWLIST_HEAD_STATIC(sla_trunks, sla_trunk);
00492
00493 static const char sla_registrar[] = "SLA";
00494
00495
00496 enum sla_event_type {
00497
00498 SLA_EVENT_HOLD,
00499
00500 SLA_EVENT_DIAL_STATE,
00501
00502 SLA_EVENT_RINGING_TRUNK,
00503 };
00504
00505 struct sla_event {
00506 enum sla_event_type type;
00507 struct sla_station *station;
00508 struct sla_trunk_ref *trunk_ref;
00509 AST_LIST_ENTRY(sla_event) entry;
00510 };
00511
00512
00513
00514 struct sla_failed_station {
00515 struct sla_station *station;
00516 struct timeval last_try;
00517 AST_LIST_ENTRY(sla_failed_station) entry;
00518 };
00519
00520
00521 struct sla_ringing_trunk {
00522 struct sla_trunk *trunk;
00523
00524 struct timeval ring_begin;
00525 AST_LIST_HEAD_NOLOCK(, sla_station_ref) timed_out_stations;
00526 AST_LIST_ENTRY(sla_ringing_trunk) entry;
00527 };
00528
00529 enum sla_station_hangup {
00530 SLA_STATION_HANGUP_NORMAL,
00531 SLA_STATION_HANGUP_TIMEOUT,
00532 };
00533
00534
00535 struct sla_ringing_station {
00536 struct sla_station *station;
00537
00538 struct timeval ring_begin;
00539 AST_LIST_ENTRY(sla_ringing_station) entry;
00540 };
00541
00542
00543
00544
00545 static struct {
00546
00547 pthread_t thread;
00548 ast_cond_t cond;
00549 ast_mutex_t lock;
00550 AST_LIST_HEAD_NOLOCK(, sla_ringing_trunk) ringing_trunks;
00551 AST_LIST_HEAD_NOLOCK(, sla_ringing_station) ringing_stations;
00552 AST_LIST_HEAD_NOLOCK(, sla_failed_station) failed_stations;
00553 AST_LIST_HEAD_NOLOCK(, sla_event) event_q;
00554 unsigned int stop:1;
00555
00556
00557 unsigned int attempt_callerid:1;
00558 } sla = {
00559 .thread = AST_PTHREADT_NULL,
00560 };
00561
00562
00563
00564 static int audio_buffers;
00565
00566
00567
00568
00569
00570
00571
00572 static char const gain_map[] = {
00573 -15,
00574 -13,
00575 -10,
00576 -6,
00577 0,
00578 0,
00579 0,
00580 6,
00581 10,
00582 13,
00583 15,
00584 };
00585
00586
00587 static int admin_exec(struct ast_channel *chan, void *data);
00588 static void *recordthread(void *args);
00589
00590 static char *istalking(int x)
00591 {
00592 if (x > 0)
00593 return "(talking)";
00594 else if (x < 0)
00595 return "(unmonitored)";
00596 else
00597 return "(not talking)";
00598 }
00599
00600 static int careful_write(int fd, unsigned char *data, int len, int block)
00601 {
00602 int res;
00603 int x;
00604
00605 while (len) {
00606 if (block) {
00607 x = DAHDI_IOMUX_WRITE | DAHDI_IOMUX_SIGEVENT;
00608 res = ioctl(fd, DAHDI_IOMUX, &x);
00609 } else
00610 res = 0;
00611 if (res >= 0)
00612 res = write(fd, data, len);
00613 if (res < 1) {
00614 if (errno != EAGAIN) {
00615 ast_log(LOG_WARNING, "Failed to write audio data to conference: %s\n", strerror(errno));
00616 return -1;
00617 } else
00618 return 0;
00619 }
00620 len -= res;
00621 data += res;
00622 }
00623
00624 return 0;
00625 }
00626
00627 static int set_talk_volume(struct ast_conf_user *user, int volume)
00628 {
00629 char gain_adjust;
00630
00631
00632
00633
00634 gain_adjust = gain_map[volume + 5];
00635
00636 return ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
00637 }
00638
00639 static int set_listen_volume(struct ast_conf_user *user, int volume)
00640 {
00641 char gain_adjust;
00642
00643
00644
00645
00646 gain_adjust = gain_map[volume + 5];
00647
00648 return ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &gain_adjust, sizeof(gain_adjust), 0);
00649 }
00650
00651 static void tweak_volume(struct volume *vol, enum volume_action action)
00652 {
00653 switch (action) {
00654 case VOL_UP:
00655 switch (vol->desired) {
00656 case 5:
00657 break;
00658 case 0:
00659 vol->desired = 2;
00660 break;
00661 case -2:
00662 vol->desired = 0;
00663 break;
00664 default:
00665 vol->desired++;
00666 break;
00667 }
00668 break;
00669 case VOL_DOWN:
00670 switch (vol->desired) {
00671 case -5:
00672 break;
00673 case 2:
00674 vol->desired = 0;
00675 break;
00676 case 0:
00677 vol->desired = -2;
00678 break;
00679 default:
00680 vol->desired--;
00681 break;
00682 }
00683 }
00684 }
00685
00686 static void tweak_talk_volume(struct ast_conf_user *user, enum volume_action action)
00687 {
00688 tweak_volume(&user->talk, action);
00689
00690
00691
00692 if (!set_talk_volume(user, user->talk.desired))
00693 user->talk.actual = 0;
00694 else
00695 user->talk.actual = user->talk.desired;
00696 }
00697
00698 static void tweak_listen_volume(struct ast_conf_user *user, enum volume_action action)
00699 {
00700 tweak_volume(&user->listen, action);
00701
00702
00703
00704 if (!set_listen_volume(user, user->listen.desired))
00705 user->listen.actual = 0;
00706 else
00707 user->listen.actual = user->listen.desired;
00708 }
00709
00710 static void reset_volumes(struct ast_conf_user *user)
00711 {
00712 signed char zero_volume = 0;
00713
00714 ast_channel_setoption(user->chan, AST_OPTION_TXGAIN, &zero_volume, sizeof(zero_volume), 0);
00715 ast_channel_setoption(user->chan, AST_OPTION_RXGAIN, &zero_volume, sizeof(zero_volume), 0);
00716 }
00717
00718 static void conf_play(struct ast_channel *chan, struct ast_conference *conf, enum entrance_sound sound)
00719 {
00720 unsigned char *data;
00721 int len;
00722 int res = -1;
00723
00724 if (!chan->_softhangup)
00725 res = ast_autoservice_start(chan);
00726
00727 AST_LIST_LOCK(&confs);
00728
00729 switch(sound) {
00730 case ENTER:
00731 data = enter;
00732 len = sizeof(enter);
00733 break;
00734 case LEAVE:
00735 data = leave;
00736 len = sizeof(leave);
00737 break;
00738 default:
00739 data = NULL;
00740 len = 0;
00741 }
00742 if (data) {
00743 careful_write(conf->fd, data, len, 1);
00744 }
00745
00746 AST_LIST_UNLOCK(&confs);
00747
00748 if (!res)
00749 ast_autoservice_stop(chan);
00750 }
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765 static struct ast_conference *build_conf(char *confno, char *pin, char *pinadmin, int make, int dynamic, int refcount)
00766 {
00767 struct ast_conference *cnf;
00768 struct dahdi_confinfo ztc = { 0, };
00769 int confno_int = 0;
00770
00771 AST_LIST_LOCK(&confs);
00772
00773 AST_LIST_TRAVERSE(&confs, cnf, list) {
00774 if (!strcmp(confno, cnf->confno))
00775 break;
00776 }
00777
00778 if (cnf || (!make && !dynamic))
00779 goto cnfout;
00780
00781
00782 if (!(cnf = ast_calloc(1, sizeof(*cnf))))
00783 goto cnfout;
00784
00785 ast_mutex_init(&cnf->playlock);
00786 ast_mutex_init(&cnf->listenlock);
00787 cnf->recordthread = AST_PTHREADT_NULL;
00788 ast_mutex_init(&cnf->recordthreadlock);
00789 cnf->announcethread = AST_PTHREADT_NULL;
00790 ast_mutex_init(&cnf->announcethreadlock);
00791 ast_copy_string(cnf->confno, confno, sizeof(cnf->confno));
00792 ast_copy_string(cnf->pin, pin, sizeof(cnf->pin));
00793 ast_copy_string(cnf->pinadmin, pinadmin, sizeof(cnf->pinadmin));
00794
00795
00796 ztc.confno = -1;
00797 ztc.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
00798 #ifdef HAVE_ZAPTEL
00799 cnf->fd = open("/dev/zap/pseudo", O_RDWR);
00800 #else
00801 cnf->fd = open("/dev/dahdi/pseudo", O_RDWR);
00802 #endif
00803 if (cnf->fd < 0 || ioctl(cnf->fd, DAHDI_SETCONF, &ztc)) {
00804 ast_log(LOG_WARNING, "Unable to open pseudo device\n");
00805 if (cnf->fd >= 0)
00806 close(cnf->fd);
00807 free(cnf);
00808 cnf = NULL;
00809 goto cnfout;
00810 }
00811
00812 cnf->zapconf = ztc.confno;
00813
00814
00815 cnf->chan = ast_request(dahdi_chan_name, AST_FORMAT_SLINEAR, "pseudo", NULL);
00816 if (cnf->chan) {
00817 ast_set_read_format(cnf->chan, AST_FORMAT_SLINEAR);
00818 ast_set_write_format(cnf->chan, AST_FORMAT_SLINEAR);
00819 ztc.chan = 0;
00820 ztc.confno = cnf->zapconf;
00821 ztc.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
00822 if (ioctl(cnf->chan->fds[0], DAHDI_SETCONF, &ztc)) {
00823 ast_log(LOG_WARNING, "Error setting conference\n");
00824 if (cnf->chan)
00825 ast_hangup(cnf->chan);
00826 else
00827 close(cnf->fd);
00828 free(cnf);
00829 cnf = NULL;
00830 goto cnfout;
00831 }
00832 }
00833
00834
00835 cnf->start = time(NULL);
00836 cnf->isdynamic = dynamic ? 1 : 0;
00837 if (option_verbose > 2)
00838 ast_verbose(VERBOSE_PREFIX_3 "Created MeetMe conference %d for conference '%s'\n", cnf->zapconf, cnf->confno);
00839 AST_LIST_INSERT_HEAD(&confs, cnf, list);
00840
00841
00842 if ((sscanf(cnf->confno, "%d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024))
00843 conf_map[confno_int] = 1;
00844
00845 cnfout:
00846 if (cnf)
00847 ast_atomic_fetchadd_int(&cnf->refcount, refcount);
00848
00849 AST_LIST_UNLOCK(&confs);
00850
00851 return cnf;
00852 }
00853
00854 static int meetme_cmd(int fd, int argc, char **argv)
00855 {
00856
00857 struct ast_conference *cnf;
00858 struct ast_conf_user *user;
00859 int hr, min, sec;
00860 int i = 0, total = 0;
00861 time_t now;
00862 char *header_format = "%-14s %-14s %-10s %-8s %-8s\n";
00863 char *data_format = "%-12.12s %4.4d %4.4s %02d:%02d:%02d %-8s\n";
00864 char cmdline[1024] = "";
00865
00866 if (argc > 8)
00867 ast_cli(fd, "Invalid Arguments.\n");
00868
00869 for (i = 0; i < argc; i++) {
00870 if (strlen(argv[i]) > 100)
00871 ast_cli(fd, "Invalid Arguments.\n");
00872 }
00873 if (argc == 1) {
00874
00875 now = time(NULL);
00876 AST_LIST_LOCK(&confs);
00877 if (AST_LIST_EMPTY(&confs)) {
00878 ast_cli(fd, "No active MeetMe conferences.\n");
00879 AST_LIST_UNLOCK(&confs);
00880 return RESULT_SUCCESS;
00881 }
00882 ast_cli(fd, header_format, "Conf Num", "Parties", "Marked", "Activity", "Creation");
00883 AST_LIST_TRAVERSE(&confs, cnf, list) {
00884 if (cnf->markedusers == 0)
00885 strcpy(cmdline, "N/A ");
00886 else
00887 snprintf(cmdline, sizeof(cmdline), "%4.4d", cnf->markedusers);
00888 hr = (now - cnf->start) / 3600;
00889 min = ((now - cnf->start) % 3600) / 60;
00890 sec = (now - cnf->start) % 60;
00891
00892 ast_cli(fd, data_format, cnf->confno, cnf->users, cmdline, hr, min, sec, cnf->isdynamic ? "Dynamic" : "Static");
00893
00894 total += cnf->users;
00895 }
00896 AST_LIST_UNLOCK(&confs);
00897 ast_cli(fd, "* Total number of MeetMe users: %d\n", total);
00898 return RESULT_SUCCESS;
00899 }
00900 if (argc < 3)
00901 return RESULT_SHOWUSAGE;
00902 ast_copy_string(cmdline, argv[2], sizeof(cmdline));
00903 if (strstr(argv[1], "lock")) {
00904 if (strcmp(argv[1], "lock") == 0) {
00905
00906 strncat(cmdline, "|L", sizeof(cmdline) - strlen(cmdline) - 1);
00907 } else {
00908
00909 strncat(cmdline, "|l", sizeof(cmdline) - strlen(cmdline) - 1);
00910 }
00911 } else if (strstr(argv[1], "mute")) {
00912 if (argc < 4)
00913 return RESULT_SHOWUSAGE;
00914 if (strcmp(argv[1], "mute") == 0) {
00915
00916 if (strcmp(argv[3], "all") == 0) {
00917 strncat(cmdline, "|N", sizeof(cmdline) - strlen(cmdline) - 1);
00918 } else {
00919 strncat(cmdline, "|M|", sizeof(cmdline) - strlen(cmdline) - 1);
00920 strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
00921 }
00922 } else {
00923
00924 if (strcmp(argv[3], "all") == 0) {
00925 strncat(cmdline, "|n", sizeof(cmdline) - strlen(cmdline) - 1);
00926 } else {
00927 strncat(cmdline, "|m|", sizeof(cmdline) - strlen(cmdline) - 1);
00928 strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
00929 }
00930 }
00931 } else if (strcmp(argv[1], "kick") == 0) {
00932 if (argc < 4)
00933 return RESULT_SHOWUSAGE;
00934 if (strcmp(argv[3], "all") == 0) {
00935
00936 strncat(cmdline, "|K", sizeof(cmdline) - strlen(cmdline) - 1);
00937 } else {
00938
00939 strncat(cmdline, "|k|", sizeof(cmdline) - strlen(cmdline) - 1);
00940 strncat(cmdline, argv[3], sizeof(cmdline) - strlen(cmdline) - 1);
00941 }
00942 } else if(strcmp(argv[1], "list") == 0) {
00943 int concise = ( 4 == argc && ( !strcasecmp(argv[3], "concise") ) );
00944
00945 if (AST_LIST_EMPTY(&confs)) {
00946 if ( !concise )
00947 ast_cli(fd, "No active conferences.\n");
00948 return RESULT_SUCCESS;
00949 }
00950
00951 AST_LIST_LOCK(&confs);
00952 AST_LIST_TRAVERSE(&confs, cnf, list) {
00953 if (strcmp(cnf->confno, argv[2]) == 0)
00954 break;
00955 }
00956 if (!cnf) {
00957 if ( !concise )
00958 ast_cli(fd, "No such conference: %s.\n",argv[2]);
00959 AST_LIST_UNLOCK(&confs);
00960 return RESULT_SUCCESS;
00961 }
00962
00963 time(&now);
00964 AST_LIST_TRAVERSE(&cnf->userlist, user, list) {
00965 hr = (now - user->jointime) / 3600;
00966 min = ((now - user->jointime) % 3600) / 60;
00967 sec = (now - user->jointime) % 60;
00968 if ( !concise )
00969 ast_cli(fd, "User #: %-2.2d %12.12s %-20.20s Channel: %s %s %s %s %s %02d:%02d:%02d\n",
00970 user->user_no,
00971 S_OR(user->chan->cid.cid_num, "<unknown>"),
00972 S_OR(user->chan->cid.cid_name, "<no name>"),
00973 user->chan->name,
00974 user->userflags & CONFFLAG_ADMIN ? "(Admin)" : "",
00975 user->userflags & CONFFLAG_MONITOR ? "(Listen only)" : "",
00976 user->adminflags & ADMINFLAG_MUTED ? "(Admin Muted)" : user->adminflags & ADMINFLAG_SELFMUTED ? "(Muted)" : "",
00977 istalking(user->talking), hr, min, sec);
00978 else
00979 ast_cli(fd, "%d!%s!%s!%s!%s!%s!%s!%d!%02d:%02d:%02d\n",
00980 user->user_no,
00981 S_OR(user->chan->cid.cid_num, ""),
00982 S_OR(user->chan->cid.cid_name, ""),
00983 user->chan->name,
00984 user->userflags & CONFFLAG_ADMIN ? "1" : "",
00985 user->userflags & CONFFLAG_MONITOR ? "1" : "",
00986 user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED) ? "1" : "",
00987 user->talking, hr, min, sec);
00988
00989 }
00990 if ( !concise )
00991 ast_cli(fd,"%d users in that conference.\n",cnf->users);
00992 AST_LIST_UNLOCK(&confs);
00993 return RESULT_SUCCESS;
00994 } else
00995 return RESULT_SHOWUSAGE;
00996 ast_log(LOG_DEBUG, "Cmdline: %s\n", cmdline);
00997 admin_exec(NULL, cmdline);
00998
00999 return 0;
01000 }
01001
01002 static char *complete_meetmecmd(const char *line, const char *word, int pos, int state)
01003 {
01004 static char *cmds[] = {"lock", "unlock", "mute", "unmute", "kick", "list", NULL};
01005
01006 int len = strlen(word);
01007 int which = 0;
01008 struct ast_conference *cnf = NULL;
01009 struct ast_conf_user *usr = NULL;
01010 char *confno = NULL;
01011 char usrno[50] = "";
01012 char *myline, *ret = NULL;
01013
01014 if (pos == 1) {
01015 return ast_cli_complete(word, cmds, state);
01016 } else if (pos == 2) {
01017 AST_LIST_LOCK(&confs);
01018 AST_LIST_TRAVERSE(&confs, cnf, list) {
01019 if (!strncasecmp(word, cnf->confno, len) && ++which > state) {
01020 ret = cnf->confno;
01021 break;
01022 }
01023 }
01024 ret = ast_strdup(ret);
01025 AST_LIST_UNLOCK(&confs);
01026 return ret;
01027 } else if (pos == 3) {
01028
01029 if (strstr(line, "mute") || strstr(line, "kick")) {
01030 if (state == 0 && (strstr(line, "kick") || strstr(line,"mute")) && !strncasecmp(word, "all", len))
01031 return strdup("all");
01032 which++;
01033 AST_LIST_LOCK(&confs);
01034
01035
01036 myline = ast_strdupa(line);
01037 if (strsep(&myline, " ") && strsep(&myline, " ") && !confno) {
01038 while((confno = strsep(&myline, " ")) && (strcmp(confno, " ") == 0))
01039 ;
01040 }
01041
01042 AST_LIST_TRAVERSE(&confs, cnf, list) {
01043 if (!strcmp(confno, cnf->confno))
01044 break;
01045 }
01046
01047 if (cnf) {
01048
01049 AST_LIST_TRAVERSE(&cnf->userlist, usr, list) {
01050 snprintf(usrno, sizeof(usrno), "%d", usr->user_no);
01051 if (!strncasecmp(word, usrno, len) && ++which > state)
01052 break;
01053 }
01054 }
01055 AST_LIST_UNLOCK(&confs);
01056 return usr ? strdup(usrno) : NULL;
01057 } else if ( strstr(line, "list") && ( 0 == state ) )
01058 return strdup("concise");
01059 }
01060
01061 return NULL;
01062 }
01063
01064 static char meetme_usage[] =
01065 "Usage: meetme (un)lock|(un)mute|kick|list [concise] <confno> <usernumber>\n"
01066 " Executes a command for the conference or on a conferee\n";
01067
01068 static const char *sla_hold_str(unsigned int hold_access)
01069 {
01070 const char *hold = "Unknown";
01071
01072 switch (hold_access) {
01073 case SLA_HOLD_OPEN:
01074 hold = "Open";
01075 break;
01076 case SLA_HOLD_PRIVATE:
01077 hold = "Private";
01078 default:
01079 break;
01080 }
01081
01082 return hold;
01083 }
01084
01085 static int sla_show_trunks(int fd, int argc, char **argv)
01086 {
01087 const struct sla_trunk *trunk;
01088
01089 ast_cli(fd, "\n"
01090 "=============================================================\n"
01091 "=== Configured SLA Trunks ===================================\n"
01092 "=============================================================\n"
01093 "===\n");
01094 AST_RWLIST_RDLOCK(&sla_trunks);
01095 AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
01096 struct sla_station_ref *station_ref;
01097 char ring_timeout[16] = "(none)";
01098 if (trunk->ring_timeout)
01099 snprintf(ring_timeout, sizeof(ring_timeout), "%u Seconds", trunk->ring_timeout);
01100 ast_cli(fd, "=== ---------------------------------------------------------\n"
01101 "=== Trunk Name: %s\n"
01102 "=== ==> Device: %s\n"
01103 "=== ==> AutoContext: %s\n"
01104 "=== ==> RingTimeout: %s\n"
01105 "=== ==> BargeAllowed: %s\n"
01106 "=== ==> HoldAccess: %s\n"
01107 "=== ==> Stations ...\n",
01108 trunk->name, trunk->device,
01109 S_OR(trunk->autocontext, "(none)"),
01110 ring_timeout,
01111 trunk->barge_disabled ? "No" : "Yes",
01112 sla_hold_str(trunk->hold_access));
01113 AST_RWLIST_RDLOCK(&sla_stations);
01114 AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry)
01115 ast_cli(fd, "=== ==> Station name: %s\n", station_ref->station->name);
01116 AST_RWLIST_UNLOCK(&sla_stations);
01117 ast_cli(fd, "=== ---------------------------------------------------------\n"
01118 "===\n");
01119 }
01120 AST_RWLIST_UNLOCK(&sla_trunks);
01121 ast_cli(fd, "=============================================================\n"
01122 "\n");
01123
01124 return RESULT_SUCCESS;
01125 }
01126
01127 static const char *trunkstate2str(enum sla_trunk_state state)
01128 {
01129 #define S(e) case e: return # e;
01130 switch (state) {
01131 S(SLA_TRUNK_STATE_IDLE)
01132 S(SLA_TRUNK_STATE_RINGING)
01133 S(SLA_TRUNK_STATE_UP)
01134 S(SLA_TRUNK_STATE_ONHOLD)
01135 S(SLA_TRUNK_STATE_ONHOLD_BYME)
01136 }
01137 return "Uknown State";
01138 #undef S
01139 }
01140
01141 static const char sla_show_trunks_usage[] =
01142 "Usage: sla show trunks\n"
01143 " This will list all trunks defined in sla.conf\n";
01144
01145 static int sla_show_stations(int fd, int argc, char **argv)
01146 {
01147 const struct sla_station *station;
01148
01149 ast_cli(fd, "\n"
01150 "=============================================================\n"
01151 "=== Configured SLA Stations =================================\n"
01152 "=============================================================\n"
01153 "===\n");
01154 AST_RWLIST_RDLOCK(&sla_stations);
01155 AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
01156 struct sla_trunk_ref *trunk_ref;
01157 char ring_timeout[16] = "(none)";
01158 char ring_delay[16] = "(none)";
01159 if (station->ring_timeout) {
01160 snprintf(ring_timeout, sizeof(ring_timeout),
01161 "%u", station->ring_timeout);
01162 }
01163 if (station->ring_delay) {
01164 snprintf(ring_delay, sizeof(ring_delay),
01165 "%u", station->ring_delay);
01166 }
01167 ast_cli(fd, "=== ---------------------------------------------------------\n"
01168 "=== Station Name: %s\n"
01169 "=== ==> Device: %s\n"
01170 "=== ==> AutoContext: %s\n"
01171 "=== ==> RingTimeout: %s\n"
01172 "=== ==> RingDelay: %s\n"
01173 "=== ==> HoldAccess: %s\n"
01174 "=== ==> Trunks ...\n",
01175 station->name, station->device,
01176 S_OR(station->autocontext, "(none)"),
01177 ring_timeout, ring_delay,
01178 sla_hold_str(station->hold_access));
01179 AST_RWLIST_RDLOCK(&sla_trunks);
01180 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
01181 if (trunk_ref->ring_timeout) {
01182 snprintf(ring_timeout, sizeof(ring_timeout),
01183 "%u", trunk_ref->ring_timeout);
01184 } else
01185 strcpy(ring_timeout, "(none)");
01186 if (trunk_ref->ring_delay) {
01187 snprintf(ring_delay, sizeof(ring_delay),
01188 "%u", trunk_ref->ring_delay);
01189 } else
01190 strcpy(ring_delay, "(none)");
01191 ast_cli(fd, "=== ==> Trunk Name: %s\n"
01192 "=== ==> State: %s\n"
01193 "=== ==> RingTimeout: %s\n"
01194 "=== ==> RingDelay: %s\n",
01195 trunk_ref->trunk->name,
01196 trunkstate2str(trunk_ref->state),
01197 ring_timeout, ring_delay);
01198 }
01199 AST_RWLIST_UNLOCK(&sla_trunks);
01200 ast_cli(fd, "=== ---------------------------------------------------------\n"
01201 "===\n");
01202 }
01203 AST_RWLIST_UNLOCK(&sla_stations);
01204 ast_cli(fd, "============================================================\n"
01205 "\n");
01206
01207 return RESULT_SUCCESS;
01208 }
01209
01210 static const char sla_show_stations_usage[] =
01211 "Usage: sla show stations\n"
01212 " This will list all stations defined in sla.conf\n";
01213
01214 static struct ast_cli_entry cli_meetme[] = {
01215 { { "meetme", NULL, NULL },
01216 meetme_cmd, "Execute a command on a conference or conferee",
01217 meetme_usage, complete_meetmecmd },
01218
01219 { { "sla", "show", "trunks", NULL },
01220 sla_show_trunks, "Show SLA Trunks",
01221 sla_show_trunks_usage, NULL },
01222
01223 { { "sla", "show", "stations", NULL },
01224 sla_show_stations, "Show SLA Stations",
01225 sla_show_stations_usage, NULL },
01226 };
01227
01228 static void conf_flush(int fd, struct ast_channel *chan)
01229 {
01230 int x;
01231
01232
01233
01234
01235 if (chan) {
01236 struct ast_frame *f;
01237
01238
01239
01240
01241 while (ast_waitfor(chan, 1)) {
01242 f = ast_read(chan);
01243 if (f)
01244 ast_frfree(f);
01245 else
01246 break;
01247 }
01248 }
01249
01250
01251 x = DAHDI_FLUSH_ALL;
01252 if (ioctl(fd, DAHDI_FLUSH, &x))
01253 ast_log(LOG_WARNING, "Error flushing channel\n");
01254
01255 }
01256
01257
01258
01259 static int conf_free(struct ast_conference *conf)
01260 {
01261 int x;
01262 struct announce_listitem *item;
01263
01264 AST_LIST_REMOVE(&confs, conf, list);
01265
01266 if (conf->recording == MEETME_RECORD_ACTIVE) {
01267 conf->recording = MEETME_RECORD_TERMINATE;
01268 AST_LIST_UNLOCK(&confs);
01269 while (1) {
01270 usleep(1);
01271 AST_LIST_LOCK(&confs);
01272 if (conf->recording == MEETME_RECORD_OFF)
01273 break;
01274 AST_LIST_UNLOCK(&confs);
01275 }
01276 }
01277
01278 for (x=0;x<AST_FRAME_BITS;x++) {
01279 if (conf->transframe[x])
01280 ast_frfree(conf->transframe[x]);
01281 if (conf->transpath[x])
01282 ast_translator_free_path(conf->transpath[x]);
01283 }
01284 if (conf->announcethread != AST_PTHREADT_NULL) {
01285 ast_mutex_lock(&conf->announcelistlock);
01286 conf->announcethread_stop = 1;
01287 ast_softhangup(conf->chan, AST_SOFTHANGUP_EXPLICIT);
01288 ast_cond_signal(&conf->announcelist_addition);
01289 ast_mutex_unlock(&conf->announcelistlock);
01290 pthread_join(conf->announcethread, NULL);
01291
01292 while ((item = AST_LIST_REMOVE_HEAD(&conf->announcelist, entry))) {
01293 ast_filedelete(item->namerecloc, NULL);
01294 ao2_ref(item, -1);
01295 }
01296 ast_mutex_destroy(&conf->announcelistlock);
01297 }
01298 if (conf->origframe)
01299 ast_frfree(conf->origframe);
01300 if (conf->lchan)
01301 ast_hangup(conf->lchan);
01302 if (conf->chan)
01303 ast_hangup(conf->chan);
01304 if (conf->fd >= 0)
01305 close(conf->fd);
01306
01307 ast_mutex_destroy(&conf->playlock);
01308 ast_mutex_destroy(&conf->listenlock);
01309 ast_mutex_destroy(&conf->recordthreadlock);
01310 ast_mutex_destroy(&conf->announcethreadlock);
01311
01312 free(conf);
01313
01314 return 0;
01315 }
01316
01317 static void conf_queue_dtmf(const struct ast_conference *conf,
01318 const struct ast_conf_user *sender, struct ast_frame *f)
01319 {
01320 struct ast_conf_user *user;
01321
01322 AST_LIST_TRAVERSE(&conf->userlist, user, list) {
01323 if (user == sender)
01324 continue;
01325 if (ast_write(user->chan, f) < 0)
01326 ast_log(LOG_WARNING, "Error writing frame to channel %s\n", user->chan->name);
01327 }
01328 }
01329
01330 static void sla_queue_event_full(enum sla_event_type type,
01331 struct sla_trunk_ref *trunk_ref, struct sla_station *station, int lock)
01332 {
01333 struct sla_event *event;
01334
01335 if (sla.thread == AST_PTHREADT_NULL) {
01336 return;
01337 }
01338
01339 if (!(event = ast_calloc(1, sizeof(*event))))
01340 return;
01341
01342 event->type = type;
01343 event->trunk_ref = trunk_ref;
01344 event->station = station;
01345
01346 if (!lock) {
01347 AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
01348 return;
01349 }
01350
01351 ast_mutex_lock(&sla.lock);
01352 AST_LIST_INSERT_TAIL(&sla.event_q, event, entry);
01353 ast_cond_signal(&sla.cond);
01354 ast_mutex_unlock(&sla.lock);
01355 }
01356
01357 static void sla_queue_event_nolock(enum sla_event_type type)
01358 {
01359 sla_queue_event_full(type, NULL, NULL, 0);
01360 }
01361
01362 static void sla_queue_event(enum sla_event_type type)
01363 {
01364 sla_queue_event_full(type, NULL, NULL, 1);
01365 }
01366
01367
01368 static void sla_queue_event_conf(enum sla_event_type type, struct ast_channel *chan,
01369 struct ast_conference *conf)
01370 {
01371 struct sla_station *station;
01372 struct sla_trunk_ref *trunk_ref = NULL;
01373 char *trunk_name;
01374
01375 trunk_name = ast_strdupa(conf->confno);
01376 strsep(&trunk_name, "_");
01377 if (ast_strlen_zero(trunk_name)) {
01378 ast_log(LOG_ERROR, "Invalid conference name for SLA - '%s'!\n", conf->confno);
01379 return;
01380 }
01381
01382 AST_RWLIST_RDLOCK(&sla_stations);
01383 AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
01384 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
01385 if (trunk_ref->chan == chan && !strcmp(trunk_ref->trunk->name, trunk_name))
01386 break;
01387 }
01388 if (trunk_ref)
01389 break;
01390 }
01391 AST_RWLIST_UNLOCK(&sla_stations);
01392
01393 if (!trunk_ref) {
01394 ast_log(LOG_DEBUG, "Trunk not found for event!\n");
01395 return;
01396 }
01397
01398 sla_queue_event_full(type, trunk_ref, station, 1);
01399 }
01400
01401
01402 static int dispose_conf(struct ast_conference *conf)
01403 {
01404 int res = 0;
01405 int confno_int = 0;
01406
01407 AST_LIST_LOCK(&confs);
01408 if (ast_atomic_dec_and_test(&conf->refcount)) {
01409
01410 if ((sscanf(conf->confno, "%d", &confno_int) == 1) && (confno_int >= 0 && confno_int < 1024))
01411 conf_map[confno_int] = 0;
01412 conf_free(conf);
01413 res = 1;
01414 }
01415 AST_LIST_UNLOCK(&confs);
01416
01417 return res;
01418 }
01419
01420 static const char *get_announce_filename(enum announcetypes type)
01421 {
01422 switch (type) {
01423 case CONF_HASLEFT:
01424 return "conf-hasleft";
01425 break;
01426 case CONF_HASJOIN:
01427 return "conf-hasjoin";
01428 break;
01429 default:
01430 return "";
01431 }
01432 }
01433
01434 static void *announce_thread(void *data)
01435 {
01436 struct announce_listitem *current;
01437 struct ast_conference *conf = data;
01438 int res;
01439 char filename[PATH_MAX] = "";
01440 AST_LIST_HEAD_NOLOCK(, announce_listitem) local_list;
01441 AST_LIST_HEAD_INIT_NOLOCK(&local_list);
01442
01443 while (!conf->announcethread_stop) {
01444 ast_mutex_lock(&conf->announcelistlock);
01445 if (conf->announcethread_stop) {
01446 ast_mutex_unlock(&conf->announcelistlock);
01447 break;
01448 }
01449 if (AST_LIST_EMPTY(&conf->announcelist))
01450 ast_cond_wait(&conf->announcelist_addition, &conf->announcelistlock);
01451
01452 AST_LIST_APPEND_LIST(&local_list, &conf->announcelist, entry);
01453 AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
01454
01455 ast_mutex_unlock(&conf->announcelistlock);
01456 if (conf->announcethread_stop) {
01457 break;
01458 }
01459
01460 for (res = 1; !conf->announcethread_stop && (current = AST_LIST_REMOVE_HEAD(&local_list, entry)); ao2_ref(current, -1)) {
01461 ast_log(LOG_DEBUG, "About to play %s\n", current->namerecloc);
01462 if (!ast_fileexists(current->namerecloc, NULL, NULL))
01463 continue;
01464 if ((current->confchan) && (current->confusers > 1) && !ast_check_hangup(current->confchan)) {
01465 if (!ast_streamfile(current->confchan, current->namerecloc, current->language))
01466 res = ast_waitstream(current->confchan, "");
01467 if (!res) {
01468 ast_copy_string(filename, get_announce_filename(current->announcetype), sizeof(filename));
01469 if (!ast_streamfile(current->confchan, filename, current->language))
01470 ast_waitstream(current->confchan, "");
01471 }
01472 }
01473 if (current->announcetype == CONF_HASLEFT) {
01474 ast_filedelete(current->namerecloc, NULL);
01475 }
01476 }
01477 }
01478
01479
01480 while ((current = AST_LIST_REMOVE_HEAD(&local_list, entry))) {
01481 ast_filedelete(current->namerecloc, NULL);
01482 ao2_ref(current, -1);
01483 }
01484 return NULL;
01485 }
01486
01487 static int can_write(struct ast_channel *chan, int confflags)
01488 {
01489 if (!(confflags & CONFFLAG_NO_AUDIO_UNTIL_UP)) {
01490 return 1;
01491 }
01492
01493 return (chan->_state == AST_STATE_UP);
01494 }
01495
01496 static int conf_run(struct ast_channel *chan, struct ast_conference *conf, int confflags, char *optargs[])
01497 {
01498 struct ast_conf_user *user = NULL;
01499 struct ast_conf_user *usr = NULL;
01500 int fd;
01501 struct dahdi_confinfo ztc, ztc_empty;
01502 struct ast_frame *f;
01503 struct ast_channel *c;
01504 struct ast_frame fr;
01505 int outfd;
01506 int ms;
01507 int nfds;
01508 int res;
01509 int flags;
01510 int retryzap;
01511 int origfd;
01512 int musiconhold = 0;
01513 int firstpass = 0;
01514 int lastmarked = 0;
01515 int currentmarked = 0;
01516 int ret = -1;
01517 int x;
01518 int menu_active = 0;
01519 int using_pseudo = 0;
01520 int duration=20;
01521 int hr, min, sec;
01522 int sent_event = 0;
01523 time_t now;
01524 struct ast_dsp *dsp=NULL;
01525 struct ast_app *app;
01526 const char *agifile;
01527 const char *agifiledefault = "conf-background.agi";
01528 char meetmesecs[30] = "";
01529 char exitcontext[AST_MAX_CONTEXT] = "";
01530 char recordingtmp[AST_MAX_EXTENSION] = "";
01531 char members[10] = "";
01532 int dtmf, opt_waitmarked_timeout = 0;
01533 time_t timeout = 0;
01534 struct dahdi_bufferinfo bi;
01535 char __buf[CONF_SIZE + AST_FRIENDLY_OFFSET];
01536 char *buf = __buf + AST_FRIENDLY_OFFSET;
01537 int setusercount = 0;
01538
01539 if (!(user = ast_calloc(1, sizeof(*user))))
01540 return ret;
01541
01542
01543 if ((confflags & CONFFLAG_WAITMARKED) &&
01544 !ast_strlen_zero(optargs[OPT_ARG_WAITMARKED]) &&
01545 (sscanf(optargs[OPT_ARG_WAITMARKED], "%d", &opt_waitmarked_timeout) == 1) &&
01546 (opt_waitmarked_timeout > 0)) {
01547 timeout = time(NULL) + opt_waitmarked_timeout;
01548 }
01549
01550 if (confflags & CONFFLAG_RECORDCONF) {
01551 if (!conf->recordingfilename) {
01552 conf->recordingfilename = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFILE");
01553 if (!conf->recordingfilename) {
01554 snprintf(recordingtmp, sizeof(recordingtmp), "meetme-conf-rec-%s-%s", conf->confno, chan->uniqueid);
01555 conf->recordingfilename = ast_strdupa(recordingtmp);
01556 }
01557 conf->recordingformat = pbx_builtin_getvar_helper(chan, "MEETME_RECORDINGFORMAT");
01558 if (!conf->recordingformat) {
01559 snprintf(recordingtmp, sizeof(recordingtmp), "wav");
01560 conf->recordingformat = ast_strdupa(recordingtmp);
01561 }
01562 ast_verbose(VERBOSE_PREFIX_4 "Starting recording of MeetMe Conference %s into file %s.%s.\n",
01563 conf->confno, conf->recordingfilename, conf->recordingformat);
01564 }
01565 }
01566
01567 ast_mutex_lock(&conf->recordthreadlock);
01568 if ((conf->recordthread == AST_PTHREADT_NULL) && (confflags & CONFFLAG_RECORDCONF) && ((conf->lchan = ast_request(dahdi_chan_name, AST_FORMAT_SLINEAR, "pseudo", NULL)))) {
01569 ast_set_read_format(conf->lchan, AST_FORMAT_SLINEAR);
01570 ast_set_write_format(conf->lchan, AST_FORMAT_SLINEAR);
01571 ztc.chan = 0;
01572 ztc.confno = conf->zapconf;
01573 ztc.confmode = DAHDI_CONF_CONFANN | DAHDI_CONF_CONFANNMON;
01574 if (ioctl(conf->lchan->fds[0], DAHDI_SETCONF, &ztc)) {
01575 ast_log(LOG_WARNING, "Error starting listen channel\n");
01576 ast_hangup(conf->lchan);
01577 conf->lchan = NULL;
01578 } else {
01579 pthread_attr_init(&conf->attr);
01580 pthread_attr_setdetachstate(&conf->attr, PTHREAD_CREATE_DETACHED);
01581 ast_pthread_create_background(&conf->recordthread, &conf->attr, recordthread, conf);
01582 pthread_attr_destroy(&conf->attr);
01583 }
01584 }
01585 ast_mutex_unlock(&conf->recordthreadlock);
01586
01587 ast_mutex_lock(&conf->announcethreadlock);
01588 if ((conf->announcethread == AST_PTHREADT_NULL) && !(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW))) {
01589 ast_mutex_init(&conf->announcelistlock);
01590 AST_LIST_HEAD_INIT_NOLOCK(&conf->announcelist);
01591 ast_pthread_create_background(&conf->announcethread, NULL, announce_thread, conf);
01592 }
01593 ast_mutex_unlock(&conf->announcethreadlock);
01594
01595 time(&user->jointime);
01596
01597 if (conf->locked && (!(confflags & CONFFLAG_ADMIN))) {
01598
01599 if (!ast_streamfile(chan, "conf-locked", chan->language))
01600 ast_waitstream(chan, "");
01601 goto outrun;
01602 }
01603
01604 ast_mutex_lock(&conf->playlock);
01605
01606 if (AST_LIST_EMPTY(&conf->userlist))
01607 user->user_no = 1;
01608 else
01609 user->user_no = AST_LIST_LAST(&conf->userlist)->user_no + 1;
01610
01611 AST_LIST_INSERT_TAIL(&conf->userlist, user, list);
01612
01613 user->chan = chan;
01614 user->userflags = confflags;
01615 user->adminflags = (confflags & CONFFLAG_STARTMUTED) ? ADMINFLAG_SELFMUTED : 0;
01616 user->talking = -1;
01617
01618 ast_mutex_unlock(&conf->playlock);
01619
01620 if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW))) {
01621 char destdir[PATH_MAX];
01622
01623 snprintf(destdir, sizeof(destdir), "%s/meetme", ast_config_AST_SPOOL_DIR);
01624
01625 if (mkdir(destdir, 0777) && errno != EEXIST) {
01626 ast_log(LOG_WARNING, "mkdir '%s' failed: %s\n", destdir, strerror(errno));
01627 goto outrun;
01628 }
01629
01630 snprintf(user->namerecloc, sizeof(user->namerecloc),
01631 "%s/meetme-username-%s-%d", destdir,
01632 conf->confno, user->user_no);
01633 if (confflags & CONFFLAG_INTROUSERNOREVIEW)
01634 res = ast_play_and_record(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, 128, 0, NULL);
01635 else
01636 res = ast_record_review(chan, "vm-rec-name", user->namerecloc, 10, "sln", &duration, NULL);
01637 if (res == -1)
01638 goto outrun;
01639 }
01640
01641 ast_mutex_lock(&conf->playlock);
01642
01643 if (confflags & CONFFLAG_MARKEDUSER)
01644 conf->markedusers++;
01645 conf->users++;
01646
01647 snprintf(members, sizeof(members), "%d", conf->users);
01648 ast_update_realtime("meetme", "confno", conf->confno, "members", members , NULL);
01649 setusercount = 1;
01650
01651
01652 if (conf->users == 1)
01653 ast_device_state_changed("meetme:%s", conf->confno);
01654
01655 ast_mutex_unlock(&conf->playlock);
01656
01657 if (confflags & CONFFLAG_EXIT_CONTEXT) {
01658 if ((agifile = pbx_builtin_getvar_helper(chan, "MEETME_EXIT_CONTEXT")))
01659 ast_copy_string(exitcontext, agifile, sizeof(exitcontext));
01660 else if (!ast_strlen_zero(chan->macrocontext))
01661 ast_copy_string(exitcontext, chan->macrocontext, sizeof(exitcontext));
01662 else
01663 ast_copy_string(exitcontext, chan->context, sizeof(exitcontext));
01664 }
01665
01666 if ( !(confflags & (CONFFLAG_QUIET | CONFFLAG_NOONLYPERSON)) ) {
01667 if (conf->users == 1 && !(confflags & CONFFLAG_WAITMARKED))
01668 if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
01669 ast_waitstream(chan, "");
01670 if ((confflags & CONFFLAG_WAITMARKED) && conf->markedusers == 0)
01671 if (!ast_streamfile(chan, "conf-waitforleader", chan->language))
01672 ast_waitstream(chan, "");
01673 }
01674
01675 if (!(confflags & CONFFLAG_QUIET) && (confflags & CONFFLAG_ANNOUNCEUSERCOUNT) && conf->users > 1) {
01676 int keepplaying = 1;
01677
01678 if (conf->users == 2) {
01679 if (!ast_streamfile(chan,"conf-onlyone",chan->language)) {
01680 res = ast_waitstream(chan, AST_DIGIT_ANY);
01681 ast_stopstream(chan);
01682 if (res > 0)
01683 keepplaying=0;
01684 else if (res == -1)
01685 goto outrun;
01686 }
01687 } else {
01688 if (!ast_streamfile(chan, "conf-thereare", chan->language)) {
01689 res = ast_waitstream(chan, AST_DIGIT_ANY);
01690 ast_stopstream(chan);
01691 if (res > 0)
01692 keepplaying=0;
01693 else if (res == -1)
01694 goto outrun;
01695 }
01696 if (keepplaying) {
01697 res = ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
01698 if (res > 0)
01699 keepplaying=0;
01700 else if (res == -1)
01701 goto outrun;
01702 }
01703 if (keepplaying && !ast_streamfile(chan, "conf-otherinparty", chan->language)) {
01704 res = ast_waitstream(chan, AST_DIGIT_ANY);
01705 ast_stopstream(chan);
01706 if (res > 0)
01707 keepplaying=0;
01708 else if (res == -1)
01709 goto outrun;
01710 }
01711 }
01712 }
01713
01714 if (!(confflags & CONFFLAG_NO_AUDIO_UNTIL_UP)) {
01715
01716 ast_indicate(chan, -1);
01717 }
01718
01719 if (ast_set_write_format(chan, AST_FORMAT_SLINEAR) < 0) {
01720 ast_log(LOG_WARNING, "Unable to set '%s' to write linear mode\n", chan->name);
01721 goto outrun;
01722 }
01723
01724 if (ast_set_read_format(chan, AST_FORMAT_SLINEAR) < 0) {
01725 ast_log(LOG_WARNING, "Unable to set '%s' to read linear mode\n", chan->name);
01726 goto outrun;
01727 }
01728
01729 retryzap = (strcasecmp(chan->tech->type, dahdi_chan_name) || (chan->audiohooks || chan->monitor) ? 1 : 0);
01730 user->zapchannel = !retryzap;
01731
01732 zapretry:
01733 origfd = chan->fds[0];
01734 if (retryzap) {
01735 #ifdef HAVE_ZAPTEL
01736 fd = open("/dev/zap/pseudo", O_RDWR);
01737 #else
01738 fd = open("/dev/dahdi/pseudo", O_RDWR);
01739 #endif
01740 if (fd < 0) {
01741 ast_log(LOG_WARNING, "Unable to open pseudo channel: %s\n", strerror(errno));
01742 goto outrun;
01743 }
01744 using_pseudo = 1;
01745
01746 flags = fcntl(fd, F_GETFL);
01747 if (flags < 0) {
01748 ast_log(LOG_WARNING, "Unable to get flags: %s\n", strerror(errno));
01749 close(fd);
01750 goto outrun;
01751 }
01752 if (fcntl(fd, F_SETFL, flags | O_NONBLOCK)) {
01753 ast_log(LOG_WARNING, "Unable to set flags: %s\n", strerror(errno));
01754 close(fd);
01755 goto outrun;
01756 }
01757
01758 memset(&bi, 0, sizeof(bi));
01759 bi.bufsize = CONF_SIZE/2;
01760 bi.txbufpolicy = DAHDI_POLICY_IMMEDIATE;
01761 bi.rxbufpolicy = DAHDI_POLICY_IMMEDIATE;
01762 bi.numbufs = audio_buffers;
01763 if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
01764 ast_log(LOG_WARNING, "Unable to set buffering information: %s\n", strerror(errno));
01765 close(fd);
01766 goto outrun;
01767 }
01768 x = 1;
01769 if (ioctl(fd, DAHDI_SETLINEAR, &x)) {
01770 ast_log(LOG_WARNING, "Unable to set linear mode: %s\n", strerror(errno));
01771 close(fd);
01772 goto outrun;
01773 }
01774 nfds = 1;
01775 } else {
01776
01777 fd = chan->fds[0];
01778 nfds = 0;
01779 }
01780 memset(&ztc, 0, sizeof(ztc));
01781 memset(&ztc_empty, 0, sizeof(ztc_empty));
01782
01783 ztc.chan = 0;
01784 if (ioctl(fd, DAHDI_GETCONF, &ztc)) {
01785 ast_log(LOG_WARNING, "Error getting conference\n");
01786 close(fd);
01787 goto outrun;
01788 }
01789 if (ztc.confmode) {
01790
01791 if (!retryzap) {
01792 ast_log(LOG_DEBUG, "%s channel is in a conference already, retrying with pseudo\n", dahdi_chan_name);
01793 retryzap = 1;
01794 goto zapretry;
01795 }
01796 }
01797 memset(&ztc, 0, sizeof(ztc));
01798
01799 ztc.chan = 0;
01800 ztc.confno = conf->zapconf;
01801
01802 if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW)) && conf->users > 1) {
01803 struct announce_listitem *item;
01804 if (!(item = ao2_alloc(sizeof(*item), NULL)))
01805 return -1;
01806 ast_copy_string(item->namerecloc, user->namerecloc, sizeof(item->namerecloc));
01807 ast_copy_string(item->language, chan->language, sizeof(item->language));
01808 item->confchan = conf->chan;
01809 item->confusers = conf->users;
01810 item->announcetype = CONF_HASJOIN;
01811 ast_mutex_lock(&conf->announcelistlock);
01812 ao2_ref(item, +1);
01813 AST_LIST_INSERT_TAIL(&conf->announcelist, item, entry);
01814 ast_cond_signal(&conf->announcelist_addition);
01815 ast_mutex_unlock(&conf->announcelistlock);
01816
01817 while (!ast_check_hangup(conf->chan) && ao2_ref(item, 0) == 2 && !ast_safe_sleep(chan, 1000)) {
01818 ;
01819 }
01820 ao2_ref(item, -1);
01821 }
01822
01823 if (confflags & CONFFLAG_WAITMARKED && !conf->markedusers)
01824 ztc.confmode = DAHDI_CONF_CONF;
01825 else if (confflags & CONFFLAG_MONITOR)
01826 ztc.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
01827 else if (confflags & CONFFLAG_TALKER)
01828 ztc.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
01829 else
01830 ztc.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
01831
01832 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
01833 ast_log(LOG_WARNING, "Error setting conference\n");
01834 close(fd);
01835 goto outrun;
01836 }
01837 ast_log(LOG_DEBUG, "Placed channel %s in %s conf %d\n", chan->name, dahdi_chan_name, conf->zapconf);
01838
01839 if (!sent_event) {
01840 manager_event(EVENT_FLAG_CALL, "MeetmeJoin",
01841 "Channel: %s\r\n"
01842 "Uniqueid: %s\r\n"
01843 "Meetme: %s\r\n"
01844 "Usernum: %d\r\n",
01845 chan->name, chan->uniqueid, conf->confno, user->user_no);
01846 sent_event = 1;
01847 }
01848
01849 if (!firstpass && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN)) {
01850 firstpass = 1;
01851 if (!(confflags & CONFFLAG_QUIET))
01852 if (!(confflags & CONFFLAG_WAITMARKED) || ((confflags & CONFFLAG_MARKEDUSER) && (conf->markedusers >= 1)))
01853 conf_play(chan, conf, ENTER);
01854 }
01855
01856 conf_flush(fd, chan);
01857
01858 if (confflags & CONFFLAG_AGI) {
01859
01860
01861
01862 agifile = pbx_builtin_getvar_helper(chan, "MEETME_AGI_BACKGROUND");
01863 if (!agifile)
01864 agifile = agifiledefault;
01865
01866 if (user->zapchannel) {
01867
01868 x = 1;
01869 ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
01870 }
01871
01872 app = pbx_findapp("agi");
01873 if (app) {
01874 char *s = ast_strdupa(agifile);
01875 ret = pbx_exec(chan, app, s);
01876 } else {
01877 ast_log(LOG_WARNING, "Could not find application (agi)\n");
01878 ret = -2;
01879 }
01880 if (user->zapchannel) {
01881
01882 x = 0;
01883 ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
01884 }
01885 } else {
01886 if (user->zapchannel && (confflags & CONFFLAG_STARMENU)) {
01887
01888 x = 1;
01889 ast_channel_setoption(chan, AST_OPTION_TONE_VERIFY, &x, sizeof(char), 0);
01890 }
01891 if (confflags & (CONFFLAG_MONITORTALKER | CONFFLAG_OPTIMIZETALKER) && !(dsp = ast_dsp_new())) {
01892 ast_log(LOG_WARNING, "Unable to allocate DSP!\n");
01893 res = -1;
01894 }
01895 for(;;) {
01896 int menu_was_active = 0;
01897
01898 outfd = -1;
01899 ms = -1;
01900
01901 if (timeout && time(NULL) >= timeout)
01902 break;
01903
01904
01905
01906
01907 if (!menu_active && menu_was_active && user->listen.desired && !user->listen.actual)
01908 set_talk_volume(user, user->listen.desired);
01909
01910 menu_was_active = menu_active;
01911
01912 currentmarked = conf->markedusers;
01913 if (!(confflags & CONFFLAG_QUIET) &&
01914 (confflags & CONFFLAG_MARKEDUSER) &&
01915 (confflags & CONFFLAG_WAITMARKED) &&
01916 lastmarked == 0) {
01917 if (currentmarked == 1 && conf->users > 1) {
01918 ast_say_number(chan, conf->users - 1, AST_DIGIT_ANY, chan->language, (char *) NULL);
01919 if (conf->users - 1 == 1) {
01920 if (!ast_streamfile(chan, "conf-userwilljoin", chan->language))
01921 ast_waitstream(chan, "");
01922 } else {
01923 if (!ast_streamfile(chan, "conf-userswilljoin", chan->language))
01924 ast_waitstream(chan, "");
01925 }
01926 }
01927 if (conf->users == 1 && ! (confflags & CONFFLAG_MARKEDUSER))
01928 if (!ast_streamfile(chan, "conf-onlyperson", chan->language))
01929 ast_waitstream(chan, "");
01930 }
01931
01932 c = ast_waitfor_nandfds(&chan, 1, &fd, nfds, NULL, &outfd, &ms);
01933
01934
01935
01936 user->userflags = confflags;
01937
01938 if (confflags & CONFFLAG_WAITMARKED) {
01939 if(currentmarked == 0) {
01940 if (lastmarked != 0) {
01941 if (!(confflags & CONFFLAG_QUIET))
01942 if (!ast_streamfile(chan, "conf-leaderhasleft", chan->language))
01943 ast_waitstream(chan, "");
01944 if(confflags & CONFFLAG_MARKEDEXIT)
01945 break;
01946 else {
01947 ztc.confmode = DAHDI_CONF_CONF;
01948 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
01949 ast_log(LOG_WARNING, "Error setting conference\n");
01950 close(fd);
01951 goto outrun;
01952 }
01953 }
01954 }
01955 if (musiconhold == 0 && (confflags & CONFFLAG_MOH)) {
01956 ast_moh_start(chan, NULL, NULL);
01957 musiconhold = 1;
01958 }
01959 } else if(currentmarked >= 1 && lastmarked == 0) {
01960
01961 timeout = 0;
01962 if (confflags & CONFFLAG_MONITOR)
01963 ztc.confmode = DAHDI_CONF_CONFMON | DAHDI_CONF_LISTENER;
01964 else if (confflags & CONFFLAG_TALKER)
01965 ztc.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER;
01966 else
01967 ztc.confmode = DAHDI_CONF_CONF | DAHDI_CONF_TALKER | DAHDI_CONF_LISTENER;
01968 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
01969 ast_log(LOG_WARNING, "Error setting conference\n");
01970 close(fd);
01971 goto outrun;
01972 }
01973 if (musiconhold && (confflags & CONFFLAG_MOH)) {
01974 ast_moh_stop(chan);
01975 musiconhold = 0;
01976 }
01977 if ( !(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MARKEDUSER)) {
01978 if (!ast_streamfile(chan, "conf-placeintoconf", chan->language))
01979 ast_waitstream(chan, "");
01980 conf_play(chan, conf, ENTER);
01981 }
01982 }
01983 }
01984
01985
01986 if ((confflags & CONFFLAG_MOH) && !(confflags & CONFFLAG_WAITMARKED)) {
01987 if (conf->users == 1) {
01988 if (musiconhold == 0) {
01989 ast_moh_start(chan, NULL, NULL);
01990 musiconhold = 1;
01991 }
01992 } else {
01993 if (musiconhold) {
01994 ast_moh_stop(chan);
01995 musiconhold = 0;
01996 }
01997 }
01998 }
01999
02000
02001 if (currentmarked == 0 && lastmarked != 0 && (confflags & CONFFLAG_MARKEDEXIT)) {
02002 ret = -1;
02003 break;
02004 }
02005
02006
02007
02008
02009 if ((user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && (ztc.confmode & DAHDI_CONF_TALKER)) {
02010 ztc.confmode ^= DAHDI_CONF_TALKER;
02011 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
02012 ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
02013 ret = -1;
02014 break;
02015 }
02016
02017 manager_event(EVENT_FLAG_CALL, "MeetmeMute",
02018 "Channel: %s\r\n"
02019 "Uniqueid: %s\r\n"
02020 "Meetme: %s\r\n"
02021 "Usernum: %i\r\n"
02022 "Status: on\r\n",
02023 chan->name, chan->uniqueid, conf->confno, user->user_no);
02024 }
02025
02026
02027 if (!(user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) && !(confflags & CONFFLAG_MONITOR) && !(ztc.confmode & DAHDI_CONF_TALKER)) {
02028 ztc.confmode |= DAHDI_CONF_TALKER;
02029 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
02030 ast_log(LOG_WARNING, "Error setting conference - Un/Mute \n");
02031 ret = -1;
02032 break;
02033 }
02034
02035 manager_event(EVENT_FLAG_CALL, "MeetmeMute",
02036 "Channel: %s\r\n"
02037 "Uniqueid: %s\r\n"
02038 "Meetme: %s\r\n"
02039 "Usernum: %i\r\n"
02040 "Status: off\r\n",
02041 chan->name, chan->uniqueid, conf->confno, user->user_no);
02042 }
02043
02044
02045 if (user->adminflags & ADMINFLAG_KICKME) {
02046
02047 if (!(confflags & CONFFLAG_QUIET) &&
02048 !ast_streamfile(chan, "conf-kicked", chan->language)) {
02049 ast_waitstream(chan, "");
02050 }
02051 ret = 0;
02052 break;
02053 }
02054
02055
02056 if (ast_check_hangup(chan))
02057 break;
02058
02059 if (c) {
02060 char dtmfstr[2] = "";
02061
02062 if (c->fds[0] != origfd || (user->zapchannel && (c->audiohooks || c->monitor))) {
02063 if (using_pseudo) {
02064
02065 close(fd);
02066 using_pseudo = 0;
02067 }
02068 ast_log(LOG_DEBUG, "Ooh, something swapped out under us, starting over\n");
02069 retryzap = (strcasecmp(c->tech->type, dahdi_chan_name) || (c->audiohooks || c->monitor) ? 1 : 0);
02070 user->zapchannel = !retryzap;
02071 goto zapretry;
02072 }
02073 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)))
02074 f = ast_read_noaudio(c);
02075 else
02076 f = ast_read(c);
02077 if (!f)
02078 break;
02079 if (f->frametype == AST_FRAME_DTMF) {
02080 dtmfstr[0] = f->subclass;
02081 dtmfstr[1] = '\0';
02082 }
02083
02084 if ((f->frametype == AST_FRAME_VOICE) && (f->subclass == AST_FORMAT_SLINEAR)) {
02085 if (user->talk.actual)
02086 ast_frame_adjust_volume(f, user->talk.actual);
02087
02088 if (confflags & (CONFFLAG_MONITORTALKER | CONFFLAG_OPTIMIZETALKER)) {
02089 int totalsilence;
02090
02091 if (user->talking == -1)
02092 user->talking = 0;
02093
02094 res = ast_dsp_silence(dsp, f, &totalsilence);
02095 if (!user->talking && totalsilence < MEETME_DELAYDETECTTALK) {
02096 user->talking = 1;
02097 if (confflags & CONFFLAG_MONITORTALKER)
02098 manager_event(EVENT_FLAG_CALL, "MeetmeTalking",
02099 "Channel: %s\r\n"
02100 "Uniqueid: %s\r\n"
02101 "Meetme: %s\r\n"
02102 "Usernum: %d\r\n"
02103 "Status: on\r\n",
02104 chan->name, chan->uniqueid, conf->confno, user->user_no);
02105 }
02106 if (user->talking && totalsilence > MEETME_DELAYDETECTENDTALK) {
02107 user->talking = 0;
02108 if (confflags & CONFFLAG_MONITORTALKER)
02109 manager_event(EVENT_FLAG_CALL, "MeetmeTalking",
02110 "Channel: %s\r\n"
02111 "Uniqueid: %s\r\n"
02112 "Meetme: %s\r\n"
02113 "Usernum: %d\r\n"
02114 "Status: off\r\n",
02115 chan->name, chan->uniqueid, conf->confno, user->user_no);
02116 }
02117 }
02118 if (using_pseudo) {
02119
02120
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130
02131 if (user->talking || !(confflags & CONFFLAG_OPTIMIZETALKER))
02132 careful_write(fd, f->data, f->datalen, 0);
02133 }
02134 } else if ((f->frametype == AST_FRAME_DTMF) && (f->subclass == '#') && (confflags & CONFFLAG_POUNDEXIT)) {
02135 if (confflags & CONFFLAG_PASS_DTMF)
02136 conf_queue_dtmf(conf, user, f);
02137 ret = 0;
02138 ast_frfree(f);
02139 break;
02140 } else if (((f->frametype == AST_FRAME_DTMF) && (f->subclass == '*') && (confflags & CONFFLAG_STARMENU)) || ((f->frametype == AST_FRAME_DTMF) && menu_active)) {
02141 if (confflags & CONFFLAG_PASS_DTMF)
02142 conf_queue_dtmf(conf, user, f);
02143 if (ioctl(fd, DAHDI_SETCONF, &ztc_empty)) {
02144 ast_log(LOG_WARNING, "Error setting conference\n");
02145 close(fd);
02146 ast_frfree(f);
02147 goto outrun;
02148 }
02149
02150
02151
02152
02153 if (!menu_active && user->talk.desired && !user->talk.actual)
02154 set_talk_volume(user, 0);
02155
02156 if (musiconhold) {
02157 ast_moh_stop(chan);
02158 }
02159 if ((confflags & CONFFLAG_ADMIN)) {
02160
02161 if (!menu_active) {
02162 menu_active = 1;
02163
02164 if (!ast_streamfile(chan, "conf-adminmenu", chan->language)) {
02165 dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
02166 ast_stopstream(chan);
02167 } else
02168 dtmf = 0;
02169 } else
02170 dtmf = f->subclass;
02171 if (dtmf) {
02172 switch(dtmf) {
02173 case '1':
02174 menu_active = 0;
02175
02176
02177 if (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))
02178 user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
02179 else
02180 user->adminflags |= (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
02181
02182 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
02183 if (!ast_streamfile(chan, "conf-muted", chan->language))
02184 ast_waitstream(chan, "");
02185 } else {
02186 if (!ast_streamfile(chan, "conf-unmuted", chan->language))
02187 ast_waitstream(chan, "");
02188 }
02189 break;
02190 case '2':
02191 menu_active = 0;
02192 if (conf->locked) {
02193 conf->locked = 0;
02194 if (!ast_streamfile(chan, "conf-unlockednow", chan->language))
02195 ast_waitstream(chan, "");
02196 } else {
02197 conf->locked = 1;
02198 if (!ast_streamfile(chan, "conf-lockednow", chan->language))
02199 ast_waitstream(chan, "");
02200 }
02201 break;
02202 case '3':
02203 menu_active = 0;
02204 usr = AST_LIST_LAST(&conf->userlist);
02205 if ((usr->chan->name == chan->name)||(usr->userflags & CONFFLAG_ADMIN)) {
02206 if(!ast_streamfile(chan, "conf-errormenu", chan->language))
02207 ast_waitstream(chan, "");
02208 } else
02209 usr->adminflags |= ADMINFLAG_KICKME;
02210 ast_stopstream(chan);
02211 break;
02212 case '4':
02213 tweak_listen_volume(user, VOL_DOWN);
02214 break;
02215 case '6':
02216 tweak_listen_volume(user, VOL_UP);
02217 break;
02218 case '7':
02219 tweak_talk_volume(user, VOL_DOWN);
02220 break;
02221 case '8':
02222 menu_active = 0;
02223 break;
02224 case '9':
02225 tweak_talk_volume(user, VOL_UP);
02226 break;
02227 default:
02228 menu_active = 0;
02229
02230 if (!ast_streamfile(chan, "conf-errormenu", chan->language))
02231 ast_waitstream(chan, "");
02232 break;
02233 }
02234 }
02235 } else {
02236
02237 if (!menu_active) {
02238 menu_active = 1;
02239 if (!ast_streamfile(chan, "conf-usermenu", chan->language)) {
02240 dtmf = ast_waitstream(chan, AST_DIGIT_ANY);
02241 ast_stopstream(chan);
02242 } else
02243 dtmf = 0;
02244 } else
02245 dtmf = f->subclass;
02246 if (dtmf) {
02247 switch(dtmf) {
02248 case '1':
02249 menu_active = 0;
02250
02251
02252 user->adminflags ^= ADMINFLAG_SELFMUTED;
02253
02254
02255 if ((confflags & CONFFLAG_MONITOR) || (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED))) {
02256 if (!ast_streamfile(chan, "conf-muted", chan->language))
02257 ast_waitstream(chan, "");
02258 } else {
02259 if (!ast_streamfile(chan, "conf-unmuted", chan->language))
02260 ast_waitstream(chan, "");
02261 }
02262 break;
02263 case '4':
02264 tweak_listen_volume(user, VOL_DOWN);
02265 break;
02266 case '6':
02267 tweak_listen_volume(user, VOL_UP);
02268 break;
02269 case '7':
02270 tweak_talk_volume(user, VOL_DOWN);
02271 break;
02272 case '8':
02273 menu_active = 0;
02274 break;
02275 case '9':
02276 tweak_talk_volume(user, VOL_UP);
02277 break;
02278 default:
02279 menu_active = 0;
02280 if (!ast_streamfile(chan, "conf-errormenu", chan->language))
02281 ast_waitstream(chan, "");
02282 break;
02283 }
02284 }
02285 }
02286 if (musiconhold)
02287 ast_moh_start(chan, NULL, NULL);
02288
02289 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
02290 ast_log(LOG_WARNING, "Error setting conference\n");
02291 close(fd);
02292 ast_frfree(f);
02293 goto outrun;
02294 }
02295
02296 conf_flush(fd, chan);
02297
02298 } else if ((f->frametype == AST_FRAME_DTMF) && (confflags & CONFFLAG_EXIT_CONTEXT) && ast_exists_extension(chan, exitcontext, dtmfstr, 1, "")) {
02299 if (confflags & CONFFLAG_PASS_DTMF)
02300 conf_queue_dtmf(conf, user, f);
02301
02302 if (!ast_goto_if_exists(chan, exitcontext, dtmfstr, 1)) {
02303 ast_log(LOG_DEBUG, "Got DTMF %c, goto context %s\n", dtmfstr[0], exitcontext);
02304 ret = 0;
02305 ast_frfree(f);
02306 break;
02307 } else if (option_debug > 1)
02308 ast_log(LOG_DEBUG, "Exit by single digit did not work in meetme. Extension '%s' does not exist in context '%s'\n", dtmfstr, exitcontext);
02309 } else if ((f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END)
02310 && confflags & CONFFLAG_PASS_DTMF) {
02311 conf_queue_dtmf(conf, user, f);
02312 } else if ((confflags & CONFFLAG_SLA_STATION) && f->frametype == AST_FRAME_CONTROL) {
02313 switch (f->subclass) {
02314 case AST_CONTROL_HOLD:
02315 sla_queue_event_conf(SLA_EVENT_HOLD, chan, conf);
02316 break;
02317 default:
02318 break;
02319 }
02320 } else if (f->frametype == AST_FRAME_NULL) {
02321
02322 } else if (option_debug) {
02323 ast_log(LOG_DEBUG,
02324 "Got unrecognized frame on channel %s, f->frametype=%d,f->subclass=%d\n",
02325 chan->name, f->frametype, f->subclass);
02326 }
02327 ast_frfree(f);
02328 } else if (outfd > -1) {
02329 res = read(outfd, buf, CONF_SIZE);
02330 if (res > 0) {
02331 memset(&fr, 0, sizeof(fr));
02332 fr.frametype = AST_FRAME_VOICE;
02333 fr.subclass = AST_FORMAT_SLINEAR;
02334 fr.datalen = res;
02335 fr.samples = res/2;
02336 fr.data = buf;
02337 fr.offset = AST_FRIENDLY_OFFSET;
02338 if (!user->listen.actual &&
02339 ((confflags & CONFFLAG_MONITOR) ||
02340 (user->adminflags & (ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED)) ||
02341 (!user->talking && (confflags & CONFFLAG_OPTIMIZETALKER))
02342 )) {
02343 int index;
02344 for (index=0;index<AST_FRAME_BITS;index++)
02345 if (chan->rawwriteformat & (1 << index))
02346 break;
02347 if (index >= AST_FRAME_BITS)
02348 goto bailoutandtrynormal;
02349 ast_mutex_lock(&conf->listenlock);
02350 if (!conf->transframe[index]) {
02351 if (conf->origframe) {
02352 if (!conf->transpath[index])
02353 conf->transpath[index] = ast_translator_build_path((1 << index), AST_FORMAT_SLINEAR);
02354 if (conf->transpath[index]) {
02355 conf->transframe[index] = ast_translate(conf->transpath[index], conf->origframe, 0);
02356 if (!conf->transframe[index])
02357 conf->transframe[index] = &ast_null_frame;
02358 }
02359 }
02360 }
02361 if (conf->transframe[index]) {
02362 if (conf->transframe[index]->frametype != AST_FRAME_NULL) {
02363 if (can_write(chan, confflags) && ast_write(chan, conf->transframe[index]))
02364 ast_log(LOG_WARNING, "Unable to write frame to channel %s\n", chan->name);
02365 }
02366 } else {
02367 ast_mutex_unlock(&conf->listenlock);
02368 goto bailoutandtrynormal;
02369 }
02370 ast_mutex_unlock(&conf->listenlock);
02371 } else {
02372 bailoutandtrynormal:
02373 if (user->listen.actual)
02374 ast_frame_adjust_volume(&fr, user->listen.actual);
02375 if (can_write(chan, confflags) && ast_write(chan, &fr) < 0) {
02376 ast_log(LOG_WARNING, "Unable to write frame to channel %s\n", chan->name);
02377 }
02378 }
02379 } else
02380 ast_log(LOG_WARNING, "Failed to read frame: %s\n", strerror(errno));
02381 }
02382 lastmarked = currentmarked;
02383 }
02384 }
02385
02386 if (musiconhold)
02387 ast_moh_stop(chan);
02388
02389 if (using_pseudo)
02390 close(fd);
02391 else {
02392
02393 ztc.chan = 0;
02394 ztc.confno = 0;
02395 ztc.confmode = 0;
02396 if (ioctl(fd, DAHDI_SETCONF, &ztc)) {
02397 ast_log(LOG_WARNING, "Error setting conference\n");
02398 }
02399 }
02400
02401 reset_volumes(user);
02402
02403 if (!(confflags & CONFFLAG_QUIET) && !(confflags & CONFFLAG_MONITOR) && !(confflags & CONFFLAG_ADMIN))
02404 conf_play(chan, conf, LEAVE);
02405
02406 if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW)) && conf->users > 1) {
02407 struct announce_listitem *item;
02408 if (!(item = ao2_alloc(sizeof(*item), NULL)))
02409 return -1;
02410 ast_copy_string(item->namerecloc, user->namerecloc, sizeof(item->namerecloc));
02411 ast_copy_string(item->language, chan->language, sizeof(item->language));
02412 item->confchan = conf->chan;
02413 item->confusers = conf->users;
02414 item->announcetype = CONF_HASLEFT;
02415 ast_mutex_lock(&conf->announcelistlock);
02416 AST_LIST_INSERT_TAIL(&conf->announcelist, item, entry);
02417 ast_cond_signal(&conf->announcelist_addition);
02418 ast_mutex_unlock(&conf->announcelistlock);
02419 } else if (!(confflags & CONFFLAG_QUIET) && ((confflags & CONFFLAG_INTROUSER) || (confflags & CONFFLAG_INTROUSERNOREVIEW)) && conf->users == 1) {
02420
02421 ast_filedelete(user->namerecloc, NULL);
02422 }
02423
02424 outrun:
02425 AST_LIST_LOCK(&confs);
02426
02427 if (dsp)
02428 ast_dsp_free(dsp);
02429
02430 if (user->user_no) {
02431 now = time(NULL);
02432 hr = (now - user->jointime) / 3600;
02433 min = ((now - user->jointime) % 3600) / 60;
02434 sec = (now - user->jointime) % 60;
02435
02436 if (sent_event) {
02437 manager_event(EVENT_FLAG_CALL, "MeetmeLeave",
02438 "Channel: %s\r\n"
02439 "Uniqueid: %s\r\n"
02440 "Meetme: %s\r\n"
02441 "Usernum: %d\r\n"
02442 "CallerIDnum: %s\r\n"
02443 "CallerIDname: %s\r\n"
02444 "Duration: %ld\r\n",
02445 chan->name, chan->uniqueid, conf->confno,
02446 user->user_no,
02447 S_OR(user->chan->cid.cid_num, "<unknown>"),
02448 S_OR(user->chan->cid.cid_name, "<unknown>"),
02449 (long)(now - user->jointime));
02450 }
02451
02452 if (setusercount) {
02453 conf->users--;
02454
02455 snprintf(members, sizeof(members), "%d", conf->users);
02456 ast_update_realtime("meetme", "confno", conf->confno, "members", members, NULL);
02457 if (confflags & CONFFLAG_MARKEDUSER)
02458 conf->markedusers--;
02459 }
02460
02461 AST_LIST_REMOVE(&conf->userlist, user, list);
02462
02463
02464 if (!conf->users)
02465 ast_device_state_changed("meetme:%s", conf->confno);
02466
02467
02468 snprintf(meetmesecs, sizeof(meetmesecs), "%d", (int) (time(NULL) - user->jointime));
02469 pbx_builtin_setvar_helper(chan, "MEETMESECS", meetmesecs);
02470 }
02471 free(user);
02472 AST_LIST_UNLOCK(&confs);
02473
02474 return ret;
02475 }
02476
02477 static struct ast_conference *find_conf_realtime(struct ast_channel *chan, char *confno, int make, int dynamic,
02478 char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags *confflags)
02479 {
02480 struct ast_variable *var, *save;
02481 struct ast_conference *cnf;
02482
02483
02484 AST_LIST_LOCK(&confs);
02485 AST_LIST_TRAVERSE(&confs, cnf, list) {
02486 if (!strcmp(confno, cnf->confno))
02487 break;
02488 }
02489 if (cnf) {
02490 cnf->refcount += refcount;
02491 }
02492 AST_LIST_UNLOCK(&confs);
02493
02494 if (!cnf) {
02495 char *pin = NULL, *pinadmin = NULL;
02496
02497 var = ast_load_realtime("meetme", "confno", confno, NULL);
02498
02499 if (!var)
02500 return NULL;
02501
02502 save = var;
02503 while (var) {
02504 if (!strcasecmp(var->name, "pin")) {
02505 pin = ast_strdupa(var->value);
02506 } else if (!strcasecmp(var->name, "adminpin")) {
02507 pinadmin = ast_strdupa(var->value);
02508 }
02509 var = var->next;
02510 }
02511 ast_variables_destroy(save);
02512
02513 cnf = build_conf(confno, pin ? pin : "", pinadmin ? pinadmin : "", make, dynamic, refcount);
02514 }
02515
02516 if (cnf) {
02517 if (confflags && !cnf->chan &&
02518 !ast_test_flag(confflags, CONFFLAG_QUIET) &&
02519 ast_test_flag(confflags, CONFFLAG_INTROUSER)) {
02520 ast_log(LOG_WARNING, "No %s channel available for conference, user introduction disabled\n", dahdi_chan_name);
02521 ast_clear_flag(confflags, CONFFLAG_INTROUSER);
02522 }
02523
02524 if (confflags && !cnf->chan &&
02525 ast_test_flag(confflags, CONFFLAG_RECORDCONF)) {
02526 ast_log(LOG_WARNING, "No %s channel available for conference, conference recording disabled\n", dahdi_chan_name);
02527 ast_clear_flag(confflags, CONFFLAG_RECORDCONF);
02528 }
02529 }
02530
02531 return cnf;
02532 }
02533
02534
02535 static struct ast_conference *find_conf(struct ast_channel *chan, char *confno, int make, int dynamic,
02536 char *dynamic_pin, size_t pin_buf_len, int refcount, struct ast_flags *confflags)
02537 {
02538 struct ast_config *cfg;
02539 struct ast_variable *var;
02540 struct ast_conference *cnf;
02541 char *parse;
02542 AST_DECLARE_APP_ARGS(args,
02543 AST_APP_ARG(confno);
02544 AST_APP_ARG(pin);
02545 AST_APP_ARG(pinadmin);
02546 );
02547
02548
02549 AST_LIST_LOCK(&confs);
02550 AST_LIST_TRAVERSE(&confs, cnf, list) {
02551 if (!strcmp(confno, cnf->confno))
02552 break;
02553 }
02554 if (cnf){
02555 cnf->refcount += refcount;
02556 }
02557 AST_LIST_UNLOCK(&confs);
02558
02559 if (!cnf) {
02560 if (dynamic) {
02561
02562 ast_log(LOG_DEBUG, "Building dynamic conference '%s'\n", confno);
02563 if (dynamic_pin) {
02564 if (dynamic_pin[0] == 'q') {
02565
02566 if (ast_app_getdata(chan, "conf-getpin", dynamic_pin, pin_buf_len - 1, 0) < 0)
02567 return NULL;
02568 }
02569 cnf = build_conf(confno, dynamic_pin, "", make, dynamic, refcount);
02570 } else {
02571 cnf = build_conf(confno, "", "", make, dynamic, refcount);
02572 }
02573 } else {
02574
02575 cfg = ast_config_load(CONFIG_FILE_NAME);
02576 if (!cfg) {
02577 ast_log(LOG_WARNING, "No %s file :(\n", CONFIG_FILE_NAME);
02578 return NULL;
02579 }
02580 for (var = ast_variable_browse(cfg, "rooms"); var; var = var->next) {
02581 if (strcasecmp(var->name, "conf"))
02582 continue;
02583
02584 if (!(parse = ast_strdupa(var->value)))
02585 return NULL;
02586
02587 AST_NONSTANDARD_APP_ARGS(args, parse, ',');
02588 if (!strcasecmp(args.confno, confno)) {
02589
02590 cnf = build_conf(args.confno,
02591 S_OR(args.pin, ""),
02592 S_OR(args.pinadmin, ""),
02593 make, dynamic, refcount);
02594 break;
02595 }
02596 }
02597 if (!var) {
02598 ast_log(LOG_DEBUG, "%s isn't a valid conference\n", confno);
02599 }
02600 ast_config_destroy(cfg);
02601 }
02602 } else if (dynamic_pin) {
02603
02604
02605
02606 if (dynamic_pin[0] == 'q')
02607 dynamic_pin[0] = '\0';
02608 }
02609
02610 if (cnf) {
02611 if (confflags && !cnf->chan &&
02612 !ast_test_flag(confflags, CONFFLAG_QUIET) &&
02613 ast_test_flag(confflags, CONFFLAG_INTROUSER)) {
02614 ast_log(LOG_WARNING, "No %s channel available for conference, user introduction disabled\n", dahdi_chan_name);
02615 ast_clear_flag(confflags, CONFFLAG_INTROUSER);
02616 }
02617
02618 if (confflags && !cnf->chan &&
02619 ast_test_flag(confflags, CONFFLAG_RECORDCONF)) {
02620 ast_log(LOG_WARNING, "No %s channel available for conference, conference recording disabled\n", dahdi_chan_name);
02621 ast_clear_flag(confflags, CONFFLAG_RECORDCONF);
02622 }
02623 }
02624
02625 return cnf;
02626 }
02627
02628
02629 static int count_exec(struct ast_channel *chan, void *data)
02630 {
02631 struct ast_module_user *u;
02632 int res = 0;
02633 struct ast_conference *conf;
02634 int count;
02635 char *localdata;
02636 char val[80] = "0";
02637 AST_DECLARE_APP_ARGS(args,
02638 AST_APP_ARG(confno);
02639 AST_APP_ARG(varname);
02640 );
02641
02642 if (ast_strlen_zero(data)) {
02643 ast_log(LOG_WARNING, "MeetMeCount requires an argument (conference number)\n");
02644 return -1;
02645 }
02646
02647 u = ast_module_user_add(chan);
02648
02649 if (!(localdata = ast_strdupa(data))) {
02650 ast_module_user_remove(u);
02651 return -1;
02652 }
02653
02654 AST_STANDARD_APP_ARGS(args, localdata);
02655
02656 conf = find_conf(chan, args.confno, 0, 0, NULL, 0, 1, NULL);
02657
02658 if (conf) {
02659 count = conf->users;
02660 dispose_conf(conf);
02661 conf = NULL;
02662 } else
02663 count = 0;
02664
02665 if (!ast_strlen_zero(args.varname)){
02666
02667 snprintf(val, sizeof(val), "%d",count);
02668 pbx_builtin_setvar_helper(chan, args.varname, val);
02669 } else {
02670 if (chan->_state != AST_STATE_UP)
02671 ast_answer(chan);
02672 res = ast_say_number(chan, count, "", chan->language, (char *) NULL);
02673 }
02674 ast_module_user_remove(u);
02675
02676 return res;
02677 }
02678
02679
02680 static int conf_exec(struct ast_channel *chan, void *data)
02681 {
02682 int res=-1;
02683 struct ast_module_user *u;
02684 char confno[MAX_CONFNUM] = "";
02685 int allowretry = 0;
02686 int retrycnt = 0;
02687 struct ast_conference *cnf = NULL;
02688 struct ast_flags confflags = {0};
02689 int dynamic = 0;
02690 int empty = 0, empty_no_pin = 0;
02691 int always_prompt = 0;
02692 char *notdata, *info, the_pin[MAX_PIN] = "";
02693 AST_DECLARE_APP_ARGS(args,
02694 AST_APP_ARG(confno);
02695 AST_APP_ARG(options);
02696 AST_APP_ARG(pin);
02697 );
02698 char *optargs[OPT_ARG_ARRAY_SIZE] = { NULL, };
02699
02700 u = ast_module_user_add(chan);
02701
02702 if (ast_strlen_zero(data)) {
02703 allowretry = 1;
02704 notdata = "";
02705 } else {
02706 notdata = data;
02707 }
02708
02709 if (chan->_state != AST_STATE_UP)
02710 ast_answer(chan);
02711
02712 info = ast_strdupa(notdata);
02713
02714 AST_STANDARD_APP_ARGS(args, info);
02715
02716 if (args.confno) {
02717 ast_copy_string(confno, args.confno, sizeof(confno));
02718 if (ast_strlen_zero(confno)) {
02719 allowretry = 1;
02720 }
02721 }
02722
02723 if (args.pin)
02724 ast_copy_string(the_pin, args.pin, sizeof(the_pin));
02725
02726 if (args.options) {
02727 ast_app_parse_options(meetme_opts, &confflags, optargs, args.options);
02728 dynamic = ast_test_flag(&confflags, CONFFLAG_DYNAMIC | CONFFLAG_DYNAMICPIN);
02729 if (ast_test_flag(&confflags, CONFFLAG_DYNAMICPIN) && ast_strlen_zero(args.pin))
02730 strcpy(the_pin, "q");
02731
02732 empty = ast_test_flag(&confflags, CONFFLAG_EMPTY | CONFFLAG_EMPTYNOPIN);
02733 empty_no_pin = ast_test_flag(&confflags, CONFFLAG_EMPTYNOPIN);
02734 always_prompt = ast_test_flag(&confflags, CONFFLAG_ALWAYSPROMPT);
02735 }
02736
02737 do {
02738 if (retrycnt > 3)
02739 allowretry = 0;
02740 if (empty) {
02741 int i;
02742 struct ast_config *cfg;
02743 struct ast_variable *var;
02744 int confno_int;
02745
02746
02747 if ((empty_no_pin) || (!dynamic)) {
02748 cfg = ast_config_load(CONFIG_FILE_NAME);
02749 if (cfg) {
02750 var = ast_variable_browse(cfg, "rooms");
02751 while (var) {
02752 if (!strcasecmp(var->name, "conf")) {
02753 char *stringp = ast_strdupa(var->value);
02754 if (stringp) {
02755 char *confno_tmp = strsep(&stringp, "|,");
02756 int found = 0;
02757 if (!dynamic) {
02758
02759 AST_LIST_LOCK(&confs);
02760 AST_LIST_TRAVERSE(&confs, cnf, list) {
02761 if (!strcmp(confno_tmp, cnf->confno)) {
02762
02763 found = 1;
02764 break;
02765 }
02766 }
02767 AST_LIST_UNLOCK(&confs);
02768 if (!found) {
02769
02770 if ((empty_no_pin && ast_strlen_zero(stringp)) || (!empty_no_pin)) {
02771
02772
02773
02774
02775 ast_copy_string(confno, confno_tmp, sizeof(confno));
02776 break;
02777
02778 }
02779 }
02780 }
02781 }
02782 }
02783 var = var->next;
02784 }
02785 ast_config_destroy(cfg);
02786 }
02787 }
02788
02789
02790 if (ast_strlen_zero(confno) && dynamic) {
02791 AST_LIST_LOCK(&confs);
02792 for (i = 0; i < sizeof(conf_map) / sizeof(conf_map[0]); i++) {
02793 if (!conf_map[i]) {
02794 snprintf(confno, sizeof(confno), "%d", i);
02795 conf_map[i] = 1;
02796 break;
02797 }
02798 }
02799 AST_LIST_UNLOCK(&confs);
02800 }
02801
02802
02803 if (ast_strlen_zero(confno)) {
02804 res = ast_streamfile(chan, "conf-noempty", chan->language);
02805 if (!res)
02806 ast_waitstream(chan, "");
02807 } else {
02808 if (sscanf(confno, "%d", &confno_int) == 1) {
02809 res = ast_streamfile(chan, "conf-enteringno", chan->language);
02810 if (!res) {
02811 ast_waitstream(chan, "");
02812 res = ast_say_digits(chan, confno_int, "", chan->language);
02813 }
02814 } else {
02815 ast_log(LOG_ERROR, "Could not scan confno '%s'\n", confno);
02816 }
02817 }
02818 }
02819
02820 while (allowretry && (ast_strlen_zero(confno)) && (++retrycnt < 4)) {
02821
02822 res = ast_app_getdata(chan, "conf-getconfno", confno, sizeof(confno) - 1, 0);
02823 if (res < 0) {
02824
02825 confno[0] = '\0';
02826 allowretry = 0;
02827 break;
02828 }
02829 }
02830 if (!ast_strlen_zero(confno)) {
02831
02832 cnf = find_conf(chan, confno, 1, dynamic, the_pin,
02833 sizeof(the_pin), 1, &confflags);
02834 if (!cnf) {
02835 cnf = find_conf_realtime(chan, confno, 1, dynamic,
02836 the_pin, sizeof(the_pin), 1, &confflags);
02837 }
02838
02839 if (!cnf) {
02840 res = ast_streamfile(chan, "conf-invalid", chan->language);
02841 if (!res)
02842 ast_waitstream(chan, "");
02843 res = -1;
02844 if (allowretry)
02845 confno[0] = '\0';
02846 } else {
02847 if ((!ast_strlen_zero(cnf->pin) &&
02848 !ast_test_flag(&confflags, CONFFLAG_ADMIN)) ||
02849 (!ast_strlen_zero(cnf->pinadmin) &&
02850 ast_test_flag(&confflags, CONFFLAG_ADMIN))) {
02851 char pin[MAX_PIN] = "";
02852 int j;
02853
02854
02855 for (j = 0; j < 3; j++) {
02856 if (*the_pin && (always_prompt == 0)) {
02857 ast_copy_string(pin, the_pin, sizeof(pin));
02858 res = 0;
02859 } else {
02860
02861 res = ast_app_getdata(chan, "conf-getpin", pin + strlen(pin), sizeof(pin) - 1 - strlen(pin), 0);
02862 }
02863 if (res >= 0) {
02864 if (!strcasecmp(pin, cnf->pin) ||
02865 (!ast_strlen_zero(cnf->pinadmin) &&
02866 !strcasecmp(pin, cnf->pinadmin))) {
02867
02868 allowretry = 0;
02869 if (!ast_strlen_zero(cnf->pinadmin) && !strcasecmp(pin, cnf->pinadmin))
02870 ast_set_flag(&confflags, CONFFLAG_ADMIN);
02871
02872 res = conf_run(chan, cnf, confflags.flags, optargs);
02873 break;
02874 } else {
02875
02876 if (!ast_streamfile(chan, "conf-invalidpin", chan->language)) {
02877 res = ast_waitstream(chan, AST_DIGIT_ANY);
02878 ast_stopstream(chan);
02879 }
02880 else {
02881 ast_log(LOG_WARNING, "Couldn't play invalid pin msg!\n");
02882 break;
02883 }
02884 if (res < 0)
02885 break;
02886 pin[0] = res;
02887 pin[1] = '\0';
02888 res = -1;
02889 if (allowretry)
02890 confno[0] = '\0';
02891 }
02892 } else {
02893
02894 res = -1;
02895 allowretry = 0;
02896
02897 break;
02898 }
02899
02900
02901 if (*the_pin && (always_prompt==0)) {
02902 break;
02903 }
02904 }
02905 } else {
02906
02907 allowretry = 0;
02908
02909
02910 res = conf_run(chan, cnf, confflags.flags, optargs);
02911 }
02912 dispose_conf(cnf);
02913 cnf = NULL;
02914 }
02915 }
02916 } while (allowretry);
02917
02918 if (cnf)
02919 dispose_conf(cnf);
02920
02921 ast_module_user_remove(u);
02922
02923 return res;
02924 }
02925
02926 static struct ast_conf_user *find_user(struct ast_conference *conf, char *callerident)
02927 {
02928 struct ast_conf_user *user = NULL;
02929 int cid;
02930
02931 sscanf(callerident, "%i", &cid);
02932 if (conf && callerident) {
02933 AST_LIST_TRAVERSE(&conf->userlist, user, list) {
02934 if (cid == user->user_no)
02935 return user;
02936 }
02937 }
02938 return NULL;
02939 }
02940
02941
02942
02943 static int admin_exec(struct ast_channel *chan, void *data) {
02944 char *params;
02945 struct ast_conference *cnf;
02946 struct ast_conf_user *user = NULL;
02947 struct ast_module_user *u;
02948 AST_DECLARE_APP_ARGS(args,
02949 AST_APP_ARG(confno);
02950 AST_APP_ARG(command);
02951 AST_APP_ARG(user);
02952 );
02953
02954 if (ast_strlen_zero(data)) {
02955 ast_log(LOG_WARNING, "MeetMeAdmin requires an argument!\n");
02956 return -1;
02957 }
02958
02959 u = ast_module_user_add(chan);
02960
02961 AST_LIST_LOCK(&confs);
02962
02963 params = ast_strdupa(data);
02964 AST_STANDARD_APP_ARGS(args, params);
02965
02966 if (!args.command) {
02967 ast_log(LOG_WARNING, "MeetmeAdmin requires a command!\n");
02968 AST_LIST_UNLOCK(&confs);
02969 ast_module_user_remove(u);
02970 return -1;
02971 }
02972 AST_LIST_TRAVERSE(&confs, cnf, list) {
02973 if (!strcmp(cnf->confno, args.confno))
02974 break;
02975 }
02976
02977 if (!cnf) {
02978 ast_log(LOG_WARNING, "Conference number '%s' not found!\n", args.confno);
02979 AST_LIST_UNLOCK(&confs);
02980 ast_module_user_remove(u);
02981 return 0;
02982 }
02983
02984 ast_atomic_fetchadd_int(&cnf->refcount, 1);
02985
02986 if (args.user)
02987 user = find_user(cnf, args.user);
02988
02989 switch (*args.command) {
02990 case 76:
02991 cnf->locked = 1;
02992 break;
02993 case 108:
02994 cnf->locked = 0;
02995 break;
02996 case 75:
02997 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
02998 user->adminflags |= ADMINFLAG_KICKME;
02999 break;
03000 case 101:
03001 user = AST_LIST_LAST(&cnf->userlist);
03002 if (!(user->userflags & CONFFLAG_ADMIN))
03003 user->adminflags |= ADMINFLAG_KICKME;
03004 else
03005 ast_log(LOG_NOTICE, "Not kicking last user, is an Admin!\n");
03006 break;
03007 case 77:
03008 if (user) {
03009 user->adminflags |= ADMINFLAG_MUTED;
03010 } else
03011 ast_log(LOG_NOTICE, "Specified User not found!\n");
03012 break;
03013 case 78:
03014 AST_LIST_TRAVERSE(&cnf->userlist, user, list) {
03015 if (!(user->userflags & CONFFLAG_ADMIN))
03016 user->adminflags |= ADMINFLAG_MUTED;
03017 }
03018 break;
03019 case 109:
03020 if (user) {
03021 user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
03022 } else
03023 ast_log(LOG_NOTICE, "Specified User not found!\n");
03024 break;
03025 case 110:
03026 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03027 user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
03028 break;
03029 case 107:
03030 if (user)
03031 user->adminflags |= ADMINFLAG_KICKME;
03032 else
03033 ast_log(LOG_NOTICE, "Specified User not found!\n");
03034 break;
03035 case 118:
03036 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03037 tweak_listen_volume(user, VOL_DOWN);
03038 break;
03039 case 86:
03040 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03041 tweak_listen_volume(user, VOL_UP);
03042 break;
03043 case 115:
03044 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03045 tweak_talk_volume(user, VOL_DOWN);
03046 break;
03047 case 83:
03048 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03049 tweak_talk_volume(user, VOL_UP);
03050 break;
03051 case 82:
03052 AST_LIST_TRAVERSE(&cnf->userlist, user, list)
03053 reset_volumes(user);
03054 break;
03055 case 114:
03056 if (user)
03057 reset_volumes(user);
03058 else
03059 ast_log(LOG_NOTICE, "Specified User not found!\n");
03060 break;
03061 case 85:
03062 if (user)
03063 tweak_listen_volume(user, VOL_UP);
03064 else
03065 ast_log(LOG_NOTICE, "Specified User not found!\n");
03066 break;
03067 case 117:
03068 if (user)
03069 tweak_listen_volume(user, VOL_DOWN);
03070 else
03071 ast_log(LOG_NOTICE, "Specified User not found!\n");
03072 break;
03073 case 84:
03074 if (user)
03075 tweak_talk_volume(user, VOL_UP);
03076 else
03077 ast_log(LOG_NOTICE, "Specified User not found!\n");
03078 break;
03079 case 116:
03080 if (user)
03081 tweak_talk_volume(user, VOL_DOWN);
03082 else
03083 ast_log(LOG_NOTICE, "Specified User not found!\n");
03084 break;
03085 }
03086
03087 AST_LIST_UNLOCK(&confs);
03088
03089 dispose_conf(cnf);
03090
03091 ast_module_user_remove(u);
03092
03093 return 0;
03094 }
03095
03096 static int meetmemute(struct mansession *s, const struct message *m, int mute)
03097 {
03098 struct ast_conference *conf;
03099 struct ast_conf_user *user;
03100 const char *confid = astman_get_header(m, "Meetme");
03101 char *userid = ast_strdupa(astman_get_header(m, "Usernum"));
03102 int userno;
03103
03104 if (ast_strlen_zero(confid)) {
03105 astman_send_error(s, m, "Meetme conference not specified");
03106 return 0;
03107 }
03108
03109 if (ast_strlen_zero(userid)) {
03110 astman_send_error(s, m, "Meetme user number not specified");
03111 return 0;
03112 }
03113
03114 userno = strtoul(userid, &userid, 10);
03115
03116 if (*userid) {
03117 astman_send_error(s, m, "Invalid user number");
03118 return 0;
03119 }
03120
03121
03122 AST_LIST_LOCK(&confs);
03123 AST_LIST_TRAVERSE(&confs, conf, list) {
03124 if (!strcmp(confid, conf->confno))
03125 break;
03126 }
03127
03128 if (!conf) {
03129 AST_LIST_UNLOCK(&confs);
03130 astman_send_error(s, m, "Meetme conference does not exist");
03131 return 0;
03132 }
03133
03134 AST_LIST_TRAVERSE(&conf->userlist, user, list)
03135 if (user->user_no == userno)
03136 break;
03137
03138 if (!user) {
03139 AST_LIST_UNLOCK(&confs);
03140 astman_send_error(s, m, "User number not found");
03141 return 0;
03142 }
03143
03144 if (mute)
03145 user->adminflags |= ADMINFLAG_MUTED;
03146 else
03147 user->adminflags &= ~(ADMINFLAG_MUTED | ADMINFLAG_SELFMUTED);
03148
03149 AST_LIST_UNLOCK(&confs);
03150
03151 ast_log(LOG_NOTICE, "Requested to %smute conf %s user %d userchan %s uniqueid %s\n", mute ? "" : "un", conf->confno, user->user_no, user->chan->name, user->chan->uniqueid);
03152
03153 astman_send_ack(s, m, mute ? "User muted" : "User unmuted");
03154 return 0;
03155 }
03156
03157 static int action_meetmemute(struct mansession *s, const struct message *m)
03158 {
03159 return meetmemute(s, m, 1);
03160 }
03161
03162 static int action_meetmeunmute(struct mansession *s, const struct message *m)
03163 {
03164 return meetmemute(s, m, 0);
03165 }
03166
03167 static void *recordthread(void *args)
03168 {
03169 struct ast_conference *cnf = args;
03170 struct ast_frame *f=NULL;
03171 int flags;
03172 struct ast_filestream *s=NULL;
03173 int res=0;
03174 int x;
03175 const char *oldrecordingfilename = NULL;
03176
03177 if (!cnf || !cnf->lchan) {
03178 pthread_exit(0);
03179 }
03180
03181 ast_stopstream(cnf->lchan);
03182 flags = O_CREAT|O_TRUNC|O_WRONLY;
03183
03184
03185 cnf->recording = MEETME_RECORD_ACTIVE;
03186 while (ast_waitfor(cnf->lchan, -1) > -1) {
03187 if (cnf->recording == MEETME_RECORD_TERMINATE) {
03188 AST_LIST_LOCK(&confs);
03189 AST_LIST_UNLOCK(&confs);
03190 break;
03191 }
03192 if (!s && cnf->recordingfilename && (cnf->recordingfilename != oldrecordingfilename)) {
03193 s = ast_writefile(cnf->recordingfilename, cnf->recordingformat, NULL, flags, 0, 0644);
03194 oldrecordingfilename = cnf->recordingfilename;
03195 }
03196
03197 f = ast_read(cnf->lchan);
03198 if (!f) {
03199 res = -1;
03200 break;
03201 }
03202 if (f->frametype == AST_FRAME_VOICE) {
03203 ast_mutex_lock(&cnf->listenlock);
03204 for (x=0;x<AST_FRAME_BITS;x++) {
03205
03206 if (cnf->transframe[x]) {
03207 ast_frfree(cnf->transframe[x]);
03208 cnf->transframe[x] = NULL;
03209 }
03210 }
03211 if (cnf->origframe)
03212 ast_frfree(cnf->origframe);
03213 cnf->origframe = ast_frdup(f);
03214 ast_mutex_unlock(&cnf->listenlock);
03215 if (s)
03216 res = ast_writestream(s, f);
03217 if (res) {
03218 ast_frfree(f);
03219 break;
03220 }
03221 }
03222 ast_frfree(f);
03223 }
03224 cnf->recording = MEETME_RECORD_OFF;
03225 if (s)
03226 ast_closestream(s);
03227
03228 pthread_exit(0);
03229 }
03230
03231
03232 static int meetmestate(const char *data)
03233 {
03234 struct ast_conference *conf;
03235
03236
03237 AST_LIST_LOCK(&confs);
03238 AST_LIST_TRAVERSE(&confs, conf, list) {
03239 if (!strcmp(data, conf->confno))
03240 break;
03241 }
03242 AST_LIST_UNLOCK(&confs);
03243 if (!conf)
03244 return AST_DEVICE_INVALID;
03245
03246
03247
03248 if (!conf->users)
03249 return AST_DEVICE_NOT_INUSE;
03250
03251 return AST_DEVICE_INUSE;
03252 }
03253
03254 static void load_config_meetme(void)
03255 {
03256 struct ast_config *cfg;
03257 const char *val;
03258
03259 audio_buffers = DEFAULT_AUDIO_BUFFERS;
03260
03261 if (!(cfg = ast_config_load(CONFIG_FILE_NAME)))
03262 return;
03263
03264 if ((val = ast_variable_retrieve(cfg, "general", "audiobuffers"))) {
03265 if ((sscanf(val, "%d", &audio_buffers) != 1)) {
03266 ast_log(LOG_WARNING, "audiobuffers setting must be a number, not '%s'\n", val);
03267 audio_buffers = DEFAULT_AUDIO_BUFFERS;
03268 } else if ((audio_buffers < DAHDI_DEFAULT_NUM_BUFS) || (audio_buffers > DAHDI_MAX_NUM_BUFS)) {
03269 ast_log(LOG_WARNING, "audiobuffers setting must be between %d and %d\n",
03270 DAHDI_DEFAULT_NUM_BUFS, DAHDI_MAX_NUM_BUFS);
03271 audio_buffers = DEFAULT_AUDIO_BUFFERS;
03272 }
03273 if (audio_buffers != DEFAULT_AUDIO_BUFFERS)
03274 ast_log(LOG_NOTICE, "Audio buffers per channel set to %d\n", audio_buffers);
03275 }
03276
03277 ast_config_destroy(cfg);
03278 }
03279
03280
03281
03282
03283 static struct sla_trunk *sla_find_trunk(const char *name)
03284 {
03285 struct sla_trunk *trunk = NULL;
03286
03287 AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
03288 if (!strcasecmp(trunk->name, name))
03289 break;
03290 }
03291
03292 return trunk;
03293 }
03294
03295
03296
03297
03298 static struct sla_station *sla_find_station(const char *name)
03299 {
03300 struct sla_station *station = NULL;
03301
03302 AST_RWLIST_TRAVERSE(&sla_stations, station, entry) {
03303 if (!strcasecmp(station->name, name))
03304 break;
03305 }
03306
03307 return station;
03308 }
03309
03310 static int sla_check_station_hold_access(const struct sla_trunk *trunk,
03311 const struct sla_station *station)
03312 {
03313 struct sla_station_ref *station_ref;
03314 struct sla_trunk_ref *trunk_ref;
03315
03316
03317 AST_LIST_TRAVERSE(&trunk->stations, station_ref, entry) {
03318 AST_LIST_TRAVERSE(&station_ref->station->trunks, trunk_ref, entry) {
03319 if (trunk_ref->trunk != trunk || station_ref->station == station)
03320 continue;
03321 if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME &&
03322 station_ref->station->hold_access == SLA_HOLD_PRIVATE)
03323 return 1;
03324 return 0;
03325 }
03326 }
03327
03328 return 0;
03329 }
03330
03331
03332
03333
03334
03335
03336
03337
03338 static struct sla_trunk_ref *sla_find_trunk_ref_byname(const struct sla_station *station,
03339 const char *name)
03340 {
03341 struct sla_trunk_ref *trunk_ref = NULL;
03342
03343 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
03344 if (strcasecmp(trunk_ref->trunk->name, name))
03345 continue;
03346
03347 if ( (trunk_ref->trunk->barge_disabled
03348 && trunk_ref->state == SLA_TRUNK_STATE_UP) ||
03349 (trunk_ref->trunk->hold_stations
03350 && trunk_ref->trunk->hold_access == SLA_HOLD_PRIVATE
03351 && trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) ||
03352 sla_check_station_hold_access(trunk_ref->trunk, station) )
03353 {
03354 trunk_ref = NULL;
03355 }
03356
03357 break;
03358 }
03359
03360 return trunk_ref;
03361 }
03362
03363 static struct sla_station_ref *sla_create_station_ref(struct sla_station *station)
03364 {
03365 struct sla_station_ref *station_ref;
03366
03367 if (!(station_ref = ast_calloc(1, sizeof(*station_ref))))
03368 return NULL;
03369
03370 station_ref->station = station;
03371
03372 return station_ref;
03373 }
03374
03375 static struct sla_ringing_station *sla_create_ringing_station(struct sla_station *station)
03376 {
03377 struct sla_ringing_station *ringing_station;
03378
03379 if (!(ringing_station = ast_calloc(1, sizeof(*ringing_station))))
03380 return NULL;
03381
03382 ringing_station->station = station;
03383 ringing_station->ring_begin = ast_tvnow();
03384
03385 return ringing_station;
03386 }
03387
03388 static void sla_change_trunk_state(const struct sla_trunk *trunk, enum sla_trunk_state state,
03389 enum sla_which_trunk_refs inactive_only, const struct sla_trunk_ref *exclude)
03390 {
03391 struct sla_station *station;
03392 struct sla_trunk_ref *trunk_ref;
03393
03394 AST_LIST_TRAVERSE(&sla_stations, station, entry) {
03395 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
03396 if (trunk_ref->trunk != trunk || (inactive_only ? trunk_ref->chan : 0)
03397 || trunk_ref == exclude)
03398 continue;
03399 trunk_ref->state = state;
03400 ast_device_state_changed("SLA:%s_%s", station->name, trunk->name);
03401 break;
03402 }
03403 }
03404 }
03405
03406 struct run_station_args {
03407 struct sla_station *station;
03408 struct sla_trunk_ref *trunk_ref;
03409 ast_mutex_t *cond_lock;
03410 ast_cond_t *cond;
03411 };
03412
03413 static void answer_trunk_chan(struct ast_channel *chan)
03414 {
03415 ast_answer(chan);
03416 ast_indicate(chan, -1);
03417 }
03418
03419 static void *run_station(void *data)
03420 {
03421 struct sla_station *station;
03422 struct sla_trunk_ref *trunk_ref;
03423 char conf_name[MAX_CONFNUM];
03424 struct ast_flags conf_flags = { 0 };
03425 struct ast_conference *conf;
03426
03427 {
03428 struct run_station_args *args = data;
03429 station = args->station;
03430 trunk_ref = args->trunk_ref;
03431 ast_mutex_lock(args->cond_lock);
03432 ast_cond_signal(args->cond);
03433 ast_mutex_unlock(args->cond_lock);
03434
03435 }
03436
03437 ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1);
03438 snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);
03439 ast_set_flag(&conf_flags,
03440 CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);
03441 answer_trunk_chan(trunk_ref->chan);
03442 conf = build_conf(conf_name, "", "", 0, 0, 1);
03443 if (conf) {
03444 conf_run(trunk_ref->chan, conf, conf_flags.flags, NULL);
03445 dispose_conf(conf);
03446 conf = NULL;
03447 }
03448 trunk_ref->chan = NULL;
03449 if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&
03450 trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {
03451 strncat(conf_name, "|K", sizeof(conf_name) - strlen(conf_name) - 1);
03452 admin_exec(NULL, conf_name);
03453 trunk_ref->trunk->hold_stations = 0;
03454 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
03455 }
03456
03457 ast_dial_join(station->dial);
03458 ast_dial_destroy(station->dial);
03459 station->dial = NULL;
03460
03461 return NULL;
03462 }
03463
03464 static void sla_stop_ringing_trunk(struct sla_ringing_trunk *ringing_trunk)
03465 {
03466 char buf[80];
03467 struct sla_station_ref *station_ref;
03468
03469 snprintf(buf, sizeof(buf), "SLA_%s|K", ringing_trunk->trunk->name);
03470 admin_exec(NULL, buf);
03471 sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
03472
03473 while ((station_ref = AST_LIST_REMOVE_HEAD(&ringing_trunk->timed_out_stations, entry)))
03474 free(station_ref);
03475
03476 free(ringing_trunk);
03477 }
03478
03479 static void sla_stop_ringing_station(struct sla_ringing_station *ringing_station,
03480 enum sla_station_hangup hangup)
03481 {
03482 struct sla_ringing_trunk *ringing_trunk;
03483 struct sla_trunk_ref *trunk_ref;
03484 struct sla_station_ref *station_ref;
03485
03486 ast_dial_join(ringing_station->station->dial);
03487 ast_dial_destroy(ringing_station->station->dial);
03488 ringing_station->station->dial = NULL;
03489
03490 if (hangup == SLA_STATION_HANGUP_NORMAL)
03491 goto done;
03492
03493
03494
03495
03496
03497
03498 AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
03499 AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
03500 if (ringing_trunk->trunk == trunk_ref->trunk)
03501 break;
03502 }
03503 if (!trunk_ref)
03504 continue;
03505 if (!(station_ref = sla_create_station_ref(ringing_station->station)))
03506 continue;
03507 AST_LIST_INSERT_TAIL(&ringing_trunk->timed_out_stations, station_ref, entry);
03508 }
03509
03510 done:
03511 free(ringing_station);
03512 }
03513
03514 static void sla_dial_state_callback(struct ast_dial *dial)
03515 {
03516 sla_queue_event(SLA_EVENT_DIAL_STATE);
03517 }
03518
03519
03520
03521
03522 static int sla_check_timed_out_station(const struct sla_ringing_trunk *ringing_trunk,
03523 const struct sla_station *station)
03524 {
03525 struct sla_station_ref *timed_out_station;
03526
03527 AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, timed_out_station, entry) {
03528 if (station == timed_out_station->station)
03529 return 1;
03530 }
03531
03532 return 0;
03533 }
03534
03535
03536
03537
03538
03539
03540
03541
03542
03543 static struct sla_ringing_trunk *sla_choose_ringing_trunk(struct sla_station *station,
03544 struct sla_trunk_ref **trunk_ref, int remove)
03545 {
03546 struct sla_trunk_ref *s_trunk_ref;
03547 struct sla_ringing_trunk *ringing_trunk = NULL;
03548
03549 AST_LIST_TRAVERSE(&station->trunks, s_trunk_ref, entry) {
03550 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
03551
03552 if (s_trunk_ref->trunk != ringing_trunk->trunk)
03553 continue;
03554
03555
03556
03557 if (sla_check_timed_out_station(ringing_trunk, station))
03558 continue;
03559
03560 if (remove)
03561 AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
03562
03563 if (trunk_ref)
03564 *trunk_ref = s_trunk_ref;
03565
03566 break;
03567 }
03568 AST_LIST_TRAVERSE_SAFE_END
03569
03570 if (ringing_trunk)
03571 break;
03572 }
03573
03574 return ringing_trunk;
03575 }
03576
03577 static void sla_handle_dial_state_event(void)
03578 {
03579 struct sla_ringing_station *ringing_station;
03580
03581 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
03582 struct sla_trunk_ref *s_trunk_ref = NULL;
03583 struct sla_ringing_trunk *ringing_trunk = NULL;
03584 struct run_station_args args;
03585 enum ast_dial_result dial_res;
03586 pthread_attr_t attr;
03587 pthread_t dont_care;
03588 ast_mutex_t cond_lock;
03589 ast_cond_t cond;
03590
03591 switch ((dial_res = ast_dial_state(ringing_station->station->dial))) {
03592 case AST_DIAL_RESULT_HANGUP:
03593 case AST_DIAL_RESULT_INVALID:
03594 case AST_DIAL_RESULT_FAILED:
03595 case AST_DIAL_RESULT_TIMEOUT:
03596 case AST_DIAL_RESULT_UNANSWERED:
03597 AST_LIST_REMOVE_CURRENT(&sla.ringing_stations, entry);
03598 sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_NORMAL);
03599 break;
03600 case AST_DIAL_RESULT_ANSWERED:
03601 AST_LIST_REMOVE_CURRENT(&sla.ringing_stations, entry);
03602
03603 ast_mutex_lock(&sla.lock);
03604 ringing_trunk = sla_choose_ringing_trunk(ringing_station->station, &s_trunk_ref, 1);
03605 ast_mutex_unlock(&sla.lock);
03606 if (!ringing_trunk) {
03607 ast_log(LOG_DEBUG, "Found no ringing trunk for station '%s' to answer!\n",
03608 ringing_station->station->name);
03609 break;
03610 }
03611
03612 s_trunk_ref->chan = ast_dial_answered(ringing_station->station->dial);
03613
03614 answer_trunk_chan(ringing_trunk->trunk->chan);
03615 sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
03616
03617
03618
03619 args.trunk_ref = s_trunk_ref;
03620 args.station = ringing_station->station;
03621 args.cond = &cond;
03622 args.cond_lock = &cond_lock;
03623 free(ringing_trunk);
03624 free(ringing_station);
03625 ast_mutex_init(&cond_lock);
03626 ast_cond_init(&cond, NULL);
03627 pthread_attr_init(&attr);
03628 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
03629 ast_mutex_lock(&cond_lock);
03630 ast_pthread_create_background(&dont_care, &attr, run_station, &args);
03631 ast_cond_wait(&cond, &cond_lock);
03632 ast_mutex_unlock(&cond_lock);
03633 ast_mutex_destroy(&cond_lock);
03634 ast_cond_destroy(&cond);
03635 pthread_attr_destroy(&attr);
03636 break;
03637 case AST_DIAL_RESULT_TRYING:
03638 case AST_DIAL_RESULT_RINGING:
03639 case AST_DIAL_RESULT_PROGRESS:
03640 case AST_DIAL_RESULT_PROCEEDING:
03641 break;
03642 }
03643 if (dial_res == AST_DIAL_RESULT_ANSWERED) {
03644
03645 sla_queue_event(SLA_EVENT_RINGING_TRUNK);
03646 sla_queue_event(SLA_EVENT_DIAL_STATE);
03647 break;
03648 }
03649 }
03650 AST_LIST_TRAVERSE_SAFE_END
03651 }
03652
03653
03654
03655
03656 static int sla_check_ringing_station(const struct sla_station *station)
03657 {
03658 struct sla_ringing_station *ringing_station;
03659
03660 AST_LIST_TRAVERSE(&sla.ringing_stations, ringing_station, entry) {
03661 if (station == ringing_station->station)
03662 return 1;
03663 }
03664
03665 return 0;
03666 }
03667
03668
03669
03670
03671 static int sla_check_failed_station(const struct sla_station *station)
03672 {
03673 struct sla_failed_station *failed_station;
03674 int res = 0;
03675
03676 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.failed_stations, failed_station, entry) {
03677 if (station != failed_station->station)
03678 continue;
03679 if (ast_tvdiff_ms(ast_tvnow(), failed_station->last_try) > 1000) {
03680 AST_LIST_REMOVE_CURRENT(&sla.failed_stations, entry);
03681 free(failed_station);
03682 break;
03683 }
03684 res = 1;
03685 }
03686 AST_LIST_TRAVERSE_SAFE_END
03687
03688 return res;
03689 }
03690
03691
03692
03693
03694 static int sla_ring_station(struct sla_ringing_trunk *ringing_trunk, struct sla_station *station)
03695 {
03696 char *tech, *tech_data;
03697 struct ast_dial *dial;
03698 struct sla_ringing_station *ringing_station;
03699 const char *cid_name = NULL, *cid_num = NULL;
03700 enum ast_dial_result res;
03701
03702 if (!(dial = ast_dial_create()))
03703 return -1;
03704
03705 ast_dial_set_state_callback(dial, sla_dial_state_callback);
03706 tech_data = ast_strdupa(station->device);
03707 tech = strsep(&tech_data, "/");
03708
03709 if (ast_dial_append(dial, tech, tech_data) == -1) {
03710 ast_dial_destroy(dial);
03711 return -1;
03712 }
03713
03714 if (!sla.attempt_callerid && !ast_strlen_zero(ringing_trunk->trunk->chan->cid.cid_name)) {
03715 cid_name = ast_strdupa(ringing_trunk->trunk->chan->cid.cid_name);
03716 free(ringing_trunk->trunk->chan->cid.cid_name);
03717 ringing_trunk->trunk->chan->cid.cid_name = NULL;
03718 }
03719 if (!sla.attempt_callerid && !ast_strlen_zero(ringing_trunk->trunk->chan->cid.cid_num)) {
03720 cid_num = ast_strdupa(ringing_trunk->trunk->chan->cid.cid_num);
03721 free(ringing_trunk->trunk->chan->cid.cid_num);
03722 ringing_trunk->trunk->chan->cid.cid_num = NULL;
03723 }
03724
03725 res = ast_dial_run(dial, ringing_trunk->trunk->chan, 1);
03726
03727 if (cid_name)
03728 ringing_trunk->trunk->chan->cid.cid_name = ast_strdup(cid_name);
03729 if (cid_num)
03730 ringing_trunk->trunk->chan->cid.cid_num = ast_strdup(cid_num);
03731
03732 if (res != AST_DIAL_RESULT_TRYING) {
03733 struct sla_failed_station *failed_station;
03734 ast_dial_destroy(dial);
03735 if (!(failed_station = ast_calloc(1, sizeof(*failed_station))))
03736 return -1;
03737 failed_station->station = station;
03738 failed_station->last_try = ast_tvnow();
03739 AST_LIST_INSERT_HEAD(&sla.failed_stations, failed_station, entry);
03740 return -1;
03741 }
03742 if (!(ringing_station = sla_create_ringing_station(station))) {
03743 ast_dial_join(dial);
03744 ast_dial_destroy(dial);
03745 return -1;
03746 }
03747
03748 station->dial = dial;
03749
03750 AST_LIST_INSERT_HEAD(&sla.ringing_stations, ringing_station, entry);
03751
03752 return 0;
03753 }
03754
03755
03756
03757 static int sla_check_inuse_station(const struct sla_station *station)
03758 {
03759 struct sla_trunk_ref *trunk_ref;
03760
03761 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
03762 if (trunk_ref->chan)
03763 return 1;
03764 }
03765
03766 return 0;
03767 }
03768
03769 static struct sla_trunk_ref *sla_find_trunk_ref(const struct sla_station *station,
03770 const struct sla_trunk *trunk)
03771 {
03772 struct sla_trunk_ref *trunk_ref = NULL;
03773
03774 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
03775 if (trunk_ref->trunk == trunk)
03776 break;
03777 }
03778
03779 return trunk_ref;
03780 }
03781
03782
03783
03784
03785
03786
03787 static int sla_check_station_delay(struct sla_station *station,
03788 struct sla_ringing_trunk *ringing_trunk)
03789 {
03790 struct sla_trunk_ref *trunk_ref;
03791 unsigned int delay = UINT_MAX;
03792 int time_left, time_elapsed;
03793
03794 if (!ringing_trunk)
03795 ringing_trunk = sla_choose_ringing_trunk(station, &trunk_ref, 0);
03796 else
03797 trunk_ref = sla_find_trunk_ref(station, ringing_trunk->trunk);
03798
03799 if (!ringing_trunk || !trunk_ref)
03800 return delay;
03801
03802
03803
03804
03805 delay = trunk_ref->ring_delay;
03806 if (!delay)
03807 delay = station->ring_delay;
03808 if (!delay)
03809 return INT_MAX;
03810
03811 time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
03812 time_left = (delay * 1000) - time_elapsed;
03813
03814 return time_left;
03815 }
03816
03817
03818
03819
03820 static void sla_ring_stations(void)
03821 {
03822 struct sla_station_ref *station_ref;
03823 struct sla_ringing_trunk *ringing_trunk;
03824
03825
03826
03827 AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
03828 AST_LIST_TRAVERSE(&ringing_trunk->trunk->stations, station_ref, entry) {
03829 int time_left;
03830
03831
03832 if (sla_check_ringing_station(station_ref->station))
03833 continue;
03834
03835
03836 if (sla_check_inuse_station(station_ref->station))
03837 continue;
03838
03839
03840
03841 if (sla_check_failed_station(station_ref->station))
03842 continue;
03843
03844
03845
03846 if (sla_check_timed_out_station(ringing_trunk, station_ref->station))
03847 continue;
03848
03849
03850 time_left = sla_check_station_delay(station_ref->station, ringing_trunk);
03851 if (time_left != INT_MAX && time_left > 0)
03852 continue;
03853
03854
03855 sla_ring_station(ringing_trunk, station_ref->station);
03856 }
03857 }
03858
03859 }
03860
03861 static void sla_hangup_stations(void)
03862 {
03863 struct sla_trunk_ref *trunk_ref;
03864 struct sla_ringing_station *ringing_station;
03865
03866 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
03867 AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
03868 struct sla_ringing_trunk *ringing_trunk;
03869 ast_mutex_lock(&sla.lock);
03870 AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
03871 if (trunk_ref->trunk == ringing_trunk->trunk)
03872 break;
03873 }
03874 ast_mutex_unlock(&sla.lock);
03875 if (ringing_trunk)
03876 break;
03877 }
03878 if (!trunk_ref) {
03879 AST_LIST_REMOVE_CURRENT(&sla.ringing_stations, entry);
03880 ast_dial_join(ringing_station->station->dial);
03881 ast_dial_destroy(ringing_station->station->dial);
03882 ringing_station->station->dial = NULL;
03883 free(ringing_station);
03884 }
03885 }
03886 AST_LIST_TRAVERSE_SAFE_END
03887 }
03888
03889 static void sla_handle_ringing_trunk_event(void)
03890 {
03891 ast_mutex_lock(&sla.lock);
03892 sla_ring_stations();
03893 ast_mutex_unlock(&sla.lock);
03894
03895
03896 sla_hangup_stations();
03897 }
03898
03899 static void sla_handle_hold_event(struct sla_event *event)
03900 {
03901 ast_atomic_fetchadd_int((int *) &event->trunk_ref->trunk->hold_stations, 1);
03902 event->trunk_ref->state = SLA_TRUNK_STATE_ONHOLD_BYME;
03903 ast_device_state_changed("SLA:%s_%s",
03904 event->station->name, event->trunk_ref->trunk->name);
03905 sla_change_trunk_state(event->trunk_ref->trunk, SLA_TRUNK_STATE_ONHOLD,
03906 INACTIVE_TRUNK_REFS, event->trunk_ref);
03907
03908 if (event->trunk_ref->trunk->active_stations == 1) {
03909
03910
03911 event->trunk_ref->trunk->on_hold = 1;
03912 ast_indicate(event->trunk_ref->trunk->chan, AST_CONTROL_HOLD);
03913 }
03914
03915 ast_softhangup(event->trunk_ref->chan, AST_CAUSE_NORMAL);
03916 event->trunk_ref->chan = NULL;
03917 }
03918
03919
03920
03921
03922
03923 static int sla_calc_trunk_timeouts(unsigned int *timeout)
03924 {
03925 struct sla_ringing_trunk *ringing_trunk;
03926 int res = 0;
03927
03928 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
03929 int time_left, time_elapsed;
03930 if (!ringing_trunk->trunk->ring_timeout)
03931 continue;
03932 time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
03933 time_left = (ringing_trunk->trunk->ring_timeout * 1000) - time_elapsed;
03934 if (time_left <= 0) {
03935 pbx_builtin_setvar_helper(ringing_trunk->trunk->chan, "SLATRUNK_STATUS", "RINGTIMEOUT");
03936 AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
03937 sla_stop_ringing_trunk(ringing_trunk);
03938 res = 1;
03939 continue;
03940 }
03941 if (time_left < *timeout)
03942 *timeout = time_left;
03943 }
03944 AST_LIST_TRAVERSE_SAFE_END
03945
03946 return res;
03947 }
03948
03949
03950
03951
03952
03953 static int sla_calc_station_timeouts(unsigned int *timeout)
03954 {
03955 struct sla_ringing_trunk *ringing_trunk;
03956 struct sla_ringing_station *ringing_station;
03957 int res = 0;
03958
03959 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_stations, ringing_station, entry) {
03960 unsigned int ring_timeout = 0;
03961 int time_elapsed, time_left = INT_MAX, final_trunk_time_left = INT_MIN;
03962 struct sla_trunk_ref *trunk_ref;
03963
03964
03965
03966
03967 AST_LIST_TRAVERSE(&ringing_station->station->trunks, trunk_ref, entry) {
03968 struct sla_station_ref *station_ref;
03969 int trunk_time_elapsed, trunk_time_left;
03970
03971 AST_LIST_TRAVERSE(&sla.ringing_trunks, ringing_trunk, entry) {
03972 if (ringing_trunk->trunk == trunk_ref->trunk)
03973 break;
03974 }
03975 if (!ringing_trunk)
03976 continue;
03977
03978
03979
03980 if (!trunk_ref->ring_timeout)
03981 break;
03982
03983
03984
03985
03986 AST_LIST_TRAVERSE(&ringing_trunk->timed_out_stations, station_ref, entry) {
03987 if (station_ref->station == ringing_station->station)
03988 break;
03989 }
03990 if (station_ref)
03991 continue;
03992
03993 trunk_time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_trunk->ring_begin);
03994 trunk_time_left = (trunk_ref->ring_timeout * 1000) - trunk_time_elapsed;
03995 if (trunk_time_left > final_trunk_time_left)
03996 final_trunk_time_left = trunk_time_left;
03997 }
03998
03999
04000 if (final_trunk_time_left == INT_MIN && !ringing_station->station->ring_timeout)
04001 continue;
04002
04003
04004 if (ringing_station->station->ring_timeout) {
04005 ring_timeout = ringing_station->station->ring_timeout;
04006 time_elapsed = ast_tvdiff_ms(ast_tvnow(), ringing_station->ring_begin);
04007 time_left = (ring_timeout * 1000) - time_elapsed;
04008 }
04009
04010
04011
04012 if (final_trunk_time_left > INT_MIN && final_trunk_time_left < time_left)
04013 time_left = final_trunk_time_left;
04014
04015
04016 if (time_left <= 0) {
04017 AST_LIST_REMOVE_CURRENT(&sla.ringing_stations, entry);
04018 sla_stop_ringing_station(ringing_station, SLA_STATION_HANGUP_TIMEOUT);
04019 res = 1;
04020 continue;
04021 }
04022
04023
04024
04025 if (time_left < *timeout)
04026 *timeout = time_left;
04027 }
04028 AST_LIST_TRAVERSE_SAFE_END
04029
04030 return res;
04031 }
04032
04033
04034
04035
04036 static int sla_calc_station_delays(unsigned int *timeout)
04037 {
04038 struct sla_station *station;
04039 int res = 0;
04040
04041 AST_LIST_TRAVERSE(&sla_stations, station, entry) {
04042 struct sla_ringing_trunk *ringing_trunk;
04043 int time_left;
04044
04045
04046 if (sla_check_ringing_station(station))
04047 continue;
04048
04049
04050 if (sla_check_inuse_station(station))
04051 continue;
04052
04053
04054 if (!(ringing_trunk = sla_choose_ringing_trunk(station, NULL, 0)))
04055 continue;
04056
04057 if ((time_left = sla_check_station_delay(station, ringing_trunk)) == INT_MAX)
04058 continue;
04059
04060
04061
04062
04063 if (time_left <= 0) {
04064 res = 1;
04065 continue;
04066 }
04067
04068 if (time_left < *timeout)
04069 *timeout = time_left;
04070 }
04071
04072 return res;
04073 }
04074
04075
04076
04077 static int sla_process_timers(struct timespec *ts)
04078 {
04079 unsigned int timeout = UINT_MAX;
04080 struct timeval tv;
04081 unsigned int change_made = 0;
04082
04083
04084 if (sla_calc_trunk_timeouts(&timeout))
04085 change_made = 1;
04086
04087
04088 if (sla_calc_station_timeouts(&timeout))
04089 change_made = 1;
04090
04091
04092 if (sla_calc_station_delays(&timeout))
04093 change_made = 1;
04094
04095
04096 if (change_made)
04097 sla_queue_event_nolock(SLA_EVENT_RINGING_TRUNK);
04098
04099
04100 if (timeout == UINT_MAX)
04101 return 0;
04102
04103 if (ts) {
04104 tv = ast_tvadd(ast_tvnow(), ast_samp2tv(timeout, 1000));
04105 ts->tv_sec = tv.tv_sec;
04106 ts->tv_nsec = tv.tv_usec * 1000;
04107 }
04108
04109 return 1;
04110 }
04111
04112 static void *sla_thread(void *data)
04113 {
04114 struct sla_failed_station *failed_station;
04115 struct sla_ringing_station *ringing_station;
04116
04117 ast_mutex_lock(&sla.lock);
04118
04119 while (!sla.stop) {
04120 struct sla_event *event;
04121 struct timespec ts = { 0, };
04122 unsigned int have_timeout = 0;
04123
04124 if (AST_LIST_EMPTY(&sla.event_q)) {
04125 if ((have_timeout = sla_process_timers(&ts)))
04126 ast_cond_timedwait(&sla.cond, &sla.lock, &ts);
04127 else
04128 ast_cond_wait(&sla.cond, &sla.lock);
04129 if (sla.stop)
04130 break;
04131 }
04132
04133 if (have_timeout)
04134 sla_process_timers(NULL);
04135
04136 while ((event = AST_LIST_REMOVE_HEAD(&sla.event_q, entry))) {
04137 ast_mutex_unlock(&sla.lock);
04138 switch (event->type) {
04139 case SLA_EVENT_HOLD:
04140 sla_handle_hold_event(event);
04141 break;
04142 case SLA_EVENT_DIAL_STATE:
04143 sla_handle_dial_state_event();
04144 break;
04145 case SLA_EVENT_RINGING_TRUNK:
04146 sla_handle_ringing_trunk_event();
04147 break;
04148 }
04149 free(event);
04150 ast_mutex_lock(&sla.lock);
04151 }
04152 }
04153
04154 ast_mutex_unlock(&sla.lock);
04155
04156 while ((ringing_station = AST_LIST_REMOVE_HEAD(&sla.ringing_stations, entry)))
04157 free(ringing_station);
04158
04159 while ((failed_station = AST_LIST_REMOVE_HEAD(&sla.failed_stations, entry)))
04160 free(failed_station);
04161
04162 return NULL;
04163 }
04164
04165 struct dial_trunk_args {
04166 struct sla_trunk_ref *trunk_ref;
04167 struct sla_station *station;
04168 ast_mutex_t *cond_lock;
04169 ast_cond_t *cond;
04170 };
04171
04172 static void *dial_trunk(void *data)
04173 {
04174 struct dial_trunk_args *args = data;
04175 struct ast_dial *dial;
04176 char *tech, *tech_data;
04177 enum ast_dial_result dial_res;
04178 char conf_name[MAX_CONFNUM];
04179 struct ast_conference *conf;
04180 struct ast_flags conf_flags = { 0 };
04181 struct sla_trunk_ref *trunk_ref = args->trunk_ref;
04182 const char *cid_name = NULL, *cid_num = NULL;
04183
04184 if (!(dial = ast_dial_create())) {
04185 ast_mutex_lock(args->cond_lock);
04186 ast_cond_signal(args->cond);
04187 ast_mutex_unlock(args->cond_lock);
04188 return NULL;
04189 }
04190
04191 tech_data = ast_strdupa(trunk_ref->trunk->device);
04192 tech = strsep(&tech_data, "/");
04193 if (ast_dial_append(dial, tech, tech_data) == -1) {
04194 ast_mutex_lock(args->cond_lock);
04195 ast_cond_signal(args->cond);
04196 ast_mutex_unlock(args->cond_lock);
04197 ast_dial_destroy(dial);
04198 return NULL;
04199 }
04200
04201 if (!sla.attempt_callerid && !ast_strlen_zero(trunk_ref->chan->cid.cid_name)) {
04202 cid_name = ast_strdupa(trunk_ref->chan->cid.cid_name);
04203 free(trunk_ref->chan->cid.cid_name);
04204 trunk_ref->chan->cid.cid_name = NULL;
04205 }
04206 if (!sla.attempt_callerid && !ast_strlen_zero(trunk_ref->chan->cid.cid_num)) {
04207 cid_num = ast_strdupa(trunk_ref->chan->cid.cid_num);
04208 free(trunk_ref->chan->cid.cid_num);
04209 trunk_ref->chan->cid.cid_num = NULL;
04210 }
04211
04212 dial_res = ast_dial_run(dial, trunk_ref->chan, 1);
04213
04214 if (cid_name)
04215 trunk_ref->chan->cid.cid_name = ast_strdup(cid_name);
04216 if (cid_num)
04217 trunk_ref->chan->cid.cid_num = ast_strdup(cid_num);
04218
04219 if (dial_res != AST_DIAL_RESULT_TRYING) {
04220 ast_mutex_lock(args->cond_lock);
04221 ast_cond_signal(args->cond);
04222 ast_mutex_unlock(args->cond_lock);
04223 ast_dial_destroy(dial);
04224 return NULL;
04225 }
04226
04227 for (;;) {
04228 unsigned int done = 0;
04229 switch ((dial_res = ast_dial_state(dial))) {
04230 case AST_DIAL_RESULT_ANSWERED:
04231 trunk_ref->trunk->chan = ast_dial_answered(dial);
04232 case AST_DIAL_RESULT_HANGUP:
04233 case AST_DIAL_RESULT_INVALID:
04234 case AST_DIAL_RESULT_FAILED:
04235 case AST_DIAL_RESULT_TIMEOUT:
04236 case AST_DIAL_RESULT_UNANSWERED:
04237 done = 1;
04238 case AST_DIAL_RESULT_TRYING:
04239 case AST_DIAL_RESULT_RINGING:
04240 case AST_DIAL_RESULT_PROGRESS:
04241 case AST_DIAL_RESULT_PROCEEDING:
04242 break;
04243 }
04244 if (done)
04245 break;
04246 }
04247
04248 if (!trunk_ref->trunk->chan) {
04249 ast_mutex_lock(args->cond_lock);
04250 ast_cond_signal(args->cond);
04251 ast_mutex_unlock(args->cond_lock);
04252 ast_dial_join(dial);
04253 ast_dial_destroy(dial);
04254 return NULL;
04255 }
04256
04257 snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);
04258 ast_set_flag(&conf_flags,
04259 CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER |
04260 CONFFLAG_PASS_DTMF | CONFFLAG_SLA_TRUNK);
04261 conf = build_conf(conf_name, "", "", 1, 1, 1);
04262
04263 ast_mutex_lock(args->cond_lock);
04264 ast_cond_signal(args->cond);
04265 ast_mutex_unlock(args->cond_lock);
04266
04267 if (conf) {
04268 conf_run(trunk_ref->trunk->chan, conf, conf_flags.flags, NULL);
04269 dispose_conf(conf);
04270 conf = NULL;
04271 }
04272
04273
04274 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
04275
04276 trunk_ref->trunk->chan = NULL;
04277 trunk_ref->trunk->on_hold = 0;
04278
04279 ast_dial_join(dial);
04280 ast_dial_destroy(dial);
04281
04282 return NULL;
04283 }
04284
04285
04286
04287 static struct sla_trunk_ref *sla_choose_idle_trunk(const struct sla_station *station)
04288 {
04289 struct sla_trunk_ref *trunk_ref = NULL;
04290
04291 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
04292 if (trunk_ref->state == SLA_TRUNK_STATE_IDLE)
04293 break;
04294 }
04295
04296 return trunk_ref;
04297 }
04298
04299 static int sla_station_exec(struct ast_channel *chan, void *data)
04300 {
04301 char *station_name, *trunk_name;
04302 struct sla_station *station;
04303 struct sla_trunk_ref *trunk_ref = NULL;
04304 char conf_name[MAX_CONFNUM];
04305 struct ast_flags conf_flags = { 0 };
04306 struct ast_conference *conf;
04307
04308 if (ast_strlen_zero(data)) {
04309 ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");
04310 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
04311 return 0;
04312 }
04313
04314 trunk_name = ast_strdupa(data);
04315 station_name = strsep(&trunk_name, "_");
04316
04317 if (ast_strlen_zero(station_name)) {
04318 ast_log(LOG_WARNING, "Invalid Arguments to SLAStation!\n");
04319 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
04320 return 0;
04321 }
04322
04323 AST_RWLIST_RDLOCK(&sla_stations);
04324 station = sla_find_station(station_name);
04325 AST_RWLIST_UNLOCK(&sla_stations);
04326
04327 if (!station) {
04328 ast_log(LOG_WARNING, "Station '%s' not found!\n", station_name);
04329 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "FAILURE");
04330 return 0;
04331 }
04332
04333 AST_RWLIST_RDLOCK(&sla_trunks);
04334 if (!ast_strlen_zero(trunk_name)) {
04335 trunk_ref = sla_find_trunk_ref_byname(station, trunk_name);
04336 } else
04337 trunk_ref = sla_choose_idle_trunk(station);
04338 AST_RWLIST_UNLOCK(&sla_trunks);
04339
04340 if (!trunk_ref) {
04341 if (ast_strlen_zero(trunk_name))
04342 ast_log(LOG_NOTICE, "No trunks available for call.\n");
04343 else {
04344 ast_log(LOG_NOTICE, "Can't join existing call on trunk "
04345 "'%s' due to access controls.\n", trunk_name);
04346 }
04347 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");
04348 return 0;
04349 }
04350
04351 if (trunk_ref->state == SLA_TRUNK_STATE_ONHOLD_BYME) {
04352 if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->hold_stations) == 1)
04353 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
04354 else {
04355 trunk_ref->state = SLA_TRUNK_STATE_UP;
04356 ast_device_state_changed("SLA:%s_%s", station->name, trunk_ref->trunk->name);
04357 }
04358 } else if (trunk_ref->state == SLA_TRUNK_STATE_RINGING) {
04359 struct sla_ringing_trunk *ringing_trunk;
04360
04361 ast_mutex_lock(&sla.lock);
04362 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
04363 if (ringing_trunk->trunk == trunk_ref->trunk) {
04364 AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
04365 break;
04366 }
04367 }
04368 AST_LIST_TRAVERSE_SAFE_END
04369 ast_mutex_unlock(&sla.lock);
04370
04371 if (ringing_trunk) {
04372 answer_trunk_chan(ringing_trunk->trunk->chan);
04373 sla_change_trunk_state(ringing_trunk->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
04374
04375 free(ringing_trunk);
04376
04377
04378 sla_queue_event(SLA_EVENT_RINGING_TRUNK);
04379 sla_queue_event(SLA_EVENT_DIAL_STATE);
04380 }
04381 }
04382
04383 trunk_ref->chan = chan;
04384
04385 if (!trunk_ref->trunk->chan) {
04386 ast_mutex_t cond_lock;
04387 ast_cond_t cond;
04388 pthread_t dont_care;
04389 pthread_attr_t attr;
04390 struct dial_trunk_args args = {
04391 .trunk_ref = trunk_ref,
04392 .station = station,
04393 .cond_lock = &cond_lock,
04394 .cond = &cond,
04395 };
04396 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
04397
04398
04399
04400 ast_autoservice_start(chan);
04401 ast_mutex_init(&cond_lock);
04402 ast_cond_init(&cond, NULL);
04403 pthread_attr_init(&attr);
04404 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
04405 ast_mutex_lock(&cond_lock);
04406 ast_pthread_create_background(&dont_care, &attr, dial_trunk, &args);
04407 ast_cond_wait(&cond, &cond_lock);
04408 ast_mutex_unlock(&cond_lock);
04409 ast_mutex_destroy(&cond_lock);
04410 ast_cond_destroy(&cond);
04411 pthread_attr_destroy(&attr);
04412 ast_autoservice_stop(chan);
04413 if (!trunk_ref->trunk->chan) {
04414 ast_log(LOG_DEBUG, "Trunk didn't get created. chan: %lx\n", (long) trunk_ref->trunk->chan);
04415 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "CONGESTION");
04416 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
04417 trunk_ref->chan = NULL;
04418 return 0;
04419 }
04420 }
04421
04422 if (ast_atomic_fetchadd_int((int *) &trunk_ref->trunk->active_stations, 1) == 0 &&
04423 trunk_ref->trunk->on_hold) {
04424 trunk_ref->trunk->on_hold = 0;
04425 ast_indicate(trunk_ref->trunk->chan, AST_CONTROL_UNHOLD);
04426 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_UP, ALL_TRUNK_REFS, NULL);
04427 }
04428
04429 snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_ref->trunk->name);
04430 ast_set_flag(&conf_flags,
04431 CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_PASS_DTMF | CONFFLAG_SLA_STATION);
04432 ast_answer(chan);
04433 conf = build_conf(conf_name, "", "", 0, 0, 1);
04434 if (conf) {
04435 conf_run(chan, conf, conf_flags.flags, NULL);
04436 dispose_conf(conf);
04437 conf = NULL;
04438 }
04439 trunk_ref->chan = NULL;
04440 if (ast_atomic_dec_and_test((int *) &trunk_ref->trunk->active_stations) &&
04441 trunk_ref->state != SLA_TRUNK_STATE_ONHOLD_BYME) {
04442 strncat(conf_name, "|K", sizeof(conf_name) - strlen(conf_name) - 1);
04443 admin_exec(NULL, conf_name);
04444 trunk_ref->trunk->hold_stations = 0;
04445 sla_change_trunk_state(trunk_ref->trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
04446 }
04447
04448 pbx_builtin_setvar_helper(chan, "SLASTATION_STATUS", "SUCCESS");
04449
04450 return 0;
04451 }
04452
04453 static struct sla_trunk_ref *create_trunk_ref(struct sla_trunk *trunk)
04454 {
04455 struct sla_trunk_ref *trunk_ref;
04456
04457 if (!(trunk_ref = ast_calloc(1, sizeof(*trunk_ref))))
04458 return NULL;
04459
04460 trunk_ref->trunk = trunk;
04461
04462 return trunk_ref;
04463 }
04464
04465 static struct sla_ringing_trunk *queue_ringing_trunk(struct sla_trunk *trunk)
04466 {
04467 struct sla_ringing_trunk *ringing_trunk;
04468
04469 if (!(ringing_trunk = ast_calloc(1, sizeof(*ringing_trunk))))
04470 return NULL;
04471
04472 ringing_trunk->trunk = trunk;
04473 ringing_trunk->ring_begin = ast_tvnow();
04474
04475 sla_change_trunk_state(trunk, SLA_TRUNK_STATE_RINGING, ALL_TRUNK_REFS, NULL);
04476
04477 ast_mutex_lock(&sla.lock);
04478 AST_LIST_INSERT_HEAD(&sla.ringing_trunks, ringing_trunk, entry);
04479 ast_mutex_unlock(&sla.lock);
04480
04481 sla_queue_event(SLA_EVENT_RINGING_TRUNK);
04482
04483 return ringing_trunk;
04484 }
04485
04486 static int sla_trunk_exec(struct ast_channel *chan, void *data)
04487 {
04488 const char *trunk_name = data;
04489 char conf_name[MAX_CONFNUM];
04490 struct ast_conference *conf;
04491 struct ast_flags conf_flags = { 0 };
04492 struct sla_trunk *trunk;
04493 struct sla_ringing_trunk *ringing_trunk;
04494
04495 AST_RWLIST_RDLOCK(&sla_trunks);
04496 trunk = sla_find_trunk(trunk_name);
04497 AST_RWLIST_UNLOCK(&sla_trunks);
04498 if (!trunk) {
04499 ast_log(LOG_ERROR, "SLA Trunk '%s' not found!\n", trunk_name);
04500 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
04501 return 0;
04502 }
04503 if (trunk->chan) {
04504 ast_log(LOG_ERROR, "Call came in on %s, but the trunk is already in use!\n",
04505 trunk_name);
04506 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
04507 return 0;
04508 }
04509 trunk->chan = chan;
04510
04511 if (!(ringing_trunk = queue_ringing_trunk(trunk))) {
04512 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
04513 return 0;
04514 }
04515
04516 snprintf(conf_name, sizeof(conf_name), "SLA_%s", trunk_name);
04517 conf = build_conf(conf_name, "", "", 1, 1, 1);
04518 if (!conf) {
04519 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "FAILURE");
04520 return 0;
04521 }
04522 ast_set_flag(&conf_flags,
04523 CONFFLAG_QUIET | CONFFLAG_MARKEDEXIT | CONFFLAG_MARKEDUSER | CONFFLAG_PASS_DTMF | CONFFLAG_NO_AUDIO_UNTIL_UP);
04524 ast_indicate(chan, AST_CONTROL_RINGING);
04525 conf_run(chan, conf, conf_flags.flags, NULL);
04526 dispose_conf(conf);
04527 conf = NULL;
04528 trunk->chan = NULL;
04529 trunk->on_hold = 0;
04530
04531 sla_change_trunk_state(trunk, SLA_TRUNK_STATE_IDLE, ALL_TRUNK_REFS, NULL);
04532
04533 if (!pbx_builtin_getvar_helper(chan, "SLATRUNK_STATUS"))
04534 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "SUCCESS");
04535
04536
04537 ast_mutex_lock(&sla.lock);
04538 AST_LIST_TRAVERSE_SAFE_BEGIN(&sla.ringing_trunks, ringing_trunk, entry) {
04539 if (ringing_trunk->trunk == trunk) {
04540 AST_LIST_REMOVE_CURRENT(&sla.ringing_trunks, entry);
04541 break;
04542 }
04543 }
04544 AST_LIST_TRAVERSE_SAFE_END
04545 ast_mutex_unlock(&sla.lock);
04546 if (ringing_trunk) {
04547 free(ringing_trunk);
04548 pbx_builtin_setvar_helper(chan, "SLATRUNK_STATUS", "UNANSWERED");
04549
04550
04551 sla_queue_event(SLA_EVENT_RINGING_TRUNK);
04552 }
04553
04554 return 0;
04555 }
04556
04557 static int sla_state(const char *data)
04558 {
04559 char *buf, *station_name, *trunk_name;
04560 struct sla_station *station;
04561 struct sla_trunk_ref *trunk_ref;
04562 int res = AST_DEVICE_INVALID;
04563
04564 trunk_name = buf = ast_strdupa(data);
04565 station_name = strsep(&trunk_name, "_");
04566
04567 AST_RWLIST_RDLOCK(&sla_stations);
04568 AST_LIST_TRAVERSE(&sla_stations, station, entry) {
04569 if (strcasecmp(station_name, station->name))
04570 continue;
04571 AST_RWLIST_RDLOCK(&sla_trunks);
04572 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
04573 if (!strcasecmp(trunk_name, trunk_ref->trunk->name))
04574 break;
04575 }
04576 if (!trunk_ref) {
04577 AST_RWLIST_UNLOCK(&sla_trunks);
04578 break;
04579 }
04580 switch (trunk_ref->state) {
04581 case SLA_TRUNK_STATE_IDLE:
04582 res = AST_DEVICE_NOT_INUSE;
04583 break;
04584 case SLA_TRUNK_STATE_RINGING:
04585 res = AST_DEVICE_RINGING;
04586 break;
04587 case SLA_TRUNK_STATE_UP:
04588 res = AST_DEVICE_INUSE;
04589 break;
04590 case SLA_TRUNK_STATE_ONHOLD:
04591 case SLA_TRUNK_STATE_ONHOLD_BYME:
04592 res = AST_DEVICE_ONHOLD;
04593 break;
04594 }
04595 AST_RWLIST_UNLOCK(&sla_trunks);
04596 }
04597 AST_RWLIST_UNLOCK(&sla_stations);
04598
04599 if (res == AST_DEVICE_INVALID) {
04600 ast_log(LOG_ERROR, "Could not determine state for trunk %s on station %s!\n",
04601 trunk_name, station_name);
04602 }
04603
04604 return res;
04605 }
04606
04607 static void destroy_trunk(struct sla_trunk *trunk)
04608 {
04609 struct sla_station_ref *station_ref;
04610
04611 if (!ast_strlen_zero(trunk->autocontext))
04612 ast_context_remove_extension(trunk->autocontext, "s", 1, sla_registrar);
04613
04614 while ((station_ref = AST_LIST_REMOVE_HEAD(&trunk->stations, entry)))
04615 free(station_ref);
04616
04617 ast_string_field_free_memory(trunk);
04618 free(trunk);
04619 }
04620
04621 static void destroy_station(struct sla_station *station)
04622 {
04623 struct sla_trunk_ref *trunk_ref;
04624
04625 if (!ast_strlen_zero(station->autocontext)) {
04626 AST_RWLIST_RDLOCK(&sla_trunks);
04627 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
04628 char exten[AST_MAX_EXTENSION];
04629 char hint[AST_MAX_APP];
04630 snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
04631 snprintf(hint, sizeof(hint), "SLA:%s", exten);
04632 ast_context_remove_extension(station->autocontext, exten,
04633 1, sla_registrar);
04634 ast_context_remove_extension(station->autocontext, hint,
04635 PRIORITY_HINT, sla_registrar);
04636 }
04637 AST_RWLIST_UNLOCK(&sla_trunks);
04638 }
04639
04640 while ((trunk_ref = AST_LIST_REMOVE_HEAD(&station->trunks, entry)))
04641 free(trunk_ref);
04642
04643 ast_string_field_free_memory(station);
04644 free(station);
04645 }
04646
04647 static void sla_destroy(void)
04648 {
04649 struct sla_trunk *trunk;
04650 struct sla_station *station;
04651
04652 AST_RWLIST_WRLOCK(&sla_trunks);
04653 while ((trunk = AST_RWLIST_REMOVE_HEAD(&sla_trunks, entry)))
04654 destroy_trunk(trunk);
04655 AST_RWLIST_UNLOCK(&sla_trunks);
04656
04657 AST_RWLIST_WRLOCK(&sla_stations);
04658 while ((station = AST_RWLIST_REMOVE_HEAD(&sla_stations, entry)))
04659 destroy_station(station);
04660 AST_RWLIST_UNLOCK(&sla_stations);
04661
04662 if (sla.thread != AST_PTHREADT_NULL) {
04663 ast_mutex_lock(&sla.lock);
04664 sla.stop = 1;
04665 ast_cond_signal(&sla.cond);
04666 ast_mutex_unlock(&sla.lock);
04667 pthread_join(sla.thread, NULL);
04668 }
04669
04670
04671 ast_context_destroy(NULL, sla_registrar);
04672
04673 ast_mutex_destroy(&sla.lock);
04674 ast_cond_destroy(&sla.cond);
04675 }
04676
04677 static int sla_check_device(const char *device)
04678 {
04679 char *tech, *tech_data;
04680
04681 tech_data = ast_strdupa(device);
04682 tech = strsep(&tech_data, "/");
04683
04684 if (ast_strlen_zero(tech) || ast_strlen_zero(tech_data))
04685 return -1;
04686
04687 return 0;
04688 }
04689
04690 static int sla_build_trunk(struct ast_config *cfg, const char *cat)
04691 {
04692 struct sla_trunk *trunk;
04693 struct ast_variable *var;
04694 const char *dev;
04695
04696 if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {
04697 ast_log(LOG_ERROR, "SLA Trunk '%s' defined with no device!\n", cat);
04698 return -1;
04699 }
04700
04701 if (sla_check_device(dev)) {
04702 ast_log(LOG_ERROR, "SLA Trunk '%s' define with invalid device '%s'!\n",
04703 cat, dev);
04704 return -1;
04705 }
04706
04707 if (!(trunk = ast_calloc(1, sizeof(*trunk))))
04708 return -1;
04709 if (ast_string_field_init(trunk, 32)) {
04710 free(trunk);
04711 return -1;
04712 }
04713
04714 ast_string_field_set(trunk, name, cat);
04715 ast_string_field_set(trunk, device, dev);
04716
04717 for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
04718 if (!strcasecmp(var->name, "autocontext"))
04719 ast_string_field_set(trunk, autocontext, var->value);
04720 else if (!strcasecmp(var->name, "ringtimeout")) {
04721 if (sscanf(var->value, "%u", &trunk->ring_timeout) != 1) {
04722 ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for trunk '%s'\n",
04723 var->value, trunk->name);
04724 trunk->ring_timeout = 0;
04725 }
04726 } else if (!strcasecmp(var->name, "barge"))
04727 trunk->barge_disabled = ast_false(var->value);
04728 else if (!strcasecmp(var->name, "hold")) {
04729 if (!strcasecmp(var->value, "private"))
04730 trunk->hold_access = SLA_HOLD_PRIVATE;
04731 else if (!strcasecmp(var->value, "open"))
04732 trunk->hold_access = SLA_HOLD_OPEN;
04733 else {
04734 ast_log(LOG_WARNING, "Invalid value '%s' for hold on trunk %s\n",
04735 var->value, trunk->name);
04736 }
04737 } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {
04738 ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",
04739 var->name, var->lineno, SLA_CONFIG_FILE);
04740 }
04741 }
04742
04743 if (!ast_strlen_zero(trunk->autocontext)) {
04744 struct ast_context *context;
04745 context = ast_context_find_or_create(NULL, trunk->autocontext, sla_registrar);
04746 if (!context) {
04747 ast_log(LOG_ERROR, "Failed to automatically find or create "
04748 "context '%s' for SLA!\n", trunk->autocontext);
04749 destroy_trunk(trunk);
04750 return -1;
04751 }
04752 if (ast_add_extension2(context, 0 , "s", 1,
04753 NULL, NULL, slatrunk_app, ast_strdup(trunk->name), ast_free, sla_registrar)) {
04754 ast_log(LOG_ERROR, "Failed to automatically create extension "
04755 "for trunk '%s'!\n", trunk->name);
04756 destroy_trunk(trunk);
04757 return -1;
04758 }
04759 }
04760
04761 AST_RWLIST_WRLOCK(&sla_trunks);
04762 AST_RWLIST_INSERT_TAIL(&sla_trunks, trunk, entry);
04763 AST_RWLIST_UNLOCK(&sla_trunks);
04764
04765 return 0;
04766 }
04767
04768 static void sla_add_trunk_to_station(struct sla_station *station, struct ast_variable *var)
04769 {
04770 struct sla_trunk *trunk;
04771 struct sla_trunk_ref *trunk_ref;
04772 struct sla_station_ref *station_ref;
04773 char *trunk_name, *options, *cur;
04774
04775 options = ast_strdupa(var->value);
04776 trunk_name = strsep(&options, ",");
04777
04778 AST_RWLIST_RDLOCK(&sla_trunks);
04779 AST_RWLIST_TRAVERSE(&sla_trunks, trunk, entry) {
04780 if (!strcasecmp(trunk->name, trunk_name))
04781 break;
04782 }
04783
04784 AST_RWLIST_UNLOCK(&sla_trunks);
04785 if (!trunk) {
04786 ast_log(LOG_ERROR, "Trunk '%s' not found!\n", var->value);
04787 return;
04788 }
04789 if (!(trunk_ref = create_trunk_ref(trunk)))
04790 return;
04791 trunk_ref->state = SLA_TRUNK_STATE_IDLE;
04792
04793 while ((cur = strsep(&options, ","))) {
04794 char *name, *value = cur;
04795 name = strsep(&value, "=");
04796 if (!strcasecmp(name, "ringtimeout")) {
04797 if (sscanf(value, "%u", &trunk_ref->ring_timeout) != 1) {
04798 ast_log(LOG_WARNING, "Invalid ringtimeout value '%s' for "
04799 "trunk '%s' on station '%s'\n", value, trunk->name, station->name);
04800 trunk_ref->ring_timeout = 0;
04801 }
04802 } else if (!strcasecmp(name, "ringdelay")) {
04803 if (sscanf(value, "%u", &trunk_ref->ring_delay) != 1) {
04804 ast_log(LOG_WARNING, "Invalid ringdelay value '%s' for "
04805 "trunk '%s' on station '%s'\n", value, trunk->name, station->name);
04806 trunk_ref->ring_delay = 0;
04807 }
04808 } else {
04809 ast_log(LOG_WARNING, "Invalid option '%s' for "
04810 "trunk '%s' on station '%s'\n", name, trunk->name, station->name);
04811 }
04812 }
04813
04814 if (!(station_ref = sla_create_station_ref(station))) {
04815 free(trunk_ref);
04816 return;
04817 }
04818 ast_atomic_fetchadd_int((int *) &trunk->num_stations, 1);
04819 AST_RWLIST_WRLOCK(&sla_trunks);
04820 AST_LIST_INSERT_TAIL(&trunk->stations, station_ref, entry);
04821 AST_RWLIST_UNLOCK(&sla_trunks);
04822 AST_LIST_INSERT_TAIL(&station->trunks, trunk_ref, entry);
04823 }
04824
04825 static int sla_build_station(struct ast_config *cfg, const char *cat)
04826 {
04827 struct sla_station *station;
04828 struct ast_variable *var;
04829 const char *dev;
04830
04831 if (!(dev = ast_variable_retrieve(cfg, cat, "device"))) {
04832 ast_log(LOG_ERROR, "SLA Station '%s' defined with no device!\n", cat);
04833 return -1;
04834 }
04835
04836 if (!(station = ast_calloc(1, sizeof(*station))))
04837 return -1;
04838 if (ast_string_field_init(station, 32)) {
04839 free(station);
04840 return -1;
04841 }
04842
04843 ast_string_field_set(station, name, cat);
04844 ast_string_field_set(station, device, dev);
04845
04846 for (var = ast_variable_browse(cfg, cat); var; var = var->next) {
04847 if (!strcasecmp(var->name, "trunk"))
04848 sla_add_trunk_to_station(station, var);
04849 else if (!strcasecmp(var->name, "autocontext"))
04850 ast_string_field_set(station, autocontext, var->value);
04851 else if (!strcasecmp(var->name, "ringtimeout")) {
04852 if (sscanf(var->value, "%u", &station->ring_timeout) != 1) {
04853 ast_log(LOG_WARNING, "Invalid ringtimeout '%s' specified for station '%s'\n",
04854 var->value, station->name);
04855 station->ring_timeout = 0;
04856 }
04857 } else if (!strcasecmp(var->name, "ringdelay")) {
04858 if (sscanf(var->value, "%u", &station->ring_delay) != 1) {
04859 ast_log(LOG_WARNING, "Invalid ringdelay '%s' specified for station '%s'\n",
04860 var->value, station->name);
04861 station->ring_delay = 0;
04862 }
04863 } else if (!strcasecmp(var->name, "hold")) {
04864 if (!strcasecmp(var->value, "private"))
04865 station->hold_access = SLA_HOLD_PRIVATE;
04866 else if (!strcasecmp(var->value, "open"))
04867 station->hold_access = SLA_HOLD_OPEN;
04868 else {
04869 ast_log(LOG_WARNING, "Invalid value '%s' for hold on station %s\n",
04870 var->value, station->name);
04871 }
04872
04873 } else if (strcasecmp(var->name, "type") && strcasecmp(var->name, "device")) {
04874 ast_log(LOG_ERROR, "Invalid option '%s' specified at line %d of %s!\n",
04875 var->name, var->lineno, SLA_CONFIG_FILE);
04876 }
04877 }
04878
04879 if (!ast_strlen_zero(station->autocontext)) {
04880 struct ast_context *context;
04881 struct sla_trunk_ref *trunk_ref;
04882 context = ast_context_find_or_create(NULL, station->autocontext, sla_registrar);
04883 if (!context) {
04884 ast_log(LOG_ERROR, "Failed to automatically find or create "
04885 "context '%s' for SLA!\n", station->autocontext);
04886 destroy_station(station);
04887 return -1;
04888 }
04889
04890
04891 if (ast_add_extension2(context, 0 , station->name, 1,
04892 NULL, NULL, slastation_app, ast_strdup(station->name), ast_free, sla_registrar)) {
04893 ast_log(LOG_ERROR, "Failed to automatically create extension "
04894 "for trunk '%s'!\n", station->name);
04895 destroy_station(station);
04896 return -1;
04897 }
04898 AST_RWLIST_RDLOCK(&sla_trunks);
04899 AST_LIST_TRAVERSE(&station->trunks, trunk_ref, entry) {
04900 char exten[AST_MAX_EXTENSION];
04901 char hint[AST_MAX_APP];
04902 snprintf(exten, sizeof(exten), "%s_%s", station->name, trunk_ref->trunk->name);
04903 snprintf(hint, sizeof(hint), "SLA:%s", exten);
04904
04905
04906 if (ast_add_extension2(context, 0 , exten, 1,
04907 NULL, NULL, slastation_app, ast_strdup(exten), ast_free, sla_registrar)) {
04908 ast_log(LOG_ERROR, "Failed to automatically create extension "
04909 "for trunk '%s'!\n", station->name);
04910 destroy_station(station);
04911 return -1;
04912 }
04913
04914
04915 if (ast_add_extension2(context, 0 , exten, PRIORITY_HINT,
04916 NULL, NULL, hint, NULL, NULL, sla_registrar)) {
04917 ast_log(LOG_ERROR, "Failed to automatically create hint "
04918 "for trunk '%s'!\n", station->name);
04919 destroy_station(station);
04920 return -1;
04921 }
04922 }
04923 AST_RWLIST_UNLOCK(&sla_trunks);
04924 }
04925
04926 AST_RWLIST_WRLOCK(&sla_stations);
04927 AST_RWLIST_INSERT_TAIL(&sla_stations, station, entry);
04928 AST_RWLIST_UNLOCK(&sla_stations);
04929
04930 return 0;
04931 }
04932
04933 static int sla_load_config(void)
04934 {
04935 struct ast_config *cfg;
04936 const char *cat = NULL;
04937 int res = 0;
04938 const char *val;
04939
04940 ast_mutex_init(&sla.lock);
04941 ast_cond_init(&sla.cond, NULL);
04942
04943 if (!(cfg = ast_config_load(SLA_CONFIG_FILE)))
04944 return 0;
04945
04946 if ((val = ast_variable_retrieve(cfg, "general", "attemptcallerid")))
04947 sla.attempt_callerid = ast_true(val);
04948
04949 while ((cat = ast_category_browse(cfg, cat)) && !res) {
04950 const char *type;
04951 if (!strcasecmp(cat, "general"))
04952 continue;
04953 if (!(type = ast_variable_retrieve(cfg, cat, "type"))) {
04954 ast_log(LOG_WARNING, "Invalid entry in %s defined with no type!\n",
04955 SLA_CONFIG_FILE);
04956 continue;
04957 }
04958 if (!strcasecmp(type, "trunk"))
04959 res = sla_build_trunk(cfg, cat);
04960 else if (!strcasecmp(type, "station"))
04961 res = sla_build_station(cfg, cat);
04962 else {
04963 ast_log(LOG_WARNING, "Entry in %s defined with invalid type '%s'!\n",
04964 SLA_CONFIG_FILE, type);
04965 }
04966 }
04967
04968 ast_config_destroy(cfg);
04969
04970 if (!AST_LIST_EMPTY(&sla_stations) || !AST_LIST_EMPTY(&sla_stations))
04971 ast_pthread_create(&sla.thread, NULL, sla_thread, NULL);
04972
04973 return res;
04974 }
04975
04976 static int load_config(int reload)
04977 {
04978 int res = 0;
04979
04980 load_config_meetme();
04981 if (!reload)
04982 res = sla_load_config();
04983
04984 return res;
04985 }
04986
04987 static int unload_module(void)
04988 {
04989 int res = 0;
04990
04991 ast_cli_unregister_multiple(cli_meetme, ARRAY_LEN(cli_meetme));
04992 res = ast_manager_unregister("MeetmeMute");
04993 res |= ast_manager_unregister("MeetmeUnmute");
04994 res |= ast_unregister_application(app3);
04995 res |= ast_unregister_application(app2);
04996 res |= ast_unregister_application(app);
04997 res |= ast_unregister_application(slastation_app);
04998 res |= ast_unregister_application(slatrunk_app);
04999
05000 ast_devstate_prov_del("Meetme");
05001 ast_devstate_prov_del("SLA");
05002
05003 ast_module_user_hangup_all();
05004
05005 sla_destroy();
05006
05007 return res;
05008 }
05009
05010 static int load_module(void)
05011 {
05012 int res = 0;
05013
05014 res |= load_config(0);
05015
05016 ast_cli_register_multiple(cli_meetme, ARRAY_LEN(cli_meetme));
05017 res |= ast_manager_register("MeetmeMute", EVENT_FLAG_CALL,
05018 action_meetmemute, "Mute a Meetme user");
05019 res |= ast_manager_register("MeetmeUnmute", EVENT_FLAG_CALL,
05020 action_meetmeunmute, "Unmute a Meetme user");
05021 res |= ast_register_application(app3, admin_exec, synopsis3, descrip3);
05022 res |= ast_register_application(app2, count_exec, synopsis2, descrip2);
05023 res |= ast_register_application(app, conf_exec, synopsis, descrip);
05024 res |= ast_register_application(slastation_app, sla_station_exec,
05025 slastation_synopsis, slastation_desc);
05026 res |= ast_register_application(slatrunk_app, sla_trunk_exec,
05027 slatrunk_synopsis, slatrunk_desc);
05028
05029 res |= ast_devstate_prov_add("Meetme", meetmestate);
05030 res |= ast_devstate_prov_add("SLA", sla_state);
05031
05032 return res;
05033 }
05034
05035 static int reload(void)
05036 {
05037 return load_config(1);
05038 }
05039
05040 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "MeetMe conference bridge",
05041 .load = load_module,
05042 .unload = unload_module,
05043 .reload = reload,
05044 );
05045