Debian Codegraph @

contrib/mailsound/mailsound.c

raw symbols ◀ ▶ SCIP
3 Debian patches touch this file
1 /*
2 * Copyright (C) 1993 Andrew Scherpbier <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <rplay.h>
23 #include <pwd.h>
•24 #include <time.h>
25 #include <unistd.h>
26 #define _REGEX_RE_COMP
27 #include <sys/types.h>
28 #include <regex.h>
29
30 #define PUBLIC
31 #define PRIVATE static
32
33 #undef TRUE
34 #define TRUE (1)
35 #undef FALSE
36 #define FALSE (0)
37
38 #define OK (0)
39 #define NOTOK (-1)
40
41 /* Private routines
42 * ================
43 */
44 PRIVATE void dump_state();
45 PRIVATE void parse_args(int ac, char **av, int size);
•46 PRIVATE int compare(char *buffer, char *from, long size);
47 PRIVATE int play_sound(char *sound, long size);
48
49 /* Private variables
50 * =================
51 */
52 PRIVATE char hostname[200] = "localhost";
53 PRIVATE int volume = 127;
54 PRIVATE int use_random = FALSE;
55 PRIVATE int use_size_volume = FALSE;
56 PRIVATE int use_size_sound = FALSE;
57 PRIVATE int min_size = 0;
58 PRIVATE int max_size = 10000;
59 PRIVATE int min_volume = 10;
60 PRIVATE int max_volume = 200;
61 PRIVATE char *sound = "youvegotmail";
62 PRIVATE char *subject_re = NULL;
63 PRIVATE char subject[200];
64 PRIVATE int debug = FALSE;
65
66
67 /* Public routines
68 * ===============
69 */
70
71 /* Public variables
72 * ================
73 */
74
75 /**************************************************************************
•76 * PUBLIC int main(int ac, char **av)
77 * PURPOSE:
78 * None
79 */
•80 PUBLIC int main(int ac, char **av)
81 {
82 char buffer[1000];
83 char path[200];
84 char address[1000];
85 char *p;
86 FILE *fl;
87 int size = 0;
88 struct passwd *pw;
89
90 /*
91 * Read the first line of the message. This is what we will use
92 * to find out what sound to play.
93 */
•94 //original version: gets(buffer);
95 if (fgets(buffer, sizeof(buffer), stdin)) {
96 buffer[strcspn(buffer, "\n")] = '\0';
97 }
98 size = strlen(buffer) + 1;
99 if (!strtok(buffer, " "))
100 exit(1);
101 p = strtok(NULL, " \t\n");
102 strcpy(address, p);
103 subject[0] = '\0';
104
105 /*
106 * We now need to read the rest of the message to determine its
107 * size.
108 */
•109 //original version: while (gets(buffer))
110 while (fgets(buffer, sizeof(buffer), stdin))
111 {
•112 buffer[strcspn(buffer, "\n")] = '\0';
113 if (strncmp(buffer, "Subject: ", 9) == 0)
114 {
115 strcpy(subject, buffer + 9);
116 }
117 size += strlen(buffer) + 1;
118 }
119
120 /*
121 * Deal with the command line arguments to be used as defaults.
122 */
123 parse_args(ac, av, size);
124
125 if (debug)
126 dump_state();
127
128 /*
129 * Find the configuration file in the home directory
130 */
131 pw = getpwuid(getuid());
132 sprintf(path, "%s/.mailsounds", pw->pw_dir);
133 fl = fopen(path, "r");
134 if (fl == NULL)
135 exit(0);
136
137 /*
138 * Search through this file to find a line which will match the
139 * address
140 */
141 while (fgets(buffer, 1000, fl) != NULL)
142 {
143 compare(buffer, address, size);
144 }
•145 /* if there is nothing to play, play nothing */
146 if (av[1]!=NULL) play_sound(av[1], 0);
147 fclose(fl);
148 exit(0);
149 }
150
151
152 /**************************************************************************
•153 * PRIVATE int compare(char *buffer, char *from, long size)
154 * PURPOSE:
155 * None
156 * PARAMETERS:
157 * None
158 */
•159 PRIVATE int compare(char *buffer, char *from, long size)
160 {
161 char *pattern, *sound_args;
162 char *p;
163
164 pattern = strtok(buffer, " \t");
165 sound_args = strtok(NULL, "\t\n");
166 if ((char *)re_comp(pattern) != NULL)
•167 return 0;
168 if (re_exec(from))
169 {
170 if (debug)
171 printf("Normal sound is played\n");
172 if (play_sound(sound_args, size) == OK)
173 exit(0);
174 }
175 }
176
177
178 /**************************************************************************
•179 * PRIVATE int play_sound(char *sound_args, long size)
180 * PURPOSE:
181 * None
182 * PARAMETERS:
183 * None
184 */
•185 PRIVATE int play_sound(char *sound_args, long size)
186 {
187 int ac;
188 char *av[50];
189 char *token;
190
191 /*
192 * We need to build an argument list from the sound_args string.
193 */
194 ac = 1;
195 av[0] = "xxxx";
196 token = strtok(sound_args, " \t");
197 while (token && ac < 50)
198 {
199 av[ac++] = token;
200 token = strtok(NULL, " \t");
201 }
202 subject_re = NULL;
203 parse_args(ac, av, size);
204
205 if (subject_re)
206 {
207 if ((char *)re_comp(subject_re) != NULL)
208 return NOTOK;
209 if (!re_exec(subject))
210 return NOTOK;
211 }
212
213 if (debug)
214 dump_state();
215
216 /*
217 * Play the sound on each of the hosts listed in the hostname.
218 */
219 token = strtok(hostname, ":");
220 while (token)
221 {
222 rplay_host_volume(token, sound, volume);
223 token = strtok(NULL, ":");
224 }
225 return OK;
226 }
227
228
229 /**************************************************************************
230 * PRIVATE void parse_args(int ac, char **av, int size)
231 * PURPOSE:
232 * Take arguments and extract data from them. The arguments are
233 * assumed to be in the same format as what is passed to the
234 * main() program.
235 */
236 PRIVATE void parse_args(int ac, char **av, int size)
237 {
238 int c;
239 int i, count;
240 extern char *optarg;
241 extern int optind;
242 char *sounds[50];
243
244 optind = 1;
245 subject_re = NULL;
246 while ((c = getopt(ac, av, "s:h:rz:Z:v:d")) != NOTOK)
247 {
248 switch (c)
249 {
250 case 'h':
251 strcpy(hostname, optarg);
252 break;
253 case 'r':
254 use_random = TRUE;
255 break;
256 case 's':
257 subject_re = strdup(optarg);
258 break;
259 case 'z':
260 sscanf(optarg, "%d:%d,%d:%d", &min_size, &min_volume, &max_size, &max_volume);
261 use_size_volume = TRUE;
262 break;
263 case 'Z':
264 sscanf(optarg, "%d,%d", &min_size, &max_size);
265 use_size_sound = TRUE;
266 break;
267 case 'v':
268 volume = atoi(optarg);
269 break;
270 case 'd':
271 debug = TRUE;
272 break;
273 }
274 }
275
276 /*
277 * The rest of the arguments are assumed to be sounds.
278 */
279 count = 0;
280 for (i = optind; i < ac && count < 50; i++)
281 {
282 sounds[count++] = av[i];
283 }
284
285 /*
286 * By default we will pick the first sound in the list
287 */
288 sound = sounds[0];
289
290 /*
291 * Now pick the sound and the volume.
292 */
293 if (use_random)
294 {
295 srandom(time(NULL));
296 sound = sounds[random() % count];
297 }
298 else if (use_size_sound)
299 {
300 if (size < min_size)
301 sound = sounds[0];
302 if (size > max_size)
303 sound = sounds[count - 1];
304 else
305 {
306 sound = sounds[(size - min_size) * count / (max_size - min_size)];
307 }
308 }
309
310 if (use_size_volume)
311 {
312 if (size < min_size)
313 volume = min_volume;
314 else if (size > max_size)
315 volume = max_volume;
316 else
317 volume = (size - min_size) * (max_volume - min_volume) / (max_size - min_size) + min_volume;
318 }
319 }
320
321
322
323 /**************************************************************************
324 * PRIVATE void dump_state()
325 * PURPOSE:
326 * Produce debug output of all the global variables which make
327 * up the state of the program
328 */
329 PRIVATE void dump_state()
330 {
331 printf("host: '%s', volume: %d, random: %d, size_volume: %d, size_sound: %d\n",
332 hostname,
333 volume,
334 use_random,
335 use_size_volume,
336 use_size_sound);
337 printf("range data: v: (%d, %d), s: (%d, %d)\n",
338 min_volume, max_volume,
339 min_size, max_size);
340 printf("sound: '%s'\n", sound);
341 if (subject_re)
342 printf("Subject regular expression: '%s'\n", subject_re);
343 printf("Subject: '%s'\n", subject);
344 }
345
346
347