On Sunday 05 October 2003 17:43, Tilghman Lesher wrote:
> On Sunday 05 October 2003 16:41, Robert Hajime Lanning wrote:
> > I sent this earlier under "Editting variable contents" but no-one
> > has responded.  So, the subject is now more to the problem, instead
> > of the solution I was trying to implement.
> >
> > ChanIsAvail returns the channel ID plus "-<session>".
> >
> > How can I edit ${AVAILCHAN} to remove this session ID, so I can use
> > its contents in a subsequent Dial statement?
>
> Oh, it's quite simple.  You just write your own application to remove
> the suffix.  Or you wait for someone else to write it.
>
> Untested code.  UAYOR.

Tested code.

-Tilghman
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Cut application
 * 
 * Copyright (c) 2003 Tilghman Lesher.  All rights reserved.
 *
 * Tilghman Lesher <[EMAIL PROTECTED]>
 *
 * This code is released by the author with no restrictions on usage.
 *
 */

#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


static char *tdesc = "Cuts up variables";

static char *app_cut = "Cut";

static char *cut_synopsis = "Cut(newvar=varname|delimiter|field)";

static char *cut_descrip =
"Cut(varname=wholestring,delimiter,field)\n"
"  newvar    - result string is set to this variable\n"
"  varname   - variable you want cut\n"
"  delimiter - defaults to -\n"
"  field     - number of the field you want (1-based offset)\n"
"  Returns 0 or -1 on hangup or error.\n";

STANDARD_LOCAL_USER;

LOCAL_USER_DECL;

static int cut_exec(struct ast_channel *chan, void *data)
{
	int res=0;
	struct localuser *u;
	char *s, *newvar, *varname, *delimiter, *field;
	int fieldnum, args_okay = 0;

	LOCAL_USER_ADD(u);

	/* Check and parse arguments */
	if (data) {
		s = strdupa((char *)data);
		if (s) {
			newvar = strsep(&s, "=");
			if (newvar && (newvar[0] != '\0')) {
				varname = strsep(&s, "|");
				if (varname && (varname[0] != '\0')) {
					delimiter = strsep(&s, "|");
					if (delimiter) {
						field = strsep(&s, "|");
						if (field && (sscanf(field,"%d",&fieldnum) == 1)) {
							args_okay = 1;
						}
					}
				}
			}
		} else {
			ast_log(LOG_ERROR, "Out of memory\n");
			res = -1;
		}
	}

	if (args_okay) {
		char d;
		char *tmp = alloca(strlen(varname) + 4);
		char *tmp2 = alloca(1024);

		if (tmp && tmp2) {
			snprintf(tmp, strlen(varname) + 4, "${%s}", varname);
			memset(tmp2, 0, sizeof(tmp2));
		} else {
			ast_log(LOG_ERROR, "Out of memory");
			return -1;
		}

		if (delimiter[0])
			d = delimiter[0];
		else
			d = '-';

		pbx_substitute_variables_helper(chan, tmp, tmp2, 1024 - 1);

		if (tmp2) {
			int i;
			for (i=1;i<fieldnum;i++)
				if ((tmp2 - 1) && (tmp2[0] != '\0'))
					tmp2 = index(tmp2, d) + 1;
		} else {
			ast_log(LOG_ERROR, "Out of memory\n");
			res = -1;
		}

		if (tmp2 - 1) {
			/* Get end, if any */
			/* (if no further fields, then tmp2 -> NULL, but we don't care anymore) */
			char *value = strsep(&tmp2, &d);
			pbx_builtin_setvar_helper(chan, newvar, value);
		} else {
			pbx_builtin_setvar_helper(chan, newvar, NULL);
		}
	} else {
		ast_log(LOG_ERROR, "Usage: %s\n", cut_synopsis);
		res = -1;
	}

	LOCAL_USER_REMOVE(u);
	return res;
}

int unload_module(void)
{
	STANDARD_HANGUP_LOCALUSERS;
	return ast_unregister_application(app_cut);
}

int load_module(void)
{
	return ast_register_application(app_cut, cut_exec, cut_synopsis, cut_descrip);
}

char *description(void)
{
	return tdesc;
}

int usecount(void)
{
	int res;
	STANDARD_USECOUNT(res);
	return res;
}

char *key()
{
	return ASTERISK_GPL_KEY;
}

Reply via email to