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 char language
[MAX_LANGUAGE
]; /*!< The language to be used on this dial, if used. */
82 int order
; /*!< The order to dial in */
83 AST_LIST_ENTRY(number
) entry
; /*!< Next Number record */
86 /*! \brief Data structure for followme scripts */
87 struct 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
]; /*!< Sound prompt name and path */
96 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
97 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
98 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
99 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
100 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
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(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
]; /*!< Sound prompt name and path */
119 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
120 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
121 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
122 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
123 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
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 time_t start_time
, answer_time
, end_time
;
153 static const char *featuredigittostr
;
154 static int featuredigittimeout
= 5000; /*!< Feature Digit Timeout */
155 static const char *defaultmoh
= "default"; /*!< Default Music-On-Hold Class */
157 static char takecall
[20] = "1", nextindp
[20] = "2";
158 static char callfromprompt
[PATH_MAX
] = "followme/call-from";
159 static char norecordingprompt
[PATH_MAX
] = "followme/no-recording";
160 static char optionsprompt
[PATH_MAX
] = "followme/options";
161 static char plsholdprompt
[PATH_MAX
] = "followme/pls-hold-while-try";
162 static char statusprompt
[PATH_MAX
] = "followme/status";
163 static char sorryprompt
[PATH_MAX
] = "followme/sorry";
166 static AST_LIST_HEAD_STATIC(followmes
, call_followme
);
167 AST_LIST_HEAD_NOLOCK(findme_user_listptr
, findme_user
);
169 static void free_numbers(struct call_followme
*f
)
171 /* Free numbers attached to the profile */
174 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->numbers
, entry
)))
175 /* Free the number */
177 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
179 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->blnumbers
, entry
)))
180 /* Free the blacklisted number */
182 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
184 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->wlnumbers
, entry
)))
185 /* Free the whitelisted number */
187 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
192 /*! \brief Allocate and initialize followme profile */
193 static struct call_followme
*alloc_profile(const char *fmname
)
195 struct call_followme
*f
;
197 if (!(f
= ast_calloc(1, sizeof(*f
))))
200 ast_mutex_init(&f
->lock
);
201 ast_copy_string(f
->name
, fmname
, sizeof(f
->name
));
203 f
->context
[0] = '\0';
204 ast_copy_string(f
->takecall
, takecall
, sizeof(f
->takecall
));
205 ast_copy_string(f
->nextindp
, nextindp
, sizeof(f
->nextindp
));
206 ast_copy_string(f
->callfromprompt
, callfromprompt
, sizeof(f
->callfromprompt
));
207 ast_copy_string(f
->norecordingprompt
, norecordingprompt
, sizeof(f
->norecordingprompt
));
208 ast_copy_string(f
->optionsprompt
, optionsprompt
, sizeof(f
->optionsprompt
));
209 ast_copy_string(f
->plsholdprompt
, plsholdprompt
, sizeof(f
->plsholdprompt
));
210 ast_copy_string(f
->statusprompt
, statusprompt
, sizeof(f
->statusprompt
));
211 ast_copy_string(f
->sorryprompt
, sorryprompt
, sizeof(f
->sorryprompt
));
212 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
213 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
214 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
218 static void init_profile(struct call_followme
*f
)
221 ast_copy_string(f
->moh
, defaultmoh
, sizeof(f
->moh
));
226 /*! \brief Set parameter in profile from configuration file */
227 static void profile_set_param(struct call_followme
*f
, const char *param
, const char *val
, int linenum
, int failunknown
)
230 if (!strcasecmp(param
, "musicclass") || !strcasecmp(param
, "musiconhold") || !strcasecmp(param
, "music"))
231 ast_copy_string(f
->moh
, val
, sizeof(f
->moh
));
232 else if (!strcasecmp(param
, "context"))
233 ast_copy_string(f
->context
, val
, sizeof(f
->context
));
234 else if (!strcasecmp(param
, "takecall"))
235 ast_copy_string(f
->takecall
, val
, sizeof(f
->takecall
));
236 else if (!strcasecmp(param
, "declinecall"))
237 ast_copy_string(f
->nextindp
, val
, sizeof(f
->nextindp
));
238 else if (!strcasecmp(param
, "call-from-prompt"))
239 ast_copy_string(f
->callfromprompt
, val
, sizeof(f
->callfromprompt
));
240 else if (!strcasecmp(param
, "followme-norecording-prompt"))
241 ast_copy_string(f
->norecordingprompt
, val
, sizeof(f
->norecordingprompt
));
242 else if (!strcasecmp(param
, "followme-options-prompt"))
243 ast_copy_string(f
->optionsprompt
, val
, sizeof(f
->optionsprompt
));
244 else if (!strcasecmp(param
, "followme-pls-hold-prompt"))
245 ast_copy_string(f
->plsholdprompt
, val
, sizeof(f
->plsholdprompt
));
246 else if (!strcasecmp(param
, "followme-status-prompt"))
247 ast_copy_string(f
->statusprompt
, val
, sizeof(f
->statusprompt
));
248 else if (!strcasecmp(param
, "followme-sorry-prompt"))
249 ast_copy_string(f
->sorryprompt
, val
, sizeof(f
->sorryprompt
));
250 else if (failunknown
) {
252 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f
->name
, param
, linenum
);
254 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s\n", f
->name
, param
);
258 /*! \brief Add a new number */
259 static struct number
*create_followme_number(char *number
, char *language
, int timeout
, int numorder
)
265 if (!(cur
= ast_calloc(1, sizeof(*cur
))))
268 cur
->timeout
= timeout
;
269 if ((tmp
= strchr(number
, ',')))
271 ast_copy_string(cur
->number
, number
, sizeof(cur
->number
));
272 ast_copy_string(cur
->language
, language
, sizeof(cur
->language
));
273 cur
->order
= numorder
;
275 ast_log(LOG_DEBUG
, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur
->number
, cur
->order
, cur
->timeout
);
280 /*! \brief Reload followme application module */
281 static int reload_followme(void)
283 struct call_followme
*f
;
284 struct ast_config
*cfg
;
285 char *cat
= NULL
, *tmp
;
286 struct ast_variable
*var
;
287 struct number
*cur
, *nm
;
293 const char *takecallstr
;
294 const char *declinecallstr
;
297 cfg
= ast_config_load("followme.conf");
299 ast_log(LOG_WARNING
, "No follow me config file (followme.conf), so no follow me\n");
303 AST_LIST_LOCK(&followmes
);
305 /* Reset Global Var Values */
306 featuredigittimeout
= 5000;
308 /* Mark all profiles as inactive for the moment */
309 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
312 featuredigittostr
= ast_variable_retrieve(cfg
, "general", "featuredigittimeout");
314 if (!ast_strlen_zero(featuredigittostr
)) {
315 if (!sscanf(featuredigittostr
, "%d", &featuredigittimeout
))
316 featuredigittimeout
= 5000;
319 takecallstr
= ast_variable_retrieve(cfg
, "general", "takecall");
320 if (!ast_strlen_zero(takecallstr
))
321 ast_copy_string(takecall
, takecallstr
, sizeof(takecall
));
323 declinecallstr
= ast_variable_retrieve(cfg
, "general", "declinecall");
324 if (!ast_strlen_zero(declinecallstr
))
325 ast_copy_string(nextindp
, declinecallstr
, sizeof(nextindp
));
327 tmpstr
= ast_variable_retrieve(cfg
, "general", "call-from-prompt");
328 if (!ast_strlen_zero(tmpstr
))
329 ast_copy_string(callfromprompt
, tmpstr
, sizeof(callfromprompt
));
331 tmpstr
= ast_variable_retrieve(cfg
, "general", "norecording-prompt");
332 if (!ast_strlen_zero(tmpstr
))
333 ast_copy_string(norecordingprompt
, tmpstr
, sizeof(norecordingprompt
));
335 tmpstr
= ast_variable_retrieve(cfg
, "general", "options-prompt");
336 if (!ast_strlen_zero(tmpstr
))
337 ast_copy_string(optionsprompt
, tmpstr
, sizeof(optionsprompt
));
339 tmpstr
= ast_variable_retrieve(cfg
, "general", "pls-hold-prompt");
340 if (!ast_strlen_zero(tmpstr
))
341 ast_copy_string(plsholdprompt
, tmpstr
, sizeof(plsholdprompt
));
343 tmpstr
= ast_variable_retrieve(cfg
, "general", "status-prompt");
344 if (!ast_strlen_zero(tmpstr
))
345 ast_copy_string(statusprompt
, tmpstr
, sizeof(statusprompt
));
347 tmpstr
= ast_variable_retrieve(cfg
, "general", "sorry-prompt");
348 if (!ast_strlen_zero(tmpstr
))
349 ast_copy_string(sorryprompt
, tmpstr
, sizeof(sorryprompt
));
351 /* Chug through config file */
352 while ((cat
= ast_category_browse(cfg
, cat
))) {
353 if (!strcasecmp(cat
, "general"))
355 /* Define a new profile */
356 /* Look for an existing one */
357 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
358 if (!strcasecmp(f
->name
, cat
))
362 ast_log(LOG_DEBUG
, "New profile %s.\n", cat
);
365 f
= alloc_profile(cat
);
372 ast_mutex_lock(&f
->lock
);
373 /* Re-initialize the profile */
376 var
= ast_variable_browse(cfg
, cat
);
378 if (!strcasecmp(var
->name
, "number")) {
379 /* Add a new number */
380 ast_copy_string(numberstr
, var
->value
, sizeof(numberstr
));
381 if ((tmp
= strchr(numberstr
, ','))) {
384 timeoutstr
= ast_strdupa(tmp
);
385 if ((tmp
= strchr(timeoutstr
, ','))) {
388 numorder
= atoi(tmp
);
393 timeout
= atoi(timeoutstr
);
403 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
)
407 cur
= create_followme_number(numberstr
, "", timeout
, numorder
);
408 AST_LIST_INSERT_TAIL(&f
->numbers
, cur
, entry
);
410 profile_set_param(f
, var
->name
, var
->value
, var
->lineno
, 1);
411 if (option_debug
> 1)
412 ast_log(LOG_DEBUG
, "Logging parameter %s with value %s from lineno %d\n", var
->name
, var
->value
, var
->lineno
);
415 } /* End while(var) loop */
418 ast_mutex_unlock(&f
->lock
);
420 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
);
444 snprintf(tmp
, sizeof(tmp
), "%s/%s", "Local", tmpuser
->dialarg
);
445 ast_cdr_setapp(outbound
->cdr
, "FollowMe", tmp
);
446 ast_cdr_update(outbound
);
447 ast_cdr_start(outbound
->cdr
);
448 ast_cdr_end(outbound
->cdr
);
449 /* If the cause wasn't handled properly */
450 if (ast_cdr_disposition(outbound
->cdr
, outbound
->hangupcause
))
451 ast_cdr_failed(outbound
->cdr
);
453 ast_log(LOG_WARNING
, "Unable to create Call Detail Record\n");
454 ast_hangup(tmpuser
->ochan
);
459 static void clear_calling_tree(struct findme_user_listptr
*findme_user_list
)
461 struct findme_user
*tmpuser
;
463 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
) {
464 clear_caller(tmpuser
);
465 tmpuser
->cleared
= 1;
472 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
)
474 struct ast_channel
*watchers
[256];
476 struct ast_channel
*winner
;
480 struct findme_user
*tmpuser
;
482 int livechannels
= 0;
484 long totalwait
= 0, wtd
, towas
= 0;
486 char *pressbuttonname
;
488 /* ------------ wait_for_winner_channel start --------------- */
490 callfromname
= ast_strdupa(tpargs
->callfromprompt
);
491 pressbuttonname
= ast_strdupa(tpargs
->optionsprompt
);
493 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
);
515 if (tmpuser
->digts
&& (tmpuser
->digts
> featuredigittimeout
)) {
516 if (option_verbose
> 2)
517 ast_verbose(VERBOSE_PREFIX_3
"We've been waiting for digits longer than we should have.\n");
518 if (!ast_strlen_zero(namerecloc
)) {
521 if (!ast_streamfile(tmpuser
->ochan
, callfromname
, tmpuser
->ochan
->language
)) {
522 ast_sched_runq(tmpuser
->ochan
->sched
);
524 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
530 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
531 ast_sched_runq(tmpuser
->ochan
->sched
);
533 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
538 if (tmpuser
->ochan
->stream
) {
539 ast_sched_runq(tmpuser
->ochan
->sched
);
540 tmpto
= ast_sched_wait(tmpuser
->ochan
->sched
);
541 if (tmpto
> 0 && tmpto
< to
)
543 else if (tmpto
< 0 && !tmpuser
->ochan
->timingfunc
) {
544 ast_stopstream(tmpuser
->ochan
);
545 if (tmpuser
->state
== 1) {
546 if (option_verbose
> 2)
547 ast_verbose(VERBOSE_PREFIX_3
"Playback of the call-from file appears to be done.\n");
548 if (!ast_streamfile(tmpuser
->ochan
, namerecloc
, tmpuser
->ochan
->language
)) {
551 ast_log(LOG_NOTICE
, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc
);
552 memset(tmpuser
->yn
, 0, sizeof(tmpuser
->yn
));
554 if (!ast_streamfile(tmpuser
->ochan
, pressbuttonname
, tmpuser
->ochan
->language
))
557 ast_log(LOG_WARNING
, "Unable to playback %s.\n", pressbuttonname
);
561 } else if (tmpuser
->state
== 2) {
562 if (option_verbose
> 2)
563 ast_verbose(VERBOSE_PREFIX_3
"Playback of name file appears to be done.\n");
564 memset(tmpuser
->yn
, 0, sizeof(tmpuser
->yn
));
566 if (!ast_streamfile(tmpuser
->ochan
, pressbuttonname
, tmpuser
->ochan
->language
)) {
572 } else if (tmpuser
->state
== 3) {
573 if (option_verbose
> 2)
574 ast_verbose(VERBOSE_PREFIX_3
"Playback of the next step file appears to be done.\n");
579 watchers
[pos
++] = tmpuser
->ochan
;
590 winner
= ast_waitfor_n(watchers
, pos
, &to
);
594 if (totalwait
<= 0) {
595 if (option_verbose
> 2)
596 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
);
597 clear_calling_tree(findme_user_list
);
601 /* Need to find out which channel this is */
603 while ((winner
!= watchers
[dg
]) && (dg
< 256))
605 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
)
606 if (tmpuser
->ochan
== winner
)
608 f
= ast_read(winner
);
610 if (f
->frametype
== AST_FRAME_CONTROL
) {
611 switch(f
->subclass
) {
612 case AST_CONTROL_HANGUP
:
613 if (option_verbose
> 2)
614 ast_verbose( VERBOSE_PREFIX_3
"%s received a hangup frame.\n", winner
->name
);
616 if (option_verbose
> 2)
617 ast_verbose( VERBOSE_PREFIX_3
"The calling channel hungup. Need to drop everyone else.\n");
618 clear_calling_tree(findme_user_list
);
622 case AST_CONTROL_ANSWER
:
623 if (option_verbose
> 2)
624 ast_verbose( VERBOSE_PREFIX_3
"%s answered %s\n", winner
->name
, caller
->name
);
625 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
626 winner
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
627 caller
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
628 if (option_verbose
> 2)
629 ast_verbose( VERBOSE_PREFIX_3
"Starting playback of %s\n", callfromname
);
631 if (!ast_strlen_zero(namerecloc
)) {
632 if (!ast_streamfile(winner
, callfromname
, winner
->language
)) {
633 ast_sched_runq(winner
->sched
);
636 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
642 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
643 ast_sched_runq(tmpuser
->ochan
->sched
);
645 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
652 case AST_CONTROL_BUSY
:
653 if (option_verbose
> 2)
654 ast_verbose( VERBOSE_PREFIX_3
"%s is busy\n", winner
->name
);
656 case AST_CONTROL_CONGESTION
:
657 if (option_verbose
> 2)
658 ast_verbose( VERBOSE_PREFIX_3
"%s is circuit-busy\n", winner
->name
);
660 case AST_CONTROL_RINGING
:
661 if (option_verbose
> 2)
662 ast_verbose( VERBOSE_PREFIX_3
"%s is ringing\n", winner
->name
);
664 case AST_CONTROL_PROGRESS
:
665 if (option_verbose
> 2)
666 ast_verbose ( VERBOSE_PREFIX_3
"%s is making progress passing it to %s\n", winner
->name
, caller
->name
);
668 case AST_CONTROL_VIDUPDATE
:
669 if (option_verbose
> 2)
670 ast_verbose ( VERBOSE_PREFIX_3
"%s requested a video update, passing it to %s\n", winner
->name
, caller
->name
);
672 case AST_CONTROL_PROCEEDING
:
673 if (option_verbose
> 2)
674 ast_verbose ( VERBOSE_PREFIX_3
"%s is proceeding passing it to %s\n", winner
->name
,caller
->name
);
676 case AST_CONTROL_HOLD
:
677 if (option_verbose
> 2)
678 ast_verbose(VERBOSE_PREFIX_3
"Call on %s placed on hold\n", winner
->name
);
680 case AST_CONTROL_UNHOLD
:
681 if (option_verbose
> 2)
682 ast_verbose(VERBOSE_PREFIX_3
"Call on %s left from hold\n", winner
->name
);
684 case AST_CONTROL_OFFHOOK
:
685 case AST_CONTROL_FLASH
:
686 /* Ignore going off hook and flash */
689 if (option_verbose
> 2)
690 ast_verbose( VERBOSE_PREFIX_3
"%s stopped sounds\n", winner
->name
);
694 ast_log(LOG_DEBUG
, "Dunno what to do with control type %d\n", f
->subclass
);
698 if (tmpuser
&& tmpuser
->state
== 3 && f
->frametype
== AST_FRAME_DTMF
) {
700 ast_stopstream(winner
);
703 ast_log(LOG_DEBUG
, "DTMF received: %c\n",(char) f
->subclass
);
704 tmpuser
->yn
[tmpuser
->ynidx
] = (char) f
->subclass
;
707 ast_log(LOG_DEBUG
, "DTMF string: %s\n", tmpuser
->yn
);
708 if (tmpuser
->ynidx
>= ynlongest
) {
710 ast_log(LOG_DEBUG
, "reached longest possible match - doing evals\n");
711 if (!strcmp(tmpuser
->yn
, tpargs
->takecall
)) {
713 ast_log(LOG_DEBUG
, "Match to take the call!\n");
715 return tmpuser
->ochan
;
717 if (!strcmp(tmpuser
->yn
, tpargs
->nextindp
)) {
719 ast_log(LOG_DEBUG
, "Next in dial plan step requested.\n");
732 ast_log(LOG_DEBUG
, "we didn't get a frame. hanging up. dg is %d\n",dg
);
734 clear_calling_tree(findme_user_list
);
741 ast_log(LOG_DEBUG
, "live channels left %d\n", livechannels
);
743 if (option_verbose
> 2)
744 ast_verbose(VERBOSE_PREFIX_3
"no live channels left. exiting.\n");
753 ast_log(LOG_DEBUG
, "timed out waiting for action\n");
757 if (option_verbose
> 2)
758 ast_verbose(VERBOSE_PREFIX_3
"couldn't reach at this number.\n");
761 /* --- WAIT FOR WINNER NUMBER END! -----------*/
765 static void findmeexec(struct fm_args
*tpargs
)
768 struct ast_channel
*outbound
;
769 struct ast_channel
*caller
;
770 struct ast_channel
*winner
= NULL
;
774 struct findme_user
*tmpuser
;
775 struct findme_user
*fmuser
;
776 struct findme_user
*headuser
;
777 struct findme_user_listptr
*findme_user_list
;
780 findme_user_list
= ast_calloc(1, sizeof(*findme_user_list
));
781 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list
);
783 /* We're going to figure out what the longest possible string of digits to collect is */
785 if (strlen(tpargs
->takecall
) > ynlongest
)
786 ynlongest
= strlen(tpargs
->takecall
);
787 if (strlen(tpargs
->nextindp
) > ynlongest
)
788 ynlongest
= strlen(tpargs
->nextindp
);
791 caller
= tpargs
->chan
;
792 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
)
793 if (nm
->order
== idx
)
798 if (option_debug
> 1)
799 ast_log(LOG_DEBUG
, "Number %s timeout %ld\n", nm
->number
,nm
->timeout
);
802 number
= ast_strdupa(nm
->number
);
803 if (option_debug
> 2)
804 ast_log(LOG_DEBUG
, "examining %s\n", number
);
806 rest
= strchr(number
, '&');
812 if (!strcmp(tpargs
->context
, ""))
813 sprintf(dialarg
, "%s", number
);
815 sprintf(dialarg
, "%s@%s", number
, tpargs
->context
);
817 tmpuser
= ast_calloc(1, sizeof(*tmpuser
));
819 ast_log(LOG_WARNING
, "Out of memory!\n");
820 free(findme_user_list
);
824 outbound
= ast_request("Local", ast_best_codec(caller
->nativeformats
), dialarg
, &dg
);
826 ast_set_callerid(outbound
, caller
->cid
.cid_num
, caller
->cid
.cid_name
, caller
->cid
.cid_num
);
827 ast_channel_inherit_variables(tpargs
->chan
, outbound
);
828 if (option_verbose
> 2)
829 ast_verbose(VERBOSE_PREFIX_3
"calling %s\n", dialarg
);
830 if (!ast_call(outbound
,dialarg
,0)) {
831 tmpuser
->ochan
= outbound
;
833 tmpuser
->cleared
= 0;
834 ast_copy_string(tmpuser
->dialarg
, dialarg
, sizeof(dialarg
));
835 AST_LIST_INSERT_TAIL(findme_user_list
, tmpuser
, entry
);
837 if (option_verbose
> 2)
838 ast_verbose(VERBOSE_PREFIX_3
"couldn't reach at this number.\n");
841 outbound
->cdr
= ast_cdr_alloc();
845 ast_cdr_init(outbound
->cdr
, outbound
);
846 snprintf(tmp
, sizeof(tmp
), "%s/%s", "Local", dialarg
);
847 ast_cdr_setapp(outbound
->cdr
, "FollowMe", tmp
);
848 ast_cdr_update(outbound
);
849 ast_cdr_start(outbound
->cdr
);
850 ast_cdr_end(outbound
->cdr
);
851 /* If the cause wasn't handled properly */
852 if (ast_cdr_disposition(outbound
->cdr
,outbound
->hangupcause
))
853 ast_cdr_failed(outbound
->cdr
);
855 ast_log(LOG_ERROR
, "Unable to create Call Detail Record\n");
856 ast_hangup(outbound
);
863 ast_log(LOG_WARNING
, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg
, ast_cause2str(dg
));
869 if (!AST_LIST_EMPTY(findme_user_list
))
870 winner
= wait_for_winner(findme_user_list
, nm
, caller
, tpargs
->namerecloc
, &status
, tpargs
);
873 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list
, fmuser
, entry
) {
874 if (!fmuser
->cleared
&& fmuser
->ochan
!= winner
)
875 clear_caller(fmuser
);
876 AST_LIST_REMOVE_CURRENT(findme_user_list
, entry
);
879 AST_LIST_TRAVERSE_SAFE_END
888 free(findme_user_list
);
893 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
)
894 if (nm
->order
== idx
)
898 free(findme_user_list
);
902 tpargs
->status
= 100;
903 tpargs
->outbound
= winner
;
911 static int app_exec(struct ast_channel
*chan
, void *data
)
913 struct fm_args targs
;
914 struct ast_bridge_config config
;
915 struct call_followme
*f
;
916 struct number
*nm
, *newnm
;
918 struct ast_module_user
*u
;
920 char namerecloc
[255];
923 struct ast_channel
*caller
;
924 struct ast_channel
*outbound
;
925 static char toast
[80];
927 AST_DECLARE_APP_ARGS(args
,
928 AST_APP_ARG(followmeid
);
929 AST_APP_ARG(options
);
932 if (!(argstr
= ast_strdupa((char *)data
))) {
933 ast_log(LOG_ERROR
, "Out of memory!\n");
938 ast_log(LOG_WARNING
, "%s requires an argument (followmeid)\n",app
);
942 u
= ast_module_user_add(chan
);
944 AST_STANDARD_APP_ARGS(args
, argstr
);
946 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
);
957 ast_log(LOG_WARNING
, "Profile requested, %s, not found in the configuration.\n", args
.followmeid
);
960 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
964 ast_app_parse_options(followme_opts
, &targs
.followmeflags
, NULL
, args
.options
);
966 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
967 ast_mutex_lock(&f
->lock
);
968 targs
.mohclass
= ast_strdupa(f
->moh
);
969 ast_copy_string(targs
.context
, f
->context
, sizeof(targs
.context
));
970 ast_copy_string(targs
.takecall
, f
->takecall
, sizeof(targs
.takecall
));
971 ast_copy_string(targs
.nextindp
, f
->nextindp
, sizeof(targs
.nextindp
));
972 ast_copy_string(targs
.callfromprompt
, f
->callfromprompt
, sizeof(targs
.callfromprompt
));
973 ast_copy_string(targs
.norecordingprompt
, f
->norecordingprompt
, sizeof(targs
.norecordingprompt
));
974 ast_copy_string(targs
.optionsprompt
, f
->optionsprompt
, sizeof(targs
.optionsprompt
));
975 ast_copy_string(targs
.plsholdprompt
, f
->plsholdprompt
, sizeof(targs
.plsholdprompt
));
976 ast_copy_string(targs
.statusprompt
, f
->statusprompt
, sizeof(targs
.statusprompt
));
977 ast_copy_string(targs
.sorryprompt
, f
->sorryprompt
, sizeof(targs
.sorryprompt
));
978 /* Copy the numbers we're going to use into another list in case the master list should get modified
979 (and locked) while we're trying to do a follow-me */
980 AST_LIST_HEAD_INIT_NOLOCK(&targs
.cnumbers
);
981 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
) {
982 newnm
= create_followme_number(nm
->number
, "", nm
->timeout
, nm
->order
);
983 AST_LIST_INSERT_TAIL(&targs
.cnumbers
, newnm
, entry
);
985 ast_mutex_unlock(&f
->lock
);
987 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_STATUSMSG
))
988 ast_stream_and_wait(chan
, targs
.statusprompt
, chan
->language
, "");
990 snprintf(namerecloc
,sizeof(namerecloc
),"%s/followme.%s",ast_config_AST_SPOOL_DIR
,chan
->uniqueid
);
993 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_RECORDNAME
))
994 if (ast_play_and_record(chan
, "vm-rec-name", namerecloc
, 5, "sln", &duration
, 128, 0, NULL
) < 0)
997 if (!ast_fileexists(namerecloc
, NULL
, chan
->language
))
998 ast_copy_string(namerecloc
, "", sizeof(namerecloc
));
1000 if (ast_streamfile(chan
, targs
.plsholdprompt
, chan
->language
))
1002 if (ast_waitstream(chan
, "") < 0)
1004 ast_moh_start(chan
, S_OR(targs
.mohclass
, NULL
), NULL
);
1008 ast_copy_string(targs
.namerecloc
, namerecloc
, sizeof(targs
.namerecloc
));
1012 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs
.cnumbers
, nm
, entry
) {
1013 AST_LIST_REMOVE_CURRENT(&targs
.cnumbers
, entry
);
1016 AST_LIST_TRAVERSE_SAFE_END
1017 if (targs
.status
!= 100) {
1019 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_UNREACHABLEMSG
))
1020 ast_stream_and_wait(chan
, targs
.sorryprompt
, chan
->language
, "");
1024 outbound
= targs
.outbound
;
1025 /* Bridge the two channels. */
1027 memset(&config
,0,sizeof(struct ast_bridge_config
));
1028 ast_set_flag(&(config
.features_callee
), AST_FEATURE_REDIRECT
);
1029 ast_set_flag(&(config
.features_callee
), AST_FEATURE_AUTOMON
);
1030 ast_set_flag(&(config
.features_caller
), AST_FEATURE_AUTOMON
);
1032 ast_moh_stop(caller
);
1033 /* Be sure no generators are left on it */
1034 ast_deactivate_generator(caller
);
1035 /* Make sure channels are compatible */
1036 res
= ast_channel_make_compatible(caller
, outbound
);
1038 ast_log(LOG_WARNING
, "Had to drop call because I couldn't make %s compatible with %s\n", caller
->name
, outbound
->name
);
1039 ast_hangup(outbound
);
1043 res
= ast_bridge_call(caller
,outbound
,&config
);
1045 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- start_time
));
1046 pbx_builtin_setvar_helper(caller
, "DIALEDTIME", toast
);
1047 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- answer_time
));
1048 pbx_builtin_setvar_helper(caller
, "ANSWEREDTIME", toast
);
1050 ast_hangup(outbound
);
1056 if (!ast_strlen_zero(namerecloc
)){
1057 fname
= alloca(strlen(namerecloc
) + 5);
1058 sprintf(fname
, "%s.sln", namerecloc
);
1062 ast_module_user_remove(u
);
1067 static int unload_module(void)
1069 struct call_followme
*f
;
1071 ast_module_user_hangup_all();
1073 ast_unregister_application(app
);
1075 /* Free Memory. Yeah! I'm free! */
1076 AST_LIST_LOCK(&followmes
);
1077 while ((f
= AST_LIST_REMOVE_HEAD(&followmes
, entry
))) {
1082 AST_LIST_UNLOCK(&followmes
);
1087 static int load_module(void)
1089 if(!reload_followme())
1090 return AST_MODULE_LOAD_DECLINE
;
1092 return ast_register_application(app
, app_exec
, synopsis
, descrip
);
1095 static int reload(void)
1102 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Find-Me/Follow-Me Application",
1103 .load
= load_module
,
1104 .unload
= unload_module
,