Merged revisions 126573 via svnmerge from
[asterisk-bristuff.git] / apps / app_followme.c
blobe582cc5df35f6b10d1f8e1e293c7effbf9085ab2
1 /*
2 * Asterisk -- A telephony toolkit for Linux.
4 * A full-featured Find-Me/Follow-Me Application
5 *
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.
20 /*! \file
22 * \brief Find-Me Follow-Me application
24 * \author BJ Weschke <bweschke@btwtech.com>
26 * \arg See \ref Config_followme
28 * \ingroup applications
31 /*** MODULEINFO
32 <depend>chan_local</depend>
33 ***/
35 #include "asterisk.h"
37 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
39 #include <signal.h>
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"
70 " Options:\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 */
78 struct number {
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 {
87 ast_mutex_t lock;
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 */
107 struct fm_args {
108 struct ast_channel *chan;
109 char *mohclass;
110 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
111 int status;
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;
126 struct findme_user {
127 struct ast_channel *ochan;
128 int state;
129 char dialarg[256];
130 char yn[10];
131 int ynidx;
132 long digts;
133 int cleared;
134 AST_LIST_ENTRY(findme_user) entry;
137 enum {
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 */
171 struct number *prev;
173 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
174 /* Free the number */
175 ast_free(prev);
176 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
178 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
179 /* Free the blacklisted number */
180 ast_free(prev);
181 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
183 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
184 /* Free the whitelisted number */
185 ast_free(prev);
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))))
196 return NULL;
198 ast_mutex_init(&f->lock);
199 ast_copy_string(f->name, fmname, sizeof(f->name));
200 f->moh[0] = '\0';
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);
213 return f;
216 static void init_profile(struct call_followme *f)
218 f->active = 1;
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) {
249 if (linenum >= 0)
250 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
251 else
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)
259 struct number *cur;
260 char *tmp;
262 if (!(cur = ast_calloc(1, sizeof(*cur))))
263 return NULL;
265 cur->timeout = timeout;
266 if ((tmp = strchr(number, ',')))
267 *tmp = '\0';
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);
272 return cur;
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;
283 char numberstr[90];
284 int timeout;
285 char *timeoutstr;
286 int numorder;
287 const char *takecallstr;
288 const char *declinecallstr;
289 const char *tmpstr;
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");
294 return 0;
295 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
296 return 0;
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) {
305 f->active = 0;
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))) {
349 int new = 0;
351 if (!strcasecmp(cat, "general"))
352 continue;
354 /* Look for an existing one */
355 AST_LIST_TRAVERSE(&followmes, f, entry) {
356 if (!strcasecmp(f->name, cat))
357 break;
360 ast_debug(1, "New profile %s.\n", cat);
362 if (!f) {
363 /* Make one then */
364 f = alloc_profile(cat);
365 new = 1;
368 /* Totally fail if we fail to find/create an entry */
369 if (!f)
370 continue;
372 if (!new)
373 ast_mutex_lock(&f->lock);
374 /* Re-initialize the profile */
375 init_profile(f);
376 free_numbers(f);
377 var = ast_variable_browse(cfg, cat);
378 while(var) {
379 if (!strcasecmp(var->name, "number")) {
380 int idx = 0;
382 /* Add a new number */
383 ast_copy_string(numberstr, var->value, sizeof(numberstr));
384 if ((tmp = strchr(numberstr, ','))) {
385 *tmp++ = '\0';
386 timeoutstr = ast_strdupa(tmp);
387 if ((tmp = strchr(timeoutstr, ','))) {
388 *tmp++ = '\0';
389 numorder = atoi(tmp);
390 if (numorder < 0)
391 numorder = 0;
392 } else
393 numorder = 0;
394 timeout = atoi(timeoutstr);
395 if (timeout < 0)
396 timeout = 25;
397 } else {
398 timeout = 25;
399 numorder = 0;
402 if (!numorder) {
403 idx = 1;
404 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
405 idx++;
406 numorder = idx;
408 cur = create_followme_number(numberstr, timeout, numorder);
409 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
410 } else {
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);
414 var = var->next;
415 } /* End while(var) loop */
417 if (!new)
418 ast_mutex_unlock(&f->lock);
419 else
420 AST_RWLIST_INSERT_HEAD(&followmes, f, entry);
423 ast_config_destroy(cfg);
425 AST_RWLIST_UNLOCK(&followmes);
427 return 1;
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();
438 if (outbound->cdr)
439 ast_cdr_init(outbound->cdr, outbound);
441 if (outbound->cdr) {
442 char tmp[256];
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);
452 } else
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];
474 int pos;
475 struct ast_channel *winner;
476 struct ast_frame *f;
477 int ctstatus = 0;
478 int dg;
479 struct findme_user *tmpuser;
480 int to = 0;
481 int livechannels = 0;
482 int tmpto;
483 long totalwait = 0, wtd = 0, towas = 0;
484 char *callfromname;
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");
494 return NULL;
497 if (!caller) {
498 ast_verb(3, "Original caller hungup. Cleanup.\n");
499 clear_calling_tree(findme_user_list);
500 return NULL;
503 totalwait = nm->timeout * 1000;
505 while (!ctstatus) {
506 to = 1000;
507 pos = 1;
508 livechannels = 0;
509 watchers[0] = caller;
511 dg = 0;
512 winner = NULL;
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)) {
520 tmpuser->state = 1;
521 tmpuser->digts = 0;
522 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
523 ast_sched_runq(tmpuser->ochan->sched);
524 } else {
525 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
526 return NULL;
528 } else {
529 tmpuser->state = 2;
530 tmpuser->digts = 0;
531 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
532 ast_sched_runq(tmpuser->ochan->sched);
533 else {
534 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
535 return NULL;
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)
543 to = tmpto;
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)) {
549 tmpuser->state = 2;
550 } else {
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));
553 tmpuser->ynidx = 0;
554 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language))
555 tmpuser->state = 3;
556 else {
557 ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
558 return NULL;
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));
564 tmpuser->ynidx = 0;
565 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
566 tmpuser->state = 3;
567 } else {
568 return NULL;
570 } else if (tmpuser->state == 3) {
571 ast_verb(3, "Playback of the next step file appears to be done.\n");
572 tmpuser->digts = 0;
576 watchers[pos++] = tmpuser->ochan;
577 livechannels++;
581 tmpto = to;
582 if (to < 0) {
583 to = 1000;
584 tmpto = 1000;
586 towas = to;
587 winner = ast_waitfor_n(watchers, pos, &to);
588 tmpto -= to;
589 totalwait -= tmpto;
590 wtd = 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);
594 return NULL;
596 if (winner) {
597 /* Need to find out which channel this is */
598 dg = 0;
599 while ((winner != watchers[dg]) && (dg < 256))
600 dg++;
601 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
602 if (tmpuser->ochan == winner)
603 break;
604 f = ast_read(winner);
605 if (f) {
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;
613 if (dg == 0) {
614 ast_verb(3, "The calling channel hungup. Need to drop everyone else.\n");
615 clear_calling_tree(findme_user_list);
616 ctstatus = -1;
618 break;
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);
625 if (dg > 0) {
626 if (!ast_strlen_zero(namerecloc)) {
627 if (!ast_streamfile(winner, callfromname, winner->language)) {
628 ast_sched_runq(winner->sched);
629 tmpuser->state = 1;
630 } else {
631 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
632 ast_frfree(f);
633 return NULL;
635 } else {
636 tmpuser->state = 2;
637 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
638 ast_sched_runq(tmpuser->ochan->sched);
639 else {
640 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
641 ast_frfree(f);
642 return NULL;
646 break;
647 case AST_CONTROL_BUSY:
648 ast_verb(3, "%s is busy\n", winner->name);
649 break;
650 case AST_CONTROL_CONGESTION:
651 ast_verb(3, "%s is circuit-busy\n", winner->name);
652 break;
653 case AST_CONTROL_RINGING:
654 ast_verb(3, "%s is ringing\n", winner->name);
655 break;
656 case AST_CONTROL_PROGRESS:
657 ast_verb(3, "%s is making progress passing it to %s\n", winner->name, caller->name);
658 break;
659 case AST_CONTROL_VIDUPDATE:
660 ast_verb(3, "%s requested a video update, passing it to %s\n", winner->name, caller->name);
661 break;
662 case AST_CONTROL_SRCUPDATE:
663 ast_verb(3, "%s requested a source update, passing it to %s\n", winner->name, caller->name);
664 break;
665 case AST_CONTROL_PROCEEDING:
666 ast_verb(3, "%s is proceeding passing it to %s\n", winner->name,caller->name);
667 break;
668 case AST_CONTROL_HOLD:
669 ast_verb(3, "Call on %s placed on hold\n", winner->name);
670 break;
671 case AST_CONTROL_UNHOLD:
672 ast_verb(3, "Call on %s left from hold\n", winner->name);
673 break;
674 case AST_CONTROL_OFFHOOK:
675 case AST_CONTROL_FLASH:
676 /* Ignore going off hook and flash */
677 break;
678 case -1:
679 ast_verb(3, "%s stopped sounds\n", winner->name);
680 break;
681 default:
682 ast_debug(1, "Dunno what to do with control type %d\n", f->subclass);
683 break;
686 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
687 if (winner->stream)
688 ast_stopstream(winner);
689 tmpuser->digts = 0;
690 ast_debug(1, "DTMF received: %c\n",(char) f->subclass);
691 tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
692 tmpuser->ynidx++;
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");
698 ast_frfree(f);
699 return tmpuser->ochan;
701 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
702 ast_debug(1, "Next in dial plan step requested.\n");
703 *status = 1;
704 ast_frfree(f);
705 return NULL;
711 ast_frfree(f);
712 } else {
713 if (winner) {
714 ast_debug(1, "we didn't get a frame. hanging up. dg is %d\n",dg);
715 if (!dg) {
716 clear_calling_tree(findme_user_list);
717 return NULL;
718 } else {
719 tmpuser->state = -1;
720 ast_hangup(winner);
721 livechannels--;
722 ast_debug(1, "live channels left %d\n", livechannels);
723 if (!livechannels) {
724 ast_verb(3, "no live channels left. exiting.\n");
725 return NULL;
731 } else
732 ast_debug(1, "timed out waiting for action\n");
735 /* --- WAIT FOR WINNER NUMBER END! -----------*/
736 return NULL;
739 static void findmeexec(struct fm_args *tpargs)
741 struct number *nm;
742 struct ast_channel *outbound;
743 struct ast_channel *caller;
744 struct ast_channel *winner = NULL;
745 char dialarg[512];
746 int dg, idx;
747 char *rest, *number;
748 struct findme_user *tmpuser;
749 struct findme_user *fmuser;
750 struct findme_user *headuser;
751 struct findme_user_listptr *findme_user_list;
752 int status;
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 */
758 ynlongest = 0;
759 if (strlen(tpargs->takecall) > ynlongest)
760 ynlongest = strlen(tpargs->takecall);
761 if (strlen(tpargs->nextindp) > ynlongest)
762 ynlongest = strlen(tpargs->nextindp);
764 idx = 1;
765 caller = tpargs->chan;
766 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
767 if (nm->order == idx)
768 break;
770 while (nm) {
772 ast_debug(2, "Number %s timeout %ld\n", nm->number,nm->timeout);
773 time(&start_time);
775 number = ast_strdupa(nm->number);
776 ast_debug(3, "examining %s\n", number);
777 do {
778 rest = strchr(number, '&');
779 if (rest) {
780 *rest = 0;
781 rest++;
784 if (!strcmp(tpargs->context, ""))
785 snprintf(dialarg, sizeof(dialarg), "%s", number);
786 else
787 snprintf(dialarg, sizeof(dialarg), "%s@%s", number, tpargs->context);
789 tmpuser = ast_calloc(1, sizeof(*tmpuser));
790 if (!tmpuser) {
791 ast_log(LOG_WARNING, "Out of memory!\n");
792 ast_free(findme_user_list);
793 return;
796 outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
797 if (outbound) {
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;
803 tmpuser->state = 0;
804 tmpuser->cleared = 0;
805 ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
806 AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
807 } else {
808 ast_verb(3, "couldn't reach at this number.\n");
809 if (outbound) {
810 if (!outbound->cdr)
811 outbound->cdr = ast_cdr_alloc();
812 if (outbound->cdr) {
813 char tmp[256];
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);
824 } else {
825 ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
826 ast_hangup(outbound);
827 outbound = NULL;
831 } else
832 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
834 number = rest;
835 } while (number);
837 status = 0;
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);
844 ast_free(fmuser);
847 fmuser = NULL;
848 tmpuser = NULL;
849 headuser = NULL;
850 if (winner)
851 break;
853 if (!caller) {
854 tpargs->status = 1;
855 ast_free(findme_user_list);
856 return;
859 idx++;
860 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry) {
861 if (nm->order == idx)
862 break;
865 ast_free(findme_user_list);
866 if (!winner)
867 tpargs->status = 1;
868 else {
869 tpargs->status = 100;
870 tpargs->outbound = winner;
873 return;
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;
882 int res = 0;
883 char *argstr;
884 char namerecloc[255];
885 int duration = 0;
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);
896 return -1;
899 if (!(argstr = ast_strdupa((char *)data))) {
900 ast_log(LOG_ERROR, "Out of memory!\n");
901 return -1;
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);
908 return -1;
911 AST_RWLIST_RDLOCK(&followmes);
912 AST_RWLIST_TRAVERSE(&followmes, f, entry) {
913 if (!strcasecmp(f->name, args.followmeid) && (f->active))
914 break;
916 AST_RWLIST_UNLOCK(&followmes);
918 ast_debug(1, "New profile %s.\n", args.followmeid);
920 if (!f) {
921 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
922 return 0;
925 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
926 if (args.options)
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);
954 duration = 5;
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)
958 goto outrun;
960 if (!ast_fileexists(namerecloc, NULL, chan->language))
961 ast_copy_string(namerecloc, "", sizeof(namerecloc));
963 if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
964 goto outrun;
965 if (ast_waitstream(chan, "") < 0)
966 goto outrun;
967 ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
969 targs.status = 0;
970 targs.chan = chan;
971 ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
973 findmeexec(&targs);
975 while ((nm = AST_LIST_REMOVE_HEAD(&targs.cnumbers, entry)))
976 ast_free(nm);
978 if (!ast_strlen_zero(namerecloc))
979 unlink(namerecloc);
981 if (targs.status != 100) {
982 ast_moh_stop(chan);
983 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG))
984 ast_stream_and_wait(chan, targs.sorryprompt, "");
985 res = 0;
986 } else {
987 caller = chan;
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);
1001 if (res < 0) {
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);
1004 goto outrun;
1006 time(&answer_time);
1007 res = ast_bridge_call(caller, outbound, &config);
1008 time(&end_time);
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);
1013 if (outbound)
1014 ast_hangup(outbound);
1017 outrun:
1019 return res;
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))) {
1031 free_numbers(f);
1032 ast_free(f);
1035 AST_RWLIST_UNLOCK(&followmes);
1037 return 0;
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)
1050 reload_followme(1);
1052 return 0;
1055 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1056 .load = load_module,
1057 .unload = unload_module,
1058 .reload = reload,