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 =
64 static char *descrip =
65 " FollowMe(followmeid|options):\n"
66 "This application performs Find-Me/Follow-Me functionality for the caller\n"
67 "as defined in the profile matching the <followmeid> parameter in\n"
68 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
69 "followme.conf, execution will be returned to the dialplan and call\n"
70 "execution will continue at the next priority.\n\n"
72 " s - Playback the incoming status message prior to starting the follow-me step(s)\n"
73 " a - Record the caller's name so it can be announced to the callee on each step\n"
74 " n - Playback the unreachable status message if we've run out of steps to reach the\n"
75 " or the callee has elected not to be reachable.\n"
81 char number[512]; /*!< Phone Number(s) and/or Extension(s) */
82 long timeout; /*!< Dial Timeout, if used. */
83 int order; /*!< The order to dial in */
84 AST_LIST_ENTRY(number) entry; /*!< Next Number record */
87 struct ast_call_followme {
89 char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
90 char moh[AST_MAX_CONTEXT]; /*!< Music On Hold Class to be used */
91 char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
92 unsigned int active; /*!< Profile is active (1), or disabled (0). */
93 char takecall[20]; /*!< Digit mapping to take a call */
94 char nextindp[20]; /*!< Digit mapping to decline a call */
95 char callfromprompt[PATH_MAX];
96 char norecordingprompt[PATH_MAX];
97 char optionsprompt[PATH_MAX];
98 char plsholdprompt[PATH_MAX];
99 char statusprompt[PATH_MAX];
100 char sorryprompt[PATH_MAX];
102 AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
103 AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
104 AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
105 AST_LIST_ENTRY(ast_call_followme) entry; /*!< Next Follow-Me record */
109 struct ast_channel *chan;
111 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
113 char context[AST_MAX_CONTEXT];
114 char namerecloc[AST_MAX_CONTEXT];
115 struct ast_channel *outbound;
116 char takecall[20]; /*!< Digit mapping to take a call */
117 char nextindp[20]; /*!< Digit mapping to decline a call */
118 char callfromprompt[PATH_MAX];
119 char norecordingprompt[PATH_MAX];
120 char optionsprompt[PATH_MAX];
121 char plsholdprompt[PATH_MAX];
122 char statusprompt[PATH_MAX];
123 char sorryprompt[PATH_MAX];
124 struct ast_flags followmeflags;
128 struct ast_channel *ochan;
135 AST_LIST_ENTRY(findme_user) entry;
139 FOLLOWMEFLAG_STATUSMSG = (1 << 0),
140 FOLLOWMEFLAG_RECORDNAME = (1 << 1),
141 FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2)
144 AST_APP_OPTIONS(followme_opts, {
145 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG ),
146 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME ),
147 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG ),
150 static int ynlongest = 0;
151 static char toast[80];
152 static time_t start_time, answer_time, end_time;
154 static char *featuredigittostr;
155 static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
156 static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
158 static char takecall[20] = "1", nextindp[20] = "2";
159 static char callfromprompt[PATH_MAX] = "followme/call-from";
160 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
161 static char optionsprompt[PATH_MAX] = "followme/followme-options";
162 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
163 static char statusprompt[PATH_MAX] = "followme/followme-status";
164 static char sorryprompt[PATH_MAX] = "followme/followme-sorry";
167 static AST_LIST_HEAD_STATIC(followmes, ast_call_followme);
168 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
170 static void free_numbers(struct ast_call_followme *f)
172 /* Free numbers attached to the profile */
175 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
176 /* Free the number */
178 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
180 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
181 /* Free the blacklisted number */
183 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
185 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
186 /* Free the whitelisted number */
188 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
193 static struct ast_call_followme *alloc_profile(const char *fmname)
195 struct ast_call_followme *f;
197 f = ast_calloc(1, sizeof(*f));
199 ast_mutex_init(&f->lock);
200 ast_copy_string(f->name, fmname, sizeof(f->name));
201 ast_copy_string(f->moh, "", sizeof(f->moh));
202 ast_copy_string(f->context, "", sizeof(f->context));
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);
218 static void init_profile(struct ast_call_followme *f)
221 ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
226 static void profile_set_param(struct ast_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 static struct number *create_followme_number(char *number, int timeout, int numorder)
262 /* Add a new number */
264 cur = ast_calloc(1, sizeof(*cur));
267 cur->timeout = timeout;
268 if ((tmp = strchr(number, ','))) {
271 ast_copy_string(cur->number, number, sizeof(cur->number));
272 cur->order = numorder;
274 ast_log(LOG_DEBUG, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
280 static int reload_followme(void)
282 struct ast_call_followme *f;
283 struct ast_config *cfg;
284 char *cat = NULL, *tmp;
285 struct ast_variable *var;
286 struct number *cur, *nm;
293 char *declinecallstr;
296 cfg = ast_config_load("followme.conf");
298 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
302 AST_LIST_LOCK(&followmes);
304 /* Reset Global Var Values */
305 featuredigittimeout = 5000;
307 /* Mark all profiles as inactive for the moment */
308 AST_LIST_TRAVERSE(&followmes, f, entry) {
311 featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
313 if (!ast_strlen_zero(featuredigittostr)) {
314 if (!sscanf(featuredigittostr, "%d", &featuredigittimeout))
315 featuredigittimeout = 5000;
318 takecallstr = ast_variable_retrieve(cfg, "general", "takecall");
319 if (!ast_strlen_zero(takecallstr))
320 ast_copy_string(takecall, takecallstr, sizeof(takecall));
322 declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall");
323 if (!ast_strlen_zero(declinecallstr))
324 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
326 tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt");
327 if (!ast_strlen_zero(tmpstr))
328 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
330 tmpstr = ast_variable_retrieve(cfg, "general", "followme-norecording-prompt");
331 if (!ast_strlen_zero(tmpstr))
332 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
334 tmpstr = ast_variable_retrieve(cfg, "general", "followme-options-prompt");
335 if (!ast_strlen_zero(tmpstr))
336 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
338 tmpstr = ast_variable_retrieve(cfg, "general", "followme-pls-hold-prompt");
339 if (!ast_strlen_zero(tmpstr))
340 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
342 tmpstr = ast_variable_retrieve(cfg, "general", "followme-status-prompt");
343 if (!ast_strlen_zero(tmpstr))
344 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
346 tmpstr = ast_variable_retrieve(cfg, "general", "followme-sorry-prompt");
347 if (!ast_strlen_zero(tmpstr))
348 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
350 /* Chug through config file */
351 while ((cat = ast_category_browse(cfg, cat))) {
352 if (!strcasecmp(cat, "general"))
354 /* Define a new profile */
355 /* Look for an existing one */
356 AST_LIST_TRAVERSE(&followmes, f, entry) {
357 if (!strcasecmp(f->name, cat))
361 ast_log(LOG_DEBUG, "New profile %s.\n", cat);
364 f = alloc_profile(cat);
371 ast_mutex_lock(&f->lock);
372 /* Re-initialize the profile */
375 var = ast_variable_browse(cfg, cat);
377 if (!strcasecmp(var->name, "number")) {
378 /* Add a new number */
379 ast_copy_string(numberstr, var->value, sizeof(numberstr));
380 if ((tmp = strchr(numberstr, ','))) {
383 timeoutstr = ast_strdupa(tmp);
384 if ((tmp = strchr(timeoutstr, ','))) {
387 numorder = atoi(tmp);
392 timeout = atoi(timeoutstr);
402 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
406 cur = create_followme_number(numberstr, timeout, numorder);
407 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
409 profile_set_param(f, var->name, var->value, var->lineno, 1);
411 ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
414 } /* End while(var) loop */
417 ast_mutex_unlock(&f->lock);
419 AST_LIST_INSERT_HEAD(&followmes, f, entry);
423 ast_config_destroy(cfg);
425 AST_LIST_UNLOCK(&followmes);
430 static void clear_caller(struct findme_user *tmpuser)
432 struct ast_channel *outbound;
434 if (tmpuser && tmpuser->ochan && tmpuser->state >= 0) {
435 outbound = tmpuser->ochan;
436 if (!outbound->cdr) {
437 outbound->cdr = ast_cdr_alloc();
439 ast_cdr_init(outbound->cdr, outbound);
443 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", tmpuser->dialarg);
444 ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
445 ast_cdr_update(outbound);
446 ast_cdr_start(outbound->cdr);
447 ast_cdr_end(outbound->cdr);
448 /* If the cause wasn't handled properly */
449 if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
450 ast_cdr_failed(outbound->cdr);
452 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
453 ast_hangup(tmpuser->ochan);
458 static void clear_calling_tree(struct findme_user_listptr *findme_user_list)
460 struct findme_user *tmpuser;
462 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
463 clear_caller(tmpuser);
464 tmpuser->cleared = 1;
471 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)
473 struct ast_channel *watchers[256];
475 struct ast_channel *winner;
479 struct findme_user *tmpuser;
481 int livechannels = 0;
483 long totalwait = 0, wtd, towas = 0;
485 char *pressbuttonname;
487 /* ------------ wait_for_winner_channel start --------------- */
489 callfromname = ast_strdupa(tpargs->callfromprompt);
490 pressbuttonname = ast_strdupa(tpargs->optionsprompt);
492 if (!AST_LIST_EMPTY(findme_user_list))
495 if (option_verbose > 2)
496 ast_verbose(VERBOSE_PREFIX_3 "Original caller hungup. Cleanup.\n");
497 clear_calling_tree(findme_user_list);
501 totalwait = nm->timeout * 1000;
507 watchers[0] = caller;
511 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
512 if (tmpuser->state >= 0 && tmpuser->ochan) {
513 if (tmpuser->state == 3) {
514 tmpuser->digts += (towas - wtd);
516 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
517 if (option_verbose > 2)
518 ast_verbose(VERBOSE_PREFIX_3 "We've been waiting for digits longer than we should have.\n");
519 if (!ast_strlen_zero(namerecloc)) {
522 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
523 ast_sched_runq(tmpuser->ochan->sched);
525 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
531 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
532 ast_sched_runq(tmpuser->ochan->sched);
534 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
539 if (tmpuser->ochan->stream) {
540 ast_sched_runq(tmpuser->ochan->sched);
541 tmpto = ast_sched_wait(tmpuser->ochan->sched);
542 if (tmpto > 0 && tmpto < to)
544 else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
545 ast_stopstream(tmpuser->ochan);
546 if (tmpuser->state == 1) {
547 if (option_verbose > 2)
548 ast_verbose(VERBOSE_PREFIX_3 "Playback of the call-from file appears to be done.\n");
549 if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
552 ast_log(LOG_NOTICE, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc);
553 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
555 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
558 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
562 } else if (tmpuser->state == 2) {
563 if (option_verbose > 2)
564 ast_verbose(VERBOSE_PREFIX_3 "Playback of name file appears to be done.\n");
565 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
567 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
573 } else if (tmpuser->state == 3) {
574 if (option_verbose > 2)
575 ast_verbose(VERBOSE_PREFIX_3 "Playback of the next step file appears to be done.\n");
580 watchers[pos++] = tmpuser->ochan;
591 winner = ast_waitfor_n(watchers, pos, &to);
595 if (totalwait <= 0) {
596 if (option_verbose > 2)
597 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);
598 clear_calling_tree(findme_user_list);
602 /* Need to find out which channel this is */
604 while ((winner != watchers[dg]) && (dg < 256))
606 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
607 if (tmpuser->ochan == winner)
609 f = ast_read(winner);
611 if (f->frametype == AST_FRAME_CONTROL) {
612 switch(f->subclass) {
613 case AST_CONTROL_HANGUP:
614 if (option_verbose > 2)
615 ast_verbose( VERBOSE_PREFIX_3 "%s received a hangup frame.\n", winner->name);
617 if (option_verbose > 2)
618 ast_verbose( VERBOSE_PREFIX_3 "The calling channel hungup. Need to drop everyone else.\n");
619 clear_calling_tree(findme_user_list);
623 case AST_CONTROL_ANSWER:
624 if (option_verbose > 2)
625 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", winner->name, caller->name);
626 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
627 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
628 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
629 if (option_verbose > 2)
630 ast_verbose( VERBOSE_PREFIX_3 "Starting playback of %s\n", callfromname);
632 if (!ast_strlen_zero(namerecloc)) {
633 if (!ast_streamfile(winner, callfromname, winner->language)) {
634 ast_sched_runq(winner->sched);
637 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
643 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
644 ast_sched_runq(tmpuser->ochan->sched);
646 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
653 case AST_CONTROL_BUSY:
654 if (option_verbose > 2)
655 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", winner->name);
657 case AST_CONTROL_CONGESTION:
658 if (option_verbose > 2)
659 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", winner->name);
661 case AST_CONTROL_RINGING:
662 if (option_verbose > 2)
663 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", winner->name);
665 case AST_CONTROL_PROGRESS:
666 if (option_verbose > 2)
667 ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", winner->name,caller->name);
669 case AST_CONTROL_VIDUPDATE:
670 if (option_verbose > 2)
671 ast_verbose ( VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", winner->name,caller->name);
673 case AST_CONTROL_PROCEEDING:
674 if (option_verbose > 2)
675 ast_verbose ( VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", winner->name,caller->name);
677 case AST_CONTROL_HOLD:
678 if (option_verbose > 2)
679 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", winner->name);
681 case AST_CONTROL_UNHOLD:
682 if (option_verbose > 2)
683 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", winner->name);
685 case AST_CONTROL_OFFHOOK:
686 case AST_CONTROL_FLASH:
687 /* Ignore going off hook and flash */
690 if (option_verbose > 2)
691 ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
695 ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
699 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
701 ast_stopstream(winner);
704 ast_log(LOG_DEBUG, "DTMF received: %c\n",(char) f->subclass);
705 tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
708 ast_log(LOG_DEBUG, "DTMF string: %s\n", tmpuser->yn);
709 if (tmpuser->ynidx >= ynlongest) {
711 ast_log(LOG_DEBUG, "reached longest possible match - doing evals\n");
712 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
714 ast_log(LOG_DEBUG, "Match to take the call!\n");
716 return tmpuser->ochan;
718 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
720 ast_log(LOG_DEBUG, "Next in dial plan step requested.\n");
733 ast_log(LOG_DEBUG, "we didn't get a frame. hanging up. dg is %d\n",dg);
735 clear_calling_tree(findme_user_list);
742 ast_log(LOG_DEBUG, "live channels left %d\n", livechannels);
744 if (option_verbose > 2)
745 ast_verbose(VERBOSE_PREFIX_3 "no live channels left. exiting.\n");
754 ast_log(LOG_DEBUG, "timed out waiting for action\n");
760 if (option_verbose > 2)
761 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
764 /* --- WAIT FOR WINNER NUMBER END! -----------*/
768 static void findmeexec(struct fm_args *tpargs)
771 struct ast_channel *outbound;
772 struct ast_channel *caller;
773 struct ast_channel *winner = NULL;
777 struct findme_user *tmpuser;
778 struct findme_user *fmuser;
779 struct findme_user *headuser;
780 struct findme_user_listptr *findme_user_list;
783 findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
784 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
786 /* We're going to figure out what the longest possible string of digits to collect is */
788 if (strlen(tpargs->takecall) > ynlongest)
789 ynlongest = strlen(tpargs->takecall);
790 if (strlen(tpargs->nextindp) > ynlongest)
791 ynlongest = strlen(tpargs->nextindp);
794 caller = tpargs->chan;
795 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
796 if (nm->order == idx)
802 ast_log(LOG_DEBUG, "Number %s timeout %ld\n",nm->number,nm->timeout);
805 number = ast_strdupa(nm->number);
807 ast_log(LOG_DEBUG, "examining %s\n", number);
809 rest = strchr(number, '&');
815 if (!strcmp(tpargs->context, ""))
816 sprintf(dialarg, "%s", number);
818 sprintf(dialarg, "%s@%s", number, tpargs->context);
820 tmpuser = ast_calloc(1, sizeof(*tmpuser));
822 ast_log(LOG_WARNING, "Out of memory!\n");
826 outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
828 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
829 ast_channel_inherit_variables(tpargs->chan, outbound);
830 if (option_verbose > 2)
831 ast_verbose(VERBOSE_PREFIX_3 "calling %s\n", dialarg);
832 if (!ast_call(outbound,dialarg,0)) {
833 tmpuser->ochan = outbound;
835 tmpuser->cleared = 0;
836 ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
837 AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
839 if (option_verbose > 2)
840 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
842 if (!outbound->cdr) {
843 outbound->cdr = ast_cdr_alloc();
847 ast_cdr_init(outbound->cdr, outbound);
848 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
849 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
850 ast_cdr_update(outbound);
851 ast_cdr_start(outbound->cdr);
852 ast_cdr_end(outbound->cdr);
853 /* If the cause wasn't handled properly */
854 if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
855 ast_cdr_failed(outbound->cdr);
857 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
858 ast_hangup(outbound);
865 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
871 if (!AST_LIST_EMPTY(findme_user_list))
872 winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
875 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list, fmuser, entry) {
876 if (!fmuser->cleared && fmuser->ochan != winner)
877 clear_caller(fmuser);
878 AST_LIST_REMOVE_CURRENT(findme_user_list, entry);
881 AST_LIST_TRAVERSE_SAFE_END
894 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
895 if (nm->order == idx)
899 free(findme_user_list);
903 tpargs->status = 100;
904 tpargs->outbound = winner;
912 static int app_exec(struct ast_channel *chan, void *data)
914 struct fm_args targs;
915 struct ast_bridge_config config;
916 struct ast_call_followme *f;
917 struct number *nm, *newnm;
921 char namerecloc[255];
923 struct ast_channel *caller;
924 struct ast_channel *outbound;
926 AST_DECLARE_APP_ARGS(args,
927 AST_APP_ARG(followmeid);
928 AST_APP_ARG(options);
931 if (!(argstr = ast_strdupa((char *)data))) {
932 ast_log(LOG_ERROR, "Out of memory!\n");
937 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n",app);
943 AST_STANDARD_APP_ARGS(args, argstr);
945 if (!ast_strlen_zero(args.followmeid))
947 AST_LIST_LOCK(&followmes);
948 AST_LIST_TRAVERSE(&followmes, f, entry) {
949 if (!strcasecmp(f->name, args.followmeid) && (f->active))
952 AST_LIST_UNLOCK(&followmes);
955 ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
958 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.", args.followmeid);
964 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
968 ast_app_parse_options(followme_opts, &targs.followmeflags, NULL, args.options);
971 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
972 ast_mutex_lock(&f->lock);
973 targs.mohclass = ast_strdupa(f->moh);
974 ast_copy_string(targs.context, f->context, sizeof(targs.context));
975 ast_copy_string(targs.takecall, f->takecall, sizeof(targs.takecall));
976 ast_copy_string(targs.nextindp, f->nextindp, sizeof(targs.nextindp));
977 ast_copy_string(targs.callfromprompt, f->callfromprompt, sizeof(targs.callfromprompt));
978 ast_copy_string(targs.norecordingprompt, f->norecordingprompt, sizeof(targs.norecordingprompt));
979 ast_copy_string(targs.optionsprompt, f->optionsprompt, sizeof(targs.optionsprompt));
980 ast_copy_string(targs.plsholdprompt, f->plsholdprompt, sizeof(targs.plsholdprompt));
981 ast_copy_string(targs.statusprompt, f->statusprompt, sizeof(targs.statusprompt));
982 ast_copy_string(targs.sorryprompt, f->sorryprompt, sizeof(targs.sorryprompt));
983 /* Copy the numbers we're going to use into another list in case the master list should get modified
984 (and locked) while we're trying to do a follow-me */
985 AST_LIST_HEAD_INIT_NOLOCK(&targs.cnumbers);
986 AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
987 newnm = create_followme_number(nm->number, nm->timeout, nm->order);
988 AST_LIST_INSERT_TAIL(&targs.cnumbers, newnm, entry);
990 ast_mutex_unlock(&f->lock);
992 if (targs.followmeflags.flags & FOLLOWMEFLAG_STATUSMSG)
993 ast_stream_and_wait(chan, targs.statusprompt, chan->language, "");
995 snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
998 if (targs.followmeflags.flags & FOLLOWMEFLAG_RECORDNAME)
999 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL) < 0)
1002 /* The following call looks like we're going to playback the file, but we're actually */
1003 /* just checking to see if we *can* play it. */
1004 if (ast_streamfile(chan, namerecloc, chan->language))
1005 ast_copy_string(namerecloc, "", sizeof(namerecloc));
1007 ast_stopstream(chan);
1009 if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1011 if (ast_waitstream(chan, "") < 0)
1013 ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1019 ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1023 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs.cnumbers, nm, entry) {
1024 AST_LIST_REMOVE_CURRENT(&targs.cnumbers, entry);
1027 AST_LIST_TRAVERSE_SAFE_END
1029 if (!ast_strlen_zero(namerecloc))
1032 if (targs.status != 100)
1035 if (targs.followmeflags.flags & FOLLOWMEFLAG_UNREACHABLEMSG)
1036 ast_stream_and_wait(chan, targs.sorryprompt, chan->language, "");
1043 outbound = targs.outbound;
1044 /* Bridge the two channels. */
1046 memset(&config,0,sizeof(struct ast_bridge_config));
1047 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1048 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1049 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1051 ast_moh_stop(caller);
1052 /* Be sure no generators are left on it */
1053 ast_deactivate_generator(caller);
1054 /* Make sure channels are compatible */
1055 res = ast_channel_make_compatible(caller, outbound);
1057 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1058 ast_hangup(outbound);
1062 res = ast_bridge_call(caller,outbound,&config);
1064 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1065 pbx_builtin_setvar_helper(caller, "DIALEDTIME", toast);
1066 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1067 pbx_builtin_setvar_helper(caller, "ANSWEREDTIME", toast);
1069 ast_hangup(outbound);
1077 LOCAL_USER_REMOVE(u);
1081 static int unload_module(void *mod)
1083 struct ast_call_followme *f;
1084 STANDARD_HANGUP_LOCALUSERS;
1085 ast_unregister_application(app);
1086 /* Free Memory. Yeah! I'm free! */
1087 AST_LIST_LOCK(&followmes);
1088 AST_LIST_TRAVERSE_SAFE_BEGIN(&followmes, f, entry) {
1090 AST_LIST_REMOVE_CURRENT(&followmes, entry);
1093 AST_LIST_TRAVERSE_SAFE_END
1094 AST_LIST_UNLOCK(&followmes);
1098 static int load_module(void *mod)
1101 return ast_register_application(app, app_exec, synopsis, descrip);
1104 static const char *description(void)
1106 return "Find-Me/Follow-Me Application";
1109 static int reload(void *mod)
1115 static const char *key(void)
1117 return ASTERISK_GPL_KEY;
1120 STD_MOD(MOD_1, reload, NULL, NULL);