Change copyright date in banner to 2015
[citadel.git] / ctdlsalearn / src / main.c
1 /*
2  * (c) 2009-2012 by Art Cancro and citadel.org
3  * This program is released under the terms of the GNU General Public License v3.
4  */
5
6 #include <config.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <getopt.h>
14
15 int verbose = 0;
16
17 void do_room(int sock, char *roomname, char *salearnargs)
18 {
19 #define MAXMSGS 1000
20         char buf[1024];
21         long msgs[MAXMSGS];
22         int num_msgs = 0;
23         FILE *fp;
24         int i;
25
26         if (verbose) printf("%s: ", roomname);
27         fflush(stdout);
28         sock_printf(sock, "GOTO %s\n", roomname);
29         sock_getln(sock, buf, sizeof buf);
30         if (buf[0] != '2') {
31                 if (verbose) printf("%s\n", &buf[4]);
32                 return;
33         }
34
35         /* Only fetch enough message pointers to fill our buffer.
36          * Since we're going to delete them, if there are more we will get them on the next run.
37          */
38         sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
39         sock_getln(sock, buf, sizeof buf);
40         if (buf[0] != '1') {
41                 if (verbose) printf("%s\n", &buf[4]);
42                 return;
43         }
44         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
45                 msgs[num_msgs++] = atol(buf);
46         }
47         if (verbose) printf("%d messages\n", num_msgs);
48
49         if (num_msgs == 0) return;
50         for (i=0; i<num_msgs; ++i) {
51                 snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
52                 if (!verbose) strcat(buf, " >/dev/null");
53                 fp = popen(buf, "w");
54                 if (fp == NULL) return;
55                 if (verbose) printf("Submitting message %ld\n", msgs[i]);
56                 sock_printf(sock, "MSG2 %ld\n", msgs[i]);
57                 sock_getln(sock, buf, sizeof buf);
58                 if (buf[0] == '1') {
59                         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
60                                 fprintf(fp, "%s\n", buf);
61                         }
62                 }
63                 if (pclose(fp) == 0) {
64                         sock_printf(sock, "DELE %ld\n", msgs[i]);
65                         sock_getln(sock, buf, sizeof buf);
66                         if (verbose) printf("%s\n", &buf[4]);
67                 }
68         }
69 }
70
71
72 int main(int argc, char **argv)
73 {
74         int server_socket = 0;
75         char buf[1024];
76         int ipgm_secret = (-1);
77         int a, c, i = 0;
78         char *ctdldir = "/usr/local/citadel" ;
79
80         while ((a = getopt(argc, argv, "vh:")) != EOF) switch(a) {
81                 case 'v':
82                         verbose = 1;
83                         break;
84                 case 'h':
85                         ctdldir = strdup(optarg);
86                         break;
87                 default:
88                         fprintf(stderr, "%s: usage: %s [-v]\n", argv[0], argv[0]);
89                         return(1);
90         }
91
92         if (verbose) {
93                 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
94                 printf("(c) 2009-2011 citadel.org GPLv3\n");
95         }
96
97         if (chdir(ctdldir) != 0) {
98                 fprintf(stderr, "%s: cannot change directory to %s: %s\n", argv[0], ctdldir, strerror(errno));
99                 return(errno);
100         }
101         else if (verbose) {
102                 fprintf(stderr, "Changed directory to %s\n", ctdldir);
103         }
104
105         server_socket = (-1);
106
107         if (verbose) fprintf(stderr, "Connecting to server...\n");
108         server_socket = uds_connectsock("citadel-admin.socket");
109
110         if (server_socket < 0) {
111                 if (verbose) fprintf(stderr, "Could not connect to Citadel server.\n");
112                 exit(1);
113         }
114
115         sock_getln(server_socket, buf, sizeof buf);
116         if (verbose) printf("%s\n", &buf[4]);
117
118         if (buf[0] == '2') {
119                 do_room(server_socket, "0000000001.spam", "--dbpath /home/spam/.spamassassin --spam");
120                 do_room(server_socket, "0000000001.ham", "--dbpath /home/spam/.spamassassin --ham");
121         }
122
123         sock_puts(server_socket, "QUIT");
124         sock_getln(server_socket, buf, sizeof buf);
125         if (verbose) printf("%s\n", &buf[4]);
126         close(server_socket);
127         exit(0);
128 }