update comment to match the state of the code
[asterisk-bristuff.git] / apps / app_followme.c
blobf4acd7bf19fac304dc4d6d2c94afa4195794b052
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 #include "asterisk.h"
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <signal.h>
41 #include "asterisk/lock.h"
42 #include "asterisk/file.h"
43 #include "asterisk/logger.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/options.h"
47 #include "asterisk/module.h"
48 #include "asterisk/translate.h"
49 #include "asterisk/say.h"
50 #include "asterisk/features.h"
51 #include "asterisk/musiconhold.h"
52 #include "asterisk/cli.h"
53 #include "asterisk/manager.h"
54 #include "asterisk/config.h"
55 #include "asterisk/monitor.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/causes.h"
58 #include "asterisk/astdb.h"
59 #include "asterisk/app.h"
61 static char *app = "FollowMe";
62 static char *synopsis = "Find-Me/Follow-Me application";
63 static char *descrip =
64 " FollowMe(followmeid|options):\n"
65 "This application performs Find-Me/Follow-Me functionality for the caller\n"
66 "as defined in the profile matching the <followmeid> parameter in\n"
67 "followme.conf. If the specified <followmeid> profile doesn't exist in\n"
68 "followme.conf, execution will be returned to the dialplan and call\n"
69 "execution will continue at the next priority.\n\n"
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 char language[MAX_LANGUAGE]; /*!< The language to be used on this dial, if used. */
82 int order; /*!< The order to dial in */
83 AST_LIST_ENTRY(number) entry; /*!< Next Number record */
86 /*! \brief Data structure for followme scripts */
87 struct call_followme {
88 ast_mutex_t lock;
89 char name[AST_MAX_EXTENSION]; /*!< Name - FollowMeID */
90 char moh[AST_MAX_CONTEXT]; /*!< Music On Hold Class to be used */
91 char context[AST_MAX_CONTEXT]; /*!< Context to dial from */
92 unsigned int active; /*!< Profile is active (1), or disabled (0). */
93 char takecall[20]; /*!< Digit mapping to take a call */
94 char nextindp[20]; /*!< Digit mapping to decline a call */
95 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
96 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
97 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
98 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
99 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
100 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
102 AST_LIST_HEAD_NOLOCK(numbers, number) numbers; /*!< Head of the list of follow-me numbers */
103 AST_LIST_HEAD_NOLOCK(blnumbers, number) blnumbers; /*!< Head of the list of black-listed numbers */
104 AST_LIST_HEAD_NOLOCK(wlnumbers, number) wlnumbers; /*!< Head of the list of white-listed numbers */
105 AST_LIST_ENTRY(call_followme) entry; /*!< Next Follow-Me record */
108 struct fm_args {
109 struct ast_channel *chan;
110 char *mohclass;
111 AST_LIST_HEAD_NOLOCK(cnumbers, number) cnumbers;
112 int status;
113 char context[AST_MAX_CONTEXT];
114 char namerecloc[AST_MAX_CONTEXT];
115 struct ast_channel *outbound;
116 char takecall[20]; /*!< Digit mapping to take a call */
117 char nextindp[20]; /*!< Digit mapping to decline a call */
118 char callfromprompt[PATH_MAX]; /*!< Sound prompt name and path */
119 char norecordingprompt[PATH_MAX]; /*!< Sound prompt name and path */
120 char optionsprompt[PATH_MAX]; /*!< Sound prompt name and path */
121 char plsholdprompt[PATH_MAX]; /*!< Sound prompt name and path */
122 char statusprompt[PATH_MAX]; /*!< Sound prompt name and path */
123 char sorryprompt[PATH_MAX]; /*!< Sound prompt name and path */
124 struct ast_flags followmeflags;
127 struct findme_user {
128 struct ast_channel *ochan;
129 int state;
130 char dialarg[256];
131 char yn[10];
132 int ynidx;
133 long digts;
134 int cleared;
135 AST_LIST_ENTRY(findme_user) entry;
138 enum {
139 FOLLOWMEFLAG_STATUSMSG = (1 << 0),
140 FOLLOWMEFLAG_RECORDNAME = (1 << 1),
141 FOLLOWMEFLAG_UNREACHABLEMSG = (1 << 2)
144 AST_APP_OPTIONS(followme_opts, {
145 AST_APP_OPTION('s', FOLLOWMEFLAG_STATUSMSG ),
146 AST_APP_OPTION('a', FOLLOWMEFLAG_RECORDNAME ),
147 AST_APP_OPTION('n', FOLLOWMEFLAG_UNREACHABLEMSG ),
150 static int ynlongest = 0;
151 static time_t start_time, answer_time, end_time;
153 static const char *featuredigittostr;
154 static int featuredigittimeout = 5000; /*!< Feature Digit Timeout */
155 static const char *defaultmoh = "default"; /*!< Default Music-On-Hold Class */
157 static char takecall[20] = "1", nextindp[20] = "2";
158 static char callfromprompt[PATH_MAX] = "followme/call-from";
159 static char norecordingprompt[PATH_MAX] = "followme/no-recording";
160 static char optionsprompt[PATH_MAX] = "followme/options";
161 static char plsholdprompt[PATH_MAX] = "followme/pls-hold-while-try";
162 static char statusprompt[PATH_MAX] = "followme/status";
163 static char sorryprompt[PATH_MAX] = "followme/sorry";
166 static AST_LIST_HEAD_STATIC(followmes, call_followme);
167 AST_LIST_HEAD_NOLOCK(findme_user_listptr, findme_user);
169 static void free_numbers(struct call_followme *f)
171 /* Free numbers attached to the profile */
172 struct number *prev;
174 while ((prev = AST_LIST_REMOVE_HEAD(&f->numbers, entry)))
175 /* Free the number */
176 free(prev);
177 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
179 while ((prev = AST_LIST_REMOVE_HEAD(&f->blnumbers, entry)))
180 /* Free the blacklisted number */
181 free(prev);
182 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
184 while ((prev = AST_LIST_REMOVE_HEAD(&f->wlnumbers, entry)))
185 /* Free the whitelisted number */
186 free(prev);
187 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
192 /*! \brief Allocate and initialize followme profile */
193 static struct call_followme *alloc_profile(const char *fmname)
195 struct call_followme *f;
197 if (!(f = ast_calloc(1, sizeof(*f))))
198 return NULL;
200 ast_mutex_init(&f->lock);
201 ast_copy_string(f->name, fmname, sizeof(f->name));
202 f->moh[0] = '\0';
203 f->context[0] = '\0';
204 ast_copy_string(f->takecall, takecall, sizeof(f->takecall));
205 ast_copy_string(f->nextindp, nextindp, sizeof(f->nextindp));
206 ast_copy_string(f->callfromprompt, callfromprompt, sizeof(f->callfromprompt));
207 ast_copy_string(f->norecordingprompt, norecordingprompt, sizeof(f->norecordingprompt));
208 ast_copy_string(f->optionsprompt, optionsprompt, sizeof(f->optionsprompt));
209 ast_copy_string(f->plsholdprompt, plsholdprompt, sizeof(f->plsholdprompt));
210 ast_copy_string(f->statusprompt, statusprompt, sizeof(f->statusprompt));
211 ast_copy_string(f->sorryprompt, sorryprompt, sizeof(f->sorryprompt));
212 AST_LIST_HEAD_INIT_NOLOCK(&f->numbers);
213 AST_LIST_HEAD_INIT_NOLOCK(&f->blnumbers);
214 AST_LIST_HEAD_INIT_NOLOCK(&f->wlnumbers);
215 return f;
218 static void init_profile(struct call_followme *f)
220 f->active = 1;
221 ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
226 /*! \brief Set parameter in profile from configuration file */
227 static void profile_set_param(struct call_followme *f, const char *param, const char *val, int linenum, int failunknown)
230 if (!strcasecmp(param, "musicclass") || !strcasecmp(param, "musiconhold") || !strcasecmp(param, "music"))
231 ast_copy_string(f->moh, val, sizeof(f->moh));
232 else if (!strcasecmp(param, "context"))
233 ast_copy_string(f->context, val, sizeof(f->context));
234 else if (!strcasecmp(param, "takecall"))
235 ast_copy_string(f->takecall, val, sizeof(f->takecall));
236 else if (!strcasecmp(param, "declinecall"))
237 ast_copy_string(f->nextindp, val, sizeof(f->nextindp));
238 else if (!strcasecmp(param, "call-from-prompt"))
239 ast_copy_string(f->callfromprompt, val, sizeof(f->callfromprompt));
240 else if (!strcasecmp(param, "followme-norecording-prompt"))
241 ast_copy_string(f->norecordingprompt, val, sizeof(f->norecordingprompt));
242 else if (!strcasecmp(param, "followme-options-prompt"))
243 ast_copy_string(f->optionsprompt, val, sizeof(f->optionsprompt));
244 else if (!strcasecmp(param, "followme-pls-hold-prompt"))
245 ast_copy_string(f->plsholdprompt, val, sizeof(f->plsholdprompt));
246 else if (!strcasecmp(param, "followme-status-prompt"))
247 ast_copy_string(f->statusprompt, val, sizeof(f->statusprompt));
248 else if (!strcasecmp(param, "followme-sorry-prompt"))
249 ast_copy_string(f->sorryprompt, val, sizeof(f->sorryprompt));
250 else if (failunknown) {
251 if (linenum >= 0)
252 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n", f->name, param, linenum);
253 else
254 ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
258 /*! \brief Add a new number */
259 static struct number *create_followme_number(char *number, char *language, int timeout, int numorder)
261 struct number *cur;
262 char *tmp;
265 if (!(cur = ast_calloc(1, sizeof(*cur))))
266 return NULL;
268 cur->timeout = timeout;
269 if ((tmp = strchr(number, ',')))
270 *tmp = '\0';
271 ast_copy_string(cur->number, number, sizeof(cur->number));
272 ast_copy_string(cur->language, language, sizeof(cur->language));
273 cur->order = numorder;
274 if (option_debug)
275 ast_log(LOG_DEBUG, "Created a number, %s, order of , %d, with a timeout of %ld.\n", cur->number, cur->order, cur->timeout);
277 return cur;
280 /*! \brief Reload followme application module */
281 static int reload_followme(void)
283 struct call_followme *f;
284 struct ast_config *cfg;
285 char *cat = NULL, *tmp;
286 struct ast_variable *var;
287 struct number *cur, *nm;
288 int new, idx;
289 char numberstr[90];
290 int timeout;
291 char *timeoutstr;
292 int numorder;
293 const char *takecallstr;
294 const char *declinecallstr;
295 const char *tmpstr;
297 cfg = ast_config_load("followme.conf");
298 if (!cfg) {
299 ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
300 return 0;
303 AST_LIST_LOCK(&followmes);
305 /* Reset Global Var Values */
306 featuredigittimeout = 5000;
308 /* Mark all profiles as inactive for the moment */
309 AST_LIST_TRAVERSE(&followmes, f, entry) {
310 f->active = 0;
312 featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
314 if (!ast_strlen_zero(featuredigittostr)) {
315 if (!sscanf(featuredigittostr, "%d", &featuredigittimeout))
316 featuredigittimeout = 5000;
319 takecallstr = ast_variable_retrieve(cfg, "general", "takecall");
320 if (!ast_strlen_zero(takecallstr))
321 ast_copy_string(takecall, takecallstr, sizeof(takecall));
323 declinecallstr = ast_variable_retrieve(cfg, "general", "declinecall");
324 if (!ast_strlen_zero(declinecallstr))
325 ast_copy_string(nextindp, declinecallstr, sizeof(nextindp));
327 tmpstr = ast_variable_retrieve(cfg, "general", "call-from-prompt");
328 if (!ast_strlen_zero(tmpstr))
329 ast_copy_string(callfromprompt, tmpstr, sizeof(callfromprompt));
331 tmpstr = ast_variable_retrieve(cfg, "general", "norecording-prompt");
332 if (!ast_strlen_zero(tmpstr))
333 ast_copy_string(norecordingprompt, tmpstr, sizeof(norecordingprompt));
335 tmpstr = ast_variable_retrieve(cfg, "general", "options-prompt");
336 if (!ast_strlen_zero(tmpstr))
337 ast_copy_string(optionsprompt, tmpstr, sizeof(optionsprompt));
339 tmpstr = ast_variable_retrieve(cfg, "general", "pls-hold-prompt");
340 if (!ast_strlen_zero(tmpstr))
341 ast_copy_string(plsholdprompt, tmpstr, sizeof(plsholdprompt));
343 tmpstr = ast_variable_retrieve(cfg, "general", "status-prompt");
344 if (!ast_strlen_zero(tmpstr))
345 ast_copy_string(statusprompt, tmpstr, sizeof(statusprompt));
347 tmpstr = ast_variable_retrieve(cfg, "general", "sorry-prompt");
348 if (!ast_strlen_zero(tmpstr))
349 ast_copy_string(sorryprompt, tmpstr, sizeof(sorryprompt));
351 /* Chug through config file */
352 while ((cat = ast_category_browse(cfg, cat))) {
353 if (!strcasecmp(cat, "general"))
354 continue;
355 /* Define a new profile */
356 /* Look for an existing one */
357 AST_LIST_TRAVERSE(&followmes, f, entry) {
358 if (!strcasecmp(f->name, cat))
359 break;
361 if (option_debug)
362 ast_log(LOG_DEBUG, "New profile %s.\n", cat);
363 if (!f) {
364 /* Make one then */
365 f = alloc_profile(cat);
366 new = 1;
367 } else
368 new = 0;
370 if (f) {
371 if (!new)
372 ast_mutex_lock(&f->lock);
373 /* Re-initialize the profile */
374 init_profile(f);
375 free_numbers(f);
376 var = ast_variable_browse(cfg, cat);
377 while(var) {
378 if (!strcasecmp(var->name, "number")) {
379 /* Add a new number */
380 ast_copy_string(numberstr, var->value, sizeof(numberstr));
381 if ((tmp = strchr(numberstr, ','))) {
382 *tmp = '\0';
383 tmp++;
384 timeoutstr = ast_strdupa(tmp);
385 if ((tmp = strchr(timeoutstr, ','))) {
386 *tmp = '\0';
387 tmp++;
388 numorder = atoi(tmp);
389 if (numorder < 0)
390 numorder = 0;
391 } else
392 numorder = 0;
393 timeout = atoi(timeoutstr);
394 if (timeout < 0)
395 timeout = 25;
396 } else {
397 timeout = 25;
398 numorder = 0;
401 if (!numorder) {
402 idx = 1;
403 AST_LIST_TRAVERSE(&f->numbers, nm, entry)
404 idx++;
405 numorder = idx;
407 cur = create_followme_number(numberstr, "", timeout, numorder);
408 AST_LIST_INSERT_TAIL(&f->numbers, cur, entry);
409 } else {
410 profile_set_param(f, var->name, var->value, var->lineno, 1);
411 if (option_debug > 1)
412 ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
414 var = var->next;
415 } /* End while(var) loop */
417 if (!new)
418 ast_mutex_unlock(&f->lock);
419 else
420 AST_LIST_INSERT_HEAD(&followmes, f, entry);
423 ast_config_destroy(cfg);
425 AST_LIST_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;
472 static struct ast_channel *wait_for_winner(struct findme_user_listptr *findme_user_list, struct number *nm, struct ast_channel *caller, char *namerecloc, int *status, struct fm_args *tpargs)
474 struct ast_channel *watchers[256];
475 int pos;
476 struct ast_channel *winner;
477 struct ast_frame *f;
478 int ctstatus;
479 int dg;
480 struct findme_user *tmpuser;
481 int to = 0;
482 int livechannels = 0;
483 int tmpto;
484 long totalwait = 0, wtd, towas = 0;
485 char *callfromname;
486 char *pressbuttonname;
488 /* ------------ wait_for_winner_channel start --------------- */
490 callfromname = ast_strdupa(tpargs->callfromprompt);
491 pressbuttonname = ast_strdupa(tpargs->optionsprompt);
493 if (!AST_LIST_EMPTY(findme_user_list)) {
494 if (!caller) {
495 if (option_verbose > 2)
496 ast_verbose(VERBOSE_PREFIX_3 "Original caller hungup. Cleanup.\n");
497 clear_calling_tree(findme_user_list);
498 return NULL;
500 ctstatus = 0;
501 totalwait = nm->timeout * 1000;
502 wtd = 0;
503 while (!ctstatus) {
504 to = 1000;
505 pos = 1;
506 livechannels = 0;
507 watchers[0] = caller;
509 dg = 0;
510 winner = NULL;
511 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry) {
512 if (tmpuser->state >= 0 && tmpuser->ochan) {
513 if (tmpuser->state == 3)
514 tmpuser->digts += (towas - wtd);
515 if (tmpuser->digts && (tmpuser->digts > featuredigittimeout)) {
516 if (option_verbose > 2)
517 ast_verbose(VERBOSE_PREFIX_3 "We've been waiting for digits longer than we should have.\n");
518 if (!ast_strlen_zero(namerecloc)) {
519 tmpuser->state = 1;
520 tmpuser->digts = 0;
521 if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
522 ast_sched_runq(tmpuser->ochan->sched);
523 } else {
524 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
525 return NULL;
527 } else {
528 tmpuser->state = 2;
529 tmpuser->digts = 0;
530 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
531 ast_sched_runq(tmpuser->ochan->sched);
532 else {
533 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
534 return NULL;
538 if (tmpuser->ochan->stream) {
539 ast_sched_runq(tmpuser->ochan->sched);
540 tmpto = ast_sched_wait(tmpuser->ochan->sched);
541 if (tmpto > 0 && tmpto < to)
542 to = tmpto;
543 else if (tmpto < 0 && !tmpuser->ochan->timingfunc) {
544 ast_stopstream(tmpuser->ochan);
545 if (tmpuser->state == 1) {
546 if (option_verbose > 2)
547 ast_verbose(VERBOSE_PREFIX_3 "Playback of the call-from file appears to be done.\n");
548 if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
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 if (option_verbose > 2)
563 ast_verbose(VERBOSE_PREFIX_3 "Playback of name file appears to be done.\n");
564 memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
565 tmpuser->ynidx = 0;
566 if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
567 tmpuser->state = 3;
569 } else {
570 return NULL;
572 } else if (tmpuser->state == 3) {
573 if (option_verbose > 2)
574 ast_verbose(VERBOSE_PREFIX_3 "Playback of the next step file appears to be done.\n");
575 tmpuser->digts = 0;
579 watchers[pos++] = tmpuser->ochan;
580 livechannels++;
584 tmpto = to;
585 if (to < 0) {
586 to = 1000;
587 tmpto = 1000;
589 towas = to;
590 winner = ast_waitfor_n(watchers, pos, &to);
591 tmpto -= to;
592 totalwait -= tmpto;
593 wtd = to;
594 if (totalwait <= 0) {
595 if (option_verbose > 2)
596 ast_verbose(VERBOSE_PREFIX_3 "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
597 clear_calling_tree(findme_user_list);
598 return NULL;
600 if (winner) {
601 /* Need to find out which channel this is */
602 dg = 0;
603 while ((winner != watchers[dg]) && (dg < 256))
604 dg++;
605 AST_LIST_TRAVERSE(findme_user_list, tmpuser, entry)
606 if (tmpuser->ochan == winner)
607 break;
608 f = ast_read(winner);
609 if (f) {
610 if (f->frametype == AST_FRAME_CONTROL) {
611 switch(f->subclass) {
612 case AST_CONTROL_HANGUP:
613 if (option_verbose > 2)
614 ast_verbose( VERBOSE_PREFIX_3 "%s received a hangup frame.\n", winner->name);
615 if (dg == 0) {
616 if (option_verbose > 2)
617 ast_verbose( VERBOSE_PREFIX_3 "The calling channel hungup. Need to drop everyone else.\n");
618 clear_calling_tree(findme_user_list);
619 ctstatus = -1;
621 break;
622 case AST_CONTROL_ANSWER:
623 if (option_verbose > 2)
624 ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", winner->name, caller->name);
625 /* If call has been answered, then the eventual hangup is likely to be normal hangup */
626 winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
627 caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
628 if (option_verbose > 2)
629 ast_verbose( VERBOSE_PREFIX_3 "Starting playback of %s\n", callfromname);
630 if (dg > 0) {
631 if (!ast_strlen_zero(namerecloc)) {
632 if (!ast_streamfile(winner, callfromname, winner->language)) {
633 ast_sched_runq(winner->sched);
634 tmpuser->state = 1;
635 } else {
636 ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
637 ast_frfree(f);
638 return NULL;
640 } else {
641 tmpuser->state = 2;
642 if (!ast_streamfile(tmpuser->ochan, tpargs->norecordingprompt, tmpuser->ochan->language))
643 ast_sched_runq(tmpuser->ochan->sched);
644 else {
645 ast_log(LOG_WARNING, "Unable to playback %s.\n", tpargs->norecordingprompt);
646 ast_frfree(f);
647 return NULL;
651 break;
652 case AST_CONTROL_BUSY:
653 if (option_verbose > 2)
654 ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", winner->name);
655 break;
656 case AST_CONTROL_CONGESTION:
657 if (option_verbose > 2)
658 ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", winner->name);
659 break;
660 case AST_CONTROL_RINGING:
661 if (option_verbose > 2)
662 ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", winner->name);
663 break;
664 case AST_CONTROL_PROGRESS:
665 if (option_verbose > 2)
666 ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", winner->name, caller->name);
667 break;
668 case AST_CONTROL_VIDUPDATE:
669 if (option_verbose > 2)
670 ast_verbose ( VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", winner->name, caller->name);
671 break;
672 case AST_CONTROL_PROCEEDING:
673 if (option_verbose > 2)
674 ast_verbose ( VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", winner->name,caller->name);
675 break;
676 case AST_CONTROL_HOLD:
677 if (option_verbose > 2)
678 ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", winner->name);
679 break;
680 case AST_CONTROL_UNHOLD:
681 if (option_verbose > 2)
682 ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", winner->name);
683 break;
684 case AST_CONTROL_OFFHOOK:
685 case AST_CONTROL_FLASH:
686 /* Ignore going off hook and flash */
687 break;
688 case -1:
689 if (option_verbose > 2)
690 ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
691 break;
692 default:
693 if (option_debug)
694 ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
695 break;
698 if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
699 if (winner->stream)
700 ast_stopstream(winner);
701 tmpuser->digts = 0;
702 if (option_debug)
703 ast_log(LOG_DEBUG, "DTMF received: %c\n",(char) f->subclass);
704 tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
705 tmpuser->ynidx++;
706 if (option_debug)
707 ast_log(LOG_DEBUG, "DTMF string: %s\n", tmpuser->yn);
708 if (tmpuser->ynidx >= ynlongest) {
709 if (option_debug)
710 ast_log(LOG_DEBUG, "reached longest possible match - doing evals\n");
711 if (!strcmp(tmpuser->yn, tpargs->takecall)) {
712 if (option_debug)
713 ast_log(LOG_DEBUG, "Match to take the call!\n");
714 ast_frfree(f);
715 return tmpuser->ochan;
717 if (!strcmp(tmpuser->yn, tpargs->nextindp)) {
718 if (option_debug)
719 ast_log(LOG_DEBUG, "Next in dial plan step requested.\n");
720 *status = 1;
721 ast_frfree(f);
722 return NULL;
728 ast_frfree(f);
729 } else {
730 if (winner) {
731 if (option_debug)
732 ast_log(LOG_DEBUG, "we didn't get a frame. hanging up. dg is %d\n",dg);
733 if (!dg) {
734 clear_calling_tree(findme_user_list);
735 return NULL;
736 } else {
737 tmpuser->state = -1;
738 ast_hangup(winner);
739 livechannels--;
740 if (option_debug)
741 ast_log(LOG_DEBUG, "live channels left %d\n", livechannels);
742 if (!livechannels) {
743 if (option_verbose > 2)
744 ast_verbose(VERBOSE_PREFIX_3 "no live channels left. exiting.\n");
745 return NULL;
751 } else
752 if (option_debug)
753 ast_log(LOG_DEBUG, "timed out waiting for action\n");
756 } else {
757 if (option_verbose > 2)
758 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
761 /* --- WAIT FOR WINNER NUMBER END! -----------*/
762 return NULL;
765 static void findmeexec(struct fm_args *tpargs)
767 struct number *nm;
768 struct ast_channel *outbound;
769 struct ast_channel *caller;
770 struct ast_channel *winner = NULL;
771 char dialarg[512];
772 int dg, idx;
773 char *rest, *number;
774 struct findme_user *tmpuser;
775 struct findme_user *fmuser;
776 struct findme_user *headuser;
777 struct findme_user_listptr *findme_user_list;
778 int status;
780 findme_user_list = ast_calloc(1, sizeof(*findme_user_list));
781 AST_LIST_HEAD_INIT_NOLOCK(findme_user_list);
783 /* We're going to figure out what the longest possible string of digits to collect is */
784 ynlongest = 0;
785 if (strlen(tpargs->takecall) > ynlongest)
786 ynlongest = strlen(tpargs->takecall);
787 if (strlen(tpargs->nextindp) > ynlongest)
788 ynlongest = strlen(tpargs->nextindp);
790 idx = 1;
791 caller = tpargs->chan;
792 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
793 if (nm->order == idx)
794 break;
796 while (nm) {
798 if (option_debug > 1)
799 ast_log(LOG_DEBUG, "Number %s timeout %ld\n", nm->number,nm->timeout);
800 time(&start_time);
802 number = ast_strdupa(nm->number);
803 if (option_debug > 2)
804 ast_log(LOG_DEBUG, "examining %s\n", number);
805 do {
806 rest = strchr(number, '&');
807 if (rest) {
808 *rest = 0;
809 rest++;
812 if (!strcmp(tpargs->context, ""))
813 sprintf(dialarg, "%s", number);
814 else
815 sprintf(dialarg, "%s@%s", number, tpargs->context);
817 tmpuser = ast_calloc(1, sizeof(*tmpuser));
818 if (!tmpuser) {
819 ast_log(LOG_WARNING, "Out of memory!\n");
820 free(findme_user_list);
821 return;
824 outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
825 if (outbound) {
826 ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
827 ast_channel_inherit_variables(tpargs->chan, outbound);
828 if (option_verbose > 2)
829 ast_verbose(VERBOSE_PREFIX_3 "calling %s\n", dialarg);
830 if (!ast_call(outbound,dialarg,0)) {
831 tmpuser->ochan = outbound;
832 tmpuser->state = 0;
833 tmpuser->cleared = 0;
834 ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
835 AST_LIST_INSERT_TAIL(findme_user_list, tmpuser, entry);
836 } else {
837 if (option_verbose > 2)
838 ast_verbose(VERBOSE_PREFIX_3 "couldn't reach at this number.\n");
839 if (outbound) {
840 if (!outbound->cdr)
841 outbound->cdr = ast_cdr_alloc();
842 if (outbound->cdr) {
843 char tmp[256];
845 ast_cdr_init(outbound->cdr, outbound);
846 snprintf(tmp, sizeof(tmp), "%s/%s", "Local", dialarg);
847 ast_cdr_setapp(outbound->cdr, "FollowMe", tmp);
848 ast_cdr_update(outbound);
849 ast_cdr_start(outbound->cdr);
850 ast_cdr_end(outbound->cdr);
851 /* If the cause wasn't handled properly */
852 if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
853 ast_cdr_failed(outbound->cdr);
854 } else {
855 ast_log(LOG_ERROR, "Unable to create Call Detail Record\n");
856 ast_hangup(outbound);
857 outbound = NULL;
862 } else
863 ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", dialarg, ast_cause2str(dg));
865 number = rest;
866 } while (number);
868 status = 0;
869 if (!AST_LIST_EMPTY(findme_user_list))
870 winner = wait_for_winner(findme_user_list, nm, caller, tpargs->namerecloc, &status, tpargs);
873 AST_LIST_TRAVERSE_SAFE_BEGIN(findme_user_list, fmuser, entry) {
874 if (!fmuser->cleared && fmuser->ochan != winner)
875 clear_caller(fmuser);
876 AST_LIST_REMOVE_CURRENT(findme_user_list, entry);
877 free(fmuser);
879 AST_LIST_TRAVERSE_SAFE_END
880 fmuser = NULL;
881 tmpuser = NULL;
882 headuser = NULL;
883 if (winner)
884 break;
886 if (!caller) {
887 tpargs->status = 1;
888 free(findme_user_list);
889 return;
892 idx++;
893 AST_LIST_TRAVERSE(&tpargs->cnumbers, nm, entry)
894 if (nm->order == idx)
895 break;
898 free(findme_user_list);
899 if (!winner)
900 tpargs->status = 1;
901 else {
902 tpargs->status = 100;
903 tpargs->outbound = winner;
907 return;
911 static int app_exec(struct ast_channel *chan, void *data)
913 struct fm_args targs;
914 struct ast_bridge_config config;
915 struct call_followme *f;
916 struct number *nm, *newnm;
917 int res = 0;
918 struct ast_module_user *u;
919 char *argstr;
920 char namerecloc[255];
921 char *fname = NULL;
922 int duration = 0;
923 struct ast_channel *caller;
924 struct ast_channel *outbound;
925 static char toast[80];
927 AST_DECLARE_APP_ARGS(args,
928 AST_APP_ARG(followmeid);
929 AST_APP_ARG(options);
932 if (!(argstr = ast_strdupa((char *)data))) {
933 ast_log(LOG_ERROR, "Out of memory!\n");
934 return -1;
937 if (!data) {
938 ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n",app);
939 return -1;
942 u = ast_module_user_add(chan);
944 AST_STANDARD_APP_ARGS(args, argstr);
946 if (!ast_strlen_zero(args.followmeid))
947 AST_LIST_LOCK(&followmes);
948 AST_LIST_TRAVERSE(&followmes, f, entry) {
949 if (!strcasecmp(f->name, args.followmeid) && (f->active))
950 break;
952 AST_LIST_UNLOCK(&followmes);
954 if (option_debug)
955 ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
956 if (!f) {
957 ast_log(LOG_WARNING, "Profile requested, %s, not found in the configuration.\n", args.followmeid);
958 res = 0;
959 } else {
960 /* XXX TODO: Reinsert the db check value to see whether or not follow-me is on or off */
963 if (args.options)
964 ast_app_parse_options(followme_opts, &targs.followmeflags, NULL, args.options);
966 /* Lock the profile lock and copy out everything we need to run with before unlocking it again */
967 ast_mutex_lock(&f->lock);
968 targs.mohclass = ast_strdupa(f->moh);
969 ast_copy_string(targs.context, f->context, sizeof(targs.context));
970 ast_copy_string(targs.takecall, f->takecall, sizeof(targs.takecall));
971 ast_copy_string(targs.nextindp, f->nextindp, sizeof(targs.nextindp));
972 ast_copy_string(targs.callfromprompt, f->callfromprompt, sizeof(targs.callfromprompt));
973 ast_copy_string(targs.norecordingprompt, f->norecordingprompt, sizeof(targs.norecordingprompt));
974 ast_copy_string(targs.optionsprompt, f->optionsprompt, sizeof(targs.optionsprompt));
975 ast_copy_string(targs.plsholdprompt, f->plsholdprompt, sizeof(targs.plsholdprompt));
976 ast_copy_string(targs.statusprompt, f->statusprompt, sizeof(targs.statusprompt));
977 ast_copy_string(targs.sorryprompt, f->sorryprompt, sizeof(targs.sorryprompt));
978 /* Copy the numbers we're going to use into another list in case the master list should get modified
979 (and locked) while we're trying to do a follow-me */
980 AST_LIST_HEAD_INIT_NOLOCK(&targs.cnumbers);
981 AST_LIST_TRAVERSE(&f->numbers, nm, entry) {
982 newnm = create_followme_number(nm->number, "", nm->timeout, nm->order);
983 AST_LIST_INSERT_TAIL(&targs.cnumbers, newnm, entry);
985 ast_mutex_unlock(&f->lock);
987 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_STATUSMSG))
988 ast_stream_and_wait(chan, targs.statusprompt, chan->language, "");
990 snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
991 duration = 5;
993 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_RECORDNAME))
994 if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL) < 0)
995 goto outrun;
997 if (!ast_fileexists(namerecloc, NULL, chan->language))
998 ast_copy_string(namerecloc, "", sizeof(namerecloc));
1000 if (ast_streamfile(chan, targs.plsholdprompt, chan->language))
1001 goto outrun;
1002 if (ast_waitstream(chan, "") < 0)
1003 goto outrun;
1004 ast_moh_start(chan, S_OR(targs.mohclass, NULL), NULL);
1006 targs.status = 0;
1007 targs.chan = chan;
1008 ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
1010 findmeexec(&targs);
1012 AST_LIST_TRAVERSE_SAFE_BEGIN(&targs.cnumbers, nm, entry) {
1013 AST_LIST_REMOVE_CURRENT(&targs.cnumbers, entry);
1014 free(nm);
1016 AST_LIST_TRAVERSE_SAFE_END
1017 if (targs.status != 100) {
1018 ast_moh_stop(chan);
1019 if (ast_test_flag(&targs.followmeflags, FOLLOWMEFLAG_UNREACHABLEMSG))
1020 ast_stream_and_wait(chan, targs.sorryprompt, chan->language, "");
1021 res = 0;
1022 } else {
1023 caller = chan;
1024 outbound = targs.outbound;
1025 /* Bridge the two channels. */
1027 memset(&config,0,sizeof(struct ast_bridge_config));
1028 ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
1029 ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
1030 ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
1032 ast_moh_stop(caller);
1033 /* Be sure no generators are left on it */
1034 ast_deactivate_generator(caller);
1035 /* Make sure channels are compatible */
1036 res = ast_channel_make_compatible(caller, outbound);
1037 if (res < 0) {
1038 ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
1039 ast_hangup(outbound);
1040 goto outrun;
1042 time(&answer_time);
1043 res = ast_bridge_call(caller,outbound,&config);
1044 time(&end_time);
1045 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
1046 pbx_builtin_setvar_helper(caller, "DIALEDTIME", toast);
1047 snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
1048 pbx_builtin_setvar_helper(caller, "ANSWEREDTIME", toast);
1049 if (outbound)
1050 ast_hangup(outbound);
1051 res = 1;
1054 outrun:
1056 if (!ast_strlen_zero(namerecloc)){
1057 fname = alloca(strlen(namerecloc) + 5);
1058 sprintf(fname, "%s.sln", namerecloc);
1059 unlink(fname);
1062 ast_module_user_remove(u);
1064 return res;
1067 static int unload_module(void)
1069 struct call_followme *f;
1071 ast_module_user_hangup_all();
1073 ast_unregister_application(app);
1075 /* Free Memory. Yeah! I'm free! */
1076 AST_LIST_LOCK(&followmes);
1077 while ((f = AST_LIST_REMOVE_HEAD(&followmes, entry))) {
1078 free_numbers(f);
1079 free(f);
1082 AST_LIST_UNLOCK(&followmes);
1084 return 0;
1087 static int load_module(void)
1089 if(!reload_followme())
1090 return AST_MODULE_LOAD_DECLINE;
1092 return ast_register_application(app, app_exec, synopsis, descrip);
1095 static int reload(void)
1097 reload_followme();
1099 return 0;
1102 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Find-Me/Follow-Me Application",
1103 .load = load_module,
1104 .unload = unload_module,
1105 .reload = reload,