aboutsummaryrefslogtreecommitdiffstats
path: root/pbx/pbx_wilcalu.c
blob: 9fdf642be5395ad72affb97f6d98668f4218125e (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/** @file pbx_wilcalu.c 
 *
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Trivial application to playback a sound file
 * 
 * Copyright (C) 1999, Mark Spencer
 *
 * Mark Spencer <markster@linux-support.net>
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License

 *  Autodialer for Asterisk 
 *  Redirect dialstring thru fifo "/var/run/autodial.ctl"
 *  Format of string is :
 *  "tech/tele,filename&" ie. "tor1/23,file&"
 */
 
#include <asterisk/lock.h>
#include <asterisk/file.h>
#include <asterisk/logger.h>
#include <asterisk/channel.h>
#include <asterisk/pbx.h>
#include <asterisk/module.h>
#include <asterisk/translate.h>
#include <asterisk/options.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "../astconf.h"


// Globals
const   char dialfile[255];
static  char *tdesc = "Wil Cal U (Auto Dialer)";
static  pthread_t autodialer_thread;
static  char buf[257];
static  char lastbuf[257];//contains last partial buffer
static  char sendbuf[257];
STANDARD_LOCAL_USER;
LOCAL_USER_DECL;

//prototype
static void *dialstring(void *string);

// types
struct alarm_data {
time_t alarm_time;
int    snooze_len;
void   *dialstr;
};

static void *autodial(void *ignore)
{
	pthread_t dialstring_thread;
	char * sendbufptr=sendbuf;
	int fd=open(dialfile,O_RDONLY|O_NONBLOCK);
	int flags = fcntl(fd, F_GETFL);
	fd_set fds;
	fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
	if (option_debug)
		ast_log(LOG_DEBUG, "Entered Wil-Calu fd=%d\n",fd);
	if(fd<0) {
		ast_log(LOG_WARNING, "Autodial: Unable to open file\n");
		pthread_exit(NULL);
	}
	memset(buf,0,257);
	memset(lastbuf,0,257);
	memset(sendbuf,0,257);
	while(1){
		ssize_t bytes;
		void *pass;

		memset(buf,0,257);
		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		ast_select(fd + 1, &fds, NULL, NULL, NULL);
		bytes=read(fd,buf,256);
		buf[(int)bytes]=0;

		if(bytes>0){
			int x;
			ast_log(LOG_DEBUG, "WilCalu : Read Buf %s\n",buf);
			sendbufptr=sendbuf;
			for(x=0;lastbuf[x]!=0 && x<257;x++);
			if(x) {
				memcpy(sendbuf,lastbuf,x);
				sendbufptr+=x;
				memset(lastbuf,0,257);
			}
			/* Process bytes read */
			for(x=0;x<bytes;x++){
				/* if & then string is complete */
				if(buf[x]=='&'){
					if(NULL!=(pass=(void *)strdup(sendbuf))){
						pthread_create(&dialstring_thread,NULL,dialstring,pass);
						sendbufptr=sendbuf;
						memset(sendbuf,0,257);
					}
					else {
						perror("Autodial:Strdup failed");
						close(fd);
						pthread_exit(NULL);
					}
				} else {
					if(buf[x]=='\n')
						continue;
					*sendbufptr=buf[x];
					sendbufptr++;
					*sendbufptr=0;
				}
			}
			if(sendbufptr!=sendbuf)
				memcpy(lastbuf,sendbuf,sendbufptr-sendbuf+1);
		}
	}
	close(fd);
	pthread_exit(NULL);
	return NULL;
}

static void *snooze_alarm(void *pass){
	
	pthread_t dialstring_thread;
	struct alarm_data *data=(struct alarm_data *)pass;
	sleep(data->snooze_len);
	pthread_create(&dialstring_thread,NULL,dialstring,data->dialstr);
	// dialstring will free data->dialstr
	free(pass);
	pthread_exit(NULL);
	return NULL;
}
static void  set_snooze_alarm(char *dialstr,int snooze_len){
	pthread_t snooze_alarm_thread;
	struct alarm_data *pass;
	ast_log(LOG_DEBUG, "Answered: Snooze Requested\n");
	if(NULL==(pass=malloc(sizeof(struct alarm_data)))){
		perror("snooze_alarm: data");
		pthread_exit(NULL);
	}
	if(NULL==(pass->dialstr=(void *)strdup(dialstr))){
		free(pass);
		perror("snooze_alarm: dialstr");
		pthread_exit(NULL);
	}
	pass->snooze_len=snooze_len;
	pthread_create(&snooze_alarm_thread,NULL,snooze_alarm,pass);
}
			
static void *dialstring(void *string){
	struct ast_channel *channel;
	char *bufptr,*destptr;
	// ms affects number of rings
	int  ms=10000;
	int  cnt=0,first;
	char tech[256];
	char tele[256];
	char filename[256];
	int  answered=0;
	for(first=0,bufptr=(char *)string,destptr=tech;*bufptr&&cnt<256;cnt++){
		if(*bufptr=='/' && !first){
			*destptr=0;
			destptr=tele;
			first=1;
		}
		else if(*bufptr==','){
			*destptr=0;
			destptr=filename;
		} else {
			*destptr=*bufptr;
			destptr++;
		}
		bufptr++;
	} 
	*destptr=0;
	ast_log(LOG_DEBUG, "Printing string arg: %s Eos\n", (char *)string);
	if(strlen(tech)+strlen(tele)+strlen(filename)>256){
		ast_log(LOG_ERROR, "Autodial:Error string too long\n");
		free(string);
		pthread_exit(NULL);
	}
	ast_log(LOG_DEBUG, "Autodial Tech %s(%d) Tele %s(%d) Filename %s(%d)\n",tech,strlen(tech),tele,strlen(tele),filename,strlen(filename));

	channel=ast_request(tech,AST_FORMAT_SLINEAR,tele);
	if(channel!=NULL){
		ast_call(channel,tele,10000);
	}
	else {
		ast_log(LOG_ERROR, "Autodial:Sorry unable to obtain channel\n");
		free(string);
		pthread_exit(NULL);
	}
	if(channel->_state==AST_STATE_UP)
		ast_log(LOG_DEBUG, "Autodial:Line is Up\n");
	while(ms>0){
		struct ast_frame *f;
		ms=ast_waitfor(channel,ms);
		f=ast_read(channel);
		if(!f){
			ast_log(LOG_DEBUG, "Autodial:Hung Up\n");
			break;
		}
		if(f->frametype==AST_FRAME_CONTROL){
			if(f->subclass==AST_CONTROL_ANSWER){
				ast_log(LOG_DEBUG, "Autodial:Phone Answered\n");
				if(channel->_state==AST_STATE_UP){
					char res;
					ast_streamfile(channel,filename,0);
					// Press Five for snooze
					res=ast_waitstream(channel, "37");
					if(res=='3'){
						answered=1;
						set_snooze_alarm((char *)string,60);
						ast_streamfile(channel,"demo-thanks",0);
						ast_waitstream(channel, "");
					}
					else if(res=='7'){
						answered=1;
						ast_streamfile(channel,"demo-thanks",0);
						ast_waitstream(channel, "");
					}
					ast_stopstream(channel);
					ms=0;
				}
			}
			else if(f->subclass==AST_CONTROL_RINGING)
				ast_log(LOG_DEBUG, "Autodial:Phone Ringing end\n");
		}
		ast_frfree(f);
	}
	if(!answered)
		set_snooze_alarm((char *)string,5);
	free(string);
	ast_hangup(channel);
	ast_log(LOG_DEBUG, "Autodial:Hung up channel\n");
	pthread_exit(NULL);
	return NULL;
}
int unload_module(void)
{
	STANDARD_HANGUP_LOCALUSERS;
	unlink(dialfile);
	return 0;
}

int load_module(void)
{
	int val;
	snprintf((char *)dialfile,sizeof(dialfile)-1,"%s/%s",(char *)ast_config_AST_RUN_DIR,"autodial.ctl");
	if((val=mkfifo(dialfile, 0700))){
		if(errno!=EEXIST){
			ast_log(LOG_ERROR, "Error:%d Creating Autodial FIFO\n",errno);
			return 0;
		}
	}
	pthread_create(&autodialer_thread,NULL,autodial,NULL);
	return 0;
}

char *description(void)
{
	return tdesc;
}

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

char *key()
{
	return ASTERISK_GPL_KEY;
}