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
32 <depend>chan_local</depend>
33 <support_level>core</support_level>
38 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
42 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
43 #include "asterisk/lock.h"
44 #include "asterisk/file.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/pbx.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/utils.h"
56 #include "asterisk/causes.h"
57 #include "asterisk/astdb.h"
58 #include "asterisk/dsp.h"
59 #include "asterisk/app.h"
62 <application name="FollowMe" language="en_US">
64 Find-Me/Follow-Me application.
67 <parameter name="followmeid" required="true" />
68 <parameter name="options">
71 <para>Record the caller's name so it can be announced to the
72 callee on each step.</para>
75 <para>Disable the 'Please hold while we try to connect your call' announcement.</para>
78 <para>Asterisk will ignore any connected line update requests
79 it may receive on this dial attempt.</para>
82 <para>Disable local call optimization so that applications with
83 audio hooks between the local bridge don't get dropped when the
84 calls get joined directly.</para>
87 <para>Don't answer the incoming call until we're ready to
88 connect the caller or give up.</para>
90 <para>This option is ignored if the call is already answered.</para>
93 <para>If the call is not already answered, the 'a' and 's'
94 options are ignored while the 'd' option is implicitly enabled.</para>
98 <para>Playback the unreachable status message if we've run out
99 of steps or the callee has elected not to be reachable.</para>
102 <para>Playback the incoming status message prior to starting
103 the follow-me step(s)</para>
109 <para>This application performs Find-Me/Follow-Me functionality for the caller
110 as defined in the profile matching the <replaceable>followmeid</replaceable> parameter in
111 <filename>followme.conf</filename>. If the specified <replaceable>followmeid</replaceable>
112 profile doesn't exist in <filename>followme.conf</filename>, execution will be returned
113 to the dialplan and call execution will continue at the next priority.</para>
114 <para>Returns -1 on hangup.</para>
119 static char *app = "FollowMe";
121 /*! Maximum accept/decline DTMF string plus terminator. */
122 #define MAX_YN_STRING 20
124 /*! \brief Number structure */
126 char number[512]; /*!< Phone Number(s) and/or Extension(s) */
127 long timeout; /*!< Dial Timeout, if used. */
128 int order; /*!< The order to dial in */
129 AST_LIST_ENTRY(number) entry; /*!< Next Number record */
132 /*! \brief Data structure for followme scripts */
133 struct call_followme {
135 char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
136 char moh[MAX_MUSICCLASS]; /*!< Music On Hold Class to be used */
137 char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
138 unsigned int active; /*!< Profile is active (1), or disabled (0). */
139 int realtime; /*!< Cached from realtime */
140 char takecall[MAX_YN_STRING]; /*!< Digit mapping to take a call */
141 char nextindp[MAX_YN_STRING]; /*!< Digit mapping to decline a call */
142 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
143 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
144 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
145 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
146 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
147 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
149 AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
150 AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
151 AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
152 AST_LIST_ENTRY(call_followme) entry; /*!< Next Follow-Me record */
157 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
158 /*! Accumulated connected line information from inbound call. */
159 struct ast_party_connected_line connected_in;
160 /*! Accumulated connected line information from outbound call. */
161 struct ast_party_connected_line connected_out;
162 /*! TRUE if connected line information from inbound call changed. */
163 unsigned int pending_in_connected_update:1;
164 /*! TRUE if connected line information from outbound call is available. */
165 unsigned int pending_out_connected_update:1;
166 char context[AST_MAX_CONTEXT];
167 char namerecloc[PATH_MAX];
168 char takecall[MAX_YN_STRING]; /*!< Digit mapping to take a call */
169 char nextindp[MAX_YN_STRING]; /*!< Digit mapping to decline a call */
170 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
171 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
172 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
173 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
174 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
175 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
176 struct ast_flags followmeflags;
180 struct ast_channel *ochan;
181 /*! Accumulated connected line information from outgoing call. */
182 struct ast_party_connected_line connected;
187 /*! Collected digits to accept/decline the call. */
188 char yn[MAX_YN_STRING];
189 /*! TRUE if connected line information is available. */
190 unsigned int pending_connected_update:1;
191 AST_LIST_ENTRY(findme_user) entry;
195 FOLLOWMEFLAG_STATUSMSG = (1 << 0),
196 FOLLOWMEFLAG_RECORDNAME = (1 << 1),
197 FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2),
198 FOLLOWMEFLAG_DISABLEHOLDPROMPT = (1 << 3),
199 FOLLOWMEFLAG_NOANSWER = (1 << 4),
200 FOLLOWMEFLAG_DISABLEOPTIMIZATION = (1 << 5),
201 FOLLOWMEFLAG_IGNORE_CONNECTEDLINE = (1 << 6),
204 AST_APP_OPTIONS(followme_opts, {
205 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME),
206 AST_APP_OPTION('d', FOLLOWMEFLAG_DISABLEHOLDPROMPT),
207 AST_APP_OPTION('I', FOLLOWMEFLAG_IGNORE_CONNECTEDLINE),
208 AST_APP_OPTION('l', FOLLOWMEFLAG_DISABLEOPTIMIZATION),
209 AST_APP_OPTION('N', FOLLOWMEFLAG_NOANSWER),
210 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG),
211 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG),
214 static const char *featuredigittostr;
215 static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
216 static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
218 static char takecall[MAX_YN_STRING] = "1";
219 static char nextindp[MAX_YN_STRING] = "2";
220 static char callfromprompt[PATH_MAX] = "followme/call-from";
221 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
222 static char optionsprompt[PATH_MAX] = "followme/options";
223 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
224 static char statusprompt[PATH_MAX] = "followme/status";
225 static char sorryprompt[PATH_MAX] = "followme/sorry";
228 static AST_RWLIST_HEAD_STATIC(followmes, call_followme);
229 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
231 static void free_numbers(struct call_followme *f)
233 /* Free numbers attached to the profile */
236 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
237 /* Free the number */
239 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
241 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
242 /* Free the blacklisted number */
244 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
246 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
247 /* Free the whitelisted number */
249 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
253 /*! \brief Allocate and initialize followme profile */
254 static struct call_followme *alloc_profile(const char *fmname)
256 struct call_followme *f;
258 if (!(f = ast_calloc(1, sizeof(*f))))
261 ast_mutex_init(&f->lock);
262 ast_copy_string(f->name, fmname, sizeof(f->name));
264 f->context[0] = '\0';
265 ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
266 ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
267 ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
268 ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
269 ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
270 ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
271 ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
272 ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
273 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
274 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
275 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
279 static void init_profile(struct call_followme *f)
282 ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
287 /*! \brief Set parameter in profile from configuration file */
288 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
291 if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
292 ast_copy_string(f->moh, val, sizeof(f->moh));
293 else if (!strcasecmp(param, "context"))
294 ast_copy_string(f->context, val, sizeof(f->context));
295 else if (!strcasecmp(param, "takecall"))
296 ast_copy_string(f->takecall, val, sizeof(f->takecall));
297 else if (!strcasecmp(param, "declinecall"))
298 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
299 else if (!strcasecmp(param, "call-from-prompt") || !strcasecmp(param, "call_from_prompt"))
300 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
301 else if (!strcasecmp(param, "followme-norecording-prompt") || !strcasecmp(param, "norecording_prompt"))
302 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
303 else if (!strcasecmp(param, "followme-options-prompt") || !strcasecmp(param, "options_prompt"))
304 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
305 else if (!strcasecmp(param, "followme-pls-hold-prompt") || !strcasecmp(param, "pls_hold_prompt"))
306 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
307 else if (!strcasecmp(param, "followme-status-prompt") || !strcasecmp(param, "status_prompt"))
308 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
309 else if (!strcasecmp(param, "followme-sorry-prompt") || !strcasecmp(param, "sorry_prompt"))
310 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
311 else if (failunknown) {
313 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
315 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
319 /*! \brief Add a new number */
320 static struct number *create_followme_number(const char *number, int timeout, int numorder)
323 char *buf = ast_strdupa(number);
326 if (!(cur = ast_calloc(1, sizeof(*cur))))
329 cur->timeout = timeout;
330 if ((tmp = strchr(buf, ',')))
332 ast_copy_string(cur->number, buf, sizeof(cur->number));
333 cur->order = numorder;
334 ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
339 /*! \brief Reload followme application module */
340 static int reload_followme(int reload)
342 struct call_followme *f;
343 struct ast_config *cfg;
344 char *cat = NULL, *tmp;
345 struct ast_variable *var;
346 struct number *cur, *nm;
350 const char *takecallstr;
351 const char *declinecallstr;
353 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
355 if (!(cfg = ast_config_load("followme.conf", config_flags))) {
356 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
358 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
360 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
361 ast_log(LOG_ERROR, "Config file followme.conf is in an invalid format. Aborting.\n");
365 AST_RWLIST_WRLOCK(&followmes);
367 /* Reset Global Var Values */
368 featuredigittimeout = 5000;
370 /* Mark all profiles as inactive for the moment */
371 AST_RWLIST_TRAVERSE(&followmes, f, entry) {
375 featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
377 if (!ast_strlen_zero(featuredigittostr)) {
378 if (!sscanf(featuredigittostr, "%30d", &featuredigittimeout))
379 featuredigittimeout = 5000;
382 if ((takecallstr = ast_variable_retrieve(cfg, "general", "takecall")) && !ast_strlen_zero(takecallstr)) {
383 ast_copy_string(takecall, takecallstr, sizeof(takecall));
386 if ((declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall")) && !ast_strlen_zero(declinecallstr)) {
387 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
390 if ((tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt")) && !ast_strlen_zero(tmpstr)) {
391 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
392 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "call_from_prompt")) && !ast_strlen_zero(tmpstr)) {
393 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
396 if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt")) && !ast_strlen_zero(tmpstr)) {
397 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
398 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "norecording_prompt")) && !ast_strlen_zero(tmpstr)) {
399 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
403 if ((tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt")) && !ast_strlen_zero(tmpstr)) {
404 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
405 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "options_prompt")) && !ast_strlen_zero(tmpstr)) {
406 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
409 if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt")) && !ast_strlen_zero(tmpstr)) {
410 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
411 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "pls_hold_prompt")) && !ast_strlen_zero(tmpstr)) {
412 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
415 if ((tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt")) && !ast_strlen_zero(tmpstr)) {
416 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
417 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "status_prompt")) && !ast_strlen_zero(tmpstr)) {
418 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
421 if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt")) && !ast_strlen_zero(tmpstr)) {
422 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
423 } else if ((tmpstr = ast_variable_retrieve(cfg, "general", "sorry_prompt")) && !ast_strlen_zero(tmpstr)) {
424 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
427 /* Chug through config file */
428 while ((cat = ast_category_browse(cfg, cat))) {
431 if (!strcasecmp(cat, "general"))
434 /* Look for an existing one */
435 AST_LIST_TRAVERSE(&followmes, f, entry) {
436 if (!strcasecmp(f->name, cat))
440 ast_debug(1, "New profile %s.\n", cat);
444 f = alloc_profile(cat);
448 /* Totally fail if we fail to find/create an entry */
453 ast_mutex_lock(&f->lock);
454 /* Re-initialize the profile */
457 var = ast_variable_browse(cfg, cat);
459 if (!strcasecmp(var->name, "number")) {
462 /* Add a new number */
463 ast_copy_string(numberstr, var->value, sizeof(numberstr));
464 if ((tmp = strchr(numberstr, ','))) {
470 if ((tmp = strchr(tmp, ','))) {
472 numorder = atoi(tmp);
484 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
488 cur = create_followme_number(numberstr, timeout, numorder);
490 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
493 profile_set_param(f, var->name, var->value, var->lineno, 1);
494 ast_debug(2, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
497 } /* End while(var) loop */
500 ast_mutex_unlock(&f->lock);
502 AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
505 ast_config_destroy(cfg);
507 AST_RWLIST_UNLOCK(&followmes);
512 static void clear_caller(struct findme_user *tmpuser)
514 struct ast_channel *outbound;
516 if (!tmpuser->ochan) {
517 /* Call already cleared. */
521 outbound = tmpuser->ochan;
522 ast_channel_lock(outbound);
523 if (!ast_channel_cdr(outbound)) {
524 ast_channel_cdr_set(outbound, ast_cdr_alloc());
525 if (ast_channel_cdr(outbound)) {
526 ast_cdr_init(ast_channel_cdr(outbound), outbound);
529 if (ast_channel_cdr(outbound)) {
532 snprintf(tmp, sizeof(tmp), "Local/%s", tmpuser->dialarg);
533 ast_cdr_setapp(ast_channel_cdr(outbound), "FollowMe", tmp);
534 ast_cdr_update(outbound);
535 ast_cdr_start(ast_channel_cdr(outbound));
536 ast_cdr_end(ast_channel_cdr(outbound));
537 /* If the cause wasn't handled properly */
538 if (ast_cdr_disposition(ast_channel_cdr(outbound), ast_channel_hangupcause(outbound))) {
539 ast_cdr_failed(ast_channel_cdr(outbound));
542 ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
544 ast_channel_unlock(outbound);
545 ast_hangup(outbound);
546 tmpuser->ochan = NULL;
549 static void clear_calling_tree(struct findme_user_listptr *findme_user_list)
551 struct findme_user *tmpuser;
553 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
554 clear_caller(tmpuser);
558 static void destroy_calling_node(struct findme_user *node)
561 ast_party_connected_line_free(&node->connected);
565 static void destroy_calling_tree(struct findme_user_listptr *findme_user_list)
567 struct findme_user *fmuser;
569 while ((fmuser = AST_LIST_REMOVE_HEAD(findme_user_list, entry))) {
570 destroy_calling_node(fmuser);
574 static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, struct fm_args *tpargs)
576 struct ast_party_connected_line connected;
577 struct ast_channel *watchers[256];
579 struct ast_channel *winner;
581 struct findme_user *tmpuser;
585 long totalwait = 0, wtd = 0, towas = 0;
587 char *pressbuttonname;
589 /* ------------ wait_for_winner_channel start --------------- */
591 callfromname = ast_strdupa(tpargs->callfromprompt);
592 pressbuttonname = ast_strdupa(tpargs->optionsprompt);
594 totalwait = nm->timeout * 1000;
600 watchers[0] = caller;
603 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
604 if (!tmpuser->ochan) {
607 if (tmpuser->state == 3) {
608 tmpuser->digts += (towas - wtd);
610 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
611 ast_verb(3, "<%s> We've been waiting for digits longer than we should have.\n",
612 ast_channel_name(tmpuser->ochan));
613 if (!ast_strlen_zero(tpargs->namerecloc)) {
616 if (!ast_streamfile(tmpuser->ochan, callfromname, ast_channel_language(tmpuser->ochan))) {
617 ast_sched_runq(ast_channel_sched(tmpuser->ochan));
619 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
620 clear_caller(tmpuser);
626 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
627 ast_sched_runq(ast_channel_sched(tmpuser->ochan));
629 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
630 clear_caller(tmpuser);
635 if (ast_channel_stream(tmpuser->ochan)) {
636 ast_sched_runq(ast_channel_sched(tmpuser->ochan));
637 tmpto = ast_sched_wait(ast_channel_sched(tmpuser->ochan));
638 if (tmpto > 0 && tmpto < to)
640 else if (tmpto < 0 && !ast_channel_timingfunc(tmpuser->ochan)) {
641 ast_stopstream(tmpuser->ochan);
642 switch (tmpuser->state) {
644 ast_verb(3, "<%s> Playback of the call-from file appears to be done.\n",
645 ast_channel_name(tmpuser->ochan));
646 if (!ast_streamfile(tmpuser->ochan, tpargs->namerecloc, ast_channel_language(tmpuser->ochan))) {
649 ast_log(LOG_NOTICE, "<%s> Unable to playback %s. Maybe the caller didn't record their name?\n",
650 ast_channel_name(tmpuser->ochan), tpargs->namerecloc);
651 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
653 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan)))
656 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
657 clear_caller(tmpuser);
663 ast_verb(3, "<%s> Playback of name file appears to be done.\n",
664 ast_channel_name(tmpuser->ochan));
665 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
667 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, ast_channel_language(tmpuser->ochan))) {
670 clear_caller(tmpuser);
675 ast_verb(3, "<%s> Playback of the next step file appears to be done.\n",
676 ast_channel_name(tmpuser->ochan));
684 watchers[pos++] = tmpuser->ochan;
688 ast_verb(3, "No live channels left for this step.\n");
698 winner = ast_waitfor_n(watchers, pos, &to);
702 if (totalwait <= 0) {
703 ast_verb(3, "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
704 clear_calling_tree(findme_user_list);
708 /* Need to find out which channel this is */
709 if (winner != caller) {
710 /* The winner is an outgoing channel. */
711 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
712 if (tmpuser->ochan == winner) {
720 f = ast_read(winner);
722 if (f->frametype == AST_FRAME_CONTROL) {
723 switch (f->subclass.integer) {
724 case AST_CONTROL_HANGUP:
725 ast_verb(3, "%s received a hangup frame.\n", ast_channel_name(winner));
726 if (f->data.uint32) {
727 ast_channel_hangupcause_set(winner, f->data.uint32);
730 ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
734 clear_caller(tmpuser);
736 case AST_CONTROL_ANSWER:
738 /* The caller answered? We want an outgoing channel to answer. */
741 ast_verb(3, "%s answered %s\n", ast_channel_name(winner), ast_channel_name(caller));
742 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
743 ast_channel_hangupcause_set(winner, AST_CAUSE_NORMAL_CLEARING);
744 ast_channel_hangupcause_set(caller, AST_CAUSE_NORMAL_CLEARING);
745 ast_verb(3, "Starting playback of %s\n", callfromname);
746 if (!ast_strlen_zero(tpargs->namerecloc)) {
747 if (!ast_streamfile(winner, callfromname, ast_channel_language(winner))) {
748 ast_sched_runq(ast_channel_sched(winner));
751 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
752 clear_caller(tmpuser);
756 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, ast_channel_language(tmpuser->ochan)))
757 ast_sched_runq(ast_channel_sched(tmpuser->ochan));
759 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
760 clear_caller(tmpuser);
764 case AST_CONTROL_BUSY:
765 ast_verb(3, "%s is busy\n", ast_channel_name(winner));
767 /* Outbound call was busy. Drop it. */
768 clear_caller(tmpuser);
771 case AST_CONTROL_CONGESTION:
772 ast_verb(3, "%s is circuit-busy\n", ast_channel_name(winner));
774 /* Outbound call was congested. Drop it. */
775 clear_caller(tmpuser);
778 case AST_CONTROL_RINGING:
779 ast_verb(3, "%s is ringing\n", ast_channel_name(winner));
781 case AST_CONTROL_PROGRESS:
782 ast_verb(3, "%s is making progress\n", ast_channel_name(winner));
784 case AST_CONTROL_VIDUPDATE:
785 ast_verb(3, "%s requested a video update\n", ast_channel_name(winner));
787 case AST_CONTROL_SRCUPDATE:
788 ast_verb(3, "%s requested a source update\n", ast_channel_name(winner));
790 case AST_CONTROL_PROCEEDING:
791 ast_verb(3, "%s is proceeding\n", ast_channel_name(winner));
793 case AST_CONTROL_HOLD:
794 ast_verb(3, "%s placed call on hold\n", ast_channel_name(winner));
796 case AST_CONTROL_UNHOLD:
797 ast_verb(3, "%s removed call from hold\n", ast_channel_name(winner));
799 case AST_CONTROL_OFFHOOK:
800 case AST_CONTROL_FLASH:
801 /* Ignore going off hook and flash */
803 case AST_CONTROL_CONNECTED_LINE:
806 * Hold connected line update from caller until we have a
810 "%s connected line has changed. Saving it until we have a winner.\n",
811 ast_channel_name(winner));
812 ast_party_connected_line_set_init(&connected, &tpargs->connected_in);
813 if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
814 ast_party_connected_line_set(&tpargs->connected_in,
816 tpargs->pending_in_connected_update = 1;
818 ast_party_connected_line_free(&connected);
821 if (ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_IGNORE_CONNECTEDLINE)) {
822 ast_verb(3, "Connected line update from %s prevented.\n",
823 ast_channel_name(winner));
826 "%s connected line has changed. Saving it until answer.\n",
827 ast_channel_name(winner));
828 ast_party_connected_line_set_init(&connected, &tmpuser->connected);
829 if (!ast_connected_line_parse_data(f->data.ptr, f->datalen, &connected)) {
830 ast_party_connected_line_set(&tmpuser->connected,
832 tmpuser->pending_connected_update = 1;
834 ast_party_connected_line_free(&connected);
837 case AST_CONTROL_REDIRECTING:
839 * Ignore because we are masking the FollowMe search progress to
844 ast_verb(3, "%s stopped sounds\n", ast_channel_name(winner));
847 ast_debug(1, "Dunno what to do with control type %d from %s\n",
848 f->subclass.integer, ast_channel_name(winner));
852 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
853 if (ast_channel_stream(winner))
854 ast_stopstream(winner);
856 ast_debug(1, "DTMF received: %c\n", (char) f->subclass.integer);
857 if (tmpuser->ynidx < ARRAY_LEN(tmpuser->yn) - 1) {
858 tmpuser->yn[tmpuser->ynidx++] = (char) f->subclass.integer;
860 ast_debug(1, "DTMF string: %s\n", tmpuser->yn);
861 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
862 ast_debug(1, "Match to take the call!\n");
864 return tmpuser->ochan;
866 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
867 ast_debug(1, "Declined to take the call.\n");
868 clear_caller(tmpuser);
874 ast_debug(1, "we didn't get a frame. hanging up.\n");
876 /* Caller hung up. */
877 ast_verb(3, "The calling channel hungup. Need to drop everyone.\n");
880 /* Outgoing channel hung up. */
881 clear_caller(tmpuser);
884 ast_debug(1, "timed out waiting for action\n");
893 * \brief Find an extension willing to take the call.
895 * \param tpargs Active Followme config.
896 * \param caller Channel initiating the outgoing calls.
898 * \retval winner Winning outgoing call.
899 * \retval NULL if could not find someone to take the call.
901 static struct ast_channel *findmeexec(struct fm_args *tpargs, struct ast_channel *caller)
904 struct ast_channel *winner = NULL;
908 struct findme_user *tmpuser;
909 struct findme_user *fmuser;
910 struct findme_user_listptr findme_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
911 struct findme_user_listptr new_user_list = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
913 for (idx = 1; !ast_check_hangup(caller); ++idx) {
914 /* Find next followme numbers to dial. */
915 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
916 if (nm->order == idx) {
921 ast_verb(3, "No more steps left.\n");
925 ast_debug(2, "Number(s) %s timeout %ld\n", nm->number, nm->timeout);
927 ast_copy_string(num, nm->number, sizeof(num));
928 for (number = num; number; number = rest) {
929 struct ast_channel *outbound;
931 rest = strchr(number, '&');
936 /* We check if the extension exists, before creating the ast_channel struct */
937 if (!ast_exists_extension(caller, tpargs->context, number, 1, S_COR(ast_channel_caller(caller)->id.number.valid, ast_channel_caller(caller)->id.number.str, NULL))) {
938 ast_log(LOG_ERROR, "Extension '%s@%s' doesn't exist\n", number, tpargs->context);
942 tmpuser = ast_calloc(1, sizeof(*tmpuser));
947 if (ast_strlen_zero(tpargs->context)) {
948 snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s%s",
950 ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
953 snprintf(tmpuser->dialarg, sizeof(tmpuser->dialarg), "%s@%s%s",
954 number, tpargs->context,
955 ast_test_flag(&tpargs->followmeflags, FOLLOWMEFLAG_DISABLEOPTIMIZATION)
959 outbound = ast_request("Local", ast_channel_nativeformats(caller), caller,
960 tmpuser->dialarg, &dg);
962 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n",
963 tmpuser->dialarg, ast_cause2str(dg));
968 ast_channel_lock_both(caller, outbound);
969 ast_connected_line_copy_from_caller(ast_channel_connected(outbound), ast_channel_caller(caller));
970 ast_channel_inherit_variables(caller, outbound);
971 ast_channel_datastore_inherit(caller, outbound);
972 ast_channel_language_set(outbound, ast_channel_language(caller));
973 ast_channel_accountcode_set(outbound, ast_channel_accountcode(caller));
974 ast_channel_musicclass_set(outbound, ast_channel_musicclass(caller));
975 ast_channel_unlock(outbound);
976 ast_channel_unlock(caller);
978 tmpuser->ochan = outbound;
981 ast_verb(3, "calling Local/%s\n", tmpuser->dialarg);
982 if (!ast_call(tmpuser->ochan, tmpuser->dialarg, 0)) {
983 AST_LIST_INSERT_TAIL(&new_user_list, tmpuser, entry);
985 ast_verb(3, "couldn't reach at this number.\n");
987 /* Destroy this failed new outgoing call. */
988 ast_channel_lock(tmpuser->ochan);
989 if (ast_channel_cdr(tmpuser->ochan)) {
990 ast_cdr_init(ast_channel_cdr(tmpuser->ochan), tmpuser->ochan);
992 ast_channel_unlock(tmpuser->ochan);
993 destroy_calling_node(tmpuser);
997 if (AST_LIST_EMPTY(&new_user_list)) {
998 /* No new channels remain at this order level. If there were any at all. */
1002 /* Add new outgoing channels to the findme list. */
1003 AST_LIST_APPEND_LIST(&findme_user_list, &new_user_list, entry);
1005 winner = wait_for_winner(&findme_user_list, nm, caller, tpargs);
1010 /* Destroy losing calls up to the winner. The rest will be destroyed later. */
1011 while ((fmuser = AST_LIST_REMOVE_HEAD(&findme_user_list, entry))) {
1012 if (fmuser->ochan == winner) {
1014 * Pass any connected line info up.
1016 * NOTE: This code must be in line with destroy_calling_node().
1018 tpargs->connected_out = fmuser->connected;
1019 tpargs->pending_out_connected_update = fmuser->pending_connected_update;
1023 /* Destroy losing call. */
1024 destroy_calling_node(fmuser);
1029 destroy_calling_tree(&findme_user_list);
1033 static struct call_followme *find_realtime(const char *name)
1035 struct ast_variable *var;
1036 struct ast_variable *v;
1037 struct ast_config *cfg;
1039 struct call_followme *new_follower;
1040 struct ast_str *str;
1042 str = ast_str_create(16);
1047 var = ast_load_realtime("followme", "name", name, SENTINEL);
1053 if (!(new_follower = alloc_profile(name))) {
1054 ast_variables_destroy(var);
1059 for (v = var; v; v = v->next) {
1060 if (!strcasecmp(v->name, "active")) {
1061 if (ast_false(v->value)) {
1062 ast_mutex_destroy(&new_follower->lock);
1063 ast_free(new_follower);
1064 ast_variables_destroy(var);
1069 profile_set_param(new_follower, v->name, v->value, 0, 0);
1073 ast_variables_destroy(var);
1074 new_follower->realtime = 1;
1077 cfg = ast_load_realtime_multientry("followme_numbers", "ordinal LIKE", "%", "name",
1080 ast_mutex_destroy(&new_follower->lock);
1081 ast_free(new_follower);
1086 for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
1088 const char *timeoutstr;
1093 if (!(numstr = ast_variable_retrieve(cfg, catg, "phonenumber"))) {
1096 if (!(timeoutstr = ast_variable_retrieve(cfg, catg, "timeout"))
1097 || sscanf(timeoutstr, "%30d", &timeout) != 1
1101 /* This one has to exist; it was part of the query */
1102 ordstr = ast_variable_retrieve(cfg, catg, "ordinal");
1103 ast_str_set(&str, 0, "%s", numstr);
1104 if ((cur = create_followme_number(ast_str_buffer(str), timeout, atoi(ordstr)))) {
1105 AST_LIST_INSERT_TAIL(&new_follower->numbers, cur, entry);
1108 ast_config_destroy(cfg);
1111 return new_follower;
1114 static void end_bridge_callback(void *data)
1118 struct ast_channel *chan = data;
1122 ast_channel_lock(chan);
1123 if (ast_channel_cdr(chan)->answer.tv_sec) {
1124 snprintf(buf, sizeof(buf), "%ld", (long) end - ast_channel_cdr(chan)->answer.tv_sec);
1125 pbx_builtin_setvar_helper(chan, "ANSWEREDTIME", buf);
1128 if (ast_channel_cdr(chan)->start.tv_sec) {
1129 snprintf(buf, sizeof(buf), "%ld", (long) end - ast_channel_cdr(chan)->start.tv_sec);
1130 pbx_builtin_setvar_helper(chan, "DIALEDTIME", buf);
1132 ast_channel_unlock(chan);
1135 static void end_bridge_callback_data_fixup(struct ast_bridge_config *bconfig, struct ast_channel *originator, struct ast_channel *terminator)
1137 bconfig->end_bridge_callback_data = originator;
1140 static int app_exec(struct ast_channel *chan, const char *data)
1142 struct fm_args *targs;
1143 struct ast_bridge_config config;
1144 struct call_followme *f;
1145 struct number *nm, *newnm;
1148 struct ast_channel *caller;
1149 struct ast_channel *outbound;
1150 AST_DECLARE_APP_ARGS(args,
1151 AST_APP_ARG(followmeid);
1152 AST_APP_ARG(options);
1155 if (ast_strlen_zero(data)) {
1156 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1160 argstr = ast_strdupa((char *) data);
1162 AST_STANDARD_APP_ARGS(args, argstr);
1164 if (ast_strlen_zero(args.followmeid)) {
1165 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n", app);
1169 targs = ast_calloc(1, sizeof(*targs));
1174 AST_RWLIST_RDLOCK(&followmes);
1175 AST_RWLIST_TRAVERSE(&followmes, f, entry) {
1176 if (!strcasecmp(f->name, args.followmeid) && (f->active))
1179 AST_RWLIST_UNLOCK(&followmes);
1181 ast_debug(1, "New profile %s.\n", args.followmeid);
1184 f = find_realtime(args.followmeid);
1188 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
1193 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
1195 ast_app_parse_options(followme_opts, &targs->followmeflags, NULL, args.options);
1198 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
1199 ast_mutex_lock(&f->lock);
1200 targs->mohclass = ast_strdupa(f->moh);
1201 ast_copy_string(targs->context, f->context, sizeof(targs->context));
1202 ast_copy_string(targs->takecall, f->takecall, sizeof(targs->takecall));
1203 ast_copy_string(targs->nextindp, f->nextindp, sizeof(targs->nextindp));
1204 ast_copy_string(targs->callfromprompt, f->callfromprompt, sizeof(targs->callfromprompt));
1205 ast_copy_string(targs->norecordingprompt, f->norecordingprompt, sizeof(targs->norecordingprompt));
1206 ast_copy_string(targs->optionsprompt, f->optionsprompt, sizeof(targs->optionsprompt));
1207 ast_copy_string(targs->plsholdprompt, f->plsholdprompt, sizeof(targs->plsholdprompt));
1208 ast_copy_string(targs->statusprompt, f->statusprompt, sizeof(targs->statusprompt));
1209 ast_copy_string(targs->sorryprompt, f->sorryprompt, sizeof(targs->sorryprompt));
1210 /* Copy the numbers we're going to use into another list in case the master list should get modified
1211 (and locked) while we're trying to do a follow-me */
1212 AST_LIST_HEAD_INIT_NOLOCK(&targs->cnumbers);
1213 AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
1214 newnm = create_followme_number(nm->number, nm->timeout, nm->order);
1216 AST_LIST_INSERT_TAIL(&targs->cnumbers, newnm, entry);
1219 ast_mutex_unlock(&f->lock);
1221 /* Forget the 'N' option if the call is already up. */
1222 if (ast_channel_state(chan) == AST_STATE_UP) {
1223 ast_clear_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER);
1226 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1227 ast_indicate(chan, AST_CONTROL_RINGING);
1229 /* Answer the call */
1230 if (ast_channel_state(chan) != AST_STATE_UP) {
1234 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_STATUSMSG)) {
1235 ast_stream_and_wait(chan, targs->statusprompt, "");
1238 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_RECORDNAME)) {
1241 snprintf(targs->namerecloc, sizeof(targs->namerecloc), "%s/followme.%s",
1242 ast_config_AST_SPOOL_DIR, ast_channel_uniqueid(chan));
1243 if (ast_play_and_record(chan, "vm-rec-name", targs->namerecloc, 5, "sln", &duration,
1244 NULL, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE), 0, NULL) < 0) {
1247 if (!ast_fileexists(targs->namerecloc, NULL, ast_channel_language(chan))) {
1248 targs->namerecloc[0] = '\0';
1252 if (!ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_DISABLEHOLDPROMPT)) {
1253 if (ast_streamfile(chan, targs->plsholdprompt, ast_channel_language(chan))) {
1256 if (ast_waitstream(chan, "") < 0)
1259 ast_moh_start(chan, S_OR(targs->mohclass, NULL), NULL);
1262 ast_channel_lock(chan);
1263 ast_connected_line_copy_from_caller(&targs->connected_in, ast_channel_caller(chan));
1264 ast_channel_unlock(chan);
1266 outbound = findmeexec(targs, chan);
1268 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1269 if (ast_channel_state(chan) != AST_STATE_UP) {
1276 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG)) {
1277 ast_stream_and_wait(chan, targs->sorryprompt, "");
1282 /* Bridge the two channels. */
1284 memset(&config, 0, sizeof(config));
1285 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1286 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1287 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1288 config.end_bridge_callback = end_bridge_callback;
1289 config.end_bridge_callback_data = chan;
1290 config.end_bridge_callback_data_fixup = end_bridge_callback_data_fixup;
1292 /* Update connected line to caller if available. */
1293 if (targs->pending_out_connected_update) {
1294 if (ast_channel_connected_line_sub(outbound, caller, &targs->connected_out, 0) &&
1295 ast_channel_connected_line_macro(outbound, caller, &targs->connected_out, 1, 0)) {
1296 ast_channel_update_connected_line(caller, &targs->connected_out, NULL);
1300 if (ast_test_flag(&targs->followmeflags, FOLLOWMEFLAG_NOANSWER)) {
1301 if (ast_channel_state(caller) != AST_STATE_UP) {
1305 ast_moh_stop(caller);
1308 /* Be sure no generators are left on it */
1309 ast_deactivate_generator(caller);
1310 /* Make sure channels are compatible */
1311 res = ast_channel_make_compatible(caller, outbound);
1313 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", ast_channel_name(caller), ast_channel_name(outbound));
1314 ast_hangup(outbound);
1318 /* Update connected line to winner if changed. */
1319 if (targs->pending_in_connected_update) {
1320 if (ast_channel_connected_line_sub(caller, outbound, &targs->connected_in, 0) &&
1321 ast_channel_connected_line_macro(caller, outbound, &targs->connected_in, 0, 0)) {
1322 ast_channel_update_connected_line(outbound, &targs->connected_in, NULL);
1326 res = ast_bridge_call(caller, outbound, &config);
1327 ast_hangup(outbound);
1331 while ((nm = AST_LIST_REMOVE_HEAD(&targs->cnumbers, entry))) {
1334 if (!ast_strlen_zero(targs->namerecloc)) {
1335 unlink(targs->namerecloc);
1337 ast_party_connected_line_free(&targs->connected_in);
1338 ast_party_connected_line_free(&targs->connected_out);
1350 static int unload_module(void)
1352 struct call_followme *f;
1354 ast_unregister_application(app);
1356 /* Free Memory. Yeah! I'm free! */
1357 AST_RWLIST_WRLOCK(&followmes);
1358 while ((f = AST_RWLIST_REMOVE_HEAD(&followmes, entry))) {
1363 AST_RWLIST_UNLOCK(&followmes);
1368 static int load_module(void)
1370 if(!reload_followme(0))
1371 return AST_MODULE_LOAD_DECLINE;
1373 return ast_register_application_xml(app, app_exec);
1376 static int reload(void)
1383 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1384 .load = load_module,
1385 .unload = unload_module,