2 * Asterisk -- A telephony toolkit for Linux.
4 * A full-featured Find-Me/Follow-Me Application
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.
22 * \brief Find-Me Follow-Me application
24 * \author BJ Weschke <bweschke@btwtech.com>
26 * \arg See \ref Config_followme
28 * \ingroup applications
33 ASTERISK_FILE_VERSION(__FILE__, "$revision$")
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/options.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/say.h"
50 #include "asterisk/features.h"
51 #include "asterisk/musiconhold.h"
52 #include "asterisk/cli.h"
53 #include "asterisk/manager.h"
54 #include "asterisk/config.h"
55 #include "asterisk/monitor.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/causes.h"
58 #include "asterisk/astdb.h"
59 #include "asterisk/app.h"
61 static char *app = "FollowMe";
62 static char *synopsis = "Find-Me/Follow-Me application";
63 static char *descrip =
64 " FollowMe(followmeid|options):\n"
65 "This application performs Find-Me/Follow-Me functionality for the caller\n"
66 "as defined in the profile matching the <followmeid> parameter in\n"
67 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
68 "followme.conf, execution will be returned to the dialplan and call\n"
69 "execution will continue at the next priority.\n\n"
71 " s - Playback the incoming status message prior to starting the follow-me step(s)\n"
72 " a - Record the caller's name so it can be announced to the callee on each step\n"
73 " n - Playback the unreachable status message if we've run out of steps to reach the\n"
74 " or the callee has elected not to be reachable.\n"
75 "Returns -1 on hangup\n";
77 /*! \brief Number structure */
79 char number[512]; /*!< Phone Number(s) and/or Extension(s) */
80 long timeout; /*!< Dial Timeout, if used. */
81 int order; /*!< The order to dial in */
82 AST_LIST_ENTRY(number) entry; /*!< Next Number record */
85 /*! \brief Data structure for followme scripts */
86 struct call_followme {
88 char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
89 char moh[AST_MAX_CONTEXT]; /*!< Music On Hold Class to be used */
90 char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
91 unsigned int active; /*!< Profile is active (1), or disabled (0). */
92 char takecall[20]; /*!< Digit mapping to take a call */
93 char nextindp[20]; /*!< Digit mapping to decline a call */
94 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
95 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
96 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
97 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
98 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
99 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
101 AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
102 AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
103 AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
104 AST_LIST_ENTRY(call_followme) entry; /*!< Next Follow-Me record */
108 struct ast_channel *chan;
110 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
112 char context[AST_MAX_CONTEXT];
113 char namerecloc[AST_MAX_CONTEXT];
114 struct ast_channel *outbound;
115 char takecall[20]; /*!< Digit mapping to take a call */
116 char nextindp[20]; /*!< Digit mapping to decline a call */
117 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
118 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
119 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
120 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
121 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
122 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
123 struct ast_flags followmeflags;
127 struct ast_channel *ochan;
134 AST_LIST_ENTRY(findme_user) entry;
138 FOLLOWMEFLAG_STATUSMSG = (1 << 0),
139 FOLLOWMEFLAG_RECORDNAME = (1 << 1),
140 FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2)
143 AST_APP_OPTIONS(followme_opts, {
144 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG ),
145 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME ),
146 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG ),
149 static int ynlongest = 0;
150 static time_t start_time, answer_time, end_time;
152 static char *featuredigittostr;
153 static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
154 static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
156 static char takecall[20] = "1", nextindp[20] = "2";
157 static char callfromprompt[PATH_MAX] = "followme/call-from";
158 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
159 static char optionsprompt[PATH_MAX] = "followme/options";
160 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
161 static char statusprompt[PATH_MAX] = "followme/status";
162 static char sorryprompt[PATH_MAX] = "followme/sorry";
165 static AST_LIST_HEAD_STATIC(followmes, call_followme);
166 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
168 static void free_numbers(struct call_followme *f)
170 /* Free numbers attached to the profile */
173 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
174 /* Free the number */
176 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
178 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
179 /* Free the blacklisted number */
181 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
183 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
184 /* Free the whitelisted number */
186 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
191 /*! \brief Allocate and initialize followme profile */
192 static struct call_followme *alloc_profile(const char *fmname)
194 struct call_followme *f;
196 if (!(f = ast_calloc(1, sizeof(*f))))
199 ast_mutex_init(&f->lock);
200 ast_copy_string(f->name, fmname, sizeof(f->name));
202 f->context[0] = '\0';
203 ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
204 ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
205 ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
206 ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
207 ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
208 ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
209 ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
210 ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
211 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
212 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
213 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
217 static void init_profile(struct call_followme *f)
220 ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
225 /*! \brief Set parameter in profile from configuration file */
226 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
229 if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
230 ast_copy_string(f->moh, val, sizeof(f->moh));
231 else if (!strcasecmp(param, "context"))
232 ast_copy_string(f->context, val, sizeof(f->context));
233 else if (!strcasecmp(param, "takecall"))
234 ast_copy_string(f->takecall, val, sizeof(f->takecall));
235 else if (!strcasecmp(param, "declinecall"))
236 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
237 else if (!strcasecmp(param, "call-from-prompt"))
238 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
239 else if (!strcasecmp(param, "followme-norecording-prompt"))
240 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
241 else if (!strcasecmp(param, "followme-options-prompt"))
242 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
243 else if (!strcasecmp(param, "followme-pls-hold-prompt"))
244 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
245 else if (!strcasecmp(param, "followme-status-prompt"))
246 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
247 else if (!strcasecmp(param, "followme-sorry-prompt"))
248 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
249 else if (failunknown) {
251 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
253 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
257 /*! \brief Add a new number */
258 static struct number *create_followme_number(char *number, int timeout, int numorder)
264 if (!(cur = ast_calloc(1, sizeof(*cur))))
267 cur->timeout = timeout;
268 if ((tmp = strchr(number, ',')))
270 ast_copy_string(cur->number, number, sizeof(cur->number));
271 cur->order = numorder;
273 ast_log(LOG_DEBUG, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
278 /*! \brief Reload followme application module */
279 static int reload_followme(void)
281 struct call_followme *f;
282 struct ast_config *cfg;
283 char *cat = NULL, *tmp;
284 struct ast_variable *var;
285 struct number *cur, *nm;
292 char *declinecallstr;
295 cfg = ast_config_load("followme.conf");
297 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
301 AST_LIST_LOCK(&followmes);
303 /* Reset Global Var Values */
304 featuredigittimeout = 5000;
306 /* Mark all profiles as inactive for the moment */
307 AST_LIST_TRAVERSE(&followmes, f, entry) {
310 featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
312 if (!ast_strlen_zero(featuredigittostr)) {
313 if (!sscanf(featuredigittostr, "%d", &featuredigittimeout))
314 featuredigittimeout = 5000;
317 takecallstr = ast_variable_retrieve(cfg, "general", "takecall");
318 if (!ast_strlen_zero(takecallstr))
319 ast_copy_string(takecall, takecallstr, sizeof(takecall));
321 declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall");
322 if (!ast_strlen_zero(declinecallstr))
323 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
325 tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt");
326 if (!ast_strlen_zero(tmpstr))
327 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
329 tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt");
330 if (!ast_strlen_zero(tmpstr))
331 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
333 tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt");
334 if (!ast_strlen_zero(tmpstr))
335 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
337 tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt");
338 if (!ast_strlen_zero(tmpstr))
339 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
341 tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt");
342 if (!ast_strlen_zero(tmpstr))
343 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
345 tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt");
346 if (!ast_strlen_zero(tmpstr))
347 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
349 /* Chug through config file */
350 while ((cat = ast_category_browse(cfg, cat))) {
351 if (!strcasecmp(cat, "general"))
353 /* Define a new profile */
354 /* Look for an existing one */
355 AST_LIST_TRAVERSE(&followmes, f, entry) {
356 if (!strcasecmp(f->name, cat))
360 ast_log(LOG_DEBUG, "New profile %s.\n", cat);
363 f = alloc_profile(cat);
370 ast_mutex_lock(&f->lock);
371 /* Re-initialize the profile */
374 var = ast_variable_browse(cfg, cat);
376 if (!strcasecmp(var->name, "number")) {
377 /* Add a new number */
378 ast_copy_string(numberstr, var->value, sizeof(numberstr));
379 if ((tmp = strchr(numberstr, ','))) {
382 timeoutstr = ast_strdupa(tmp);
383 if ((tmp = strchr(timeoutstr, ','))) {
386 numorder = atoi(tmp);
391 timeout = atoi(timeoutstr);
401 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
405 cur = create_followme_number(numberstr, timeout, numorder);
406 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
408 profile_set_param(f, var->name, var->value, var->lineno, 1);
409 if (option_debug > 1)
410 ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
413 } /* End while(var) loop */
416 ast_mutex_unlock(&f->lock);
418 AST_LIST_INSERT_HEAD(&followmes, f, entry);
421 ast_config_destroy(cfg);
423 AST_LIST_UNLOCK(&followmes);
428 static void clear_caller(struct findme_user *tmpuser)
430 struct ast_channel *outbound;
432 if (tmpuser && tmpuser->ochan && tmpuser->state >= 0) {
433 outbound = tmpuser->ochan;
434 if (!outbound->cdr) {
435 outbound->cdr = ast_cdr_alloc();
437 ast_cdr_init(outbound->cdr, outbound);
442 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", tmpuser->dialarg);
443 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
444 ast_cdr_update(outbound);
445 ast_cdr_start(outbound->cdr);
446 ast_cdr_end(outbound->cdr);
447 /* If the cause wasn't handled properly */
448 if (ast_cdr_disposition(outbound->cdr, outbound->hangupcause))
449 ast_cdr_failed(outbound->cdr);
451 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
452 ast_hangup(tmpuser->ochan);
457 static void clear_calling_tree(struct findme_user_listptr *findme_user_list)
459 struct findme_user *tmpuser;
461 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
462 clear_caller(tmpuser);
463 tmpuser->cleared = 1;
470 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)
472 struct ast_channel *watchers[256];
474 struct ast_channel *winner;
478 struct findme_user *tmpuser;
480 int livechannels = 0;
482 long totalwait = 0, wtd, towas = 0;
484 char *pressbuttonname;
486 /* ------------ wait_for_winner_channel start --------------- */
488 callfromname = ast_strdupa(tpargs->callfromprompt);
489 pressbuttonname = ast_strdupa(tpargs->optionsprompt);
491 if (!AST_LIST_EMPTY(findme_user_list)) {
493 if (option_verbose > 2)
494 ast_verbose(VERBOSE_PREFIX_3 "Original caller hungup. Cleanup.\n");
495 clear_calling_tree(findme_user_list);
499 totalwait = nm->timeout * 1000;
505 watchers[0] = caller;
509 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
510 if (tmpuser->state >= 0 && tmpuser->ochan) {
511 if (tmpuser->state == 3)
512 tmpuser->digts += (towas - wtd);
513 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
514 if (option_verbose > 2)
515 ast_verbose(VERBOSE_PREFIX_3 "We've been waiting for digits longer than we should have.\n");
516 if (!ast_strlen_zero(namerecloc)) {
519 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
520 ast_sched_runq(tmpuser->ochan->sched);
522 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
528 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
529 ast_sched_runq(tmpuser->ochan->sched);
531 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
536 if (tmpuser->ochan->stream) {
537 ast_sched_runq(tmpuser->ochan->sched);
538 tmpto = ast_sched_wait(tmpuser->ochan->sched);
539 if (tmpto > 0 && tmpto < to)
541 else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
542 ast_stopstream(tmpuser->ochan);
543 if (tmpuser->state == 1) {
544 if (option_verbose > 2)
545 ast_verbose(VERBOSE_PREFIX_3 "Playback of the call-from file appears to be done.\n");
546 if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
549 ast_log(LOG_NOTICE, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc);
550 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
552 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
555 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
559 } else if (tmpuser->state == 2) {
560 if (option_verbose > 2)
561 ast_verbose(VERBOSE_PREFIX_3 "Playback of name file appears to be done.\n");
562 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
564 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
570 } else if (tmpuser->state == 3) {
571 if (option_verbose > 2)
572 ast_verbose(VERBOSE_PREFIX_3 "Playback of the next step file appears to be done.\n");
577 watchers[pos++] = tmpuser->ochan;
588 winner = ast_waitfor_n(watchers, pos, &to);
592 if (totalwait <= 0) {
593 if (option_verbose > 2)
594 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);
595 clear_calling_tree(findme_user_list);
599 /* Need to find out which channel this is */
601 while ((winner != watchers[dg]) && (dg < 256))
603 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
604 if (tmpuser->ochan == winner)
606 f = ast_read(winner);
608 if (f->frametype == AST_FRAME_CONTROL) {
609 switch(f->subclass) {
610 case AST_CONTROL_HANGUP:
611 if (option_verbose > 2)
612 ast_verbose( VERBOSE_PREFIX_3 "%s received a hangup frame.\n", winner->name);
614 if (option_verbose > 2)
615 ast_verbose( VERBOSE_PREFIX_3 "The calling channel hungup. Need to drop everyone else.\n");
616 clear_calling_tree(findme_user_list);
620 case AST_CONTROL_ANSWER:
621 if (option_verbose > 2)
622 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", winner->name, caller->name);
623 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
624 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
625 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
626 if (option_verbose > 2)
627 ast_verbose( VERBOSE_PREFIX_3 "Starting playback of %s\n", callfromname);
629 if (!ast_strlen_zero(namerecloc)) {
630 if (!ast_streamfile(winner, callfromname, winner->language)) {
631 ast_sched_runq(winner->sched);
634 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
640 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
641 ast_sched_runq(tmpuser->ochan->sched);
643 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
650 case AST_CONTROL_BUSY:
651 if (option_verbose > 2)
652 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", winner->name);
654 case AST_CONTROL_CONGESTION:
655 if (option_verbose > 2)
656 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", winner->name);
658 case AST_CONTROL_RINGING:
659 if (option_verbose > 2)
660 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", winner->name);
662 case AST_CONTROL_PROGRESS:
663 if (option_verbose > 2)
664 ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", winner->name, caller->name);
666 case AST_CONTROL_VIDUPDATE:
667 if (option_verbose > 2)
668 ast_verbose ( VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", winner->name, caller->name);
670 case AST_CONTROL_PROCEEDING:
671 if (option_verbose > 2)
672 ast_verbose ( VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", winner->name,caller->name);
674 case AST_CONTROL_HOLD:
675 if (option_verbose > 2)
676 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", winner->name);
678 case AST_CONTROL_UNHOLD:
679 if (option_verbose > 2)
680 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", winner->name);
682 case AST_CONTROL_OFFHOOK:
683 case AST_CONTROL_FLASH:
684 /* Ignore going off hook and flash */
687 if (option_verbose > 2)
688 ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
692 ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
696 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
698 ast_stopstream(winner);
701 ast_log(LOG_DEBUG, "DTMF received: %c\n",(char) f->subclass);
702 tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
705 ast_log(LOG_DEBUG, "DTMF string: %s\n", tmpuser->yn);
706 if (tmpuser->ynidx >= ynlongest) {
708 ast_log(LOG_DEBUG, "reached longest possible match - doing evals\n");
709 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
711 ast_log(LOG_DEBUG, "Match to take the call!\n");
713 return tmpuser->ochan;
715 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
717 ast_log(LOG_DEBUG, "Next in dial plan step requested.\n");
730 ast_log(LOG_DEBUG, "we didn't get a frame. hanging up. dg is %d\n",dg);
732 clear_calling_tree(findme_user_list);
739 ast_log(LOG_DEBUG, "live channels left %d\n", livechannels);
741 if (option_verbose > 2)
742 ast_verbose(VERBOSE_PREFIX_3 "no live channels left. exiting.\n");
751 ast_log(LOG_DEBUG, "timed out waiting for action\n");
755 if (option_verbose > 2)
756 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
759 /* --- WAIT FOR WINNER NUMBER END! -----------*/
763 static void findmeexec(struct fm_args *tpargs)
766 struct ast_channel *outbound;
767 struct ast_channel *caller;
768 struct ast_channel *winner = NULL;
772 struct findme_user *tmpuser;
773 struct findme_user *fmuser;
774 struct findme_user *headuser;
775 struct findme_user_listptr *findme_user_list;
778 findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
779 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
781 /* We're going to figure out what the longest possible string of digits to collect is */
783 if (strlen(tpargs->takecall) > ynlongest)
784 ynlongest = strlen(tpargs->takecall);
785 if (strlen(tpargs->nextindp) > ynlongest)
786 ynlongest = strlen(tpargs->nextindp);
789 caller = tpargs->chan;
790 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
791 if (nm->order == idx)
796 if (option_debug > 1)
797 ast_log(LOG_DEBUG, "Number %s timeout %ld\n", nm->number,nm->timeout);
800 number = ast_strdupa(nm->number);
801 if (option_debug > 2)
802 ast_log(LOG_DEBUG, "examining %s\n", number);
804 rest = strchr(number, '&');
810 if (!strcmp(tpargs->context, ""))
811 sprintf(dialarg, "%s", number);
813 sprintf(dialarg, "%s@%s", number, tpargs->context);
815 tmpuser = ast_calloc(1, sizeof(*tmpuser));
817 ast_log(LOG_WARNING, "Out of memory!\n");
818 free(findme_user_list);
822 outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
824 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
825 ast_channel_inherit_variables(tpargs->chan, outbound);
826 if (option_verbose > 2)
827 ast_verbose(VERBOSE_PREFIX_3 "calling %s\n", dialarg);
828 if (!ast_call(outbound,dialarg,0)) {
829 tmpuser->ochan = outbound;
831 tmpuser->cleared = 0;
832 ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
833 AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
835 if (option_verbose > 2)
836 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
839 outbound->cdr = ast_cdr_alloc();
843 ast_cdr_init(outbound->cdr, outbound);
844 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
845 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
846 ast_cdr_update(outbound);
847 ast_cdr_start(outbound->cdr);
848 ast_cdr_end(outbound->cdr);
849 /* If the cause wasn't handled properly */
850 if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
851 ast_cdr_failed(outbound->cdr);
853 ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
854 ast_hangup(outbound);
861 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
867 if (!AST_LIST_EMPTY(findme_user_list))
868 winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
871 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list, fmuser, entry) {
872 if (!fmuser->cleared && fmuser->ochan != winner)
873 clear_caller(fmuser);
874 AST_LIST_REMOVE_CURRENT(findme_user_list, entry);
877 AST_LIST_TRAVERSE_SAFE_END
886 free(findme_user_list);
891 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
892 if (nm->order == idx)
896 free(findme_user_list);
900 tpargs->status = 100;
901 tpargs->outbound = winner;
909 static int app_exec(struct ast_channel *chan, void *data)
911 struct fm_args targs;
912 struct ast_bridge_config config;
913 struct call_followme *f;
914 struct number *nm, *newnm;
916 struct ast_module_user *u;
918 char namerecloc[255];
920 struct ast_channel *caller;
921 struct ast_channel *outbound;
922 static char toast[80];
924 AST_DECLARE_APP_ARGS(args,
925 AST_APP_ARG(followmeid);
926 AST_APP_ARG(options);
929 if (!(argstr = ast_strdupa((char *)data))) {
930 ast_log(LOG_ERROR, "Out of memory!\n");
935 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n",app);
939 u = ast_module_user_add(chan);
941 AST_STANDARD_APP_ARGS(args, argstr);
943 if (!ast_strlen_zero(args.followmeid))
944 AST_LIST_LOCK(&followmes);
945 AST_LIST_TRAVERSE(&followmes, f, entry) {
946 if (!strcasecmp(f->name, args.followmeid) && (f->active))
949 AST_LIST_UNLOCK(&followmes);
952 ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
954 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.", args.followmeid);
957 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
961 ast_app_parse_options(followme_opts, &targs.followmeflags, NULL, args.options);
963 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
964 ast_mutex_lock(&f->lock);
965 targs.mohclass = ast_strdupa(f->moh);
966 ast_copy_string(targs.context, f->context, sizeof(targs.context));
967 ast_copy_string(targs.takecall, f->takecall, sizeof(targs.takecall));
968 ast_copy_string(targs.nextindp, f->nextindp, sizeof(targs.nextindp));
969 ast_copy_string(targs.callfromprompt, f->callfromprompt, sizeof(targs.callfromprompt));
970 ast_copy_string(targs.norecordingprompt, f->norecordingprompt, sizeof(targs.norecordingprompt));
971 ast_copy_string(targs.optionsprompt, f->optionsprompt, sizeof(targs.optionsprompt));
972 ast_copy_string(targs.plsholdprompt, f->plsholdprompt, sizeof(targs.plsholdprompt));
973 ast_copy_string(targs.statusprompt, f->statusprompt, sizeof(targs.statusprompt));
974 ast_copy_string(targs.sorryprompt, f->sorryprompt, sizeof(targs.sorryprompt));
975 /* Copy the numbers we're going to use into another list in case the master list should get modified
976 (and locked) while we're trying to do a follow-me */
977 AST_LIST_HEAD_INIT_NOLOCK(&targs.cnumbers);
978 AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
979 newnm = create_followme_number(nm->number, nm->timeout, nm->order);
980 AST_LIST_INSERT_TAIL(&targs.cnumbers, newnm, entry);
982 ast_mutex_unlock(&f->lock);
984 if (targs.followmeflags.flags & FOLLOWMEFLAG_STATUSMSG)
985 ast_stream_and_wait(chan, targs.statusprompt, chan->language, "");
987 snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
990 if (targs.followmeflags.flags & FOLLOWMEFLAG_RECORDNAME)
991 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL) < 0)
994 /* The following call looks like we're going to playback the file, but we're actually */
995 /* just checking to see if we *can* play it. */
996 if (ast_streamfile(chan, namerecloc, chan->language))
997 ast_copy_string(namerecloc, "", sizeof(namerecloc));
999 ast_stopstream(chan);
1001 if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1003 if (ast_waitstream(chan, "") < 0)
1005 ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1009 ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1013 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs.cnumbers, nm, entry) {
1014 AST_LIST_REMOVE_CURRENT(&targs.cnumbers, entry);
1017 AST_LIST_TRAVERSE_SAFE_END
1019 if (!ast_strlen_zero(namerecloc))
1022 if (targs.status != 100) {
1024 if (targs.followmeflags.flags & FOLLOWMEFLAG_UNREACHABLEMSG)
1025 ast_stream_and_wait(chan, targs.sorryprompt, chan->language, "");
1029 outbound = targs.outbound;
1030 /* Bridge the two channels. */
1032 memset(&config,0,sizeof(struct ast_bridge_config));
1033 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1034 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1035 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1037 ast_moh_stop(caller);
1038 /* Be sure no generators are left on it */
1039 ast_deactivate_generator(caller);
1040 /* Make sure channels are compatible */
1041 res = ast_channel_make_compatible(caller, outbound);
1043 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1044 ast_hangup(outbound);
1048 res = ast_bridge_call(caller,outbound,&config);
1050 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1051 pbx_builtin_setvar_helper(caller, "DIALEDTIME", toast);
1052 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1053 pbx_builtin_setvar_helper(caller, "ANSWEREDTIME", toast);
1055 ast_hangup(outbound);
1061 ast_module_user_remove(u);
1066 static int unload_module(void)
1068 struct call_followme *f;
1070 ast_module_user_hangup_all();
1072 ast_unregister_application(app);
1074 /* Free Memory. Yeah! I'm free! */
1075 AST_LIST_LOCK(&followmes);
1076 while ((f = AST_LIST_REMOVE_HEAD(&followmes, entry))) {
1081 AST_LIST_UNLOCK(&followmes);
1086 static int load_module(void)
1088 if(!reload_followme())
1089 return AST_MODULE_LOAD_DECLINE;
1091 return ast_register_application(app, app_exec, synopsis, descrip);
1094 static int reload(void)
1101 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1102 .load = load_module,
1103 .unload = unload_module,