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$")
41 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR */
42 #include "asterisk/lock.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/module.h"
47 #include "asterisk/translate.h"
48 #include "asterisk/say.h"
49 #include "asterisk/features.h"
50 #include "asterisk/musiconhold.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/config.h"
54 #include "asterisk/monitor.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"
61 static char *app
= "FollowMe";
62 static char *synopsis
= "Find-Me/Follow-Me application";
63 static char *descrip
=
64 " FollowMe(followmeid[,options]):\n"
65 "This application performs Find-Me/Follow-Me functionality for the caller\n"
66 "as defined in the profile matching the <followmeid> parameter in\n"
67 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
68 "followme.conf, execution will be returned to the dialplan and call\n"
69 "execution will continue at the next priority.\n\n"
71 " s - Playback the incoming status message prior to starting the follow-me step(s)\n"
72 " a - Record the caller's name so it can be announced to the callee on each step\n"
73 " n - Playback the unreachable status message if we've run out of steps to reach the\n"
74 " or the callee has elected not to be reachable.\n"
75 "Returns -1 on hangup\n";
77 /*! \brief Number structure */
79 char number
[512]; /*!< Phone Number(s) and/or Extension(s) */
80 long timeout
; /*!< Dial Timeout, if used. */
81 int order
; /*!< The order to dial in */
82 AST_LIST_ENTRY(number
) entry
; /*!< Next Number record */
85 /*! \brief Data structure for followme scripts */
86 struct call_followme
{
88 char name
[AST_MAX_EXTENSION
]; /*!< Name - FollowMeID */
89 char moh
[AST_MAX_CONTEXT
]; /*!< Music On Hold Class to be used */
90 char context
[AST_MAX_CONTEXT
]; /*!< Context to dial from */
91 unsigned int active
; /*!< Profile is active (1), or disabled (0). */
92 char takecall
[20]; /*!< Digit mapping to take a call */
93 char nextindp
[20]; /*!< Digit mapping to decline a call */
94 char callfromprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
95 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
96 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
97 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
98 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
99 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
101 AST_LIST_HEAD_NOLOCK(numbers
, number
) numbers
; /*!< Head of the list of follow-me numbers */
102 AST_LIST_HEAD_NOLOCK(blnumbers
, number
) blnumbers
; /*!< Head of the list of black-listed numbers */
103 AST_LIST_HEAD_NOLOCK(wlnumbers
, number
) wlnumbers
; /*!< Head of the list of white-listed numbers */
104 AST_LIST_ENTRY(call_followme
) entry
; /*!< Next Follow-Me record */
108 struct ast_channel
*chan
;
110 AST_LIST_HEAD_NOLOCK(cnumbers
, number
) cnumbers
;
112 char context
[AST_MAX_CONTEXT
];
113 char namerecloc
[AST_MAX_CONTEXT
];
114 struct ast_channel
*outbound
;
115 char takecall
[20]; /*!< Digit mapping to take a call */
116 char nextindp
[20]; /*!< Digit mapping to decline a call */
117 char callfromprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
118 char norecordingprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
119 char optionsprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
120 char plsholdprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
121 char statusprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
122 char sorryprompt
[PATH_MAX
]; /*!< Sound prompt name and path */
123 struct ast_flags followmeflags
;
127 struct ast_channel
*ochan
;
134 AST_LIST_ENTRY(findme_user
) entry
;
138 FOLLOWMEFLAG_STATUSMSG
= (1 << 0),
139 FOLLOWMEFLAG_RECORDNAME
= (1 << 1),
140 FOLLOWMEFLAG_UNREACHABLEMSG
= (1 << 2)
143 AST_APP_OPTIONS(followme_opts
, {
144 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG
),
145 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME
),
146 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG
),
149 static int ynlongest
= 0;
150 static time_t start_time
, answer_time
, end_time
;
152 static const char *featuredigittostr
;
153 static int featuredigittimeout
= 5000; /*!< Feature Digit Timeout */
154 static const char *defaultmoh
= "default"; /*!< Default Music-On-Hold Class */
156 static char takecall
[20] = "1", nextindp
[20] = "2";
157 static char callfromprompt
[PATH_MAX
] = "followme/call-from";
158 static char norecordingprompt
[PATH_MAX
] = "followme/no-recording";
159 static char optionsprompt
[PATH_MAX
] = "followme/options";
160 static char plsholdprompt
[PATH_MAX
] = "followme/pls-hold-while-try";
161 static char statusprompt
[PATH_MAX
] = "followme/status";
162 static char sorryprompt
[PATH_MAX
] = "followme/sorry";
165 static AST_RWLIST_HEAD_STATIC(followmes
, call_followme
);
166 AST_LIST_HEAD_NOLOCK(findme_user_listptr
, findme_user
);
168 static void free_numbers(struct call_followme
*f
)
170 /* Free numbers attached to the profile */
173 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->numbers
, entry
)))
174 /* Free the number */
176 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
178 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->blnumbers
, entry
)))
179 /* Free the blacklisted number */
181 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
183 while ((prev
= AST_LIST_REMOVE_HEAD(&f
->wlnumbers
, entry
)))
184 /* Free the whitelisted number */
186 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
190 /*! \brief Allocate and initialize followme profile */
191 static struct call_followme
*alloc_profile(const char *fmname
)
193 struct call_followme
*f
;
195 if (!(f
= ast_calloc(1, sizeof(*f
))))
198 ast_mutex_init(&f
->lock
);
199 ast_copy_string(f
->name
, fmname
, sizeof(f
->name
));
201 f
->context
[0] = '\0';
202 ast_copy_string(f
->takecall
, takecall
, sizeof(f
->takecall
));
203 ast_copy_string(f
->nextindp
, nextindp
, sizeof(f
->nextindp
));
204 ast_copy_string(f
->callfromprompt
, callfromprompt
, sizeof(f
->callfromprompt
));
205 ast_copy_string(f
->norecordingprompt
, norecordingprompt
, sizeof(f
->norecordingprompt
));
206 ast_copy_string(f
->optionsprompt
, optionsprompt
, sizeof(f
->optionsprompt
));
207 ast_copy_string(f
->plsholdprompt
, plsholdprompt
, sizeof(f
->plsholdprompt
));
208 ast_copy_string(f
->statusprompt
, statusprompt
, sizeof(f
->statusprompt
));
209 ast_copy_string(f
->sorryprompt
, sorryprompt
, sizeof(f
->sorryprompt
));
210 AST_LIST_HEAD_INIT_NOLOCK(&f
->numbers
);
211 AST_LIST_HEAD_INIT_NOLOCK(&f
->blnumbers
);
212 AST_LIST_HEAD_INIT_NOLOCK(&f
->wlnumbers
);
216 static void init_profile(struct call_followme
*f
)
219 ast_copy_string(f
->moh
, defaultmoh
, sizeof(f
->moh
));
224 /*! \brief Set parameter in profile from configuration file */
225 static void profile_set_param(struct call_followme
*f
, const char *param
, const char *val
, int linenum
, int failunknown
)
228 if (!strcasecmp(param
, "musicclass") || !strcasecmp(param
, "musiconhold") || !strcasecmp(param
, "music"))
229 ast_copy_string(f
->moh
, val
, sizeof(f
->moh
));
230 else if (!strcasecmp(param
, "context"))
231 ast_copy_string(f
->context
, val
, sizeof(f
->context
));
232 else if (!strcasecmp(param
, "takecall"))
233 ast_copy_string(f
->takecall
, val
, sizeof(f
->takecall
));
234 else if (!strcasecmp(param
, "declinecall"))
235 ast_copy_string(f
->nextindp
, val
, sizeof(f
->nextindp
));
236 else if (!strcasecmp(param
, "call-from-prompt"))
237 ast_copy_string(f
->callfromprompt
, val
, sizeof(f
->callfromprompt
));
238 else if (!strcasecmp(param
, "followme-norecording-prompt"))
239 ast_copy_string(f
->norecordingprompt
, val
, sizeof(f
->norecordingprompt
));
240 else if (!strcasecmp(param
, "followme-options-prompt"))
241 ast_copy_string(f
->optionsprompt
, val
, sizeof(f
->optionsprompt
));
242 else if (!strcasecmp(param
, "followme-pls-hold-prompt"))
243 ast_copy_string(f
->plsholdprompt
, val
, sizeof(f
->plsholdprompt
));
244 else if (!strcasecmp(param
, "followme-status-prompt"))
245 ast_copy_string(f
->statusprompt
, val
, sizeof(f
->statusprompt
));
246 else if (!strcasecmp(param
, "followme-sorry-prompt"))
247 ast_copy_string(f
->sorryprompt
, val
, sizeof(f
->sorryprompt
));
248 else if (failunknown
) {
250 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f
->name
, param
, linenum
);
252 ast_log(LOG_WARNING
, "Unknown keyword in profile '%s': %s\n", f
->name
, param
);
256 /*! \brief Add a new number */
257 static struct number
*create_followme_number(char *number
, int timeout
, int numorder
)
262 if (!(cur
= ast_calloc(1, sizeof(*cur
))))
265 cur
->timeout
= timeout
;
266 if ((tmp
= strchr(number
, ',')))
268 ast_copy_string(cur
->number
, number
, sizeof(cur
->number
));
269 cur
->order
= numorder
;
270 ast_debug(1, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur
->number
, cur
->order
, cur
->timeout
);
275 /*! \brief Reload followme application module */
276 static int reload_followme(int reload
)
278 struct call_followme
*f
;
279 struct ast_config
*cfg
;
280 char *cat
= NULL
, *tmp
;
281 struct ast_variable
*var
;
282 struct number
*cur
, *nm
;
287 const char *takecallstr
;
288 const char *declinecallstr
;
290 struct ast_flags config_flags
= { reload
? CONFIG_FLAG_FILEUNCHANGED
: 0 };
292 if (!(cfg
= ast_config_load("followme.conf", config_flags
))) {
293 ast_log(LOG_WARNING
, "No follow me config file (followme.conf), so no follow me\n");
295 } else if (cfg
== CONFIG_STATUS_FILEUNCHANGED
)
298 AST_RWLIST_WRLOCK(&followmes
);
300 /* Reset Global Var Values */
301 featuredigittimeout
= 5000;
303 /* Mark all profiles as inactive for the moment */
304 AST_RWLIST_TRAVERSE(&followmes
, f
, entry
) {
308 featuredigittostr
= ast_variable_retrieve(cfg
, "general", "featuredigittimeout");
310 if (!ast_strlen_zero(featuredigittostr
)) {
311 if (!sscanf(featuredigittostr
, "%d", &featuredigittimeout
))
312 featuredigittimeout
= 5000;
315 takecallstr
= ast_variable_retrieve(cfg
, "general", "takecall");
316 if (!ast_strlen_zero(takecallstr
))
317 ast_copy_string(takecall
, takecallstr
, sizeof(takecall
));
319 declinecallstr
= ast_variable_retrieve(cfg
, "general", "declinecall");
320 if (!ast_strlen_zero(declinecallstr
))
321 ast_copy_string(nextindp
, declinecallstr
, sizeof(nextindp
));
323 tmpstr
= ast_variable_retrieve(cfg
, "general", "call-from-prompt");
324 if (!ast_strlen_zero(tmpstr
))
325 ast_copy_string(callfromprompt
, tmpstr
, sizeof(callfromprompt
));
327 tmpstr
= ast_variable_retrieve(cfg
, "general", "norecording-prompt");
328 if (!ast_strlen_zero(tmpstr
))
329 ast_copy_string(norecordingprompt
, tmpstr
, sizeof(norecordingprompt
));
331 tmpstr
= ast_variable_retrieve(cfg
, "general", "options-prompt");
332 if (!ast_strlen_zero(tmpstr
))
333 ast_copy_string(optionsprompt
, tmpstr
, sizeof(optionsprompt
));
335 tmpstr
= ast_variable_retrieve(cfg
, "general", "pls-hold-prompt");
336 if (!ast_strlen_zero(tmpstr
))
337 ast_copy_string(plsholdprompt
, tmpstr
, sizeof(plsholdprompt
));
339 tmpstr
= ast_variable_retrieve(cfg
, "general", "status-prompt");
340 if (!ast_strlen_zero(tmpstr
))
341 ast_copy_string(statusprompt
, tmpstr
, sizeof(statusprompt
));
343 tmpstr
= ast_variable_retrieve(cfg
, "general", "sorry-prompt");
344 if (!ast_strlen_zero(tmpstr
))
345 ast_copy_string(sorryprompt
, tmpstr
, sizeof(sorryprompt
));
347 /* Chug through config file */
348 while ((cat
= ast_category_browse(cfg
, cat
))) {
351 if (!strcasecmp(cat
, "general"))
354 /* Look for an existing one */
355 AST_LIST_TRAVERSE(&followmes
, f
, entry
) {
356 if (!strcasecmp(f
->name
, cat
))
360 ast_debug(1, "New profile %s.\n", cat
);
364 f
= alloc_profile(cat
);
368 /* Totally fail if we fail to find/create an entry */
373 ast_mutex_lock(&f
->lock
);
374 /* Re-initialize the profile */
377 var
= ast_variable_browse(cfg
, cat
);
379 if (!strcasecmp(var
->name
, "number")) {
382 /* Add a new number */
383 ast_copy_string(numberstr
, var
->value
, sizeof(numberstr
));
384 if ((tmp
= strchr(numberstr
, ','))) {
386 timeoutstr
= ast_strdupa(tmp
);
387 if ((tmp
= strchr(timeoutstr
, ','))) {
389 numorder
= atoi(tmp
);
394 timeout
= atoi(timeoutstr
);
404 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
)
408 cur
= create_followme_number(numberstr
, timeout
, numorder
);
409 AST_LIST_INSERT_TAIL(&f
->numbers
, cur
, entry
);
411 profile_set_param(f
, var
->name
, var
->value
, var
->lineno
, 1);
412 ast_debug(2, "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_RWLIST_INSERT_HEAD(&followmes
, f
, entry
);
423 ast_config_destroy(cfg
);
425 AST_RWLIST_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;
471 static struct ast_channel
*wait_for_winner(struct findme_user_listptr
*findme_user_list
, struct number
*nm
, struct ast_channel
*caller
, char *namerecloc
, int *status
, struct fm_args
*tpargs
)
473 struct ast_channel
*watchers
[256];
475 struct ast_channel
*winner
;
479 struct findme_user
*tmpuser
;
481 int livechannels
= 0;
483 long totalwait
= 0, wtd
= 0, towas
= 0;
485 char *pressbuttonname
;
487 /* ------------ wait_for_winner_channel start --------------- */
489 callfromname
= ast_strdupa(tpargs
->callfromprompt
);
490 pressbuttonname
= ast_strdupa(tpargs
->optionsprompt
);
492 if (AST_LIST_EMPTY(findme_user_list
)) {
493 ast_verb(3, "couldn't reach at this number.\n");
498 ast_verb(3, "Original caller hungup. Cleanup.\n");
499 clear_calling_tree(findme_user_list
);
503 totalwait
= nm
->timeout
* 1000;
509 watchers
[0] = caller
;
513 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
) {
514 if (tmpuser
->state
>= 0 && tmpuser
->ochan
) {
515 if (tmpuser
->state
== 3)
516 tmpuser
->digts
+= (towas
- wtd
);
517 if (tmpuser
->digts
&& (tmpuser
->digts
> featuredigittimeout
)) {
518 ast_verb(3, "We've been waiting for digits longer than we should have.\n");
519 if (!ast_strlen_zero(namerecloc
)) {
522 if (!ast_streamfile(tmpuser
->ochan
, callfromname
, tmpuser
->ochan
->language
)) {
523 ast_sched_runq(tmpuser
->ochan
->sched
);
525 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
531 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
532 ast_sched_runq(tmpuser
->ochan
->sched
);
534 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
539 if (tmpuser
->ochan
->stream
) {
540 ast_sched_runq(tmpuser
->ochan
->sched
);
541 tmpto
= ast_sched_wait(tmpuser
->ochan
->sched
);
542 if (tmpto
> 0 && tmpto
< to
)
544 else if (tmpto
< 0 && !tmpuser
->ochan
->timingfunc
) {
545 ast_stopstream(tmpuser
->ochan
);
546 if (tmpuser
->state
== 1) {
547 ast_verb(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 ast_verb(3, "Playback of name file appears to be done.\n");
563 memset(tmpuser
->yn
, 0, sizeof(tmpuser
->yn
));
565 if (!ast_streamfile(tmpuser
->ochan
, pressbuttonname
, tmpuser
->ochan
->language
)) {
570 } else if (tmpuser
->state
== 3) {
571 ast_verb(3, "Playback of the next step file appears to be done.\n");
576 watchers
[pos
++] = tmpuser
->ochan
;
587 winner
= ast_waitfor_n(watchers
, pos
, &to
);
591 if (totalwait
<= 0) {
592 ast_verb(3, "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait
);
593 clear_calling_tree(findme_user_list
);
597 /* Need to find out which channel this is */
599 while ((winner
!= watchers
[dg
]) && (dg
< 256))
601 AST_LIST_TRAVERSE(findme_user_list
, tmpuser
, entry
)
602 if (tmpuser
->ochan
== winner
)
604 f
= ast_read(winner
);
606 if (f
->frametype
== AST_FRAME_CONTROL
) {
607 switch(f
->subclass
) {
608 case AST_CONTROL_HANGUP
:
609 ast_verb(3, "%s received a hangup frame.\n", winner
->name
);
610 if (f
->data
.uint32
) {
611 winner
->hangupcause
= f
->data
.uint32
;
614 ast_verb(3, "The calling channel hungup. Need to drop everyone else.\n");
615 clear_calling_tree(findme_user_list
);
619 case AST_CONTROL_ANSWER
:
620 ast_verb(3, "%s answered %s\n", winner
->name
, caller
->name
);
621 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
622 winner
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
623 caller
->hangupcause
= AST_CAUSE_NORMAL_CLEARING
;
624 ast_verb(3, "Starting playback of %s\n", callfromname
);
626 if (!ast_strlen_zero(namerecloc
)) {
627 if (!ast_streamfile(winner
, callfromname
, winner
->language
)) {
628 ast_sched_runq(winner
->sched
);
631 ast_log(LOG_WARNING
, "Unable to playback %s.\n", callfromname
);
637 if (!ast_streamfile(tmpuser
->ochan
, tpargs
->norecordingprompt
, tmpuser
->ochan
->language
))
638 ast_sched_runq(tmpuser
->ochan
->sched
);
640 ast_log(LOG_WARNING
, "Unable to playback %s.\n", tpargs
->norecordingprompt
);
647 case AST_CONTROL_BUSY
:
648 ast_verb(3, "%s is busy\n", winner
->name
);
650 case AST_CONTROL_CONGESTION
:
651 ast_verb(3, "%s is circuit-busy\n", winner
->name
);
653 case AST_CONTROL_RINGING
:
654 ast_verb(3, "%s is ringing\n", winner
->name
);
656 case AST_CONTROL_PROGRESS
:
657 ast_verb(3, "%s is making progress passing it to %s\n", winner
->name
, caller
->name
);
659 case AST_CONTROL_VIDUPDATE
:
660 ast_verb(3, "%s requested a video update, passing it to %s\n", winner
->name
, caller
->name
);
662 case AST_CONTROL_SRCUPDATE
:
663 ast_verb(3, "%s requested a source update, passing it to %s\n", winner
->name
, caller
->name
);
665 case AST_CONTROL_PROCEEDING
:
666 ast_verb(3, "%s is proceeding passing it to %s\n", winner
->name
,caller
->name
);
668 case AST_CONTROL_HOLD
:
669 ast_verb(3, "Call on %s placed on hold\n", winner
->name
);
671 case AST_CONTROL_UNHOLD
:
672 ast_verb(3, "Call on %s left from hold\n", winner
->name
);
674 case AST_CONTROL_OFFHOOK
:
675 case AST_CONTROL_FLASH
:
676 /* Ignore going off hook and flash */
679 ast_verb(3, "%s stopped sounds\n", winner
->name
);
682 ast_debug(1, "Dunno what to do with control type %d\n", f
->subclass
);
686 if (tmpuser
&& tmpuser
->state
== 3 && f
->frametype
== AST_FRAME_DTMF
) {
688 ast_stopstream(winner
);
690 ast_debug(1, "DTMF received: %c\n",(char) f
->subclass
);
691 tmpuser
->yn
[tmpuser
->ynidx
] = (char) f
->subclass
;
693 ast_debug(1, "DTMF string: %s\n", tmpuser
->yn
);
694 if (tmpuser
->ynidx
>= ynlongest
) {
695 ast_debug(1, "reached longest possible match - doing evals\n");
696 if (!strcmp(tmpuser
->yn
, tpargs
->takecall
)) {
697 ast_debug(1, "Match to take the call!\n");
699 return tmpuser
->ochan
;
701 if (!strcmp(tmpuser
->yn
, tpargs
->nextindp
)) {
702 ast_debug(1, "Next in dial plan step requested.\n");
714 ast_debug(1, "we didn't get a frame. hanging up. dg is %d\n",dg
);
716 clear_calling_tree(findme_user_list
);
722 ast_debug(1, "live channels left %d\n", livechannels
);
724 ast_verb(3, "no live channels left. exiting.\n");
732 ast_debug(1, "timed out waiting for action\n");
735 /* --- WAIT FOR WINNER NUMBER END! -----------*/
739 static void findmeexec(struct fm_args
*tpargs
)
742 struct ast_channel
*outbound
;
743 struct ast_channel
*caller
;
744 struct ast_channel
*winner
= NULL
;
748 struct findme_user
*tmpuser
;
749 struct findme_user
*fmuser
;
750 struct findme_user
*headuser
;
751 struct findme_user_listptr
*findme_user_list
;
754 findme_user_list
= ast_calloc(1, sizeof(*findme_user_list
));
755 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list
);
757 /* We're going to figure out what the longest possible string of digits to collect is */
759 if (strlen(tpargs
->takecall
) > ynlongest
)
760 ynlongest
= strlen(tpargs
->takecall
);
761 if (strlen(tpargs
->nextindp
) > ynlongest
)
762 ynlongest
= strlen(tpargs
->nextindp
);
765 caller
= tpargs
->chan
;
766 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
)
767 if (nm
->order
== idx
)
772 ast_debug(2, "Number %s timeout %ld\n", nm
->number
,nm
->timeout
);
775 number
= ast_strdupa(nm
->number
);
776 ast_debug(3, "examining %s\n", number
);
778 rest
= strchr(number
, '&');
784 if (!strcmp(tpargs
->context
, ""))
785 snprintf(dialarg
, sizeof(dialarg
), "%s", number
);
787 snprintf(dialarg
, sizeof(dialarg
), "%s@%s", number
, tpargs
->context
);
789 tmpuser
= ast_calloc(1, sizeof(*tmpuser
));
791 ast_log(LOG_WARNING
, "Out of memory!\n");
792 ast_free(findme_user_list
);
796 outbound
= ast_request("Local", ast_best_codec(caller
->nativeformats
), dialarg
, &dg
);
798 ast_set_callerid(outbound
, caller
->cid
.cid_num
, caller
->cid
.cid_name
, caller
->cid
.cid_num
);
799 ast_channel_inherit_variables(tpargs
->chan
, outbound
);
800 ast_verb(3, "calling %s\n", dialarg
);
801 if (!ast_call(outbound
,dialarg
,0)) {
802 tmpuser
->ochan
= outbound
;
804 tmpuser
->cleared
= 0;
805 ast_copy_string(tmpuser
->dialarg
, dialarg
, sizeof(dialarg
));
806 AST_LIST_INSERT_TAIL(findme_user_list
, tmpuser
, entry
);
808 ast_verb(3, "couldn't reach at this number.\n");
811 outbound
->cdr
= ast_cdr_alloc();
815 ast_cdr_init(outbound
->cdr
, outbound
);
816 snprintf(tmp
, sizeof(tmp
), "%s/%s", "Local", dialarg
);
817 ast_cdr_setapp(outbound
->cdr
, "FollowMe", tmp
);
818 ast_cdr_update(outbound
);
819 ast_cdr_start(outbound
->cdr
);
820 ast_cdr_end(outbound
->cdr
);
821 /* If the cause wasn't handled properly */
822 if (ast_cdr_disposition(outbound
->cdr
,outbound
->hangupcause
))
823 ast_cdr_failed(outbound
->cdr
);
825 ast_log(LOG_ERROR
, "Unable to create Call Detail Record\n");
826 ast_hangup(outbound
);
832 ast_log(LOG_WARNING
, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg
, ast_cause2str(dg
));
838 if (!AST_LIST_EMPTY(findme_user_list
))
839 winner
= wait_for_winner(findme_user_list
, nm
, caller
, tpargs
->namerecloc
, &status
, tpargs
);
841 while ((fmuser
= AST_LIST_REMOVE_HEAD(findme_user_list
, entry
))) {
842 if (!fmuser
->cleared
&& fmuser
->ochan
!= winner
)
843 clear_caller(fmuser
);
855 ast_free(findme_user_list
);
860 AST_LIST_TRAVERSE(&tpargs
->cnumbers
, nm
, entry
) {
861 if (nm
->order
== idx
)
865 ast_free(findme_user_list
);
869 tpargs
->status
= 100;
870 tpargs
->outbound
= winner
;
876 static int app_exec(struct ast_channel
*chan
, void *data
)
878 struct fm_args targs
;
879 struct ast_bridge_config config
;
880 struct call_followme
*f
;
881 struct number
*nm
, *newnm
;
884 char namerecloc
[255];
886 struct ast_channel
*caller
;
887 struct ast_channel
*outbound
;
888 static char toast
[80];
889 AST_DECLARE_APP_ARGS(args
,
890 AST_APP_ARG(followmeid
);
891 AST_APP_ARG(options
);
894 if (ast_strlen_zero(data
)) {
895 ast_log(LOG_WARNING
, "%s requires an argument (followmeid)\n", app
);
899 if (!(argstr
= ast_strdupa((char *)data
))) {
900 ast_log(LOG_ERROR
, "Out of memory!\n");
904 AST_STANDARD_APP_ARGS(args
, argstr
);
906 if (ast_strlen_zero(args
.followmeid
)) {
907 ast_log(LOG_WARNING
, "%s requires an argument (followmeid)\n", app
);
911 AST_RWLIST_RDLOCK(&followmes
);
912 AST_RWLIST_TRAVERSE(&followmes
, f
, entry
) {
913 if (!strcasecmp(f
->name
, args
.followmeid
) && (f
->active
))
916 AST_RWLIST_UNLOCK(&followmes
);
918 ast_debug(1, "New profile %s.\n", args
.followmeid
);
921 ast_log(LOG_WARNING
, "Profile requested, %s, not found in the configuration.\n", args
.followmeid
);
925 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
927 ast_app_parse_options(followme_opts
, &targs
.followmeflags
, NULL
, args
.options
);
929 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
930 ast_mutex_lock(&f
->lock
);
931 targs
.mohclass
= ast_strdupa(f
->moh
);
932 ast_copy_string(targs
.context
, f
->context
, sizeof(targs
.context
));
933 ast_copy_string(targs
.takecall
, f
->takecall
, sizeof(targs
.takecall
));
934 ast_copy_string(targs
.nextindp
, f
->nextindp
, sizeof(targs
.nextindp
));
935 ast_copy_string(targs
.callfromprompt
, f
->callfromprompt
, sizeof(targs
.callfromprompt
));
936 ast_copy_string(targs
.norecordingprompt
, f
->norecordingprompt
, sizeof(targs
.norecordingprompt
));
937 ast_copy_string(targs
.optionsprompt
, f
->optionsprompt
, sizeof(targs
.optionsprompt
));
938 ast_copy_string(targs
.plsholdprompt
, f
->plsholdprompt
, sizeof(targs
.plsholdprompt
));
939 ast_copy_string(targs
.statusprompt
, f
->statusprompt
, sizeof(targs
.statusprompt
));
940 ast_copy_string(targs
.sorryprompt
, f
->sorryprompt
, sizeof(targs
.sorryprompt
));
941 /* Copy the numbers we're going to use into another list in case the master list should get modified
942 (and locked) while we're trying to do a follow-me */
943 AST_LIST_HEAD_INIT_NOLOCK(&targs
.cnumbers
);
944 AST_LIST_TRAVERSE(&f
->numbers
, nm
, entry
) {
945 newnm
= create_followme_number(nm
->number
, nm
->timeout
, nm
->order
);
946 AST_LIST_INSERT_TAIL(&targs
.cnumbers
, newnm
, entry
);
948 ast_mutex_unlock(&f
->lock
);
950 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_STATUSMSG
))
951 ast_stream_and_wait(chan
, targs
.statusprompt
, "");
953 snprintf(namerecloc
,sizeof(namerecloc
),"%s/followme.%s",ast_config_AST_SPOOL_DIR
,chan
->uniqueid
);
956 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_RECORDNAME
))
957 if (ast_play_and_record(chan
, "vm-rec-name", namerecloc
, 5, "sln", &duration
, ast_dsp_get_threshold_from_settings(THRESHOLD_SILENCE
), 0, NULL
) < 0)
960 if (!ast_fileexists(namerecloc
, NULL
, chan
->language
))
961 ast_copy_string(namerecloc
, "", sizeof(namerecloc
));
963 if (ast_streamfile(chan
, targs
.plsholdprompt
, chan
->language
))
965 if (ast_waitstream(chan
, "") < 0)
967 ast_moh_start(chan
, S_OR(targs
.mohclass
, NULL
), NULL
);
971 ast_copy_string(targs
.namerecloc
, namerecloc
, sizeof(targs
.namerecloc
));
975 while ((nm
= AST_LIST_REMOVE_HEAD(&targs
.cnumbers
, entry
)))
978 if (!ast_strlen_zero(namerecloc
))
981 if (targs
.status
!= 100) {
983 if (ast_test_flag(&targs
.followmeflags
, FOLLOWMEFLAG_UNREACHABLEMSG
))
984 ast_stream_and_wait(chan
, targs
.sorryprompt
, "");
988 outbound
= targs
.outbound
;
989 /* Bridge the two channels. */
991 memset(&config
, 0, sizeof(config
));
992 ast_set_flag(&(config
.features_callee
), AST_FEATURE_REDIRECT
);
993 ast_set_flag(&(config
.features_callee
), AST_FEATURE_AUTOMON
);
994 ast_set_flag(&(config
.features_caller
), AST_FEATURE_AUTOMON
);
996 ast_moh_stop(caller
);
997 /* Be sure no generators are left on it */
998 ast_deactivate_generator(caller
);
999 /* Make sure channels are compatible */
1000 res
= ast_channel_make_compatible(caller
, outbound
);
1002 ast_log(LOG_WARNING
, "Had to drop call because I couldn't make %s compatible with %s\n", caller
->name
, outbound
->name
);
1003 ast_hangup(outbound
);
1007 res
= ast_bridge_call(caller
, outbound
, &config
);
1009 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- start_time
));
1010 pbx_builtin_setvar_helper(caller
, "DIALEDTIME", toast
);
1011 snprintf(toast
, sizeof(toast
), "%ld", (long)(end_time
- answer_time
));
1012 pbx_builtin_setvar_helper(caller
, "ANSWEREDTIME", toast
);
1014 ast_hangup(outbound
);
1022 static int unload_module(void)
1024 struct call_followme
*f
;
1026 ast_unregister_application(app
);
1028 /* Free Memory. Yeah! I'm free! */
1029 AST_RWLIST_WRLOCK(&followmes
);
1030 while ((f
= AST_RWLIST_REMOVE_HEAD(&followmes
, entry
))) {
1035 AST_RWLIST_UNLOCK(&followmes
);
1040 static int load_module(void)
1042 if(!reload_followme(0))
1043 return AST_MODULE_LOAD_DECLINE
;
1045 return ast_register_application(app
, app_exec
, synopsis
, descrip
);
1048 static int reload(void)
1055 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "Find-Me/Follow-Me Application",
1056 .load
= load_module
,
1057 .unload
= unload_module
,