| 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 |
/* if there is nothing to play, play nothing */ |
| 136 |
if (av[1]!=NULL) 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 0; |
| 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 |
subject_re = NULL; |
| 193 |
parse_args(ac, av, size); |
| 194 |
|
| 195 |
if (subject_re) |
| 196 |
{ |
| 197 |
if ((char *)re_comp(subject_re) != NULL) |
| 198 |
return NOTOK; |
| 199 |
if (!re_exec(subject)) |
| 200 |
return NOTOK; |
| 201 |
} |
| 202 |
|
| 203 |
if (debug) |
| 204 |
dump_state(); |
| 205 |
|
| 206 |
/* |
| 207 |
* Play the sound on each of the hosts listed in the hostname. |
| 208 |
*/ |
| 209 |
token = strtok(hostname, ":"); |
| 210 |
while (token) |
| 211 |
{ |
| 212 |
rplay_host_volume(token, sound, volume); |
| 213 |
token = strtok(NULL, ":"); |
| 214 |
} |
| 215 |
return OK; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/************************************************************************** |
| 220 |
* PRIVATE void parse_args(int ac, char **av, int size) |
| 221 |
* PURPOSE: |
| 222 |
* Take arguments and extract data from them. The arguments are |
| 223 |
* assumed to be in the same format as what is passed to the |
| 224 |
* main() program. |
| 225 |
*/ |
| 226 |
PRIVATE void parse_args(int ac, char **av, int size) |
| 227 |
{ |
| 228 |
int c; |
| 229 |
int i, count; |
| 230 |
extern char *optarg; |
| 231 |
extern int optind; |
| 232 |
char *sounds[50]; |
| 233 |
|
| 234 |
optind = 1; |
| 235 |
subject_re = NULL; |
| 236 |
while ((c = getopt(ac, av, "s:h:rz:Z:v:d")) != NOTOK) |
| 237 |
{ |
| 238 |
switch (c) |
| 239 |
{ |
| 240 |
case 'h': |
| 241 |
strcpy(hostname, optarg); |
| 242 |
break; |
| 243 |
case 'r': |
| 244 |
use_random = TRUE; |
| 245 |
break; |
| 246 |
case 's': |
| 247 |
subject_re = strdup(optarg); |
| 248 |
break; |
| 249 |
case 'z': |
| 250 |
sscanf(optarg, "%d:%d,%d:%d", &min_size, &min_volume, &max_size, &max_volume); |
| 251 |
use_size_volume = TRUE; |
| 252 |
break; |
| 253 |
case 'Z': |
| 254 |
sscanf(optarg, "%d,%d", &min_size, &max_size); |
| 255 |
use_size_sound = TRUE; |
| 256 |
break; |
| 257 |
case 'v': |
| 258 |
volume = atoi(optarg); |
| 259 |
break; |
| 260 |
case 'd': |
| 261 |
debug = TRUE; |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* The rest of the arguments are assumed to be sounds. |
| 268 |
*/ |
| 269 |
count = 0; |
| 270 |
for (i = optind; i < ac && count < 50; i++) |
| 271 |
{ |
| 272 |
sounds[count++] = av[i]; |
| 273 |
} |
| 274 |
|
| 275 |
/* |
| 276 |
* By default we will pick the first sound in the list |
| 277 |
*/ |
| 278 |
sound = sounds[0]; |
| 279 |
|
| 280 |
/* |
| 281 |
* Now pick the sound and the volume. |
| 282 |
*/ |
| 283 |
if (use_random) |
| 284 |
{ |
| 285 |
srandom(time(NULL)); |
| 286 |
sound = sounds[random() % count]; |
| 287 |
} |
| 288 |
else if (use_size_sound) |
| 289 |
{ |
| 290 |
if (size < min_size) |
| 291 |
sound = sounds[0]; |
| 292 |
if (size > max_size) |
| 293 |
sound = sounds[count - 1]; |
| 294 |
else |
| 295 |
{ |
| 296 |
sound = sounds[(size - min_size) * count / (max_size - min_size)]; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
if (use_size_volume) |
| 301 |
{ |
| 302 |
if (size < min_size) |
| 303 |
volume = min_volume; |
| 304 |
else if (size > max_size) |
| 305 |
volume = max_volume; |
| 306 |
else |
| 307 |
volume = (size - min_size) * (max_volume - min_volume) / (max_size - min_size) + min_volume; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
/************************************************************************** |
| 314 |
* PRIVATE void dump_state() |
| 315 |
* PURPOSE: |
| 316 |
* Produce debug output of all the global variables which make |
| 317 |
* up the state of the program |
| 318 |
*/ |
| 319 |
PRIVATE void dump_state() |
| 320 |
{ |
| 321 |
printf("host: '%s', volume: %d, random: %d, size_volume: %d, size_sound: %d\n", |
| 322 |
hostname, |
| 323 |
volume, |
| 324 |
use_random, |
| 325 |
use_size_volume, |
| 326 |
use_size_sound); |
| 327 |
printf("range data: v: (%d, %d), s: (%d, %d)\n", |
| 328 |
min_volume, max_volume, |
| 329 |
min_size, max_size); |
| 330 |
printf("sound: '%s'\n", sound); |
| 331 |
if (subject_re) |
| 332 |
printf("Subject regular expression: '%s'\n", subject_re); |
| 333 |
printf("Subject: '%s'\n", subject); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|