"Fossies" - the Fresh Open Source Software Archive

Member "postal-0.76/rabid.cpp" (18 Aug 2014, 4129 Bytes) of package /linux/privat/postal-0.76.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "rabid.cpp" see the Fossies "Dox" file reference documentation.

    1 #ifndef _GNU_SOURCE
    2 #define _GNU_SOURCE
    3 #endif
    4 
    5 #include "userlist.h"
    6 #include "client.h"
    7 #include <cstdlib>
    8 #include <unistd.h>
    9 #include <sys/wait.h>
   10 #include <signal.h>
   11 #include <stdio.h>
   12 #include <strings.h>
   13 #include "postal.h"
   14 #include "logit.h"
   15 #ifdef USE_GNUTLS
   16 #include <errno.h>
   17 #if GNUTLS_VERSION_NUMBER <= 0x020b00
   18 #include <gcrypt.h>
   19 GCRY_THREAD_OPTION_PTHREAD_IMPL;
   20 #endif /* GNUTLS_VERSION_NUMBER */
   21 #endif
   22 #include <cstring>
   23 
   24 void usage()
   25 {
   26   printf("Usage: rabid [-r max-connections-per-minute] [-p processes] [-l local-address]\n"
   27          "             [-c messages-per-connection] [-a] [-i imap-percentage]\n"
   28 #ifdef USE_SSL
   29          "             [-s ssl-percentage] [-d download-percentage[:delete-percentage]]\n"
   30 #endif
   31          "             [-[z|Z] debug-file] [-u]\n"
   32          "             pop-server user-list-filename\n"
   33          "\n"
   34          "Rabid Version: " VER_STR "\n");
   35   exit(eParam);
   36 }
   37 
   38 int exitCount = 0;
   39 
   40 void endit(int)
   41 {
   42   exitCount++;
   43   if(exitCount > 2)
   44     exit(1);
   45 }
   46 
   47 int main(int argc, char **argv)
   48 {
   49 
   50   int processes = 1;
   51   int connectionsPerMinute = 0;
   52   int msgsPerConnection = -1;
   53   const char *ourAddr = NULL;
   54   bool logAll = false;
   55 #ifdef USE_SSL
   56   int ssl = 0;
   57 #endif
   58   int imap = 0;
   59   int downloadPercent = 100, deletePercent = 100;
   60   TRISTATE qmail_pop = eNONE;
   61   PCCHAR debugName = NULL;
   62   bool debugMultipleFiles = false;
   63   bool strip_domain = false;
   64 #ifdef USE_GNUTLS
   65 #if GNUTLS_VERSION_NUMBER <= 0x020b00
   66   gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
   67 #endif
   68   gnutls_global_init();
   69   if(!gnutls_check_version(GNUTLS_VER))
   70   {
   71     fprintf(stderr, "Needs version " GNUTLS_VER " of GNUTLS\n");
   72     exit(1);
   73   }
   74 #endif
   75 
   76   int c;
   77   while(-1 != (c = getopt(argc, argv, "ab:d:c:i:l:p:r:s:uz:Z:")) )
   78   {
   79     switch(char(c))
   80     {
   81       case '?':
   82       case ':':
   83         usage();
   84       break;
   85       case 'a':
   86         logAll = true;
   87       break;
   88       case 'b':
   89         if(!strcasecmp(optarg, "qmail-pop"))
   90           qmail_pop = eWONT;
   91       break;
   92       case 'c':
   93         msgsPerConnection = atoi(optarg);
   94       break;
   95       case 'd':
   96         if(sscanf(optarg, "%d:%d", &downloadPercent, &deletePercent) < 2)
   97           deletePercent = 100;
   98       break;
   99       case 'i':
  100         imap = atoi(optarg);
  101       break;
  102       case 'l':
  103         ourAddr = optarg;
  104       break;
  105       case 'p':
  106         processes = atoi(optarg);
  107       break;
  108       case 'r':
  109         connectionsPerMinute = atoi(optarg);
  110       break;
  111       case 's':
  112 #ifdef USE_SSL
  113         ssl = atoi(optarg);
  114 #else
  115         usage();
  116 #endif
  117       break;
  118       case 'u':
  119         strip_domain = true;
  120       break;
  121       case 'Z':
  122         debugMultipleFiles = true;
  123       case 'z':
  124         debugName = optarg;
  125       break;
  126     }
  127   }
  128   if(processes < 1 || processes > MAX_PROCESSES || connectionsPerMinute < 0)
  129     usage();
  130 #ifdef USE_SSL
  131   if(ssl < 0 || ssl > 100)
  132     usage();
  133 #endif
  134   if(imap < 0 || imap > 100)
  135     usage();
  136   if(downloadPercent < 0 || downloadPercent > 100)
  137     usage();
  138   if(deletePercent < 0 || deletePercent > 100)
  139     usage();
  140   if(optind + 2 != argc)
  141     usage();
  142 
  143   UserList ul(argv[optind + 1], true, strip_domain);
  144 
  145   struct sigaction sa;
  146   memset(&sa, 0, sizeof(sa));
  147   sa.sa_sigaction = NULL;
  148   sa.sa_handler = SIG_IGN;
  149   sa.sa_flags = 0;
  150   if(sigaction(SIGPIPE, &sa, NULL))
  151   {
  152     printf("Can't block SIGPIPE.\n");
  153     return eSystem;
  154   }
  155 
  156   sa.sa_flags = SA_SIGINFO;
  157   sa.sa_handler = &endit;
  158   if(sigaction(SIGINT, &sa, NULL))
  159   {
  160     printf("Can't handle SIGINT.\n");
  161     return eSystem;
  162   }
  163 
  164   printf("time,messages,data(K),errors,connections"
  165 #ifdef USE_SSL
  166          ",SSL connections"
  167 #endif
  168          ",IMAP connections\n");
  169   Logit log("rabid.log", logAll, false, 0);
  170   Logit *debug = NULL;
  171  
  172   if(debugName)
  173     debug = new Logit(debugName, false, debugMultipleFiles, 0);
  174   client popper(&exitCount, argv[optind], ourAddr, ul, processes, msgsPerConnection, &log
  175 #ifdef USE_SSL
  176               , ssl
  177 #endif
  178               , qmail_pop, imap, downloadPercent, deletePercent, debug);
  179 
  180   int rc = popper.doAllWork(connectionsPerMinute);
  181   if(debug)
  182     delete debug;
  183   return rc;
  184 }
  185