comparison src/context.cpp @ 154:89ce226e5383

add auto whitelisting
author carl
date Sat, 07 Jul 2007 21:24:38 -0700
parents 8d7c439bb6fa
children a220bfb9211f
comparison
equal deleted inserted replaced
153:8d7c439bb6fa 154:89ce226e5383
423 WHITELISTER::WHITELISTER(char *f, int d) { 423 WHITELISTER::WHITELISTER(char *f, int d) {
424 fn = f; 424 fn = f;
425 days = d; 425 days = d;
426 pthread_mutex_init(&mutex, 0); 426 pthread_mutex_init(&mutex, 0);
427 need = false; 427 need = false;
428 ifstream ifs;
429 ifs.open(fn);
430 if (!ifs.fail()) {
431 const int maxlen = 1000;
432 char buf[maxlen];
433 while (ifs.getline(buf, maxlen)) {
434 char *p = strchr(buf, ' ');
435 if (p) {
436 *p = '\0';
437 char *who = strdup(buf);
438 int when = atoi(p+1);
439 rcpts[who] = when;
440 }
441 }
442 }
443 ifs.close();
428 } 444 }
429 445
430 446
431 void WHITELISTER::writer() { 447 void WHITELISTER::writer() {
432 pthread_mutex_lock(&mutex); 448 pthread_mutex_lock(&mutex);
433 time_t limit = time(NULL) - days*86400; 449 time_t limit = time(NULL) - days*86400;
434 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) { 450 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) {
435 time_t when = (*i).second; 451 time_t when = (*i).second;
436 if (when < limit) { 452 if (when < limit) {
453 char *who = (*i).first;
454 free(who);
437 autowhite_sent::iterator j = i; 455 autowhite_sent::iterator j = i;
438 j++; 456 j++;
439 rcpts.erase(i); 457 rcpts.erase(i);
440 i = j; 458 i = j;
441 need = true; 459 need = true;
442 } 460 }
443 else i++; 461 else i++;
444 } 462 }
445 if (need) { 463 if (need) {
446 // dump the file 464 // dump the file
447 ofstream os; 465 ofstream ofs;
448 os.open(fn); 466 ofs.open(fn);
449 if (!os.fail()) { 467 if (!ofs.fail()) {
450 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end(); i++) { 468 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end(); i++) {
451 char *who = (*i).first; 469 char *who = (*i).first;
452 int when = (*i).second; 470 int when = (*i).second;
453 os << who << " " << when << endl; 471 ofs << who << " " << when << endl;
454 } 472 }
455 } 473 }
456 os.close(); 474 ofs.close();
457 } 475 }
458 pthread_mutex_unlock(&mutex); 476 pthread_mutex_unlock(&mutex);
459 } 477 }
460 478
461 479
462 void WHITELISTER::sent(char *to) { 480 void WHITELISTER::sent(char *to) {
481 // we take ownership of the string
463 pthread_mutex_lock(&mutex); 482 pthread_mutex_lock(&mutex);
464 need = true; 483 need = true;
465 rcpts[to] = time(NULL); 484 rcpts[to] = time(NULL);
466 pthread_mutex_unlock(&mutex); 485 pthread_mutex_unlock(&mutex);
467 } 486 }