[asterisk-commits] branch bweschke/findme_followme r9214 -
/team/bweschke/findme_followme/apps/
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Wed Feb 8 08:38:22 MST 2006
Author: bweschke
Date: Wed Feb 8 09:38:21 2006
New Revision: 9214
URL: http://svn.digium.com/view/asterisk?rev=9214&view=rev
Log:
Fix compile errors and strip \r chars
Modified:
team/bweschke/findme_followme/apps/app_followme.c
Modified: team/bweschke/findme_followme/apps/app_followme.c
URL: http://svn.digium.com/view/asterisk/team/bweschke/findme_followme/apps/app_followme.c?rev=9214&r1=9213&r2=9214&view=diff
==============================================================================
--- team/bweschke/findme_followme/apps/app_followme.c (original)
+++ team/bweschke/findme_followme/apps/app_followme.c Wed Feb 8 09:38:21 2006
@@ -1,1020 +1,1022 @@
-/*
- * Asterisk -- A telephony toolkit for Linux.
- *
- * A full-featured Find-Me/Follow-Me Application
- *
- * Copyright (C) 2005, BJ Weschke
- *
- * BJ Weschke <bweschke at btwtech.com>
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <signal.h>
-
-#include "asterisk.h"
-
-ASTERISK_FILE_VERSION(__FILE__, "$version$")
-
-#include "asterisk/lock.h"
-#include "asterisk/file.h"
-#include "asterisk/logger.h"
-#include "asterisk/channel.h"
-#include "asterisk/pbx.h"
-#include "asterisk/options.h"
-#include "asterisk/module.h"
-#include "asterisk/translate.h"
-#include "asterisk/say.h"
-#include "asterisk/features.h"
-#include "asterisk/musiconhold.h"
-#include "asterisk/cli.h"
-#include "asterisk/manager.h"
-#include "asterisk/config.h"
-#include "asterisk/monitor.h"
-#include "asterisk/utils.h"
-#include "asterisk/causes.h"
-#include "asterisk/astdb.h"
-#include "asterisk/app.h"
-
-static char *tdesc = "Find-Me/Follow-Me Application";
-static char *app = "FollowMe";
-static char *synopsis =
-"Find-Me/Follow-Me.";
-static char *descrip =
-" FollowMe(followmeid):\n"
-"Performs Find-Me/Follow-Me functionality as defined in followme.conf.\n";
-
-static char *defaultmoh = "default"; /* Default Music-On-Hold Class */
-
-STANDARD_LOCAL_USER;
-
-LOCAL_USER_DECL;
-
-struct number {
- char number[512]; /* Phone Number and/or Extension */
- unsigned int timeout; /* Dial Timeout, if used. */
- struct number *next; /* Next Number record */
-};
-
-struct ast_call_followme {
- ast_mutex_t lock;
- char name[AST_MAX_EXTENSION]; /* Name - FollowMeID */
- char moh[AST_MAX_CONTEXT]; /* Music On Hold Class to be used */
- char context[AST_MAX_CONTEXT]; /* Context to dial from */
- unsigned int active; /* Profile is active (1), or disabled (0). */
- char vm[AST_MAX_CONTEXT]; /* Voicemail Box Defined for the profile */
-
- struct number *numbers; /* Head of the list of follow-me numbers */
- struct number *blnumbers; /* Head of the list of black-listed numbers */
- struct number *wlnumbers; /* Head of the list of white-listed numbers */
- struct ast_call_followme *next; /* Next Follow-Me record */
-};
-
-struct thread_args {
- struct ast_channel *chan;
- struct number *numbers;
- int status;
- char context[AST_MAX_CONTEXT];
- char namerecloc[AST_MAX_CONTEXT];
- struct ast_channel *outbound;
-};
-
-struct findme_user {
- struct ast_channel *ochan;
- int state;
- char dialarg[256];
- struct findme_user *next;
-};
-
-struct thread_args targs;
-struct ast_bridge_config config;
-int res, ret = 0;
-char toast[80];
-time_t start_time, answer_time, end_time;
-
-char *featuredigittostr;
-int featuredigittimeout = 5000;
-int maxretries = 3;
-
-char takecall[20] = "1", nextindp[20] = "2", nextinfmfm[20] = "", blindxferexten[20] = "", atxferexten[20] = "";
-
-static struct ast_call_followme *followmes = NULL;
-AST_MUTEX_DEFINE_STATIC(fmlock);
-
-
-static void free_numbers(struct ast_call_followme *f, int all)
-{
- /* Free numbers attached to the profile */
- struct number *curn, *next, *prev;
-
- curn = f->numbers;
- prev = NULL;
- while(curn) {
- next = curn->next;
- if (all) {
- if (prev)
- prev->next = next;
- else
- f->numbers = next;
- free(curn);
- } else
- prev = curn;
- curn = next;
- }
-
- curn = f->blnumbers;
- prev = NULL;
- while(curn) {
- next = curn->next;
- if (all) {
- if (prev)
- prev->next = next;
- else
- f->blnumbers = next;
- free(curn);
- } else
- prev = curn;
- curn = next;
- }
-
- curn = f->wlnumbers;
- prev = NULL;
- while(curn) {
- next = curn->next;
- if (all) {
- if (prev)
- prev->next = next;
- else
- f->wlnumbers = next;
- free(curn);
- } else
- prev = curn;
- curn = next;
- }
-
-}
-
-
-static struct ast_call_followme *alloc_profile(const char *fmname)
-{
- struct ast_call_followme *f;
-
- f = malloc(sizeof(*f));
- if (f) {
- memset(f, 0, sizeof(*f));
- ast_mutex_init(&f->lock);
- ast_copy_string(f->name, fmname, sizeof(f->name));
- ast_copy_string(f->moh, "", sizeof(f->moh));
- ast_copy_string(f->context, "", sizeof(f->context));
- ast_copy_string(f->vm, "", sizeof(f->vm));
- }
- return f;
-}
-
-static void init_profile(struct ast_call_followme *f)
-{
- f->active = 1;
- ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
-}
-
-
-
-static void profile_set_param(struct ast_call_followme *f, const char *param, const char *val, int linenum, int failunknown)
-{
- if (!strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
- ast_copy_string(f->moh, val, sizeof(f->moh));
- } else if (!strcasecmp(param, "context")) {
- ast_copy_string(f->context, val, sizeof(f->context));
- } else if (!strcasecmp(param, "vmbox")) {
- ast_copy_string(f->vm, val, sizeof(f->vm));
- } else if(failunknown) {
- if (linenum >= 0) {
- ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n",
- f->name, param, linenum);
- } else {
- ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
- }
- }
-}
-
-static struct number *create_followme_number(char *number, int timeout)
-{
- struct number *cur;
- char *tmp;
-
- /* Add a new number */
-
- cur = malloc(sizeof(struct number));
-
- if (cur) {
- memset(cur, 0, sizeof(struct number));
- cur->timeout = timeout;
- if (strchr(number, ','))
- {
- tmp = strchr(number, ',');
- *tmp = '\0';
- }
- ast_copy_string(cur->number, number, sizeof(cur->number));
- ast_log(LOG_DEBUG, "Created a number, %s, with a timeout of %d.\n", cur->number, cur->timeout);
-
- }
-
- return cur;
-}
-
-static void reload_followme(void)
-{
- struct ast_call_followme *f;
- struct ast_config *cfg;
- char *cat, *tmp;
- struct ast_variable *var;
- struct number *prev, *cur;
- int new;
- char numberstr[90];
- int timeout;
-
- cfg = ast_config_load("followme.conf");
- if (!cfg) {
- ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
- return;
- }
- ast_mutex_lock(&fmlock);
- /* Mark all profiles as inactive for the moment */
- f = followmes;
- while(f) {
- f->active = 0;
- f = f->next;
- }
- featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
-
- if (!ast_strlen_zero(featuredigittostr)) {
- if (!scanf("%d", &featuredigittimeout))
- featuredigittimeout = 5000;
- }
-
-
- /* Chug through config file */
- cat = ast_category_browse(cfg, NULL);
- while(cat) {
- /* Define a new profile */
- /* Look for an existing one */
- f = followmes;
- while(f) {
- if (!strcmp(f->name, cat))
- break;
- f = f->next;
- }
- ast_log(LOG_DEBUG, "New profile %s.\n", cat);
- if (!f) {
- /* Make one then */
- f = alloc_profile(cat);
- new = 1;
- } else
- new = 0;
- if (f) {
- if (!new)
- ast_mutex_lock(&f->lock);
- /* Re-initialize the profile */
- init_profile(f);
- free_numbers(f, 1);
- prev = f->numbers;
- if (prev) {
- /* find the end of any dynamic numbers */
- while(prev->next)
- prev = prev->next;
- }
- var = ast_variable_browse(cfg, cat);
- while(var) {
- if (!strcasecmp(var->name, "number")) {
- /* Add a new number */
- ast_copy_string(numberstr, var->value, sizeof(numberstr));
- if ((tmp = strchr(numberstr, ','))) {
- *tmp = '\0';
- tmp++;
- timeout = atoi(tmp);
- if (timeout < 0) {
- timeout = 12;
- }
- } else
- timeout = 12;
- cur = create_followme_number(numberstr, timeout);
-
- if (cur) {
- if (prev)
- prev->next = cur;
- else
- f->numbers = cur;
- prev = cur;
- }
- } else {
- profile_set_param(f, var->name, var->value, var->lineno, 1);
- ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
- }
- var = var->next;
- }
- if (!new)
- ast_mutex_unlock(&f->lock);
- if (new) {
- f->next = followmes;
- followmes = f;
- }
- }
- cat = ast_category_browse(cfg, cat);
- }
- ast_config_destroy(cfg);
- ast_mutex_unlock(&fmlock);
-}
-
-static void clear_calling_tree(struct findme_user *headuser)
-{
- struct findme_user *tmpuser = NULL;
- struct ast_channel *outbound = NULL;
-
- tmpuser = headuser;
-
- while (tmpuser) {
- if (tmpuser->ochan) {
- outbound = tmpuser->ochan;
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", tmpuser->dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- /* If the cause wasn't handled properly */
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(tmpuser->ochan);
- }
- tmpuser = tmpuser->next;
- }
-}
-
-static struct ast_channel *wait_for_winner(struct findme_user *headuser, struct number *nm, struct ast_channel *caller)
-{
-
- struct ast_channel *watchers[256];
- int pos;
- struct ast_channel *winner;
- struct ast_frame *f;
- char *rest, *number;
- int ctstatus;
- int dg;
- struct findme_user *fmuser = NULL;
- struct findme_user *tmpuser = NULL;
-
- /* ------------ wait_for_winner_channel start --------------- */
-
- if (headuser->ochan)
- {
- pos = 1;
- watchers[0] = caller;
-
- dg = 0;
-
- tmpuser = headuser;
-
- while (tmpuser) {
- watchers[pos++] = tmpuser->ochan;
- tmpuser = tmpuser->next;
- }
- ctstatus = 0;
-
- while (!ctstatus) {
- winner = ast_waitfor_n(watchers, pos, (nm->timeout*1000));
-
- if (winner) {
- /* Need to find out which channel this is */
- dg = 0;
- while ((winner != watchers[dg]) && (dg < 256))
- dg++;
- tmpuser = headuser;
- while ((tmpuser) && (tmpuser->ochan != winner)) {
- tmpuser = tmpuser->next;
- }
- f = ast_read(winner);
- if (f) {
- if (f->frametype == AST_FRAME_CONTROL) {
- switch(f->subclass) {
- case AST_CONTROL_HANGUP:
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s received a hangup frame.\n", winner->name);
- if (dg == 0) {
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "The calling channel hungup. Need to drop everyone else.\n");
- clear_calling_tree(headuser);
- ctstatus = -1;
- }
- break;
- case AST_CONTROL_ANSWER:
- /* This is our guy if someone answered. */
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s answered %s\n", winner->name, caller->name);
- /* If call has been answered, then the eventual hangup is likely to be normal hangup */
- winner->hangupcause = AST_CAUSE_NORMAL_CLEARING;
- caller->hangupcause = AST_CAUSE_NORMAL_CLEARING;
- if (dg > 0) {
- if (!ast_streamfile(winner, "call-from", winner->language)) {
-
- }
- }
- break;
- case AST_CONTROL_BUSY:
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s is busy\n", winner->name);
- break;
- case AST_CONTROL_CONGESTION:
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s is circuit-busy\n", winner->name);
- break;
- case AST_CONTROL_RINGING:
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s is ringing\n", winner->name);
- break;
- case AST_CONTROL_PROGRESS:
- if (option_verbose > 2)
- ast_verbose ( VERBOSE_PREFIX_3 "%s is making progress passing it to %s\n", winner->name,caller->name);
- break;
- case AST_CONTROL_VIDUPDATE:
- if (option_verbose > 2)
- ast_verbose ( VERBOSE_PREFIX_3 "%s requested a video update, passing it to %s\n", winner->name,caller->name);
- break;
- case AST_CONTROL_PROCEEDING:
- if (option_verbose > 2)
- ast_verbose ( VERBOSE_PREFIX_3 "%s is proceeding passing it to %s\n", winner->name,caller->name);
- break;
- case AST_CONTROL_HOLD:
- if (option_verbose > 2)
- ast_verbose(VERBOSE_PREFIX_3 "Call on %s placed on hold\n", winner->name);
- break;
- case AST_CONTROL_UNHOLD:
- if (option_verbose > 2)
- ast_verbose(VERBOSE_PREFIX_3 "Call on %s left from hold\n", winner->name);
- break;
- case AST_CONTROL_OFFHOOK:
- case AST_CONTROL_FLASH:
- /* Ignore going off hook and flash */
- break;
- case -1:
- if (option_verbose > 2)
- ast_verbose( VERBOSE_PREFIX_3 "%s stopped sounds\n", winner->name);
- break;
- default:
- ast_log(LOG_DEBUG, "Dunno what to do with control type %d\n", f->subclass);
- }
- }
- ast_frfree(f);
- }
- } else {
- if (option_verbose > 2)
- ast_verbose(VERBOSE_PREFIX_3 "%s timeout on the dialing channel %s\n", caller->name, winner->name);
- }
- }
- /*
- if (tpargs->status == -50)
- {
- ast_log(LOG_DEBUG, "Got a signal that the other end hung up. exiting thread.\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
- pthread_exit(0);
-
- }
- if (winner->_state == AST_STATE_UP)
- {
- ast_log(LOG_DEBUG, "Looks up! Sleeping!\n");
- ast_safe_sleep(outbound, 500);
- if (!ast_streamfile(outbound, "call-from", outbound->language)) {
- if (ast_waitstream(outbound, "") < 0)
- goto outrun;
- } else
- goto outrun;
- if (!ast_streamfile(outbound, tpargs->namerecloc, outbound->language)) {
- if (ast_waitstream(outbound, "") < 0)
- goto outrun;
- } else
- goto outrun;
- ast_streamfile(outbound, "press-1-to-be-connected-or", outbound->language);
- if (outbound->stream) {
- d = ast_waitstream(outbound, AST_DIGIT_ANY);
- ast_stopstream(outbound);
- usleep(1000);
- ast_log(LOG_DEBUG, "digit is in?\n");
- }
-
- if (!d)
- d = -1;
-
- while ((!passed) && (attempts < maxretries)) {
-
- if (outbound->stream)
- d = ast_waitstream(outbound, AST_DIGIT_ANY);
-
- if (d && (d != -1)) {
- ast_log(LOG_DEBUG, "waitfordigit result %d\n", d);
- if (d) {
- yn[ynidx] = d;
- ynidx++;
- }
- }
-
- ast_log(LOG_DEBUG, "d: %d ynidx: %d ynlongest: %d \n", d, ynidx, ynlongest);
-
- while ((d != 0) && (ynidx <= 3) && (ynidx < (ynlongest))) {
- d = ast_waitfordigit(outbound, featuredigittimeout);
- yn[ynidx] = d;
- ynidx++;
- }
- yn[ynidx] = 0;
-
- if ( (strlen((void *)&yn) > 0) && (strcmp(yn, nextinfmfm))) {
- ast_log(LOG_DEBUG, "YN is: %s\n", yn);
-
- if (!strcmp(yn, takecall))
- {
- tpargs->outbound = outbound;
- tpargs->status = 100;
- pthread_exit(0);
-
- } else if (!strcmp(yn, blindxferexten)) {
- ast_log(LOG_DEBUG, "blind xfer exten dtmf featuremap received.\n");
-
- } else if (!strcmp(yn, atxferexten)) {
- ast_log(LOG_DEBUG, "blind xfer exten dtmf featuremap received.\n");
- }
- else if (!strcmp(yn, nextindp))
- {
- ast_log(LOG_DEBUG, "caller doesn't want to bothered right now.\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
- tpargs->status = 99;
- pthread_exit(0);
- }
- else
- {
- ast_log(LOG_DEBUG, "we got a call, and a digit, but it wasn't a 1 or a 2\n");
- ast_streamfile(outbound, "press-1-to-be-connected-or", outbound->language);
- ynidx = 0;
- }
- }
- else
- {
- // digit collection timed out
- ast_log(LOG_NOTICE, "Digit collection timed out. Proceed on to the next number (if any)\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
- }
-
-
- attempts++;
-
- if (attempts >= maxretries) {
- ast_log(LOG_DEBUG, "caller still isn't inputting correct DTMF after max attempts.\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
- tpargs->status = 99;
- pthread_exit(0);
- }
-
-
- if (!outbound)
- break;
-
- } // while attempt
-
-
- outrun:
- tpargs->status = 200;
- } else {
- ast_log(LOG_DEBUG, "timeout or dial failure.\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
-
- }
-
-
- */
-
- }
- else
- {
- ast_log(LOG_NOTICE, "couldn't reach at this number.\n");
-
- }
-
- /* --- WAIT FOR WINNER NUMBER END! -----------*/
-
-}
-
-static void *findmethread(void *args)
-{
- struct thread_args *tpargs;
- struct number *nm;
- struct ast_channel *outbound = NULL;
- struct ast_channel *caller;
- char dialarg[512];
- int ynidx = 0, ynlongest = 0, passed = 0, attempts = 0;
- char yn[5];
- int d = 0,dg;
- char *rest, *number;
- struct followme_user *tmpuser;
-
- tpargs = (struct thread_args *)args;
-
- if (!tpargs->chan) {
- pthread_exit(0);
- }
-
- /* We're going to figure out what the longest possible string of digits to collect is */
- ynlongest = 0;
- if (strlen((void *)&takecall) > ynlongest)
- ynlongest = strlen((void *)&takecall);
- if (strlen((void *)&nextindp) > ynlongest)
- ynlongest = strlen((void *)&nextindp);
- if (strlen((void *)&nextinfmfm) > ynlongest)
- ynlongest = strlen((void *)&nextinfmfm);
- if (strlen((void *)&blindxferexten) > ynlongest)
- ynlongest = strlen((void *)&blindxferexten);
- if (strlen((void *)&atxferexten) > ynlongest)
- ynlongest = strlen((void *)&atxferexten);
-
-
- caller = tpargs->chan;
- nm = tpargs->numbers;
- while (nm)
- {
-
- ast_log(LOG_DEBUG, "sleeping now inside the thread. Number %s timeout %d\n",nm->number,nm->timeout);
- time(&start_time);
-
- number = ast_strdupa(nm->number);
- do {
- rest = strchr(number, '&');
- if (rest) {
- *rest = 0;
- rest++;
- }
-
- if (!strcmp(tpargs->context, ""))
- sprintf(dialarg, "%s", number);
- else
- sprintf(dialarg, "%s@%s", number, tpargs->context);
-
- tmpuser = malloc(sizeof(struct findme_user));
- if (!tmpuser) {
- ast_log(LOG_WARNING, "Out of memory!\n");
- pthread_exit(0);
- }
- memset(tmpuser, 0, sizeof(struct findme_user));
-
- outbound = ast_request("Local", ast_best_codec(caller->nativeformats), dialarg, &dg);
- if (outbound) {
- ast_set_callerid(outbound, caller->cid.cid_num, caller->cid.cid_name, caller->cid.cid_num);
- ast_channel_inherit_variables(tpargs->chan, outbound);
-
- if (!ast_call(outbound,dialarg,0)) {
- tmpuser->ochan = outbound;
- tmpuser->state = 0;
- ast_copy_string(tmpuser->dialarg, dialarg, sizeof(dialarg));
- tmpuser->next = fmuser;
- fmuser = tmpuser;
- } else {
- ast_log(LOG_NOTICE, "couldn't reach at this number.\n");
- if (outbound) {
- if (!outbound->cdr) {
- outbound->cdr = ast_cdr_alloc();
- if (outbound->cdr)
- ast_cdr_init(outbound->cdr, outbound);
- }
- if (outbound->cdr) {
- char tmp[256];
- snprintf(tmp, 256, "%s/%s", "Local", dialarg);
- ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
- ast_cdr_update(outbound);
- ast_cdr_start(outbound->cdr);
- ast_cdr_end(outbound->cdr);
- /* If the cause wasn't handled properly */
- if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
- ast_cdr_failed(outbound->cdr);
- } else
- ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
- ast_hangup(outbound);
- outbound = NULL;
- }
-
- }
-
-
- } else {
- ast_log(LOG_WARNING, "Unable to allocate a channel for Local/%s cause: %s\n", &dialarg, ast_cause2str(dg));
- }
-
- number = rest;
- } while (number);
-
- headuser = fmuser;
-
- if (headuser->ochan)
- wait_for_winner(headeruser, nm, caller);
-
- nm = nm->next;
- }
- tpargs->status = 1;
- pthread_exit(0);
-
-}
-
-static int app_exec(struct ast_channel *chan, void *data)
-{
- struct ast_call_followme *f;
- struct number *nm;
- int res = 0;
- struct localuser *u;
- char *argstr;
- char namerecloc[255];
- char dbgetval[50];
- int duration = 0;
- struct ast_channel *caller;
- struct ast_channel *outbound;
-
- pthread_t finderthread;
- pthread_attr_t tattr;
-
- AST_DECLARE_APP_ARGS(args,
- AST_APP_ARG(followmeid);
- );
-
- if (!(argstr = ast_strdupa((char *)data))) {
- ast_log(LOG_ERROR, "Out of memory!\n");
- return -1;
- }
-
- if (!data) {
- ast_log(LOG_WARNING, "%s requires an argument (followmeid)\n",app);
- return -1;
- }
-
- LOCAL_USER_ADD(u);
-
- AST_STANDARD_APP_ARGS(args, argstr);
-
- if (!ast_strlen_zero(args.followmeid))
- ast_log(LOG_DEBUG, "Follow me ID value is : %s\n", args.followmeid);
-
-
- f = followmes;
- while(f) {
- if (!strcmp(f->name, args.followmeid) && (f->active))
- break;
- f = f->next;
- }
- ast_log(LOG_DEBUG, "New profile %s.\n", args.followmeid);
- if (!f)
- {
- ast_log(LOG_WARNING, "Profile not found.");
- res = -1;
- }
- else
- {
-
- if (ast_db_get("FollowMe", args.followmeid, (void *)&dbgetval, 49))
- {
-
- snprintf(namerecloc,sizeof(namerecloc),"%s/followme.%s",ast_config_AST_SPOOL_DIR,chan->uniqueid);
- duration = 5;
- ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL);
- if (!ast_streamfile(chan, "pls-hold-while-try", chan->language)) {
- } else
- goto outrun;
-
- targs.status = 0;
- targs.chan = chan;
- targs.numbers = f->numbers;
- ast_copy_string(targs.namerecloc, namerecloc, sizeof(targs.namerecloc));
- ast_copy_string(targs.context, f->context, sizeof(targs.context));
-
-
- pthread_attr_init(&tattr);
- pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED);
- ast_pthread_create(&finderthread, &tattr, findmethread, &targs);
-
-
- if (ast_waitstream(chan, "") < 0)
- goto outrun;
- nm = f->numbers;
- if (!strcmp(f->moh, ""))
- ast_moh_start(chan, NULL);
- else
- ast_moh_start(chan, f->moh);
- while (nm) {
- ast_log(LOG_DEBUG, "Number %s, timeout %d\n", nm->number, nm->timeout);
- nm = nm->next;
- }
-
-
-
- while (finderthread)
- {
- res = ast_safe_sleep(chan, 250);
-
- if ((res < 0) || (targs.status > 0))
- {
- ast_log(LOG_DEBUG, "original caller appears to have hungup early. killing off dialing thread.\n");
- if (res < 0)
- targs.status = -50;
- finderthread = 0;
- }
-
- }
- unlink(namerecloc);
- if (targs.status != 100)
- {
- ast_moh_stop(chan);
- res = 0;
- }
- else
- {
- caller = chan;
- outbound = targs.outbound;
- /* Bridge the two channels. */
-
- memset(&config,0,sizeof(struct ast_bridge_config));
- ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
- ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
- ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
-
- ast_moh_stop(caller);
- /* Be sure no generators are left on it */
- ast_deactivate_generator(caller);
- /* Make sure channels are compatible */
- res = ast_channel_make_compatible(caller, outbound);
- if (res < 0) {
- ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n", caller->name, outbound->name);
- ast_hangup(outbound);
- goto outrun;
- }
- time(&answer_time);
- res = ast_bridge_call(caller,outbound,&config);
- time(&end_time);
- snprintf(toast, sizeof(toast), "%ld", (long)(end_time - start_time));
- pbx_builtin_setvar_helper(caller, "DIALEDTIME", toast);
- snprintf(toast, sizeof(toast), "%ld", (long)(end_time - answer_time));
- pbx_builtin_setvar_helper(caller, "ANSWEREDTIME", toast);
- if (outbound)
- ast_hangup(outbound);
- res = 1;
-
- }
-
- }
- else
- {
- ast_log(LOG_NOTICE, "DB Key found for this profile. Therefore, followme is disabled.\n");
- }
-
-
-
- }
- outrun:
-
- /* Do our thing here */
- LOCAL_USER_REMOVE(u);
- return res;
-}
-
-int unload_module(void)
-{
- STANDARD_HANGUP_LOCALUSERS;
- return ast_unregister_application(app);
-}
-
-int load_module(void)
-{
- reload_followme();
- return ast_register_application(app, app_exec, synopsis, descrip);
-}
-
-char *description(void)
-{
- return tdesc;
-}
-
-int usecount(void)
-{
- int res;
- STANDARD_USECOUNT(res);
- return res;
-}
-
-int reload(void)
-{
- reload_followme();
- return 0;
-}
-
-char *key()
-{
- return ASTERISK_GPL_KEY;
-}
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * A full-featured Find-Me/Follow-Me Application
+ *
+ * Copyright (C) 2005, BJ Weschke
+ *
+ * BJ Weschke <bweschke at btwtech.com>
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+
+#include "asterisk.h"
+
+ASTERISK_FILE_VERSION(__FILE__, "$version$")
+
+#include "asterisk/lock.h"
+#include "asterisk/file.h"
+#include "asterisk/logger.h"
+#include "asterisk/channel.h"
+#include "asterisk/pbx.h"
+#include "asterisk/options.h"
+#include "asterisk/module.h"
+#include "asterisk/translate.h"
+#include "asterisk/say.h"
+#include "asterisk/features.h"
+#include "asterisk/musiconhold.h"
+#include "asterisk/cli.h"
+#include "asterisk/manager.h"
+#include "asterisk/config.h"
+#include "asterisk/monitor.h"
+#include "asterisk/utils.h"
+#include "asterisk/causes.h"
+#include "asterisk/astdb.h"
+#include "asterisk/app.h"
+
+static char *tdesc = "Find-Me/Follow-Me Application";
+static char *app = "FollowMe";
+static char *synopsis =
+"Find-Me/Follow-Me.";
+static char *descrip =
+" FollowMe(followmeid):\n"
+"Performs Find-Me/Follow-Me functionality as defined in followme.conf.\n";
+
+static char *defaultmoh = "default"; /* Default Music-On-Hold Class */
+
+STANDARD_LOCAL_USER;
+
+LOCAL_USER_DECL;
+
+struct number {
+ char number[512]; /* Phone Number and/or Extension */
+ unsigned int timeout; /* Dial Timeout, if used. */
+ struct number *next; /* Next Number record */
+};
+
+struct ast_call_followme {
+ ast_mutex_t lock;
+ char name[AST_MAX_EXTENSION]; /* Name - FollowMeID */
+ char moh[AST_MAX_CONTEXT]; /* Music On Hold Class to be used */
+ char context[AST_MAX_CONTEXT]; /* Context to dial from */
+ unsigned int active; /* Profile is active (1), or disabled (0). */
+ char vm[AST_MAX_CONTEXT]; /* Voicemail Box Defined for the profile */
+
+ struct number *numbers; /* Head of the list of follow-me numbers */
+ struct number *blnumbers; /* Head of the list of black-listed numbers */
+ struct number *wlnumbers; /* Head of the list of white-listed numbers */
+ struct ast_call_followme *next; /* Next Follow-Me record */
+};
+
+struct thread_args {
+ struct ast_channel *chan;
+ struct number *numbers;
+ int status;
+ char context[AST_MAX_CONTEXT];
+ char namerecloc[AST_MAX_CONTEXT];
+ struct ast_channel *outbound;
+};
+
+struct findme_user {
+ struct ast_channel *ochan;
+ int state;
+ char dialarg[256];
+ struct findme_user *next;
+};
+
+struct thread_args targs;
+struct ast_bridge_config config;
+int res, ret = 0;
+char toast[80];
+time_t start_time, answer_time, end_time;
+
+char *featuredigittostr;
+int featuredigittimeout = 5000;
+int maxretries = 3;
+
+char takecall[20] = "1", nextindp[20] = "2", nextinfmfm[20] = "", blindxferexten[20] = "", atxferexten[20] = "";
+
+static struct ast_call_followme *followmes = NULL;
+AST_MUTEX_DEFINE_STATIC(fmlock);
+
+
+static void free_numbers(struct ast_call_followme *f, int all)
+{
+ /* Free numbers attached to the profile */
+ struct number *curn, *next, *prev;
+
+ curn = f->numbers;
+ prev = NULL;
+ while(curn) {
+ next = curn->next;
+ if (all) {
+ if (prev)
+ prev->next = next;
+ else
+ f->numbers = next;
+ free(curn);
+ } else
+ prev = curn;
+ curn = next;
+ }
+
+ curn = f->blnumbers;
+ prev = NULL;
+ while(curn) {
+ next = curn->next;
+ if (all) {
+ if (prev)
+ prev->next = next;
+ else
+ f->blnumbers = next;
+ free(curn);
+ } else
+ prev = curn;
+ curn = next;
+ }
+
+ curn = f->wlnumbers;
+ prev = NULL;
+ while(curn) {
+ next = curn->next;
+ if (all) {
+ if (prev)
+ prev->next = next;
+ else
+ f->wlnumbers = next;
+ free(curn);
+ } else
+ prev = curn;
+ curn = next;
+ }
+
+}
+
+
+static struct ast_call_followme *alloc_profile(const char *fmname)
+{
+ struct ast_call_followme *f;
+
+ f = malloc(sizeof(*f));
+ if (f) {
+ memset(f, 0, sizeof(*f));
+ ast_mutex_init(&f->lock);
+ ast_copy_string(f->name, fmname, sizeof(f->name));
+ ast_copy_string(f->moh, "", sizeof(f->moh));
+ ast_copy_string(f->context, "", sizeof(f->context));
+ ast_copy_string(f->vm, "", sizeof(f->vm));
+ }
+ return f;
+}
+
+static void init_profile(struct ast_call_followme *f)
+{
+ f->active = 1;
+ ast_copy_string(f->moh, defaultmoh, sizeof(f->moh));
+}
+
+
+
+static void profile_set_param(struct ast_call_followme *f, const char *param, const char *val, int linenum, int failunknown)
+{
+ if (!strcasecmp(param, "music") || !strcasecmp(param, "musiconhold")) {
+ ast_copy_string(f->moh, val, sizeof(f->moh));
+ } else if (!strcasecmp(param, "context")) {
+ ast_copy_string(f->context, val, sizeof(f->context));
+ } else if (!strcasecmp(param, "vmbox")) {
+ ast_copy_string(f->vm, val, sizeof(f->vm));
+ } else if(failunknown) {
+ if (linenum >= 0) {
+ ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s at line %d of followme.conf\n",
+ f->name, param, linenum);
+ } else {
+ ast_log(LOG_WARNING, "Unknown keyword in profile '%s': %s\n", f->name, param);
+ }
+ }
+}
+
+static struct number *create_followme_number(char *number, int timeout)
+{
+ struct number *cur;
+ char *tmp;
+
+ /* Add a new number */
+
+ cur = malloc(sizeof(struct number));
+
+ if (cur) {
+ memset(cur, 0, sizeof(struct number));
+ cur->timeout = timeout;
+ if (strchr(number, ','))
+ {
+ tmp = strchr(number, ',');
+ *tmp = '\0';
+ }
+ ast_copy_string(cur->number, number, sizeof(cur->number));
+ ast_log(LOG_DEBUG, "Created a number, %s, with a timeout of %d.\n", cur->number, cur->timeout);
+
+ }
+
+ return cur;
+}
+
+static void reload_followme(void)
+{
+ struct ast_call_followme *f;
+ struct ast_config *cfg;
+ char *cat, *tmp;
+ struct ast_variable *var;
+ struct number *prev, *cur;
+ int new;
+ char numberstr[90];
+ int timeout;
+
+ cfg = ast_config_load("followme.conf");
+ if (!cfg) {
+ ast_log(LOG_WARNING, "No follow me config file (followme.conf), so no follow me\n");
+ return;
+ }
+ ast_mutex_lock(&fmlock);
+ /* Mark all profiles as inactive for the moment */
+ f = followmes;
+ while(f) {
+ f->active = 0;
+ f = f->next;
+ }
+ featuredigittostr = ast_variable_retrieve(cfg, "general", "featuredigittimeout");
+
+ if (!ast_strlen_zero(featuredigittostr)) {
+ if (!scanf("%d", &featuredigittimeout))
+ featuredigittimeout = 5000;
+ }
+
+
+ /* Chug through config file */
+ cat = ast_category_browse(cfg, NULL);
+ while(cat) {
+ /* Define a new profile */
+ /* Look for an existing one */
+ f = followmes;
+ while(f) {
+ if (!strcmp(f->name, cat))
+ break;
+ f = f->next;
+ }
+ ast_log(LOG_DEBUG, "New profile %s.\n", cat);
+ if (!f) {
+ /* Make one then */
+ f = alloc_profile(cat);
+ new = 1;
+ } else
+ new = 0;
+ if (f) {
+ if (!new)
+ ast_mutex_lock(&f->lock);
+ /* Re-initialize the profile */
+ init_profile(f);
+ free_numbers(f, 1);
+ prev = f->numbers;
+ if (prev) {
+ /* find the end of any dynamic numbers */
+ while(prev->next)
+ prev = prev->next;
+ }
+ var = ast_variable_browse(cfg, cat);
+ while(var) {
+ if (!strcasecmp(var->name, "number")) {
+ /* Add a new number */
+ ast_copy_string(numberstr, var->value, sizeof(numberstr));
+ if ((tmp = strchr(numberstr, ','))) {
+ *tmp = '\0';
+ tmp++;
+ timeout = atoi(tmp);
+ if (timeout < 0) {
+ timeout = 12;
+ }
+ } else
+ timeout = 12;
+ cur = create_followme_number(numberstr, timeout);
+
+ if (cur) {
+ if (prev)
+ prev->next = cur;
+ else
+ f->numbers = cur;
+ prev = cur;
+ }
+ } else {
+ profile_set_param(f, var->name, var->value, var->lineno, 1);
+ ast_log(LOG_DEBUG, "Logging parameter %s with value %s from lineno %d\n", var->name, var->value, var->lineno);
+ }
+ var = var->next;
+ }
+ if (!new)
+ ast_mutex_unlock(&f->lock);
+ if (new) {
+ f->next = followmes;
+ followmes = f;
+ }
+ }
+ cat = ast_category_browse(cfg, cat);
+ }
+ ast_config_destroy(cfg);
+ ast_mutex_unlock(&fmlock);
+}
+
+static void clear_calling_tree(struct findme_user *headuser)
+{
+ struct findme_user *tmpuser = NULL;
+ struct ast_channel *outbound = NULL;
+
+ tmpuser = headuser;
+
+ while (tmpuser) {
+ if (tmpuser->ochan) {
+ outbound = tmpuser->ochan;
+ if (!outbound->cdr) {
+ outbound->cdr = ast_cdr_alloc();
+ if (outbound->cdr)
+ ast_cdr_init(outbound->cdr, outbound);
+ }
+ if (outbound->cdr) {
+ char tmp[256];
+ snprintf(tmp, 256, "%s/%s", "Local", tmpuser->dialarg);
+ ast_cdr_setapp(outbound->cdr,"FollowMe",tmp);
+ ast_cdr_update(outbound);
+ ast_cdr_start(outbound->cdr);
+ ast_cdr_end(outbound->cdr);
+ /* If the cause wasn't handled properly */
+ if (ast_cdr_disposition(outbound->cdr,outbound->hangupcause))
+ ast_cdr_failed(outbound->cdr);
+ } else
+ ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
+ ast_hangup(tmpuser->ochan);
+ }
+ tmpuser = tmpuser->next;
+ }
+}
+
+static struct ast_channel *wait_for_winner(struct findme_user *headuser, struct number *nm, struct ast_channel *caller)
+{
+
+ struct ast_channel *watchers[256];
+ int pos;
+ struct ast_channel *winner;
+ struct ast_frame *f;
+ char *rest, *number;
+ int ctstatus;
+ int dg;
+ struct findme_user *fmuser = NULL;
+ struct findme_user *tmpuser = NULL;
+
+ /* ------------ wait_for_winner_channel start --------------- */
+
+ if (headuser->ochan)
+ {
+ pos = 1;
+ watchers[0] = caller;
+
+ dg = 0;
+
+ tmpuser = headuser;
+
[... 634 lines stripped ...]
More information about the asterisk-commits
mailing list