"Fossies" - the Fresh Open Source Software Archive 
Member "postal-0.76/postal.cpp" (18 Aug 2014, 4414 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 "postal.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 "smtp.h"
7 #include <unistd.h>
8 #include <sys/wait.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include "postal.h"
12 #include "logit.h"
13 #ifdef USE_GNUTLS
14 #include <errno.h>
15 #if GNUTLS_VERSION_NUMBER <= 0x020b00
16 #include <gcrypt.h>
17 GCRY_THREAD_OPTION_PTHREAD_IMPL;
18 #endif /* GNUTLS_VERSION_NUMBER */
19 #endif
20
21 void usage()
22 {
23 printf("Usage: postal [-m maximum-message-size] [-M minimum-message-size] [-t threads]\n"
24 " [-c messages-per-connection] [-r messages-per-minute] [-a]\n"
25 " [-b [no]netscape] [-p port] [-[z|Z] debug-file]\n"
26 #ifdef USE_SSL
27 " [-s ssl-percentage]\n"
28 #endif
29 " [-L] [-l local-address] [-f sender-file]\n"
30 " smtp-server user-list-filename\n"
31 "\n"
32 "Postal Version: " VER_STR "\n");
33 exit(eParam);
34 }
35
36 int exitCount = 0;
37
38 void endit(int)
39 {
40 exitCount++;
41 if(exitCount > 2)
42 exit(1);
43 }
44
45 int main(int argc, char **argv)
46 {
47 int maxMsgSize = 10;
48 int minMsgSize = 0;
49 int processes = 1;
50 int msgsPerMinute = 0; // 0 for unlimited
51 int msgsPerConnection = 1;
52 const char *ourAddr = NULL;
53 bool allLog = false;
54 TRISTATE netscape = eNONE;
55 PCCHAR debugName = NULL;
56 PCCHAR senderFile = NULL;
57 bool debugMultipleFiles = false;
58 bool useLMTP = false;
59 #ifdef USE_SSL
60 int ssl = 0;
61 #endif
62 #ifdef USE_GNUTLS
63 #if GNUTLS_VERSION_NUMBER <= 0x020b00
64 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
65 #endif
66 gnutls_global_init();
67 if(!gnutls_check_version(GNUTLS_VER))
68 {
69 fprintf(stderr, "Needs version " GNUTLS_VER " of GNUTLS\n");
70 exit(1);
71 }
72 #endif
73 unsigned short port = 25;
74
75 int c;
76 while(-1 != (c = getopt(argc, argv, "ab:f:m:M:p:s:t:c:r:Ll:z:Z:")) )
77 {
78 switch(char(c))
79 {
80 case '?':
81 case ':':
82 usage();
83 break;
84 case 'a':
85 allLog = true;
86 break;
87 case 'b':
88 if(!strcasecmp(optarg, "netscape"))
89 netscape = eWONT;
90 else if(!strcasecmp(optarg, "nonetscape"))
91 netscape = eMUST;
92 break;
93 case 'f':
94 senderFile = optarg;
95 break;
96 case 'L':
97 useLMTP = true;
98 break;
99 case 'l':
100 ourAddr = optarg;
101 break;
102 case 'm':
103 maxMsgSize = atoi(optarg);
104 break;
105 case 'M':
106 minMsgSize = atoi(optarg);
107 break;
108 case 'p':
109 port = 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 't':
119 processes = atoi(optarg);
120 break;
121 case 'c':
122 msgsPerConnection = atoi(optarg);
123 break;
124 case 'r':
125 msgsPerMinute = atoi(optarg);
126 break;
127 case 'Z':
128 debugMultipleFiles = true;
129 case 'z':
130 debugName = optarg;
131 break;
132 }
133 }
134 if(minMsgSize < 0 || maxMsgSize > MAX_MSG_SIZE || maxMsgSize < minMsgSize)
135 usage();
136
137 if(processes < 1 || processes > MAX_PROCESSES)
138 usage();
139 if(msgsPerMinute < 0 || msgsPerConnection < -1 )
140 usage();
141 #ifdef USE_SSL
142 if(ssl < 0 || ssl > 100)
143 usage();
144 #endif
145
146 if(optind + 2 != argc)
147 usage();
148
149 UserList ul(argv[optind + 1], false);
150 UserList *senderList = NULL;
151 if(senderFile)
152 senderList = new UserList(senderFile, false);
153
154 int fd[2];
155 if(pipe(fd))
156 {
157 printf("Can't create pipe.\n");
158 return eSystem;
159 }
160 struct sigaction sa;
161 memset(&sa, 0, sizeof(sa));
162 sa.sa_sigaction = NULL;
163 sa.sa_handler = SIG_IGN;
164 sa.sa_flags = 0;
165 if(sigaction(SIGPIPE, &sa, NULL))
166 {
167 printf("Can't block SIGPIPE.\n");
168 return eSystem;
169 }
170
171 sa.sa_flags = SA_SIGINFO;
172 sa.sa_handler = &endit;
173 if(sigaction(SIGINT, &sa, NULL))
174 {
175 printf("Can't handle SIGINT.\n");
176 return eSystem;
177 }
178
179 printf("time,messages,data(K),errors,connections"
180 #ifdef USE_SSL
181 ",SSL connections"
182 #endif
183 "\n");
184
185 Logit log("postal.log", allLog, false, 0);
186 Logit *debug = NULL;
187
188 if(debugName)
189 debug = new Logit(debugName, false, debugMultipleFiles, 0);
190
191 smtp mailer(&exitCount, argv[optind], ourAddr, ul, senderList, minMsgSize
192 , maxMsgSize, msgsPerConnection, processes, &log, netscape, useLMTP
193 #ifdef USE_SSL
194 , ssl
195 #endif
196 , port, debug);
197
198 int rc = mailer.doAllWork(msgsPerMinute);
199 if(debug)
200 delete debug;
201 return rc;
202 }
203