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>
37 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
45 #include "asterisk/lock.h"
46 #include "asterisk/file.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/options.h"
51 #include "asterisk/module.h"
52 #include "asterisk/translate.h"
53 #include "asterisk/say.h"
54 #include "asterisk/features.h"
55 #include "asterisk/musiconhold.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/manager.h"
58 #include "asterisk/config.h"
59 #include "asterisk/monitor.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/causes.h"
62 #include "asterisk/astdb.h"
63 #include "asterisk/app.h"
65 static char *app
= "FollowMe";
66 static char *synopsis
= "Find-Me/Follow-Me application";
67 static char *descrip
=
68 " FollowMe(followmeid|options):\n"
69 "This application performs Find-Me/Follow-Me functionality for the caller\n"
70 "as defined in the profile matching the <followmeid> parameter in\n"
71 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
72 "followme.conf, execution will be returned to the dialplan and call\n"
73 "execution will continue at the next priority.\n\n"
75 " s - Playback the incoming status message prior to starting the follow-me step(s)\n"
76 " a - Record the caller's name so it can be announced to the callee on each step\n"
77 " n - Playback the unreachable status message if we've run out of steps to reach the\n"
78 " or the callee has elected not to be reachable.\n"
79 "Returns -1 on hangup\n";
81 /*! \brief Number structure */
83 char number
[512]; /*!< Phone Number(s) and/or Extension(s) */
84 long timeout
; /*!< Dial Timeout, if used. */
85 char language
[MAX_LANGUAGE
]; /*!< The language to be used on this dial, if used. */
86 int order
; /*!< The order to dial in */
87 AST_LIST_ENTRY(number
) entry
; /*!< Next Number record */
90 /*! \brief Data structure for followme scripts */
91 struct call_followme
{
93 char name
[AST_MAX_EXTENSION
]; /*!< Name - FollowMeID */
94 char moh
[AST_MAX_CONTEXT
]; /*!< Music On Hold Class to be used */
95 char context
[AST_MAX_CONTEXT
]; /*!< Context to dial from */
96 unsigned int active
; /*!< Profile is active (1), or disabled (0). */
97 char takecall
[20]; /*!< Digit mapping to take a call */
98 char nextindp
[20]; /*!< Digit mapping to decline a call */
99 char callfromprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
100 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
101 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
102 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
103 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
104 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
106 AST_LIST_HEAD_NOLOCK(numbers
, number
) numbers
; /*!< Head of the list of follow-me numbers */
107 AST_LIST_HEAD_NOLOCK(blnumbers
, number
) blnumbers
; /*!< Head of the list of black-listed numbers */
108 AST_LIST_HEAD_NOLOCK(wlnumbers
, number
) wlnumbers
; /*!< Head of the list of white-listed numbers */
109 AST_LIST_ENTRY(call_followme
) entry
; /*!< Next Follow-Me record */
113 struct ast_channel
*chan
;
115 AST_LIST_HEAD_NOLOCK(cnumbers
, number
) cnumbers
;
117 char context
[AST_MAX_CONTEXT
];
118 char namerecloc
[AST_MAX_CONTEXT
];
119 struct ast_channel
*outbound
;
120 char takecall
[20]; /*!< Digit mapping to take a call */
121 char nextindp
[20]; /*!< Digit mapping to decline a call */
122 char callfromprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
123 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
124 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
125 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
126 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
127 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
128 struct ast_flags followmeflags
;
132 struct ast_channel
*ochan
;
139 AST_LIST_ENTRY(findme_user
) entry
;
143 FOLLOWMEFLAG_STATUSMSG
= (1 << 0),
144 FOLLOWMEFLAG_RECORDNAME
= (1 << 1),
145 FOLLOWMEFLAG_UNREACHABLEMSG
= (1 << 2)
148 AST_APP_OPTIONS(followme_opts
, {
149 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG
),
150 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME
),
151 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG
),
154 static int ynlongest
= 0;
155 static time_t start_time
, answer_time
, end_time
;
157 static const char *featuredigittostr
;
158 static int featuredigittimeout
= 5000; /*!< Feature Digit Timeout */
159 static const char *defaultmoh
= "default"; /*!< Default Music-On-Hold Class */
161 static char takecall
[20] = "1", nextindp
[20] = "2";
162 static char callfromprompt
[PATH_MAX
] = "followme/call-from";
163 static char norecordingprompt
[PATH_MAX
] = "followme/no-recording";
164 static char optionsprompt
[PATH_MAX
] = "followme/options";
165 static char plsholdprompt
[PATH_MAX
] = "followme/pls-hold-while-try";
166 static char statusprompt
[PATH_MAX
] = "followme/status";
167 static char sorryprompt
[PATH_MAX
] = "followme/sorry";
170 static AST_LIST_HEAD_STATIC(followmes
, call_followme
);
171 AST_LIST_HEAD_NOLOCK(findme_user_listptr
, findme_user
);
173 static void free_numbers(struct call_followme
*f
)
175 /* Free numbers attached to the profile */
178 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->numbers
, entry
)))
179 /* Free the number */
181 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
183 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->blnumbers
, entry
)))
184 /* Free the blacklisted number */
186 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
188 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->wlnumbers
, entry
)))
189 /* Free the whitelisted number */
191 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
196 /*! \brief Allocate and initialize followme profile */
197 static struct call_followme
*alloc_profile(const char *fmname
)
199 struct call_followme
*f
;
201 if (!(f
= ast_calloc(1, sizeof(*f
))))
204 ast_mutex_init(&f
->lock
);
205 ast_copy_string(f
->name
, fmname
, sizeof(f
->name
));
207 f
->context
[0] = '\0';
208 ast_copy_string(f
->takecall
, takecall
, sizeof(f
->takecall
));
209 ast_copy_string(f
->nextindp
, nextindp
, sizeof(f
->nextindp
));
210 ast_copy_string(f
->callfromprompt
, callfromprompt
, sizeof(f
->callfromprompt
));
211 ast_copy_string(f
->norecordingprompt
, norecordingprompt
, sizeof(f
->norecordingprompt
));
212 ast_copy_string(f
->optionsprompt
, optionsprompt
, sizeof(f
->optionsprompt
));
213 ast_copy_string(f
->plsholdprompt
, plsholdprompt
, sizeof(f
->plsholdprompt
));
214 ast_copy_string(f
->statusprompt
, statusprompt
, sizeof(f
->statusprompt
));
215 ast_copy_string(f
->sorryprompt
, sorryprompt
, sizeof(f
->sorryprompt
));
216 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
217 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
218 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
222 static void init_profile(struct call_followme
*f
)
225 ast_copy_string(f
->moh
, defaultmoh
, sizeof(f
->moh
));
230 /*! \brief Set parameter in profile from configuration file */
231 static void profile_set_param(struct call_followme
*f
, const char *param
, const char *val
, int linenum
, int failunknown
)
234 if (!strcasecmp(param
, "musicclass") || !strcasecmp(param
, "musiconhold") || !strcasecmp(param
, "music"))
235 ast_copy_string(f
->moh
, val
, sizeof(f
->moh
));
236 else if (!strcasecmp(param
, "context"))
237 ast_copy_string(f
->context
, val
, sizeof(f
->context
));
238 else if (!strcasecmp(param
, "takecall"))
239 ast_copy_string(f
->takecall
, val
, sizeof(f
->takecall
));
240 else if (!strcasecmp(param
, "declinecall"))
241 ast_copy_string(f
->nextindp
, val
, sizeof(f
->nextindp
));
242 else if (!strcasecmp(param
, "call-from-prompt"))
243 ast_copy_string(f
->callfromprompt
, val
, sizeof(f
->callfromprompt
));
244 else if (!strcasecmp(param
, "followme-norecording-prompt"))
245 ast_copy_string(f
->norecordingprompt
, val
, sizeof(f
->norecordingprompt
));
246 else if (!strcasecmp(param
, "followme-options-prompt"))
247 ast_copy_string(f
->optionsprompt
, val
, sizeof(f
->optionsprompt
));
248 else if (!strcasecmp(param
, "followme-pls-hold-prompt"))
249 ast_copy_string(f
->plsholdprompt
, val
, sizeof(f
->plsholdprompt
));
250 else if (!strcasecmp(param
, "followme-status-prompt"))
251 ast_copy_string(f
->statusprompt
, val
, sizeof(f
->statusprompt
));
252 else if (!strcasecmp(param
, "followme-sorry-prompt"))
253 ast_copy_string(f
->sorryprompt
, val
, sizeof(f
->sorryprompt
));
254 else if (failunknown
) {
256 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f
->name
, param
, linenum
);
258 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s\n", f
->name
, param
);
262 /*! \brief Add a new number */
263 static struct number
*create_followme_number(char *number
, char *language
, int timeout
, int numorder
)
269 if (!(cur
= ast_calloc(1, sizeof(*cur
))))
272 cur
->timeout
= timeout
;
273 if ((tmp
= strchr(number
, ',')))
275 ast_copy_string(cur
->number
, number
, sizeof(cur
->number
));
276 ast_copy_string(cur
->language
, language
, sizeof(cur
->language
));
277 cur
->order
= numorder
;
279 ast_log(LOG_DEBUG
, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur
->number
, cur
->order
, cur
->timeout
);
284 /*! \brief Reload followme application module */
285 static int reload_followme(void)
287 struct call_followme
*f
;
288 struct ast_config
*cfg
;
289 char *cat
= NULL
, *tmp
;
290 struct ast_variable
*var
;
291 struct number
*cur
, *nm
;
297 const char *takecallstr
;
298 const char *declinecallstr
;
301 cfg
= ast_config_load("followme.conf");
303 ast_log(LOG_WARNING
, "No follow me config file (followme.conf), so no follow me\n");
307 AST_LIST_LOCK(&followmes
);
309 /* Reset Global Var Values */
310 featuredigittimeout
= 5000;
312 /* Mark all profiles as inactive for the moment */
313 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
316 featuredigittostr
= ast_variable_retrieve(cfg
, "general", "featuredigittimeout");
318 if (!ast_strlen_zero(featuredigittostr
)) {
319 if (!sscanf(featuredigittostr
, "%d", &featuredigittimeout
))
320 featuredigittimeout
= 5000;
323 takecallstr
= ast_variable_retrieve(cfg
, "general", "takecall");
324 if (!ast_strlen_zero(takecallstr
))
325 ast_copy_string(takecall
, takecallstr
, sizeof(takecall
));
327 declinecallstr
= ast_variable_retrieve(cfg
, "general", "declinecall");
328 if (!ast_strlen_zero(declinecallstr
))
329 ast_copy_string(nextindp
, declinecallstr
, sizeof(nextindp
));
331 tmpstr
= ast_variable_retrieve(cfg
, "general", "call-from-prompt");
332 if (!ast_strlen_zero(tmpstr
))
333 ast_copy_string(callfromprompt
, tmpstr
, sizeof(callfromprompt
));
335 tmpstr
= ast_variable_retrieve(cfg
, "general", "norecording-prompt");
336 if (!ast_strlen_zero(tmpstr
))
337 ast_copy_string(norecordingprompt
, tmpstr
, sizeof(norecordingprompt
));
339 tmpstr
= ast_variable_retrieve(cfg
, "general", "options-prompt");
340 if (!ast_strlen_zero(tmpstr
))
341 ast_copy_string(optionsprompt
, tmpstr
, sizeof(optionsprompt
));
343 tmpstr
= ast_variable_retrieve(cfg
, "general", "pls-hold-prompt");
344 if (!ast_strlen_zero(tmpstr
))
345 ast_copy_string(plsholdprompt
, tmpstr
, sizeof(plsholdprompt
));
347 tmpstr
= ast_variable_retrieve(cfg
, "general", "status-prompt");
348 if (!ast_strlen_zero(tmpstr
))
349 ast_copy_string(statusprompt
, tmpstr
, sizeof(statusprompt
));
351 tmpstr
= ast_variable_retrieve(cfg
, "general", "sorry-prompt");
352 if (!ast_strlen_zero(tmpstr
))
353 ast_copy_string(sorryprompt
, tmpstr
, sizeof(sorryprompt
));
355 /* Chug through config file */
356 while ((cat
= ast_category_browse(cfg
, cat
))) {
357 if (!strcasecmp(cat
, "general"))
359 /* Define a new profile */
360 /* Look for an existing one */
361 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
362 if (!strcasecmp(f
->name
, cat
))
366 ast_log(LOG_DEBUG
, "New profile %s.\n", cat
);
369 f
= alloc_profile(cat
);
376 ast_mutex_lock(&f
->lock
);
377 /* Re-initialize the profile */
380 var
= ast_variable_browse(cfg
, cat
);
382 if (!strcasecmp(var
->name
, "number")) {
383 /* Add a new number */
384 ast_copy_string(numberstr
, var
->value
, sizeof(numberstr
));
385 if ((tmp
= strchr(numberstr
, ','))) {
388 timeoutstr
= ast_strdupa(tmp
);
389 if ((tmp
= strchr(timeoutstr
, ','))) {
392 numorder
= atoi(tmp
);
397 timeout
= atoi(timeoutstr
);
407 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
)
411 cur
= create_followme_number(numberstr
, "", timeout
, numorder
);
412 AST_LIST_INSERT_TAIL(&f
->numbers
, cur
, entry
);
414 profile_set_param(f
, var
->name
, var
->value
, var
->lineno
, 1);
415 if (option_debug
> 1)
416 ast_log(LOG_DEBUG
, "Logging parameter %s with value %s from lineno %d\n", var
->name
, var
->value
, var
->lineno
);
419 } /* End while(var) loop */
422 ast_mutex_unlock(&f
->lock
);
424 AST_LIST_INSERT_HEAD(&followmes
, f
, entry
);
427 ast_config_destroy(cfg
);
429 AST_LIST_UNLOCK(&followmes
);
434 static void clear_caller(struct findme_user
*tmpuser
)
436 struct ast_channel
*outbound
;
438 if (tmpuser
&& tmpuser
->ochan
&& tmpuser
->state
>= 0) {
439 outbound
= tmpuser
->ochan
;
440 if (!outbound
->cdr
) {
441 outbound
->cdr
= ast_cdr_alloc();
443 ast_cdr_init(outbound
->cdr
, outbound
);
448 snprintf(tmp
, sizeof(tmp
), "%s/%s", "Local", tmpuser
->dialarg
);
449 ast_cdr_setapp(outbound
->cdr
, "FollowMe", tmp
);
450 ast_cdr_update(outbound
);
451 ast_cdr_start(outbound
->cdr
);
452 ast_cdr_end(outbound
->cdr
);
453 /* If the cause wasn't handled properly */
454 if (ast_cdr_disposition(outbound
->cdr
, outbound
->hangupcause
))
455 ast_cdr_failed(outbound
->cdr
);
457 ast_log(LOG_WARNING
, "Unable to create Call Detail Record\n");
458 ast_hangup(tmpuser
->ochan
);
463 static void clear_calling_tree(struct findme_user_listptr
*findme_user_list
)
465 struct findme_user
*tmpuser
;
467 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
) {
468 clear_caller(tmpuser
);
469 tmpuser
->cleared
= 1;
476 static struct ast_channel
*wait_for_winner(struct findme_user_listptr
*findme_user_list
, struct number
*nm
, struct ast_channel
*caller
, char *namerecloc
, int *status
, struct fm_args
*tpargs
)
478 struct ast_channel
*watchers
[256];
480 struct ast_channel
*winner
;
484 struct findme_user
*tmpuser
;
486 int livechannels
= 0;
488 long totalwait
= 0, wtd
, towas
= 0;
490 char *pressbuttonname
;
492 /* ------------ wait_for_winner_channel start --------------- */
494 callfromname
= ast_strdupa(tpargs
->callfromprompt
);
495 pressbuttonname
= ast_strdupa(tpargs
->optionsprompt
);
497 if (!AST_LIST_EMPTY(findme_user_list
)) {
499 if (option_verbose
> 2)
500 ast_verbose(VERBOSE_PREFIX_3
"Original caller hungup. Cleanup.\n");
501 clear_calling_tree(findme_user_list
);
505 totalwait
= nm
->timeout
* 1000;
511 watchers
[0] = caller
;
515 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
) {
516 if (tmpuser
->state
>= 0 && tmpuser
->ochan
) {
517 if (tmpuser
->state
== 3)
518 tmpuser
->digts
+= (towas
- wtd
);
519 if (tmpuser
->digts
&& (tmpuser
->digts
> featuredigittimeout
)) {
520 if (option_verbose
> 2)
521 ast_verbose(VERBOSE_PREFIX_3
"We've been waiting for digits longer than we should have.\n");
522 if (!ast_strlen_zero(namerecloc
)) {
525 if (!ast_streamfile(tmpuser
->ochan
, callfromname
, tmpuser
->ochan
->language
)) {
526 ast_sched_runq(tmpuser
->ochan
->sched
);
528 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
534 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
535 ast_sched_runq(tmpuser
->ochan
->sched
);
537 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
542 if (tmpuser
->ochan
->stream
) {
543 ast_sched_runq(tmpuser
->ochan
->sched
);
544 tmpto
= ast_sched_wait(tmpuser
->ochan
->sched
);
545 if (tmpto
> 0 && tmpto
< to
)
547 else if (tmpto
< 0 && !tmpuser
->ochan
->timingfunc
) {
548 ast_stopstream(tmpuser
->ochan
);
549 if (tmpuser
->state
== 1) {
550 if (option_verbose
> 2)
551 ast_verbose(VERBOSE_PREFIX_3
"Playback of the call-from file appears to be done.\n");
552 if (!ast_streamfile(tmpuser
->ochan
, namerecloc
, tmpuser
->ochan
->language
)) {
555 ast_log(LOG_NOTICE
, "Unable to playback %s. Maybe the caller didn't record their name?\n", namerecloc
);
556 memset(tmpuser
->yn
, 0, sizeof(tmpuser
->yn
));
558 if (!ast_streamfile(tmpuser
->ochan
, pressbuttonname
, tmpuser
->ochan
->language
))
561 ast_log(LOG_WARNING
, "Unable to playback %s.\n", pressbuttonname
);
565 } else if (tmpuser
->state
== 2) {
566 if (option_verbose
> 2)
567 ast_verbose(VERBOSE_PREFIX_3
"Playback of name file appears to be done.\n");
568 memset(tmpuser
->yn
, 0, sizeof(tmpuser
->yn
));
570 if (!ast_streamfile(tmpuser
->ochan
, pressbuttonname
, tmpuser
->ochan
->language
)) {
576 } else if (tmpuser
->state
== 3) {
577 if (option_verbose
> 2)
578 ast_verbose(VERBOSE_PREFIX_3
"Playback of the next step file appears to be done.\n");
583 watchers
[pos
++] = tmpuser
->ochan
;
594 winner
= ast_waitfor_n(watchers
, pos
, &to
);
598 if (totalwait
<= 0) {
599 if (option_verbose
> 2)
600 ast_verbose(VERBOSE_PREFIX_3
"We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait
);
601 clear_calling_tree(findme_user_list
);
605 /* Need to find out which channel this is */
607 while ((winner
!= watchers
[dg
]) && (dg
< 256))
609 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
)
610 if (tmpuser
->ochan
== winner
)
612 f
= ast_read(winner
);
614 if (f
->frametype
== AST_FRAME_CONTROL
) {
615 switch(f
->subclass
) {
616 case AST_CONTROL_HANGUP
:
617 if (option_verbose
> 2)
618 ast_verbose( VERBOSE_PREFIX_3
"%s received a hangup frame.\n", winner
->name
);
620 if (option_verbose
> 2)
621 ast_verbose( VERBOSE_PREFIX_3
"The calling channel hungup. Need to drop everyone else.\n");
622 clear_calling_tree(findme_user_list
);
626 case AST_CONTROL_ANSWER
:
627 if (option_verbose
> 2)
628 ast_verbose( VERBOSE_PREFIX_3
"%s answered %s\n", winner
->name
, caller
->name
);
629 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
630 winner
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
631 caller
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
632 if (option_verbose
> 2)
633 ast_verbose( VERBOSE_PREFIX_3
"Starting playback of %s\n", callfromname
);
635 if (!ast_strlen_zero(namerecloc
)) {
636 if (!ast_streamfile(winner
, callfromname
, winner
->language
)) {
637 ast_sched_runq(winner
->sched
);
640 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
646 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
647 ast_sched_runq(tmpuser
->ochan
->sched
);
649 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
656 case AST_CONTROL_BUSY
:
657 if (option_verbose
> 2)
658 ast_verbose( VERBOSE_PREFIX_3
"%s is busy\n", winner
->name
);
660 case AST_CONTROL_CONGESTION
:
661 if (option_verbose
> 2)
662 ast_verbose( VERBOSE_PREFIX_3
"%s is circuit-busy\n", winner
->name
);
664 case AST_CONTROL_RINGING
:
665 if (option_verbose
> 2)
666 ast_verbose( VERBOSE_PREFIX_3
"%s is ringing\n", winner
->name
);
668 case AST_CONTROL_PROGRESS
:
669 if (option_verbose
> 2)
670 ast_verbose ( VERBOSE_PREFIX_3
"%s is making progress passing it to %s\n", winner
->name
, caller
->name
);
672 case AST_CONTROL_VIDUPDATE
:
673 if (option_verbose
> 2)
674 ast_verbose ( VERBOSE_PREFIX_3
"%s requested a video update, passing it to %s\n", winner
->name
, caller
->name
);
676 case AST_CONTROL_SRCUPDATE
:
677 if (option_verbose
> 2)
678 ast_verbose ( VERBOSE_PREFIX_3
"%s requested a source update, passing it to %s\n", winner
->name
, caller
->name
);
680 case AST_CONTROL_PROCEEDING
:
681 if (option_verbose
> 2)
682 ast_verbose ( VERBOSE_PREFIX_3
"%s is proceeding passing it to %s\n", winner
->name
,caller
->name
);
684 case AST_CONTROL_HOLD
:
685 if (option_verbose
> 2)
686 ast_verbose(VERBOSE_PREFIX_3
"Call on %s placed on hold\n", winner
->name
);
688 case AST_CONTROL_UNHOLD
:
689 if (option_verbose
> 2)
690 ast_verbose(VERBOSE_PREFIX_3
"Call on %s left from hold\n", winner
->name
);
692 case AST_CONTROL_OFFHOOK
:
693 case AST_CONTROL_FLASH
:
694 /* Ignore going off hook and flash */
697 if (option_verbose
> 2)
698 ast_verbose( VERBOSE_PREFIX_3
"%s stopped sounds\n", winner
->name
);
702 ast_log(LOG_DEBUG
, "Dunno what to do with control type %d\n", f
->subclass
);
706 if (tmpuser
&& tmpuser
->state
== 3 && f
->frametype
== AST_FRAME_DTMF
) {
708 ast_stopstream(winner
);
711 ast_log(LOG_DEBUG
, "DTMF received: %c\n",(char) f
->subclass
);
712 tmpuser
->yn
[tmpuser
->ynidx
] = (char) f
->subclass
;
715 ast_log(LOG_DEBUG
, "DTMF string: %s\n", tmpuser
->yn
);
716 if (tmpuser
->ynidx
>= ynlongest
) {
718 ast_log(LOG_DEBUG
, "reached longest possible match - doing evals\n");
719 if (!strcmp(tmpuser
->yn
, tpargs
->takecall
)) {
721 ast_log(LOG_DEBUG
, "Match to take the call!\n");
723 return tmpuser
->ochan
;
725 if (!strcmp(tmpuser
->yn
, tpargs
->nextindp
)) {
727 ast_log(LOG_DEBUG
, "Next in dial plan step requested.\n");
740 ast_log(LOG_DEBUG
, "we didn't get a frame. hanging up. dg is %d\n",dg
);
742 clear_calling_tree(findme_user_list
);
749 ast_log(LOG_DEBUG
, "live channels left %d\n", livechannels
);
751 if (option_verbose
> 2)
752 ast_verbose(VERBOSE_PREFIX_3
"no live channels left. exiting.\n");
761 ast_log(LOG_DEBUG
, "timed out waiting for action\n");
765 if (option_verbose
> 2)
766 ast_verbose(VERBOSE_PREFIX_3
"couldn't reach at this number.\n");
769 /* --- WAIT FOR WINNER NUMBER END! -----------*/
773 static void findmeexec(struct fm_args
*tpargs
)
776 struct ast_channel
*outbound
;
777 struct ast_channel
*caller
;
778 struct ast_channel
*winner
= NULL
;
782 struct findme_user
*tmpuser
;
783 struct findme_user
*fmuser
;
784 struct findme_user
*headuser
;
785 struct findme_user_listptr
*findme_user_list
;
788 findme_user_list
= ast_calloc(1, sizeof(*findme_user_list
));
789 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list
);
791 /* We're going to figure out what the longest possible string of digits to collect is */
793 if (strlen(tpargs
->takecall
) > ynlongest
)
794 ynlongest
= strlen(tpargs
->takecall
);
795 if (strlen(tpargs
->nextindp
) > ynlongest
)
796 ynlongest
= strlen(tpargs
->nextindp
);
799 caller
= tpargs
->chan
;
800 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
)
801 if (nm
->order
== idx
)
806 if (option_debug
> 1)
807 ast_log(LOG_DEBUG
, "Number %s timeout %ld\n", nm
->number
,nm
->timeout
);
810 number
= ast_strdupa(nm
->number
);
811 if (option_debug
> 2)
812 ast_log(LOG_DEBUG
, "examining %s\n", number
);
814 rest
= strchr(number
, '&');
820 if (!strcmp(tpargs
->context
, ""))
821 snprintf(dialarg
, sizeof(dialarg
), "%s", number
);
823 snprintf(dialarg
, sizeof(dialarg
), "%s@%s", number
, tpargs
->context
);
825 tmpuser
= ast_calloc(1, sizeof(*tmpuser
));
827 ast_log(LOG_WARNING
, "Out of memory!\n");
828 free(findme_user_list
);
832 outbound
= ast_request("Local", ast_best_codec(caller
->nativeformats
), dialarg
, &dg
);
834 ast_set_callerid(outbound
, caller
->cid
.cid_num
, caller
->cid
.cid_name
, caller
->cid
.cid_num
);
835 ast_channel_inherit_variables(tpargs
->chan
, outbound
);
836 if (option_verbose
> 2)
837 ast_verbose(VERBOSE_PREFIX_3
"calling %s\n", dialarg
);
838 if (!ast_call(outbound
,dialarg
,0)) {
839 tmpuser
->ochan
= outbound
;
841 tmpuser
->cleared
= 0;
842 ast_copy_string(tmpuser
->dialarg
, dialarg
, sizeof(dialarg
));
843 AST_LIST_INSERT_TAIL(findme_user_list
, tmpuser
, entry
);
845 if (option_verbose
> 2)
846 ast_verbose(VERBOSE_PREFIX_3
"couldn't reach at this number.\n");
849 outbound
->cdr
= ast_cdr_alloc();
853 ast_cdr_init(outbound
->cdr
, outbound
);
854 snprintf(tmp
, sizeof(tmp
), "%s/%s", "Local", dialarg
);
855 ast_cdr_setapp(outbound
->cdr
, "FollowMe", tmp
);
856 ast_cdr_update(outbound
);
857 ast_cdr_start(outbound
->cdr
);
858 ast_cdr_end(outbound
->cdr
);
859 /* If the cause wasn't handled properly */
860 if (ast_cdr_disposition(outbound
->cdr
,outbound
->hangupcause
))
861 ast_cdr_failed(outbound
->cdr
);
863 ast_log(LOG_ERROR
, "Unable to create Call Detail Record\n");
864 ast_hangup(outbound
);
871 ast_log(LOG_WARNING
, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg
, ast_cause2str(dg
));
877 if (!AST_LIST_EMPTY(findme_user_list
))
878 winner
= wait_for_winner(findme_user_list
, nm
, caller
, tpargs
->namerecloc
, &status
, tpargs
);
881 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list
, fmuser
, entry
) {
882 if (!fmuser
->cleared
&& fmuser
->ochan
!= winner
)
883 clear_caller(fmuser
);
884 AST_LIST_REMOVE_CURRENT(findme_user_list
, entry
);
887 AST_LIST_TRAVERSE_SAFE_END
896 free(findme_user_list
);
901 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
)
902 if (nm
->order
== idx
)
906 free(findme_user_list
);
910 tpargs
->status
= 100;
911 tpargs
->outbound
= winner
;
919 static int app_exec(struct ast_channel
*chan
, void *data
)
921 struct fm_args targs
;
922 struct ast_bridge_config config
;
923 struct call_followme
*f
;
924 struct number
*nm
, *newnm
;
926 struct ast_module_user
*u
;
928 char namerecloc
[255];
931 struct ast_channel
*caller
;
932 struct ast_channel
*outbound
;
933 static char toast
[80];
935 AST_DECLARE_APP_ARGS(args
,
936 AST_APP_ARG(followmeid
);
937 AST_APP_ARG(options
);
940 if (!(argstr
= ast_strdupa((char *)data
))) {
941 ast_log(LOG_ERROR
, "Out of memory!\n");
946 ast_log(LOG_WARNING
, "%s requires an argument (followmeid)\n",app
);
950 u
= ast_module_user_add(chan
);
952 AST_STANDARD_APP_ARGS(args
, argstr
);
954 if (!ast_strlen_zero(args
.followmeid
))
955 AST_LIST_LOCK(&followmes
);
956 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
957 if (!strcasecmp(f
->name
, args
.followmeid
) && (f
->active
))
960 AST_LIST_UNLOCK(&followmes
);
963 ast_log(LOG_DEBUG
, "New profile %s.\n", args
.followmeid
);
965 ast_log(LOG_WARNING
, "Profile requested, %s, not found in the configuration.\n", args
.followmeid
);
968 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
972 ast_app_parse_options(followme_opts
, &targs
.followmeflags
, NULL
, args
.options
);
974 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
975 ast_mutex_lock(&f
->lock
);
976 targs
.mohclass
= ast_strdupa(f
->moh
);
977 ast_copy_string(targs
.context
, f
->context
, sizeof(targs
.context
));
978 ast_copy_string(targs
.takecall
, f
->takecall
, sizeof(targs
.takecall
));
979 ast_copy_string(targs
.nextindp
, f
->nextindp
, sizeof(targs
.nextindp
));
980 ast_copy_string(targs
.callfromprompt
, f
->callfromprompt
, sizeof(targs
.callfromprompt
));
981 ast_copy_string(targs
.norecordingprompt
, f
->norecordingprompt
, sizeof(targs
.norecordingprompt
));
982 ast_copy_string(targs
.optionsprompt
, f
->optionsprompt
, sizeof(targs
.optionsprompt
));
983 ast_copy_string(targs
.plsholdprompt
, f
->plsholdprompt
, sizeof(targs
.plsholdprompt
));
984 ast_copy_string(targs
.statusprompt
, f
->statusprompt
, sizeof(targs
.statusprompt
));
985 ast_copy_string(targs
.sorryprompt
, f
->sorryprompt
, sizeof(targs
.sorryprompt
));
986 /* Copy the numbers we're going to use into another list in case the master list should get modified
987 (and locked) while we're trying to do a follow-me */
988 AST_LIST_HEAD_INIT_NOLOCK(&targs
.cnumbers
);
989 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
) {
990 newnm
= create_followme_number(nm
->number
, "", nm
->timeout
, nm
->order
);
991 AST_LIST_INSERT_TAIL(&targs
.cnumbers
, newnm
, entry
);
993 ast_mutex_unlock(&f
->lock
);
995 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_STATUSMSG
))
996 ast_stream_and_wait(chan
, targs
.statusprompt
, chan
->language
, "");
998 snprintf(namerecloc
,sizeof(namerecloc
),"%s/followme.%s",ast_config_AST_SPOOL_DIR
,chan
->uniqueid
);
1001 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_RECORDNAME
))
1002 if (ast_play_and_record(chan
, "vm-rec-name", namerecloc
, 5, "sln", &duration
, 128, 0, NULL
) < 0)
1005 if (!ast_fileexists(namerecloc
, NULL
, chan
->language
))
1006 ast_copy_string(namerecloc
, "", sizeof(namerecloc
));
1008 if (ast_streamfile(chan
, targs
.plsholdprompt
, chan
->language
))
1010 if (ast_waitstream(chan
, "") < 0)
1012 ast_moh_start(chan
, S_OR(targs
.mohclass
, NULL
), NULL
);
1016 ast_copy_string(targs
.namerecloc
, namerecloc
, sizeof(targs
.namerecloc
));
1020 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs
.cnumbers
, nm
, entry
) {
1021 AST_LIST_REMOVE_CURRENT(&targs
.cnumbers
, entry
);
1024 AST_LIST_TRAVERSE_SAFE_END
1025 if (targs
.status
!= 100) {
1027 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_UNREACHABLEMSG
))
1028 ast_stream_and_wait(chan
, targs
.sorryprompt
, chan
->language
, "");
1032 outbound
= targs
.outbound
;
1033 /* Bridge the two channels. */
1035 memset(&config
,0,sizeof(struct ast_bridge_config
));
1036 ast_set_flag(&(config
.features_callee
), AST_FEATURE_REDIRECT
);
1037 ast_set_flag(&(config
.features_callee
), AST_FEATURE_AUTOMON
);
1038 ast_set_flag(&(config
.features_caller
), AST_FEATURE_AUTOMON
);
1040 ast_moh_stop(caller
);
1041 /* Be sure no generators are left on it */
1042 ast_deactivate_generator(caller
);
1043 /* Make sure channels are compatible */
1044 res
= ast_channel_make_compatible(caller
, outbound
);
1046 ast_log(LOG_WARNING
, "Had to drop call because I couldn't make %s compatible with %s\n", caller
->name
, outbound
->name
);
1047 ast_hangup(outbound
);
1051 res
= ast_bridge_call(caller
,outbound
,&config
);
1053 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- start_time
));
1054 pbx_builtin_setvar_helper(caller
, "DIALEDTIME", toast
);
1055 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- answer_time
));
1056 pbx_builtin_setvar_helper(caller
, "ANSWEREDTIME", toast
);
1058 ast_hangup(outbound
);
1063 if (!ast_strlen_zero(namerecloc
)){
1064 fname
= alloca(strlen(namerecloc
) + 5);
1065 sprintf(fname
, "%s.sln", namerecloc
);
1069 ast_module_user_remove(u
);
1074 static int unload_module(void)
1076 struct call_followme
*f
;
1078 ast_module_user_hangup_all();
1080 ast_unregister_application(app
);
1082 /* Free Memory. Yeah! I'm free! */
1083 AST_LIST_LOCK(&followmes
);
1084 while ((f
= AST_LIST_REMOVE_HEAD(&followmes
, entry
))) {
1089 AST_LIST_UNLOCK(&followmes
);
1094 static int load_module(void)
1096 if(!reload_followme())
1097 return AST_MODULE_LOAD_DECLINE
;
1099 return ast_register_application(app
, app_exec
, synopsis
, descrip
);
1102 static int reload(void)
1109 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Find-Me/Follow-Me Application",
1110 .load
= load_module
,
1111 .unload
= unload_module
,