(closes issue #12846)
[asterisk-bristuff.git] / apps / app_followme.c
blobd383cf989515582392f64046a8bd2c20c5fdf0f9
1 /*
2 * Asterisk -- A telephony toolkit for Linux.
4 * A full-featured Find-Me/Follow-Me Application
5 *
6 * Copyright (C) 2005-2006, BJ Weschke All Rights Reserved.
8 * BJ Weschke <bweschke@btwtech.com>
10 * This code is released by the author with no restrictions on usage.
12 * See http://www.asterisk.org for more information about
13 * the Asterisk project. Please do not directly contact
14 * any of the maintainers of this project for assistance;
15 * the project provides a web site, mailing lists and IRC
16 * channels for your use.
20 /*! \file
22 * \brief Find-Me Follow-Me application
24 * \author BJ Weschke <bweschke@btwtech.com>
26 * \arg See \ref Config_followme
28 * \ingroup applications
31 /*** MODULEINFO
32 <depend>chan_local</depend>
33 ***/
35 #include "asterisk.h"
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <string.h>
43 #include <signal.h>
45 #include "asterisk/lock.h"
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/options.h"
51 #include "asterisk/module.h"
52 #include "asterisk/translate.h"
53 #include "asterisk/say.h"
54 #include "asterisk/features.h"
55 #include "asterisk/musiconhold.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/manager.h"
58 #include "asterisk/config.h"
59 #include "asterisk/monitor.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/causes.h"
62 #include "asterisk/astdb.h"
63 #include "asterisk/app.h"
65 static char *app = "FollowMe";
66 static char *synopsis = "Find-Me/Follow-Me application";
67 static char *descrip =
68 " FollowMe(followmeid|options):\n"
69 "This application performs Find-Me/Follow-Me functionality for the caller\n"
70 "as defined in the profile matching the <followmeid> parameter in\n"
71 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
72 "followme.conf, execution will be returned to the dialplan and call\n"
73 "execution will continue at the next priority.\n\n"
74 " Options:\n"
75 " s - Playback the incoming status message prior to starting the follow-me step(s)\n"
76 " a - Record the caller's name so it can be announced to the callee on each step\n"
77 " n - Playback the unreachable status message if we've run out of steps to reach the\n"
78 " or the callee has elected not to be reachable.\n"
79 "Returns -1 on hangup\n";
81 /*! \brief Number structure */
82 struct number {
83 char number[512]; /*!< Phone Number(s) and/or Extension(s) */
84 long timeout; /*!< Dial Timeout, if used. */
85 char language[MAX_LANGUAGE]; /*!< The language to be used on this dial, if used. */
86 int order; /*!< The order to dial in */
87 AST_LIST_ENTRY(number) entry; /*!< Next Number record */
90 /*! \brief Data structure for followme scripts */
91 struct call_followme {
92 ast_mutex_t lock;
93 char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
94 char moh[AST_MAX_CONTEXT]; /*!< Music On Hold Class to be used */
95 char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
96 unsigned int active; /*!< Profile is active (1), or disabled (0). */
97 char takecall[20]; /*!< Digit mapping to take a call */
98 char nextindp[20]; /*!< Digit mapping to decline a call */
99 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
100 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
101 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
102 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
103 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
104 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
106 AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
107 AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
108 AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
109 AST_LIST_ENTRY(call_followme) entry; /*!< Next Follow-Me record */
112 struct fm_args {
113 struct ast_channel *chan;
114 char *mohclass;
115 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
116 int status;
117 char context[AST_MAX_CONTEXT];
118 char namerecloc[AST_MAX_CONTEXT];
119 struct ast_channel *outbound;
120 char takecall[20]; /*!< Digit mapping to take a call */
121 char nextindp[20]; /*!< Digit mapping to decline a call */
122 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
123 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
124 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
125 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
126 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
127 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
128 struct ast_flags followmeflags;
131 struct findme_user {
132 struct ast_channel *ochan;
133 int state;
134 char dialarg[256];
135 char yn[10];
136 int ynidx;
137 long digts;
138 int cleared;
139 AST_LIST_ENTRY(findme_user) entry;
142 enum {
143 FOLLOWMEFLAG_STATUSMSG = (1 << 0),
144 FOLLOWMEFLAG_RECORDNAME = (1 << 1),
145 FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2)
148 AST_APP_OPTIONS(followme_opts, {
149 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG ),
150 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME ),
151 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG ),
154 static int ynlongest = 0;
155 static time_t start_time, answer_time, end_time;
157 static const char *featuredigittostr;
158 static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
159 static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
161 static char takecall[20] = "1", nextindp[20] = "2";
162 static char callfromprompt[PATH_MAX] = "followme/call-from";
163 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
164 static char optionsprompt[PATH_MAX] = "followme/options";
165 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
166 static char statusprompt[PATH_MAX] = "followme/status";
167 static char sorryprompt[PATH_MAX] = "followme/sorry";
170 static AST_LIST_HEAD_STATIC(followmes, call_followme);
171 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
173 static void free_numbers(struct call_followme *f)
175 /* Free numbers attached to the profile */
176 struct number *prev;
178 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
179 /* Free the number */
180 free(prev);
181 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
183 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
184 /* Free the blacklisted number */
185 free(prev);
186 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
188 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
189 /* Free the whitelisted number */
190 free(prev);
191 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
196 /*! \brief Allocate and initialize followme profile */
197 static struct call_followme *alloc_profile(const char *fmname)
199 struct call_followme *f;
201 if (!(f = ast_calloc(1, sizeof(*f))))
202 return NULL;
204 ast_mutex_init(&f->lock);
205 ast_copy_string(f->name, fmname, sizeof(f->name));
206 f->moh[0] = '\0';
207 f->context[0] = '\0';
208 ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
209 ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
210 ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
211 ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
212 ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
213 ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
214 ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
215 ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
216 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
217 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
218 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
219 return f;
222 static void init_profile(struct call_followme *f)
224 f->active = 1;
225 ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
230 /*! \brief Set parameter in profile from configuration file */
231 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
234 if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
235 ast_copy_string(f->moh, val, sizeof(f->moh));
236 else if (!strcasecmp(param, "context"))
237 ast_copy_string(f->context, val, sizeof(f->context));
238 else if (!strcasecmp(param, "takecall"))
239 ast_copy_string(f->takecall, val, sizeof(f->takecall));
240 else if (!strcasecmp(param, "declinecall"))
241 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
242 else if (!strcasecmp(param, "call-from-prompt"))
243 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
244 else if (!strcasecmp(param, "followme-norecording-prompt"))
245 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
246 else if (!strcasecmp(param, "followme-options-prompt"))
247 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
248 else if (!strcasecmp(param, "followme-pls-hold-prompt"))
249 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
250 else if (!strcasecmp(param, "followme-status-prompt"))
251 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
252 else if (!strcasecmp(param, "followme-sorry-prompt"))
253 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
254 else if (failunknown) {
255 if (linenum >= 0)
256 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
257 else
258 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
262 /*! \brief Add a new number */
263 static struct number *create_followme_number(char *number, char *language, int timeout, int numorder)
265 struct number *cur;
266 char *tmp;
269 if (!(cur = ast_calloc(1, sizeof(*cur))))
270 return NULL;
272 cur->timeout = timeout;
273 if ((tmp = strchr(number, ',')))
274 *tmp = '\0';
275 ast_copy_string(cur->number, number, sizeof(cur->number));
276 ast_copy_string(cur->language, language, sizeof(cur->language));
277 cur->order = numorder;
278 if (option_debug)
279 ast_log(LOG_DEBUG, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
281 return cur;
284 /*! \brief Reload followme application module */
285 static int reload_followme(void)
287 struct call_followme *f;
288 struct ast_config *cfg;
289 char *cat = NULL, *tmp;
290 struct ast_variable *var;
291 struct number *cur, *nm;
292 int new, idx;
293 char numberstr[90];
294 int timeout;
295 char *timeoutstr;
296 int numorder;
297 const char *takecallstr;
298 const char *declinecallstr;
299 const char *tmpstr;
301 cfg = ast_config_load("followme.conf");
302 if (!cfg) {
303 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
304 return 0;
307 AST_LIST_LOCK(&followmes);
309 /* Reset Global Var Values */
310 featuredigittimeout = 5000;
312 /* Mark all profiles as inactive for the moment */
313 AST_LIST_TRAVERSE(&followmes, f, entry) {
314 f->active = 0;
316 featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
318 if (!ast_strlen_zero(featuredigittostr)) {
319 if (!sscanf(featuredigittostr, "%d", &featuredigittimeout))
320 featuredigittimeout = 5000;
323 takecallstr = ast_variable_retrieve(cfg, "general", "takecall");
324 if (!ast_strlen_zero(takecallstr))
325 ast_copy_string(takecall, takecallstr, sizeof(takecall));
327 declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall");
328 if (!ast_strlen_zero(declinecallstr))
329 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
331 tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt");
332 if (!ast_strlen_zero(tmpstr))
333 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
335 tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt");
336 if (!ast_strlen_zero(tmpstr))
337 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
339 tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt");
340 if (!ast_strlen_zero(tmpstr))
341 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
343 tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt");
344 if (!ast_strlen_zero(tmpstr))
345 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
347 tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt");
348 if (!ast_strlen_zero(tmpstr))
349 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
351 tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt");
352 if (!ast_strlen_zero(tmpstr))
353 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
355 /* Chug through config file */
356 while ((cat = ast_category_browse(cfg, cat))) {
357 if (!strcasecmp(cat, "general"))
358 continue;
359 /* Define a new profile */
360 /* Look for an existing one */
361 AST_LIST_TRAVERSE(&followmes, f, entry) {
362 if (!strcasecmp(f->name, cat))
363 break;
365 if (option_debug)
366 ast_log(LOG_DEBUG, "New profile %s.\n", cat);
367 if (!f) {
368 /* Make one then */
369 f = alloc_profile(cat);
370 new = 1;
371 } else
372 new = 0;
374 if (f) {
375 if (!new)
376 ast_mutex_lock(&f->lock);
377 /* Re-initialize the profile */
378 init_profile(f);
379 free_numbers(f);
380 var = ast_variable_browse(cfg, cat);
381 while(var) {
382 if (!strcasecmp(var->name, "number")) {
383 /* Add a new number */
384 ast_copy_string(numberstr, var->value, sizeof(numberstr));
385 if ((tmp = strchr(numberstr, ','))) {
386 *tmp = '\0';
387 tmp++;
388 timeoutstr = ast_strdupa(tmp);
389 if ((tmp = strchr(timeoutstr, ','))) {
390 *tmp = '\0';
391 tmp++;
392 numorder = atoi(tmp);
393 if (numorder < 0)
394 numorder = 0;
395 } else
396 numorder = 0;
397 timeout = atoi(timeoutstr);
398 if (timeout < 0)
399 timeout = 25;
400 } else {
401 timeout = 25;
402 numorder = 0;
405 if (!numorder) {
406 idx = 1;
407 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
408 idx++;
409 numorder = idx;
411 cur = create_followme_number(numberstr, "", timeout, numorder);
412 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
413 } else {
414 profile_set_param(f, var->name, var->value, var->lineno, 1);
415 if (option_debug > 1)
416 ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
418 var = var->next;
419 } /* End while(var) loop */
421 if (!new)
422 ast_mutex_unlock(&f->lock);
423 else
424 AST_LIST_INSERT_HEAD(&followmes, f, entry);
427 ast_config_destroy(cfg);
429 AST_LIST_UNLOCK(&followmes);
431 return 1;
434 static void clear_caller(struct findme_user *tmpuser)
436 struct ast_channel *outbound;
438 if (tmpuser && tmpuser->ochan && tmpuser->state >= 0) {
439 outbound = tmpuser->ochan;
440 if (!outbound->cdr) {
441 outbound->cdr = ast_cdr_alloc();
442 if (outbound->cdr)
443 ast_cdr_init(outbound->cdr, outbound);
445 if (outbound->cdr) {
446 char tmp[256];
448 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", tmpuser->dialarg);
449 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
450 ast_cdr_update(outbound);
451 ast_cdr_start(outbound->cdr);
452 ast_cdr_end(outbound->cdr);
453 /* If the cause wasn't handled properly */
454 if (ast_cdr_disposition(outbound->cdr, outbound->hangupcause))
455 ast_cdr_failed(outbound->cdr);
456 } else
457 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
458 ast_hangup(tmpuser->ochan);
463 static void clear_calling_tree(struct findme_user_listptr *findme_user_list)
465 struct findme_user *tmpuser;
467 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
468 clear_caller(tmpuser);
469 tmpuser->cleared = 1;
476 static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, char *namerecloc, int *status, struct fm_args *tpargs)
478 struct ast_channel *watchers[256];
479 int pos;
480 struct ast_channel *winner;
481 struct ast_frame *f;
482 int ctstatus;
483 int dg;
484 struct findme_user *tmpuser;
485 int to = 0;
486 int livechannels = 0;
487 int tmpto;
488 long totalwait = 0, wtd, towas = 0;
489 char *callfromname;
490 char *pressbuttonname;
492 /* ------------ wait_for_winner_channel start --------------- */
494 callfromname = ast_strdupa(tpargs->callfromprompt);
495 pressbuttonname = ast_strdupa(tpargs->optionsprompt);
497 if (!AST_LIST_EMPTY(findme_user_list)) {
498 if (!caller) {
499 if (option_verbose > 2)
500 ast_verbose(VERBOSE_PREFIX_3 "Original caller hungup. Cleanup.\n");
501 clear_calling_tree(findme_user_list);
502 return NULL;
504 ctstatus = 0;
505 totalwait = nm->timeout * 1000;
506 wtd = 0;
507 while (!ctstatus) {
508 to = 1000;
509 pos = 1;
510 livechannels = 0;
511 watchers[0] = caller;
513 dg = 0;
514 winner = NULL;
515 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
516 if (tmpuser->state >= 0 && tmpuser->ochan) {
517 if (tmpuser->state == 3)
518 tmpuser->digts += (towas - wtd);
519 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
520 if (option_verbose > 2)
521 ast_verbose(VERBOSE_PREFIX_3 "We've been waiting for digits longer than we should have.\n");
522 if (!ast_strlen_zero(namerecloc)) {
523 tmpuser->state = 1;
524 tmpuser->digts = 0;
525 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
526 ast_sched_runq(tmpuser->ochan->sched);
527 } else {
528 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
529 return NULL;
531 } else {
532 tmpuser->state = 2;
533 tmpuser->digts = 0;
534 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
535 ast_sched_runq(tmpuser->ochan->sched);
536 else {
537 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
538 return NULL;
542 if (tmpuser->ochan->stream) {
543 ast_sched_runq(tmpuser->ochan->sched);
544 tmpto = ast_sched_wait(tmpuser->ochan->sched);
545 if (tmpto > 0 && tmpto < to)
546 to = tmpto;
547 else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
548 ast_stopstream(tmpuser->ochan);
549 if (tmpuser->state == 1) {
550 if (option_verbose > 2)
551 ast_verbose(VERBOSE_PREFIX_3 "Playback of the call-from file appears to be done.\n");
552 if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
553 tmpuser->state = 2;
554 } else {
555 ast_log(LOG_NOTICE, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc);
556 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
557 tmpuser->ynidx = 0;
558 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
559 tmpuser->state = 3;
560 else {
561 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
562 return NULL;
565 } else if (tmpuser->state == 2) {
566 if (option_verbose > 2)
567 ast_verbose(VERBOSE_PREFIX_3 "Playback of name file appears to be done.\n");
568 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
569 tmpuser->ynidx = 0;
570 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
571 tmpuser->state = 3;
573 } else {
574 return NULL;
576 } else if (tmpuser->state == 3) {
577 if (option_verbose > 2)
578 ast_verbose(VERBOSE_PREFIX_3 "Playback of the next step file appears to be done.\n");
579 tmpuser->digts = 0;
583 watchers[pos++] = tmpuser->ochan;
584 livechannels++;
588 tmpto = to;
589 if (to < 0) {
590 to = 1000;
591 tmpto = 1000;
593 towas = to;
594 winner = ast_waitfor_n(watchers, pos, &to);
595 tmpto -= to;
596 totalwait -= tmpto;
597 wtd = to;
598 if (totalwait <= 0) {
599 if (option_verbose > 2)
600 ast_verbose(VERBOSE_PREFIX_3 "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
601 clear_calling_tree(findme_user_list);
602 return NULL;
604 if (winner) {
605 /* Need to find out which channel this is */
606 dg = 0;
607 while ((winner != watchers[dg]) && (dg < 256))
608 dg++;
609 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
610 if (tmpuser->ochan == winner)
611 break;
612 f = ast_read(winner);
613 if (f) {
614 if (f->frametype == AST_FRAME_CONTROL) {
615 switch(f->subclass) {
616 case AST_CONTROL_HANGUP:
617 if (option_verbose > 2)
618 ast_verbose( VERBOSE_PREFIX_3 "%s received a hangup frame.\n", winner->name);
619 if (dg == 0) {
620 if (option_verbose > 2)
621 ast_verbose( VERBOSE_PREFIX_3 "The calling channel hungup. Need to drop everyone else.\n");
622 clear_calling_tree(findme_user_list);
623 ctstatus = -1;
625 break;
626 case AST_CONTROL_ANSWER:
627 if (option_verbose > 2)
628 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", winner->name, caller->name);
629 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
630 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
631 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
632 if (option_verbose > 2)
633 ast_verbose( VERBOSE_PREFIX_3 "Starting playback of %s\n", callfromname);
634 if (dg > 0) {
635 if (!ast_strlen_zero(namerecloc)) {
636 if (!ast_streamfile(winner, callfromname, winner->language)) {
637 ast_sched_runq(winner->sched);
638 tmpuser->state = 1;
639 } else {
640 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
641 ast_frfree(f);
642 return NULL;
644 } else {
645 tmpuser->state = 2;
646 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
647 ast_sched_runq(tmpuser->ochan->sched);
648 else {
649 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
650 ast_frfree(f);
651 return NULL;
655 break;
656 case AST_CONTROL_BUSY:
657 if (option_verbose > 2)
658 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", winner->name);
659 break;
660 case AST_CONTROL_CONGESTION:
661 if (option_verbose > 2)
662 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", winner->name);
663 break;
664 case AST_CONTROL_RINGING:
665 if (option_verbose > 2)
666 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", winner->name);
667 break;
668 case AST_CONTROL_PROGRESS:
669 if (option_verbose > 2)
670 ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", winner->name, caller->name);
671 break;
672 case AST_CONTROL_VIDUPDATE:
673 if (option_verbose > 2)
674 ast_verbose ( VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", winner->name, caller->name);
675 break;
676 case AST_CONTROL_SRCUPDATE:
677 if (option_verbose > 2)
678 ast_verbose ( VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", winner->name, caller->name);
679 break;
680 case AST_CONTROL_PROCEEDING:
681 if (option_verbose > 2)
682 ast_verbose ( VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", winner->name,caller->name);
683 break;
684 case AST_CONTROL_HOLD:
685 if (option_verbose > 2)
686 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", winner->name);
687 break;
688 case AST_CONTROL_UNHOLD:
689 if (option_verbose > 2)
690 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", winner->name);
691 break;
692 case AST_CONTROL_OFFHOOK:
693 case AST_CONTROL_FLASH:
694 /* Ignore going off hook and flash */
695 break;
696 case -1:
697 if (option_verbose > 2)
698 ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
699 break;
700 default:
701 if (option_debug)
702 ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
703 break;
706 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
707 if (winner->stream)
708 ast_stopstream(winner);
709 tmpuser->digts = 0;
710 if (option_debug)
711 ast_log(LOG_DEBUG, "DTMF received: %c\n",(char) f->subclass);
712 tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
713 tmpuser->ynidx++;
714 if (option_debug)
715 ast_log(LOG_DEBUG, "DTMF string: %s\n", tmpuser->yn);
716 if (tmpuser->ynidx >= ynlongest) {
717 if (option_debug)
718 ast_log(LOG_DEBUG, "reached longest possible match - doing evals\n");
719 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
720 if (option_debug)
721 ast_log(LOG_DEBUG, "Match to take the call!\n");
722 ast_frfree(f);
723 return tmpuser->ochan;
725 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
726 if (option_debug)
727 ast_log(LOG_DEBUG, "Next in dial plan step requested.\n");
728 *status = 1;
729 ast_frfree(f);
730 return NULL;
736 ast_frfree(f);
737 } else {
738 if (winner) {
739 if (option_debug)
740 ast_log(LOG_DEBUG, "we didn't get a frame. hanging up. dg is %d\n",dg);
741 if (!dg) {
742 clear_calling_tree(findme_user_list);
743 return NULL;
744 } else {
745 tmpuser->state = -1;
746 ast_hangup(winner);
747 livechannels--;
748 if (option_debug)
749 ast_log(LOG_DEBUG, "live channels left %d\n", livechannels);
750 if (!livechannels) {
751 if (option_verbose > 2)
752 ast_verbose(VERBOSE_PREFIX_3 "no live channels left. exiting.\n");
753 return NULL;
759 } else
760 if (option_debug)
761 ast_log(LOG_DEBUG, "timed out waiting for action\n");
764 } else {
765 if (option_verbose > 2)
766 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
769 /* --- WAIT FOR WINNER NUMBER END! -----------*/
770 return NULL;
773 static void findmeexec(struct fm_args *tpargs)
775 struct number *nm;
776 struct ast_channel *outbound;
777 struct ast_channel *caller;
778 struct ast_channel *winner = NULL;
779 char dialarg[512];
780 int dg, idx;
781 char *rest, *number;
782 struct findme_user *tmpuser;
783 struct findme_user *fmuser;
784 struct findme_user *headuser;
785 struct findme_user_listptr *findme_user_list;
786 int status;
788 findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
789 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
791 /* We're going to figure out what the longest possible string of digits to collect is */
792 ynlongest = 0;
793 if (strlen(tpargs->takecall) > ynlongest)
794 ynlongest = strlen(tpargs->takecall);
795 if (strlen(tpargs->nextindp) > ynlongest)
796 ynlongest = strlen(tpargs->nextindp);
798 idx = 1;
799 caller = tpargs->chan;
800 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
801 if (nm->order == idx)
802 break;
804 while (nm) {
806 if (option_debug > 1)
807 ast_log(LOG_DEBUG, "Number %s timeout %ld\n", nm->number,nm->timeout);
808 time(&start_time);
810 number = ast_strdupa(nm->number);
811 if (option_debug > 2)
812 ast_log(LOG_DEBUG, "examining %s\n", number);
813 do {
814 rest = strchr(number, '&');
815 if (rest) {
816 *rest = 0;
817 rest++;
820 if (!strcmp(tpargs->context, ""))
821 snprintf(dialarg, sizeof(dialarg), "%s", number);
822 else
823 snprintf(dialarg, sizeof(dialarg), "%s@%s", number, tpargs->context);
825 tmpuser = ast_calloc(1, sizeof(*tmpuser));
826 if (!tmpuser) {
827 ast_log(LOG_WARNING, "Out of memory!\n");
828 free(findme_user_list);
829 return;
832 outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
833 if (outbound) {
834 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
835 ast_channel_inherit_variables(tpargs->chan, outbound);
836 if (option_verbose > 2)
837 ast_verbose(VERBOSE_PREFIX_3 "calling %s\n", dialarg);
838 if (!ast_call(outbound,dialarg,0)) {
839 tmpuser->ochan = outbound;
840 tmpuser->state = 0;
841 tmpuser->cleared = 0;
842 ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
843 AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
844 } else {
845 if (option_verbose > 2)
846 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
847 if (outbound) {
848 if (!outbound->cdr)
849 outbound->cdr = ast_cdr_alloc();
850 if (outbound->cdr) {
851 char tmp[256];
853 ast_cdr_init(outbound->cdr, outbound);
854 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
855 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
856 ast_cdr_update(outbound);
857 ast_cdr_start(outbound->cdr);
858 ast_cdr_end(outbound->cdr);
859 /* If the cause wasn't handled properly */
860 if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
861 ast_cdr_failed(outbound->cdr);
862 } else {
863 ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
864 ast_hangup(outbound);
865 outbound = NULL;
870 } else
871 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
873 number = rest;
874 } while (number);
876 status = 0;
877 if (!AST_LIST_EMPTY(findme_user_list))
878 winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
881 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list, fmuser, entry) {
882 if (!fmuser->cleared && fmuser->ochan != winner)
883 clear_caller(fmuser);
884 AST_LIST_REMOVE_CURRENT(findme_user_list, entry);
885 free(fmuser);
887 AST_LIST_TRAVERSE_SAFE_END
888 fmuser = NULL;
889 tmpuser = NULL;
890 headuser = NULL;
891 if (winner)
892 break;
894 if (!caller) {
895 tpargs->status = 1;
896 free(findme_user_list);
897 return;
900 idx++;
901 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
902 if (nm->order == idx)
903 break;
906 free(findme_user_list);
907 if (!winner)
908 tpargs->status = 1;
909 else {
910 tpargs->status = 100;
911 tpargs->outbound = winner;
915 return;
919 static int app_exec(struct ast_channel *chan, void *data)
921 struct fm_args targs;
922 struct ast_bridge_config config;
923 struct call_followme *f;
924 struct number *nm, *newnm;
925 int res = 0;
926 struct ast_module_user *u;
927 char *argstr;
928 char namerecloc[255];
929 char *fname = NULL;
930 int duration = 0;
931 struct ast_channel *caller;
932 struct ast_channel *outbound;
933 static char toast[80];
935 AST_DECLARE_APP_ARGS(args,
936 AST_APP_ARG(followmeid);
937 AST_APP_ARG(options);
940 if (!(argstr = ast_strdupa((char *)data))) {
941 ast_log(LOG_ERROR, "Out of memory!\n");
942 return -1;
945 if (!data) {
946 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n",app);
947 return -1;
950 u = ast_module_user_add(chan);
952 AST_STANDARD_APP_ARGS(args, argstr);
954 if (!ast_strlen_zero(args.followmeid))
955 AST_LIST_LOCK(&followmes);
956 AST_LIST_TRAVERSE(&followmes, f, entry) {
957 if (!strcasecmp(f->name, args.followmeid) && (f->active))
958 break;
960 AST_LIST_UNLOCK(&followmes);
962 if (option_debug)
963 ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
964 if (!f) {
965 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
966 res = 0;
967 } else {
968 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
971 if (args.options)
972 ast_app_parse_options(followme_opts, &targs.followmeflags, NULL, args.options);
974 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
975 ast_mutex_lock(&f->lock);
976 targs.mohclass = ast_strdupa(f->moh);
977 ast_copy_string(targs.context, f->context, sizeof(targs.context));
978 ast_copy_string(targs.takecall, f->takecall, sizeof(targs.takecall));
979 ast_copy_string(targs.nextindp, f->nextindp, sizeof(targs.nextindp));
980 ast_copy_string(targs.callfromprompt, f->callfromprompt, sizeof(targs.callfromprompt));
981 ast_copy_string(targs.norecordingprompt, f->norecordingprompt, sizeof(targs.norecordingprompt));
982 ast_copy_string(targs.optionsprompt, f->optionsprompt, sizeof(targs.optionsprompt));
983 ast_copy_string(targs.plsholdprompt, f->plsholdprompt, sizeof(targs.plsholdprompt));
984 ast_copy_string(targs.statusprompt, f->statusprompt, sizeof(targs.statusprompt));
985 ast_copy_string(targs.sorryprompt, f->sorryprompt, sizeof(targs.sorryprompt));
986 /* Copy the numbers we're going to use into another list in case the master list should get modified
987 (and locked) while we're trying to do a follow-me */
988 AST_LIST_HEAD_INIT_NOLOCK(&targs.cnumbers);
989 AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
990 newnm = create_followme_number(nm->number, "", nm->timeout, nm->order);
991 AST_LIST_INSERT_TAIL(&targs.cnumbers, newnm, entry);
993 ast_mutex_unlock(&f->lock);
995 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_STATUSMSG))
996 ast_stream_and_wait(chan, targs.statusprompt, chan->language, "");
998 snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
999 duration = 5;
1001 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_RECORDNAME))
1002 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL) < 0)
1003 goto outrun;
1005 if (!ast_fileexists(namerecloc, NULL, chan->language))
1006 ast_copy_string(namerecloc, "", sizeof(namerecloc));
1008 if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1009 goto outrun;
1010 if (ast_waitstream(chan, "") < 0)
1011 goto outrun;
1012 ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1014 targs.status = 0;
1015 targs.chan = chan;
1016 ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1018 findmeexec(&targs);
1020 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs.cnumbers, nm, entry) {
1021 AST_LIST_REMOVE_CURRENT(&targs.cnumbers, entry);
1022 free(nm);
1024 AST_LIST_TRAVERSE_SAFE_END
1025 if (targs.status != 100) {
1026 ast_moh_stop(chan);
1027 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG))
1028 ast_stream_and_wait(chan, targs.sorryprompt, chan->language, "");
1029 res = 0;
1030 } else {
1031 caller = chan;
1032 outbound = targs.outbound;
1033 /* Bridge the two channels. */
1035 memset(&config,0,sizeof(struct ast_bridge_config));
1036 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1037 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1038 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1040 ast_moh_stop(caller);
1041 /* Be sure no generators are left on it */
1042 ast_deactivate_generator(caller);
1043 /* Make sure channels are compatible */
1044 res = ast_channel_make_compatible(caller, outbound);
1045 if (res < 0) {
1046 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1047 ast_hangup(outbound);
1048 goto outrun;
1050 time(&answer_time);
1051 res = ast_bridge_call(caller,outbound,&config);
1052 time(&end_time);
1053 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1054 pbx_builtin_setvar_helper(caller, "DIALEDTIME", toast);
1055 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1056 pbx_builtin_setvar_helper(caller, "ANSWEREDTIME", toast);
1057 if (outbound)
1058 ast_hangup(outbound);
1061 outrun:
1063 if (!ast_strlen_zero(namerecloc)){
1064 fname = alloca(strlen(namerecloc) + 5);
1065 sprintf(fname, "%s.sln", namerecloc);
1066 unlink(fname);
1069 ast_module_user_remove(u);
1071 return res;
1074 static int unload_module(void)
1076 struct call_followme *f;
1078 ast_module_user_hangup_all();
1080 ast_unregister_application(app);
1082 /* Free Memory. Yeah! I'm free! */
1083 AST_LIST_LOCK(&followmes);
1084 while ((f = AST_LIST_REMOVE_HEAD(&followmes, entry))) {
1085 free_numbers(f);
1086 free(f);
1089 AST_LIST_UNLOCK(&followmes);
1091 return 0;
1094 static int load_module(void)
1096 if(!reload_followme())
1097 return AST_MODULE_LOAD_DECLINE;
1099 return ast_register_application(app, app_exec, synopsis, descrip);
1102 static int reload(void)
1104 reload_followme();
1106 return 0;
1109 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1110 .load = load_module,
1111 .unload = unload_module,
1112 .reload = reload,