Version 0.1.9 from FTP
[asterisk/asterisk.git] / pbx / pbx_wilcalu.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Trivial application to playback a sound file
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
14 #include <asterisk/file.h>
15 #include <asterisk/logger.h>
16 #include <asterisk/channel.h>
17 #include <asterisk/pbx.h>
18 #include <asterisk/module.h>
19 #include <asterisk/translate.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <pthread.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <errno.h>
26
27 const   char *dialfile="/var/run/autodial.ctl";
28 static  char *tdesc = "Wil Cal U (Auto Dialer)";
29 static  pthread_t autodialer_thread;
30 static  char buf[256];
31 extern  int errno;
32 STANDARD_LOCAL_USER;
33 LOCAL_USER_DECL;
34
35 static void *autodial(void *ignore)
36 {
37         while(1){
38                 struct ast_channel *channel;
39                 int fd=open(dialfile,O_RDONLY);
40                 char *bufptr,*destptr;
41                 int  ms=10000;
42                 int  cnt=0,first;
43                 char tech[256];
44                 char tele[256];
45                 char filename[256];
46
47                 if(fd<0) {
48                         printf("Autodial: Unable to open file\n");
49                         pthread_exit(NULL);
50                 }
51                 memset(buf,0,256);
52                 read(fd,buf,256);
53                 for(first=0,bufptr=buf,destptr=tech;*bufptr&&cnt<256;cnt++){
54                         if(*bufptr=='/' && !first){
55                                 *destptr=0;
56                                 destptr=tele;
57                                 first=1;
58                         }
59                         else if(*bufptr==','){
60                                 *destptr=0;
61                                 destptr=filename;
62                         } else {
63                                 *destptr=*bufptr;
64                                 destptr++;
65                         }
66                         bufptr++;
67                 } destptr--;*destptr=0;
68                 if(strlen(tech)+strlen(tele)+strlen(filename)>256){
69                         printf("Autodial:Error string Error too long\n");
70                         pthread_exit(NULL);
71                 }
72                         
73 #if 0
74                 printf("Autodial Tech %s(%d) Tele %s(%d) Filename %s(%d)\n",tech,strlen(tech),tele,strlen(tele),filename,strlen(filename));
75 #endif
76                 channel=ast_request(tech,AST_FORMAT_SLINEAR,tele);
77                 if(channel!=NULL){
78                         ast_call(channel,tele,10000);
79                 }
80                 else {
81                         printf("Autodial:Sorry unable to obtain channel\n");
82                         continue;
83                 }
84                 if(channel->state==AST_STATE_UP)
85                         printf("Autodial:Line is Up\n");
86                 while(ms>0){
87                         struct ast_frame *f;
88                         ms=ast_waitfor(channel,ms);
89                         f=ast_read(channel);
90                         if(!f){
91                                 printf("Autodial:Hung Up\n");
92                                 break;
93                         }
94                         if(f->frametype==AST_FRAME_CONTROL){
95                                 if(f->subclass==AST_CONTROL_ANSWER){
96                                         printf("Autodial:Phone Answered\n");
97                                         if(channel->state==AST_STATE_UP){
98                                                 ast_streamfile(channel,filename,0);
99                                                 ast_waitstream(channel, "");
100                                                 ast_stopstream(channel);
101                                                 ms=0;
102                                         }
103                                 }
104                                 else if(f->subclass==AST_CONTROL_RINGING)
105                                         printf("Autodial:Phone Ringing end\n");
106                         }
107                         ast_frfree(f);
108                 }
109                 ast_hangup(channel);
110                 printf("Autodial:Hung up channel\n");
111                 close(fd);
112         }
113         // never reached
114         return NULL;
115 }
116 int unload_module(void)
117 {
118         STANDARD_HANGUP_LOCALUSERS;
119         unlink(dialfile);
120         return 0;
121 }
122
123 int load_module(void)
124 {
125         int val;
126         if((val=mkfifo(dialfile,O_RDWR))){
127                 if(errno!=EEXIST){
128                         printf("Error:%d Creating Autodial FIFO\n",errno);
129                         return 0;
130                 }
131         }
132         pthread_create(&autodialer_thread,NULL,autodial,NULL);
133         return 0;
134 }
135
136 char *description(void)
137 {
138         return tdesc;
139 }
140
141 int usecount(void)
142 {
143         int res;
144         STANDARD_USECOUNT(res);
145         return res;
146 }
147
148 char *key()
149 {
150         return ASTERISK_GPL_KEY;
151 }