Add -lncurses to LDD flags
[lcr.git] / mail.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** use mailer to send mail about message                                     **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 static const char *months[] = {
15         "January", "February", "March", "April", "Mai", "June", "July",
16         "August", "September", "October", "November", "December"
17 };
18
19
20 /*
21  * create mail with or without sample
22  * the process creates forks to keep pbx running
23  */
24 struct mail_args {
25         char    email[128];
26         char    filename[256];
27         int     year;
28         int     mon;
29         int     mday;
30         int     hour;
31         int     min;
32         char    callerid[64];
33         char    callerintern[32];
34         char    callername[64];
35         char    terminal[32];
36 };
37
38 static void *mail_child(void *arg)
39 {
40         struct mail_args *args = (struct mail_args *)arg;
41         char *email = args->email;
42         char *filename = args->filename;
43         int year = args->year;
44         int mon = args->mon;
45         int mday = args->mday;
46         int hour = args->hour;
47         int min = args->min;
48         char *callerid = args->callerid;
49         char *callerintern = args->callerintern;
50         char *callername = args->callername;
51         char *terminal = args->terminal;
52
53         char command[128];
54         char buffer[256];
55         char rbuf[54];
56         FILE *ph;
57         int fh;
58         unsigned char e1, e2, e3;
59         int i, n, cnt;
60         unsigned char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
61         struct sched_param schedp;
62         int ret;
63
64         PDEBUG(DEBUG_EPOINT, "child process started for sending a mail\n");
65
66         /* lower priority to keep pbx running fluently */
67         if (options.schedule > 0) {
68                 memset(&schedp, 0, sizeof(schedp));
69                 schedp.sched_priority = 0;
70                 ret = sched_setscheduler(0, SCHED_OTHER, &schedp);
71                 if (ret < 0) {
72                         PERROR("Scheduling to normal priority failed (errno = %d).\nExitting child process...\n", errno);
73                         goto done;
74                 }
75         }
76
77         /* open process */
78         SPRINT(command, "%s -f%s %s", SENDMAIL, options.email, email);
79         if ((ph = popen(command, "w")) < 0) {
80                 PERROR("Cannot send mail using command '%s'\n", command);
81                 goto done;
82         }
83
84         /* send header */
85         fprintf(ph, "MIME-Version: 1.0\n");
86         fprintf(ph, "Content-Type: multipart/mixed;\n\tboundary=\"next_part\"\n");
87         fprintf(ph, "From: %s <%s>\n", NAME, options.email);
88         fprintf(ph, "To: %s\n", email);
89         fprintf(ph, "Subject: Message from '%s' recorded.\n\n", callerid);
90
91         /* send message */
92         fprintf(ph, "This is a MIME-encapsulated message\n--next_part\n");
93         fprintf(ph, "Content-Type: text/plain; charset=us-ascii\nContent-Transfer-Encoding: 7bit\n\n");
94         fprintf(ph, "\nThe voice box of %s has recorded a message:\n\n * extension: %s\n * from: %s", NAME, terminal, callerid);
95         if (callerintern[0])
96                 fprintf(ph, " (intern %s)", callerintern);
97         if (callername[0])
98                 fprintf(ph, " %s", callername);
99         fprintf(ph, "\n * date: %s %d %d %d:%02d\n\n", months[mon], mday, year+1900, hour, min);
100
101         /* attach audio file */
102         if (filename[0]) {
103             if ((fh = open(filename, O_RDONLY))) {
104                 while(strchr(filename, '/'))
105                         filename = strchr(filename, '/')+1;
106                 fprintf(ph, "--next_part\n");
107                 if (strlen(filename) >= 4)
108                 if (!strcasecmp(filename+strlen(filename)-4, ".wav"))
109                         fprintf(ph, "Content-Type: audio/x-wav;\n\tname=\"%s\"\n", filename);
110                 fprintf(ph, "Content-Transfer-Encoding: base64\nContent-Disposition: inline;\n\tfilename=\"%s\"\n\n", filename);
111
112                 /* stream from disk and encode */
113                 while(42) {
114                         /* read exactly one line */
115                         cnt = read(fh, rbuf, 54);
116                         if (cnt <= 0)
117                                 break;
118                         /* encode */
119                         n = cnt;
120                         while (n%3) {
121                                 rbuf[n] = 0;
122                                 n++;
123                         }
124                         n = n/3;
125                         i = 0;
126                         while(i<n) {
127                                 e1 = rbuf[i+i+i];
128                                 e2 = rbuf[i+i+i+1];
129                                 e3 = rbuf[i+i+i+2];
130                                 buffer[(i<<2)+3] = base64[e3 & 0x3f];
131                                 buffer[(i<<2)+2] = base64[((e3>>6)+(e2<<2)) & 0x3f];
132                                 buffer[(i<<2)+1] = base64[((e2>>4)+(e1<<4)) & 0x3f];
133                                 buffer[i<<2] = base64[e1 >> 2];
134                                 i++;
135                         }
136                         if ((cnt%3) > 0)
137                                 buffer[(i<<2)-1] = '=';
138                         if ((cnt%3) == 1)
139                                 buffer[(i<<2)-2] = '=';
140                         buffer[(i<<2)] = '\n';
141                         buffer[(i<<2)+1] = '\0';
142                         /* write */
143                         fprintf(ph, "%s", buffer);
144                 }
145
146                 fprintf(ph, "\n\n");
147                 close(fh);
148             } else {
149                 SPRINT(buffer, "-Error- Failed to read audio file: '%s'.\n\n", filename);
150                 fprintf(ph, "%s", buffer);
151                 PERROR("%s", buffer);
152             }
153         }
154
155         /* finish mail */
156         fprintf(ph, ".\n");
157
158         /* wait for mail to be sent and close process */
159         pclose(ph);
160
161         done:
162         PDEBUG(DEBUG_EPOINT, "child process done for sending a mail\n");
163
164         /* exit process */
165         FREE(args, sizeof(struct mail_args));
166         amemuse--;
167         return(NULL);
168 }
169
170 void send_mail(char *filename, char *callerid, char *callerintern, char *callername, char *vbox_email, int vbox_year, int vbox_mon, int vbox_mday, int vbox_hour, int vbox_min, char *terminal)
171 {
172         struct mail_args *arg;
173         pthread_t tid;
174
175         arg = (struct mail_args *)MALLOC(sizeof(struct mail_args));
176         amemuse++;
177
178         SCPY(arg->email, vbox_email);
179         SCPY(arg->filename, filename);
180         arg->year = vbox_year;
181         arg->mon = vbox_mon;
182         arg->mday = vbox_mday;
183         arg->hour = vbox_hour;
184         arg->min = vbox_min;
185         SCPY(arg->callerid, callerid);
186         SCPY(arg->callerintern, callerintern);
187         SCPY(arg->callername, callername);
188         SCPY(arg->terminal, terminal);
189
190         if ((pthread_create(&tid, NULL, mail_child, arg)<0)) {
191                 PERROR("failed to create mail-thread.\n");
192                 return;
193         }
194
195         PDEBUG(DEBUG_EPOINT, "EPOINT '%s' send mail: child process created for sending a mail\n", terminal);
196 }
197