[asterisk-commits] branch oej/test-this-branch r16300 - in /team/oej/test-this-branch: ./ apps/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Wed Mar 29 16:53:06 MST 2006


Author: oej
Date: Wed Mar 29 17:53:05 2006
New Revision: 16300

URL: http://svn.digium.com/view/asterisk?rev=16300&view=rev
Log:
Adding BJ's findme/followme application (Issue 5574)

Added:
    team/oej/test-this-branch/apps/app_followme.c   (with props)
Modified:
    team/oej/test-this-branch/README.test-this-branch
    team/oej/test-this-branch/README.test-this-branch.html

Modified: team/oej/test-this-branch/README.test-this-branch
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/README.test-this-branch?rev=16300&r1=16299&r2=16300&view=diff
==============================================================================
--- team/oej/test-this-branch/README.test-this-branch (original)
+++ team/oej/test-this-branch/README.test-this-branch Wed Mar 29 17:53:05 2006
@@ -37,6 +37,7 @@
 - rtptiming: Support for timed RTP (#5374, cmantunes/DEA/sokhapkin)
 - gain_functions: Support for gain adjustments in the CHANNEL function 
   (#6793, bweschke)
+- findme_followme: Followme application (#5574, bweschke)
 
 And the following stand-alone patches
 - Manager sendtext event (ZX81, #6131)

Modified: team/oej/test-this-branch/README.test-this-branch.html
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/README.test-this-branch.html?rev=16300&r1=16299&r2=16300&view=diff
==============================================================================
--- team/oej/test-this-branch/README.test-this-branch.html (original)
+++ team/oej/test-this-branch/README.test-this-branch.html Wed Mar 29 17:53:05 2006
@@ -38,6 +38,9 @@
 (<a href="http://bugs.digium.com/view.php?idi=5374">#5374</a>, cmantunes/DEA/sokhapkin)</li>
 <li>gain_functions: Support for gain adjustments in the CHANNEL function 
 (<a href="http://bugs.digium.com/view.php?idi=6793">#6793</a>, bweschke)</li>
+<li> findme_followme: Followme application 
+(<a href="http://bugs.digium.com/view.php?idi=5574">#5574</a>, bweschke)</li>
+
 </ul>
 </p>
 <i>And the following stand-alone patches:</i><p>

Added: team/oej/test-this-branch/apps/app_followme.c
URL: http://svn.digium.com/view/asterisk/team/oej/test-this-branch/apps/app_followme.c?rev=16300&view=auto
==============================================================================
--- team/oej/test-this-branch/apps/app_followme.c (added)
+++ team/oej/test-this-branch/apps/app_followme.c Wed Mar 29 17:53:05 2006
@@ -1,0 +1,958 @@
+/*
+ * Asterisk -- A telephony toolkit for Linux.
+ *
+ * A full-featured Find-Me/Follow-Me Application
+ * 
+ * Copyright (C) 2005-2006, BJ Weschke All Rights Reserved.
+ *
+ * BJ Weschke <bweschke at btwtech.com>
+ *
+ * This code is released by the author with no restrictions on usage.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ */
+
+/*! \file
+ *
+ * \brief Find-Me Follow-Me application
+ *
+ * \author BJ Weschke <bweschke at btwtech.com>
+ *
+ * \ingroup applications
+ */
+
+
+#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 */
+	long 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];
+	char yn[10];
+	int ynidx; 
+	int digts;
+	struct findme_user *next;	
+};
+
+struct thread_args targs;
+struct ast_bridge_config config;
+int res, ret = 0, ynlongest = 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 %ld.\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,int wholetree) 
+{
+	struct findme_user *tmpuser = NULL;
+	struct ast_channel *outbound = NULL;
+	
+	tmpuser = headuser;
+	
+	while (tmpuser) {		
+		if (tmpuser->ochan && tmpuser->state >= 0) {
+			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);
+		}
+		if (wholetree)
+			tmpuser = tmpuser->next;
+		else
+			tmpuser = NULL;
+	}
+}
+
+
+
+static struct ast_channel *wait_for_winner(struct findme_user *headuser, struct number *nm, struct ast_channel *caller, char *namerecloc, int *status)
+{
+
+		struct ast_channel *watchers[256];
+		int pos;
+		struct ast_channel *winner;
+		struct ast_frame *f;
+		int ctstatus;
+		int dg;
+		struct findme_user *tmpuser = NULL;
+		int *to = malloc(sizeof(int));
+		int livechannels = 0;
+		int tmpto;
+		long totalwait = 0;
+		char *callfromname;
+		char *pressbuttonname;
+
+		/* ------------ wait_for_winner_channel start --------------- */ 
+
+		callfromname = ast_strdupa("call-from");
+		pressbuttonname = ast_strdupa("press-1-to-be-connected-or");	
+	
+		if (headuser->ochan)
+		{
+			if (!caller) {
+				ast_log(LOG_NOTICE, "Original caller hungup. Cleanup.\n");
+				clear_calling_tree(headuser,1);
+				return 0;
+			}
+			ctstatus = 0;
+			totalwait = nm->timeout * 1000;
+			while (!ctstatus) {
+				*to = 1000;
+				pos = 1;
+				livechannels = 0;
+				watchers[0] = caller;
+			
+				dg = 0;	
+				winner = NULL;	
+				tmpuser = headuser;
+				while (tmpuser) {
+					if (tmpuser->state >= 0 && tmpuser->ochan) {
+						if (tmpuser->digts && (tmpuser->digts - totalwait) > featuredigittimeout) {
+							ast_log(LOG_NOTICE, "We've been waiting for digits longer than we should have.\n");
+							tmpuser->state = 1;
+							tmpuser->digts = 0;
+							if (!ast_streamfile(tmpuser->ochan, callfromname, tmpuser->ochan->language)) {
+								ast_sched_runq(tmpuser->ochan->sched);
+							} else {
+								ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
+								return NULL;
+							}							
+						}
+						if (tmpuser->ochan->stream) {
+							ast_sched_runq(tmpuser->ochan->sched);
+							tmpto = ast_sched_wait(tmpuser->ochan->sched);
+							if (tmpto > 0 && tmpto < *to)
+								*to = tmpto;
+							else if (tmpto < 0) {
+								ast_stopstream(tmpuser->ochan);
+								if (tmpuser->state == 1) {
+									ast_log(LOG_NOTICE, "Playback of the file appears to be done.\n");
+									if (!ast_streamfile(tmpuser->ochan, namerecloc, tmpuser->ochan->language)) {
+										tmpuser->state = 2;
+									} else {
+										ast_log(LOG_WARNING, "Unable to playback %s.\n", namerecloc);
+										return NULL;
+									}
+								} else if (tmpuser->state == 2) {
+									ast_log(LOG_NOTICE, "Playback of name file appears to be done.\n");
+									memset(tmpuser->yn, 0, sizeof(tmpuser->yn));
+									tmpuser->ynidx = 0;
+									tmpuser->digts = 0;
+									if (!ast_streamfile(tmpuser->ochan, pressbuttonname, tmpuser->ochan->language)) {
+										tmpuser->state = 3;
+										
+									} else {
+										ast_log(LOG_WARNING, "Unable to playback %s.\n", pressbuttonname);
+										return NULL;
+									} 
+								} else if (tmpuser->state == 3) {
+									ast_log(LOG_NOTICE, "Playback of the next step file appears to be done.\n");
+								}
+							}
+						}
+						watchers[pos++] = tmpuser->ochan;
+						livechannels++;
+					}
+					tmpuser = tmpuser->next;
+				}
+				tmpto = *to;
+				winner = ast_waitfor_n(watchers, pos, to);
+				tmpto -= *to;
+				totalwait -= tmpto;
+				if (totalwait <= 0) {
+					ast_log(LOG_NOTICE, "We've hit our timeout for this step. Drop everyone and move on to the next one. %ld\n", totalwait);
+					clear_calling_tree(headuser,1);
+					return 0;
+				}
+				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,1);
+									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, callfromname, winner->language)) {
+										ast_sched_runq(winner->sched);
+										tmpuser->state = 1;
+									} else {
+										ast_log(LOG_WARNING, "Unable to playback %s.\n", callfromname);
+										return NULL;
+									}							
+								}
+								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);
+							}
+						} 
+						if (tmpuser && tmpuser->state == 3 && f->frametype == AST_FRAME_DTMF) {
+							if (winner->stream)
+								ast_stopstream(winner);
+							if (!tmpuser->digts)
+								tmpuser->digts = totalwait;
+							ast_log(LOG_NOTICE, "DTMF received: %c\n",(char) f->subclass);
+							tmpuser->yn[tmpuser->ynidx] = (char) f->subclass;
+							tmpuser->ynidx++;
+							ast_log(LOG_NOTICE, "DTMF string: %s\n", tmpuser->yn);
+							if (tmpuser->ynidx >= ynlongest) {
+								ast_log(LOG_NOTICE, "reached longest possible match\n");
+								if (!strcmp(tmpuser->yn, takecall)) {
+									ast_log(LOG_NOTICE, "Match to take the call!\n");
+									return tmpuser->ochan;	
+								}
+								if (!strcmp(tmpuser->yn, nextindp)) {
+									ast_log(LOG_NOTICE, "Next in dial plan step requested.\n");
+									*status = 1;
+									return 0;
+								}	
+								if (!strcmp(tmpuser->yn, nextinfmfm)) {
+									ast_log(LOG_NOTICE, "Next in find me/follow me step requested.\n");
+									*status = 2;
+									return 0;
+								}	
+								if (!strcmp(tmpuser->yn, blindxferexten)) {
+									ast_log(LOG_NOTICE, "Blind Transfer requested.\n");
+									*status = 3;
+									return 0;
+								}	
+								if (!strcmp(tmpuser->yn, atxferexten)) {
+									ast_log(LOG_NOTICE, "Attended Transfer requested.\n");
+									*status = 4;
+									return 0;
+								}	
+
+							}
+						}
+						
+						ast_frfree(f);
+					} else {
+						if (winner) {
+
+							ast_log(LOG_NOTICE, "we didn't get a frame. hanging up. dg is %d\n",dg);					      
+							if (!dg) {
+								clear_calling_tree(headuser,1);
+								return 0;
+							} else {
+								tmpuser->state = -1;
+							 	ast_hangup(winner);  
+								livechannels--;
+								ast_log(LOG_NOTICE, "live channels left %d\n", livechannels);
+								if (!livechannels) {
+									ast_log(LOG_NOTICE, "no live channels left. exiting.\n");
+									return 0;
+								}
+							}
+						}
+					}					
+					
+				} 
+			}
+			
+		}
+		else
+		{
+			ast_log(LOG_NOTICE, "couldn't reach at this number.\n");
+		}
+		
+		/* --- WAIT FOR WINNER NUMBER END! -----------*/
+		return NULL;
+}
+
+static void findmethread(void *args)
+{
+				struct thread_args *tpargs;
+				struct number *nm;
+				struct ast_channel *outbound = NULL;
+				struct ast_channel *caller;
+				struct ast_channel *winner = NULL;
+				char dialarg[512];
+				int dg;
+				char *rest, *number;
+				struct findme_user *tmpuser = NULL;
+			        struct findme_user *fmuser = NULL;
+			        struct findme_user *headuser = NULL;
+				int status;
+					
+	
+				tpargs = (struct thread_args *)args;
+				
+				if (!tpargs->chan) {
+					return;
+				}
+
+				/* 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 %ld\n",nm->number,nm->timeout);
+				time(&start_time);
+
+				number = ast_strdupa(nm->number);
+				ast_log(LOG_NOTICE, "examining %s\n", 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");
+						return;
+					}
+					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);
+						ast_log(LOG_NOTICE, "calling %s\n", dialarg);
+						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;
+			
+				status = 0;	
+				if (headuser->ochan)
+					winner = wait_for_winner(headuser, nm, caller, tpargs->namerecloc, &status);
+		
+					
+				fmuser = headuser;
+				while (fmuser) {
+					headuser = fmuser->next;
+					if (fmuser->ochan != winner)
+						clear_calling_tree(fmuser, 0);
+					free(fmuser);
+					fmuser = headuser;
+				}
+				fmuser = NULL;
+				tmpuser = NULL;
+				headuser = NULL;	
+				nm = nm->next;
+				if (winner)
+					break;
+				}
+				if (!winner) {
+					tpargs->status = 1;
+				} else {
+					tpargs->status = 100;
+					tpargs->outbound = winner;
+				}
+				return;
+		
+}
+
+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;
+	
+	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;
+				if (ast_play_and_record(chan, "vm-rec-name", namerecloc, 5, "sln", &duration, 128, 0, NULL) < 0)
+					goto outrun;
+				if (ast_streamfile(chan, "pls-hold-while-try", chan->language))
+					goto outrun;
+				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 %ld\n", nm->number, nm->timeout);
+					 nm = nm->next;
+				}
+
+
+				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));
+
+				findmethread(&targs);			
+	
+				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;
+}

Propchange: team/oej/test-this-branch/apps/app_followme.c
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: team/oej/test-this-branch/apps/app_followme.c
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: team/oej/test-this-branch/apps/app_followme.c
------------------------------------------------------------------------------
    svn:mime-type = text/plain



More information about the asterisk-commits mailing list