| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| •76 |
|
| 77 |
|
| 78 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| •94 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| •109 |
|
| 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 |
|
| 122 |
|
| 123 |
parse_args(ac, av, size); |
| 124 |
|
| 125 |
if (debug) |
| 126 |
dump_state(); |
| 127 |
|
| 128 |
|
| 129 |
|
| 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 |
|
| 139 |
|
| 140 |
|
| 141 |
while (fgets(buffer, 1000, fl) != NULL) |
| 142 |
{ |
| 143 |
compare(buffer, address, size); |
| 144 |
} |
| •145 |
|
| 146 |
if (av[1]!=NULL) play_sound(av[1], 0); |
| 147 |
fclose(fl); |
| 148 |
exit(0); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| •153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 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 |
|
| 278 |
|
| 279 |
count = 0; |
| 280 |
for (i = optind; i < ac && count < 50; i++) |
| 281 |
{ |
| 282 |
sounds[count++] = av[i]; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
sound = sounds[0]; |
| 289 |
|
| 290 |
|
| 291 |
|
| 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 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 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 |
|