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

Comparing FreeBSDAdmin/Mail/SpamUpdate.c (file contents):
Revision 833 by douglas, 2006-07-07T07:27:48-07:00 vs.
Revision 912 by douglas, 2007-05-03T15:46:15-07:00

# Line 14 | Line 14
14  
15   #include <c-client/c-client.h>
16  
17 < #define NEGATIVE "Spam/False Negative"
18 < #define POSITIVE "Spam/False Positive"
17 > #define NEGATIVE "Spam.False Negative"
18 > #define POSITIVE "Spam.False Positive"
19 >
20 > #define SERVER "{mail.douglasthrift.net/tls/novalidate-cert}"
21  
22   #define SPAMC "/usr/local/bin/spamc"
23   #define SPAMD "/var/run/spamd/spamd.sock"
# Line 30 | Line 32 | static MAILSTREAM *streams[2];
32   static uint8_t critical = 0;
33   static unsigned long *messages[] = { NULL, NULL };
34   static size_t counts[] = { 0, 0 };
35 + static char *pass;
36  
37   static int check(int value, jmp_buf environment)
38   {
# Line 47 | Line 50 | static void *mcheck(void *value, jmp_buf
50          return value;
51   }
52  
53 + static char *fcheck(char *value, FILE *stream, jmp_buf environment)
54 + {
55 +        if (!value)
56 +        {
57 +                if (ferror(stream))
58 +                        longjmp(environment, 1);
59 +                else
60 +                {
61 +                        clearerr(stream);
62 +
63 +                        if (stream == stdin)
64 +                                printf("\n");
65 +
66 +                        return "";
67 +                }
68 +        }
69 +
70 +        return value;
71 + }
72 +
73   static void learn()
74   {
75          typedef struct { const char *verb, *learn, *collab, *mailbox; } Job;
76 <        
76 >
77          Job jobs[] = { { .verb = "report", .learn = "spam", .collab = "report", .mailbox = NEGATIVE }, { .verb = "revok", .learn = "ham", .collab = "revoke", .mailbox = POSITIVE } };
78  
79          for (Job *job = jobs; job != jobs + sizeof (jobs) / sizeof (*jobs); ++job)
# Line 58 | Line 81 | static void learn()
81                  printf("\nLearning and %sing from the %s mailbox:\n", job->verb, job->mailbox);
82  
83                  SEARCHPGM search = { .undeleted = 1 };
84 <                
84 >
85                  mail_search_full(streams[job - jobs], NIL, &search, SE_UID);
86  
87                  const unsigned long *messages_ = messages[job - jobs];
88                  const size_t count = counts[job - jobs];
89 <                size_t learned = 0;
89 >                size_t learned = 0, collabed = 0;
90  
91                  for (size_t index = 0; index != count; ++index)
92                  {
# Line 76 | Line 99 | static void learn()
99                          check(lseek(file_, 0, SEEK_SET), environment);
100  
101                          pid_t learn;
102 +                        int pipe_[2][2];
103 +
104 +                        check(pipe(pipe_[0]), environment);
105  
106                          if (!(learn = check(fork(), environment)))
107                          {
108                                  check(dup2(file_, 0), environment);
109 +                                check(dup2(pipe_[0][1], 1), environment);
110 +                                check(close(pipe_[0][0]), environment);
111                                  check(execl(SPAMC, "spamc", "-L", job->learn, "-U", SPAMD, NULL), environment);
112                          }
113  
114 +                        check(close(pipe_[0][1]), environment);
115 +
116                          pid_t collab;
117  
118 +                        check(pipe(pipe_[1]), environment);
119 +
120                          if (!(collab = check(fork(), environment)))
121                          {
122                                  check(dup2(file_, 0), environment);
123 +                                check(dup2(pipe_[1][1], 1), environment);
124 +                                check(close(pipe_[1][0]), environment);
125                                  check(execl(SPAMC, "spamc", "-L", job->collab, "-U", SPAMD, NULL), environment);
126                          }
127  
128                          check(close(file_), environment);
129                          check(unlink(file), environment);
130 +                        check(close(pipe_[1][1]), environment);
131  
132 <                        int status;
133 <
134 <                        check(waitpid(learn, &status, 0), environment);
132 >                        FILE *learn_ = fdopen(pipe_[0][0], "r");
133 >                        size_t size_;
134 >                        char *line = fcheck(fgetln(learn_, &size_), learn_, environment);
135  
136 <                        if (WEXITSTATUS(status) == 5)
136 >                        if (strncmp(line, "Message successfully un/learned\n", size_) == 0)
137                                  ++learned;
138  
139 +                        fclose(learn_);
140 +
141 +                        FILE *collab_ = fdopen(pipe_[1][0], "r");
142 +
143 +                        line = fcheck(fgetln(collab_, &size_), collab_, environment);
144 +
145 +                        if (strncmp(line, "Message successfully reported/revoked\n", size_) == 0)
146 +                                ++collabed;
147 +
148 +                        fclose(collab_);
149 +
150 +                        int status;
151 +
152 +                        check(waitpid(learn, &status, 0), environment);
153                          check(waitpid(collab, &status, 0), environment);
154                  }
155  
156 <                printf("    %u message(s) learned and %u message(s) %sed.\n", learned, count, job->verb);
156 >                printf("    %u message(s) learned and %u message(s) %sed.\n", learned, collabed, job->verb);
157          }
158   }
159  
# Line 118 | Line 167 | void delete()
167  
168                  const unsigned long *messages_ = messages[mailbox - mailboxes];
169                  const size_t count = counts[mailbox - mailboxes];
121                char *sequence = calloc(1, sizeof (char));
122                size_t size = 1;
170  
171 <                for (size_t index = 0; index != count; ++index)
171 >                if (count)
172                  {
173 <                        char *message;
173 >                        char *sequence = calloc(1, sizeof (char));
174 >                        size_t size = 1;
175 >
176 >                        for (size_t index = 0; index != count; ++index)
177 >                        {
178 >                                char *message;
179 >
180 >                                asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : "");
181  
182 <                        asprintf(&message, "%lu%s", messages_[index], index + 1 != count ? "," : "");
182 >                                sequence = realloc(sequence, (size += strlen(message)) * sizeof (char));
183  
184 <                        sequence = realloc(sequence, (size += strlen(message)) * sizeof (char));
184 >                                strlcat(sequence, message, size);
185 >                                free(message);
186 >                        }
187  
188 <                        strlcat(sequence, message, size);
189 <                        free(message);
188 >                        mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT);
189 >                        free(sequence);
190                  }
191  
136                mail_setflag_full(streams[mailbox - mailboxes], sequence, "\\Deleted", ST_UID | ST_SILENT);
137                free(sequence);
192                  printf("    %u message(s) marked deleted.\n", count);
193          }
194   }
# Line 168 | Line 222 | int main(int argc, char *argv[])
222                  else
223                          return printf("Usage: %s [-debug]\n", argv[0]), 1;
224  
225 <        printf("Information:\n    If you receive spam in your Inbox mailbox, copy it to the " NEGATIVE " mailbox.\n\n    If you receive mail that is not spam in your Spam/Assassin mailbox, copy it to the " POSITIVE " mailbox.\n");
225 >        printf("Information:\n    If you receive spam in your Inbox mailbox, copy it to the " NEGATIVE " mailbox.\n\n    If you receive mail that is not spam in your Spam mailbox, copy it to the " POSITIVE " mailbox.\n");
226  
227 +        check(asprintf(&pass, "%s/.CreditCardReminder.pass", getenv("HOME")), environment);
228          check(sigemptyset(&set), environment);
229          check(sigaddset(&set, SIGHUP), environment);
230          check(sigaddset(&set, SIGINT), environment);
231  
232   #       include <c-client/linkage.c>
233  
234 <        streams[0] = mcheck(mail_open(NIL, NEGATIVE, debug ? OP_DEBUG : 0), environment);
235 <        streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment);
234 >        streams[0] = mcheck(mail_open(NIL, SERVER NEGATIVE, debug ? OP_DEBUG : 0), environment);
235 >        streams[1] = mcheck(mail_open(NIL, SERVER POSITIVE, debug ? OP_DEBUG : 0), environment);
236  
237          learn();
238          delete();
# Line 263 | Line 318 | void mm_dlog(char *string)
318          fprintf(stderr, "%s\n", string);
319   }
320  
321 < void mm_login(NETMBX *mb, char *user, char *pwd, long trial) {}
321 > void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
322 > {
323 >        strcpy(user, "douglas");
324 >
325 >        FILE *file = fopen(pass, "r");
326 >
327 >        if (!file)
328 >                longjmp(environment, 1);
329 >
330 >        size_t size;
331 >        char *line = fcheck(fgetln(file, &size), file, environment);
332 >
333 >        strlcpy(pwd, line, size);
334 > }
335  
336   void mm_critical(MAILSTREAM *stream)
337   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines