aboutsummaryrefslogtreecommitdiffstats
path: root/pbx/pbx_wilcalu.c
blob: 9a98cda7de96699af4afd0234a7d4cb6259d1eac (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
/*
 * 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
 */
 
#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 <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>

const   char *dialfile="/var/run/autodial.ctl";
static  char *tdesc = "Wil Cal U (Auto Dialer)";
static  pthread_t autodialer_thread;
static  char buf[256];
extern  int errno;
STANDARD_LOCAL_USER;
LOCAL_USER_DECL;

static void *autodial(void *ignore)
{
	while(1){
		struct ast_channel *channel;
		int fd=open(dialfile,O_RDONLY);
		char *bufptr,*destptr;
		int  ms=10000;
		int  cnt=0,first;
		char tech[256];
		char tele[256];
		char filename[256];

		if(fd<0) {
			printf("Autodial: Unable to open file\n");
			pthread_exit(NULL);
		}
		memset(buf,0,256);
		read(fd,buf,256);
		for(first=0,bufptr=buf,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--;*destptr=0;
		if(strlen(tech)+strlen(tele)+strlen(filename)>256){
			printf("Autodial:Error string Error too long\n");
			pthread_exit(NULL);
		}
			
#if 0
		printf("Autodial Tech %s(%d) Tele %s(%d) Filename %s(%d)\n",tech,strlen(tech),tele,strlen(tele),filename,strlen(filename));
#endif
		channel=ast_request(tech,AST_FORMAT_SLINEAR,tele);
		if(channel!=NULL){
			ast_call(channel,tele,10000);
		}
		else {
			printf("Autodial:Sorry unable to obtain channel\n");
			continue;
		}
		if(channel->state==AST_STATE_UP)
			printf("Autodial:Line is Up\n");
		while(ms>0){
			struct ast_frame *f;
			ms=ast_waitfor(channel,ms);
			f=ast_read(channel);
			if(!f){
				printf("Autodial:Hung Up\n");
				break;
			}
			if(f->frametype==AST_FRAME_CONTROL){
				if(f->subclass==AST_CONTROL_ANSWER){
					printf("Autodial:Phone Answered\n");
					if(channel->state==AST_STATE_UP){
						ast_streamfile(channel,filename,0);
						ast_waitstream(channel, "");
						ast_stopstream(channel);
						ms=0;
					}
				}
				else if(f->subclass==AST_CONTROL_RINGING)
					printf("Autodial:Phone Ringing end\n");
			}
			ast_frfree(f);
		}
		ast_hangup(channel);
		printf("Autodial:Hung up channel\n");
		close(fd);
	}
	// never reached
	return NULL;
}
int unload_module(void)
{
	STANDARD_HANGUP_LOCALUSERS;
	unlink(dialfile);
	return 0;
}

int load_module(void)
{
	int val;
	if((val=mkfifo(dialfile,O_RDWR))){
		if(errno!=EEXIST){
			printf("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;
}