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 817 by douglas, 2006-07-03T21:45:49-07:00 vs.
Revision 827 by douglas, 2006-07-06T01:41:14-07:00

# Line 17 | Line 17
17   #define NEGATIVE "Spam/False Negative"
18   #define POSITIVE "Spam/False Positive"
19  
20 < #define SED "/usr/bin/sed"
21 < #define ASSASSIN "/usr/local/bin/spamassassin"
20 > /*#define SED "/usr/bin/sed"
21 > #define ASSASSIN "/usr/local/bin/spamassassin"*/
22 > #define SPAMC "/usr/local/bin/spamc"
23 > #define SPAMD "/var/run/spamd/spamd.sock"
24  
25 < #define INDENT "s/^/    /"
26 < #define PERIOD "s/[^.]$/&./"
25 > /*#define INDENT "s/^/    /"
26 > #define PERIOD "s/[^.]$/&./"*/
27  
28   #define TEMP "/tmp/SpamUpdate.XXXXXX"
29  
# Line 42 | Line 44 | static int check(int value, jmp_buf envi
44          return value;
45   }
46  
47 < static char *fcheck(char *value, FILE *stream, jmp_buf environment)
47 > /*static char *fcheck(char *value, FILE *stream, jmp_buf environment)
48   {
49          if (!value)
50          {
# Line 60 | Line 62 | static char *fcheck(char *value, FILE *s
62          }
63  
64          return value;
65 < }
65 > }*/
66  
67   static void *mcheck(void *value, jmp_buf environment)
68   {
# Line 72 | Line 74 | static void *mcheck(void *value, jmp_buf
74  
75   static void learn()
76   {
77 <        typedef struct { const char *verb, *args, *mailbox; } Job;
77 >        typedef struct { const char *verb, *learn, *collab, *mailbox; } Job;
78          
79 <        Job jobs[] = { { .verb = "reporting", .args = "-rR", .mailbox = NEGATIVE }, { .verb = "revoking", .args = "-kW", .mailbox = POSITIVE } };
79 >        Job jobs[] = { { .verb = "report", .learn = "spam", .collab = "report", .mailbox = NEGATIVE }, { .verb = "revok", .learn = "ham", .collab = "revoke", .mailbox = POSITIVE } };
80  
81          for (Job *job = jobs; job != jobs + sizeof (jobs) / sizeof (*jobs); ++job)
82          {
83 <                printf("\nLearning and %s from the %s mailbox:\n", job->verb, job->mailbox);
83 >                printf("\nLearning and %sing from the %s mailbox:\n", job->verb, job->mailbox);
84  
85                  SEARCHPGM search = { .undeleted = 1 };
86                  
# Line 86 | Line 88 | static void learn()
88  
89                  const unsigned long *messages_ = messages[job - jobs];
90                  const size_t count = counts[job - jobs];
91 <                char files[count][sizeof (TEMP) / sizeof (char)];
91 >                size_t learned = 0;
92  
93                  for (size_t index = 0; index != count; ++index)
94                  {
95                          unsigned long size;
96                          char *header = mcheck(mail_fetchheader_full(streams[job - jobs], messages_[index], NIL, &size, FT_UID), environment);
97 +                        char file[sizeof (TEMP) / sizeof (char)] = TEMP;
98 +                        int file_ = check(mkstemp(file), environment);
99  
100 <                        strcpy(files[index], TEMP);
97 <
98 <                        int file = check(mkstemp(files[index]), environment);
99 <
100 <                        check(write(file, header, size), environment);
100 >                        check(write(file_, header, size), environment);
101  
102                          char *text = mcheck(mail_fetchtext_full(streams[job - jobs], messages_[index], &size, FT_UID), environment);
103  
104 <                        check(write(file, text, size), environment);
105 <                        check(close(file), environment);
106 <                }
107 <
108 <                int pipe_[2];
104 >                        check(write(file_, text, size), environment);
105 >                        check(lseek(file_, 0, SEEK_SET), environment);
106  
107 <                check(pipe(pipe_), environment);
107 >                        pid_t learn;
108  
109 <                pid_t assassin;
109 >                        if (!(learn = check(fork(), environment)))
110 >                        {
111 >                                check(dup2(file_, 0), environment);
112 >                                check(execl(SPAMC, "spamc", "-L", job->learn, "-U", SPAMD, NULL), environment);
113 >                        }
114  
115 <                if (!(assassin = check(fork(), environment)))
115 <                {
116 <                        check(dup2(pipe_[1], 1), environment);
117 <                        check(close(pipe_[0]), environment);
115 >                        pid_t collab;
116  
117 <                        const char *args[count ? count + 3 : 5];
120 <                        
121 <                        args[0] = "spamassassin";
122 <                        args[1] = job->args;
123 <
124 <                        if (count)
125 <                                for (size_t index = 0; index != count; ++index)
126 <                                        args[index + 2] = files[index];
127 <                        else
117 >                        if (!(collab = check(fork(), environment)))
118                          {
119 <                                args[2] = "--mbox";
120 <                                args[3] = "/dev/null";
119 >                                check(dup2(file_, 0), environment);
120 >                                check(execl(SPAMC, "spamc", "-L", job->collab, "-U", SPAMD, NULL), environment);
121                          }
122  
123 <                        args[count ? count + 2 : 4] = NULL;
123 >                        check(close(file_), environment);
124 >                        check(unlink(file), environment);
125  
126 <                        check(execv(ASSASSIN, (char *const *)args), environment);
136 <                }
126 >                        int status;
127  
128 <                check(close(pipe_[1]), environment);
128 >                        check(waitpid(learn, &status, 0), environment);
129  
130 <                pid_t sed;
130 >                        if (WEXITSTATUS(status) == 5)
131 >                                ++learned;
132  
133 <                if (!(sed = check(fork(), environment)))
143 <                {
144 <                        check(dup2(pipe_[0], 0), environment);
145 <                        check(execl(SED, "sed", "-e", INDENT, "-e", PERIOD, NULL), environment);
133 >                        check(waitpid(collab, &status, 0), environment);
134                  }
135  
136 <                check(close(pipe_[0]), environment);
149 <
150 <                int status;
151 <
152 <                check(waitpid(assassin, &status, 0), environment);
153 <                check(waitpid(sed, &status, 0), environment);
154 <
155 <                for (size_t index = 0; index != count; ++index)
156 <                        check(unlink(files[index]), environment);
136 >                printf("    %u message(s) learned and %u message(s) %sed.\n", learned, count, job->verb);
137          }
138   }
139  
140 < static void deliver()
140 > // XXX: this is going to be moved to a deliver daemon that will run more often
141 > /*static void deliver()
142   {
143          printf("\nDelivering messages from the " POSITIVE " mailbox:\n");
144  
# Line 218 | Line 199 | static void deliver()
199                          {
200                                  if (size_ == 1)
201                                  {
202 +                                        assert(*line == '\n');
203 +
204                                          header_ = false;
205  
206                                          goto crlf;
# Line 253 | Line 236 | crlf:                          assert(message);
236          streams[0] = mcheck(mail_open(streams[0], NEGATIVE, debug ? OP_DEBUG : 0), environment);
237  
238          printf("    %u message(s) delivered.\n", count);
239 < }
239 > }*/
240  
241   void delete()
242   {
# Line 327 | Line 310 | int main(int argc, char *argv[])
310          streams[1] = mcheck(mail_open(NIL, POSITIVE, debug ? OP_DEBUG : 0), environment);
311  
312          learn();
313 <        deliver();
313 >        //deliver();
314          delete();
315          mail_close(streams[0]);
316          mail_close(streams[1]);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines