[asterisk-commits] branch bweschke/findme_followme - r7437 /team/bweschke/findme_followme/apps/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Mon Dec 12 14:09:43 CST 2005


Author: bweschke
Date: Mon Dec 12 14:09:41 2005
New Revision: 7437

URL: http://svn.digium.com/view/asterisk?rev=7437&view=rev
Log:
 Initial code import / app_followme.c and Makefile adjustment to build the module.

Added:
    team/bweschke/findme_followme/apps/app_followme.c
Modified:
    team/bweschke/findme_followme/apps/Makefile

Modified: team/bweschke/findme_followme/apps/Makefile
URL: http://svn.digium.com/view/asterisk/team/bweschke/findme_followme/apps/Makefile?rev=7437&r1=7436&r2=7437&view=diff
==============================================================================
--- team/bweschke/findme_followme/apps/Makefile (original)
+++ team/bweschke/findme_followme/apps/Makefile Mon Dec 12 14:09:41 2005
@@ -24,7 +24,8 @@
      app_setcdruserfield.so app_settransfercapability.so app_softhangup.so     \
      app_stack.so app_system.so app_talkdetect.so app_test.so app_transfer.so  \
      app_userevent.so app_url.so app_verbose.so app_voicemail.so               \
-     app_waitforring.so app_waitforsilence.so app_while.so app_zapateller.so
+     app_waitforring.so app_waitforsilence.so app_while.so app_zapateller.so   \
+     app_followme.so
      
 #
 # Obsolete things...

Added: team/bweschke/findme_followme/apps/app_followme.c
URL: http://svn.digium.com/view/asterisk/team/bweschke/findme_followme/apps/app_followme.c?rev=7437&view=auto
==============================================================================
--- team/bweschke/findme_followme/apps/app_followme.c (added)
+++ team/bweschke/findme_followme/apps/app_followme.c Mon Dec 12 14:09:41 2005
@@ -1,0 +1,822 @@
+/*
+ * 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[AST_MAX_EXTENSION];	/* 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 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 *findmethread(void *args)
+{
+				struct thread_args *tpargs;
+				struct number *nm;
+				struct ast_channel *outbound = NULL;
+				struct ast_channel *caller;
+				char dialarg[AST_MAX_EXTENSION];
+				int ynidx = 0, ynlongest = 0, passed = 0, attempts = 0;
+				char yn[5];
+				int d = 0,dg;
+				
+				
+				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);
+				if (!strcmp(tpargs->context, ""))
+					sprintf(dialarg, "%s", nm->number);
+				else
+				{
+					sprintf(dialarg, "%s@%s", nm->number, tpargs->context);
+				}
+
+				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))
+					{
+					dg = 0;	
+					while ((outbound) && (outbound->_state != AST_STATE_UP) && (tpargs->status != -50) && (dg < (nm->timeout*4)))
+					{
+						ast_safe_sleep(outbound, 250);
+						dg++;
+					}
+					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 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;
+								}
+						pthread_exit(0);		
+
+					}
+					if (outbound->_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");
+						}
+						
+						while ((!passed) && (attempts < maxretries)) {
+							
+							yn[0] = 0;
+							while ((d != 0) && (ynidx <= 3) && (ynidx < (ynlongest-1))) {
+								yn[ynidx] = d;
+								ynidx++;
+								d = ast_waitfordigit(outbound, featuredigittimeout);
+							}
+							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))
+								{
+									/* Caller doesn't want to be bothered right now. */
+									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 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;
+									}
+									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);
+									if (outbound->stream) 
+										d = ast_waitstream(outbound, AST_DIGIT_ANY);
+									ast_log(LOG_DEBUG, "waitfordigit result %d\n", d);
+								}
+							}
+							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 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;
+								}
+							}
+							
+						
+							attempts++;
+
+							if (attempts >= maxretries) {
+								/* The caller still hasn't gotten in right after the max number of attempts. */
+								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 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;
+								}
+								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 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_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_NOTICE, "couldn't reach at this number.\n");
+										
+				}
+				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;
+}



More information about the asterisk-commits mailing list