Debian Codegraph @

contrib/mailsound/mailsound.c

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