ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/Mail/SpamUpdate.c
(Generate patch)

Comparing FreeBSDAdmin/Mail/SpamUpdate.c (file contents):
Revision 815 by douglas, 2006-07-03T14:03:04-07:00 vs.
Revision 816 by douglas, 2006-07-03T19:12:17-07:00

# Line 32 | Line 32 | static MAILSTREAM *streams[2];
32   static unsigned long *messages[] = { NULL, NULL };
33   static size_t counts[] = { 0, 0 };
34  
35 < /*static*/ int check(int value, jmp_buf environment)
35 > static int check(int value, jmp_buf environment)
36   {
37          if (value == -1)
38                  longjmp(environment, 1);
# Line 40 | Line 40 | static size_t counts[] = { 0, 0 };
40          return value;
41   }
42  
43 < /*static*/ char *fcheck(char *value, FILE *stream, jmp_buf environment)
43 > static char *fcheck(char *value, FILE *stream, jmp_buf environment)
44   {
45          if (!value)
46          {
# Line 81 | Line 81 | static void *mcheck(void *value, jmp_buf
81          return value;
82   }
83  
84 < static void learn(jmp_buf environment)
84 > static void learn()
85   {
86          typedef struct { const char *verb, *args, *mailbox; } Job;
87          
# Line 95 | Line 95 | static void learn(jmp_buf environment)
95                  
96                  mail_search_full(streams[job - jobs], NIL, &search, SE_UID);
97  
98 <                unsigned long *messages_ = messages[job - jobs];
98 >                /*unsigned long *messages_ = messages[job - jobs];
99                  size_t count = counts[job - jobs];
100                  char files[count][sizeof (TEMP) / sizeof (char)];
101  
# Line 164 | Line 164 | static void learn(jmp_buf environment)
164                  check(waitpid(sed, &status, 0), environment);
165  
166                  for (size_t index = 0; index != count; ++index)
167 <                        check(unlink(files[index]), environment);
167 >                        check(unlink(files[index]), environment);*/
168          }
169   }
170  
171 + static void deliver()
172 + {
173 +        printf("\nDelivering messages from the " POSITIVE " mailbox:\n");
174 +
175 +        unsigned long *messages_ = messages[1];
176 +        size_t count = counts[1];
177 +        streams[0] = mcheck(mail_open(streams[0], "INBOX", (debug ? OP_DEBUG : 0) | OP_SHORTCACHE), environment);
178 +
179 +        for (int index = 0; index != count; ++index)
180 +        {
181 +                int out[2], in[2];
182 +
183 +                check(pipe(out), environment);
184 +                check(pipe(in), environment);
185 +
186 +                pid_t assassin;
187 +
188 +                if (!(assassin = check(fork(), environment)))
189 +                {
190 +                        check(dup2(out[0], 0), environment);
191 +                        check(close(out[1]), environment);
192 +                        check(dup2(in[1], 1), environment);
193 +                        check(close(in[0]), environment);
194 +                        check(execl(ASSASSIN, "spamassassin", "-d", NULL), environment);
195 +                }
196 +
197 +                check(close(out[0]), environment);
198 +                check(close(in[1]), environment);
199 +
200 +                unsigned long size;
201 +                char *header = mcheck(mail_fetchheader_full(streams[1], messages_[index], NIL, &size, FT_UID), environment);
202 +
203 +                check(write(out[1], header, size), environment);
204 +
205 +                char *text = mcheck(mail_fetchtext_full(streams[1], messages_[index], &size, FT_UID), environment);
206 +
207 +                check(write(out[1], text, size), environment);
208 +                check(close(out[1]), environment);
209 +
210 +                FILE *assassin_ = fdopen(in[0], "r");
211 +                size_t size_;
212 +                bool header_ = true;
213 +                char *message = NULL;
214 +
215 +                size = 0;
216 +
217 +                do
218 +                {
219 +                        char *line = fcheck(fgetln(assassin_, &size_), assassin_, environment);
220 +
221 +                        message = message ? realloc(message, (size + size_) * sizeof (char)) : malloc((size + size_) * sizeof (char));
222 +
223 +                        memcpy(message + size, line, size_);
224 +
225 +                        size += size_;
226 +
227 +                        if (header_)
228 +                        {
229 +                                if (size_ == 1)
230 +                                {
231 +                                        header_ = false;
232 +
233 +                                        goto crlf;
234 +                                }
235 +                                else if (size_ == 2 && !memcmp(line, "\r\n", 2))
236 +                                        header_ = false;
237 +                                else if (!(size_ > 2 && !memcmp(line + size_ - 2, "\r\n", 2)))
238 +                                {
239 + crlf:                           assert(message);
240 +
241 +                                        message = realloc(message, ++size * sizeof (char));
242 +
243 +                                        memcpy(message + size - 2, "\r\n", 2);
244 +                                }
245 +                        }
246 +                }
247 +                while (size_ != 0);
248 +
249 +                if (fclose(assassin_))
250 +                        longjmp(environment, 1);
251 +
252 +                int status;
253 +
254 +                check(waitpid(assassin, &status, 0), environment);
255 +
256 +                STRING message_;
257 +
258 +                INIT(&message_, mail_string, message, size);
259 +                mcheck((void *)mail_append_full(streams[0], "INBOX", "\\Seen", NIL, &message_), environment);
260 +                free(message);
261 +        }
262 +
263 +        streams[0] = mcheck(mail_open(streams[0], NEGATIVE, debug ? OP_DEBUG : 0), environment);
264 +
265 +        printf("    %u message(s) delivered.\n", count);
266 + }
267 +
268   int main(int argc, char *argv[])
269   {
270          int exception;
# Line 204 | Line 301 | int main(int argc, char *argv[])
301          streams[0] = mcheck(mail_open(NIL, NEGATIVE, debug ? OP_DEBUG : 0), environment);
302          streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment);
303  
304 <        learn(environment);
304 >        learn();
305 >        deliver();
306          mail_close(streams[0]);
307          mail_close(streams[1]);
308 +        free(messages[0]);
309 +        free(messages[1]);
310  
311          return 0;
312   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines