summaryrefslogtreecommitdiff
path: root/funcs/func_strings.c
blob: 61cef2625f42a1f9877607cbbacf1d509acfc50b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * String manipulation dialplan functions
 * 
 * Copyright (C) 2005, Digium, Inc.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>

#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/logger.h"
#include "asterisk/utils.h"
#include "asterisk/app.h"
#include "asterisk/localtime.h"

static char *function_fieldqty(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len)
{
	char *varname, *varval, workspace[256];
	char *delim = ast_strdupa(data);
	int fieldcount = 0;

	if (delim) {
		varname = strsep(&delim, "|");
		pbx_retrieve_variable(chan, varname, &varval, workspace, sizeof(workspace), NULL);
		while (strsep(&varval, delim))
			fieldcount++;
		snprintf(buf, len, "%d", fieldcount);
	} else {
		ast_log(LOG_ERROR, "Out of memory\n");
		strncpy(buf, "1", len);
	}
	return buf;
}

#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function fieldqty_function = {
	.name = "FIELDQTY",
	.synopsis = "Count the fields, with an arbitrary delimiter",
	.syntax = "FIELDQTY(<varname>,<delim>)",
	.read = function_fieldqty,
};

static char *builtin_function_regex(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
{
	char *ret_true = "1", *ret_false = "0", *ret;
	char *arg, *earg, *tmp, errstr[256] = "";
	int errcode;
	regex_t regexbuf;

	ret = ret_false; /* convince me otherwise */
	tmp = ast_strdupa(data);
	if (tmp) {
		/* Regex in quotes */
		arg = strchr(tmp, '"');
		if (arg) {
			arg++;
			earg = strrchr(arg, '"');
			if (earg) {
				*earg = '\0';
			}
		} else {
			arg = tmp;
		}

		if ((errcode = regcomp(&regexbuf, arg, REG_EXTENDED | REG_NOSUB))) {
			regerror(errcode, &regexbuf, errstr, sizeof(errstr));
			ast_log(LOG_WARNING, "Malformed input %s(%s): %s\n", cmd, data, errstr);
			ret = NULL;
		} else {
			ret = regexec(&regexbuf, data, 0, NULL, 0) ? ret_false : ret_true;
		}
		regfree(&regexbuf);
	} else {
		ast_log(LOG_ERROR, "Out of memory in %s(%s)\n", cmd, data);
	}

	return ret;
}

#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function regex_function = {
	.name = "REGEX",
	.synopsis = "Regular Expression: Returns 1 if data matches regular expression.",
	.syntax = "REGEX(\"<regular expression>\" <data>)",
	.read = builtin_function_regex,
};

static char *builtin_function_len(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
{
	int length = 0;
	if (data) {
		length = strlen(data);
	}
	snprintf(buf, len, "%d", length);
	return buf;
}

#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function len_function = {
	.name = "LEN",
	.synopsis = "Returns the length of the argument given",
	.syntax = "LEN(<string>)",
	.read = builtin_function_len,
};

static char *acf_strftime(struct ast_channel *chan, char *cmd, char *data, char *buf, size_t len) 
{
	char *format, *epoch, *timezone;
	long epochi;
	struct timeval tv;
	struct tm time;

	if (data) {
		format = ast_strdupa(data);
		if (format) {
			epoch = strsep(&format, "|");
			timezone = strsep(&format, "|");

			if (epoch && !ast_strlen_zero(epoch) && sscanf(epoch, "%ld", &epochi) == 1) {
			} else if (!gettimeofday(&tv, NULL)) {
				epochi = tv.tv_sec;
			} else {
				ast_log(LOG_ERROR, "Cannot gettimeofday() ?!!\n");
				return "";
			}

			ast_localtime(&epochi, &time, timezone);

			if (!format) {
				format = "%c";
			}

			buf[0] = '\0';
			if (! strftime(buf, len, format, &time)) {
				ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
			}
			buf[len - 1] = '\0';

			return buf;
		} else {
			ast_log(LOG_ERROR, "Out of memory\n");
		}
	} else {
		ast_log(LOG_ERROR, "Asterisk function STRFTIME() requires an argument.\n");
	}
	return "";
}

#ifndef BUILTIN_FUNC
static
#endif
struct ast_custom_function strftime_function = {
	.name = "STRFTIME",
	.synopsis = "Returns the current date/time in a specified format.",
	.syntax = "STRFTIME([<epoch>][,[timezone][,format]])",
	.read = acf_strftime,
};