f6627bc429b26a4b0990fbde0afb929aa947c396
[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
14 int verbose = 0;
15
16 void do_room(int sock, char *roomname, char *salearnargs)
17 {
18 #define MAXMSGS 1000
19         char buf[1024];
20         long msgs[MAXMSGS];
21         int num_msgs = 0;
22         FILE *fp;
23         int i;
24
25         if (verbose) printf("%s: ", roomname);
26         fflush(stdout);
27         sock_printf(sock, "GOTO %s\n", roomname);
28         sock_getln(sock, buf, sizeof buf);
29         if (buf[0] != '2') {
30                 if (verbose) printf("%s\n", &buf[4]);
31                 return;
32         }
33
34         /* Only fetch enough message pointers to fill our buffer.
35          * Since we're going to delete them, if there are more we will get them on the next run.
36          */
37         sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
38         sock_getln(sock, buf, sizeof buf);
39         if (buf[0] != '1') {
40                 if (verbose) printf("%s\n", &buf[4]);
41                 return;
42         }
43         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
44                 msgs[num_msgs++] = atol(buf);
45         }
46         if (verbose) printf("%d messages\n", num_msgs);
47
48         if (num_msgs == 0) return;
49         for (i=0; i<num_msgs; ++i) {
50                 snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
51                 if (!verbose) strcat(buf, " >/dev/null");
52                 fp = popen(buf, "w");
53                 if (fp == NULL) return;
54                 if (verbose) printf("Submitting message %ld\n", msgs[i]);
55                 sock_printf(sock, "MSG2 %ld\n", msgs[i]);
56                 sock_getln(sock, buf, sizeof buf);
57                 if (buf[0] == '1') {
58                         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
59                                 fprintf(fp, "%s\n", buf);
60                         }
61                 }
62                 if (pclose(fp) == 0) {
63                         sock_printf(sock, "DELE %ld\n", msgs[i]);
64                         sock_getln(sock, buf, sizeof buf);
65                         if (verbose) printf("%s\n", &buf[4]);
66                 }
67         }
68 }
69
70
71 int main(int argc, char **argv)
72 {
73         int server_socket = 0;
74         char buf[1024];
75         int ipgm_secret = (-1);
76         int c;
77         int i;
78         char ctdldir[256];
79
80         if ( (argc >= 2) && (!strcmp(argv[1], "-v")) ) {
81                 verbose = 1;
82         }
83
84         if (verbose) {
85                 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
86                 printf("(c) 2009-2011 citadel.org GPLv3\n");
87         }
88
89         char *socket_locations[] = {
90                 "/usr/local/citadel/citadel-admin.socket",
91                 "/appl/citadel/citadel-admin.socket",
92                 "/root/citadel/citadel/citadel-admin.socket"
93         };
94
95         server_socket = (-1);
96
97         for (i=0; i<(sizeof socket_locations / sizeof(char *)); ++i) {
98                 if (server_socket < 0) {
99                         if (verbose) printf("Trying %s...\n", socket_locations[i]);
100                         server_socket = uds_connectsock(socket_locations[i]);
101                 }
102         }
103
104         if (server_socket < 0) {
105                 exit(1);
106         }
107
108         sock_getln(server_socket, buf, sizeof buf);
109         if (verbose) printf("%s\n", &buf[4]);
110
111         if (buf[0] == '2') {
112                 do_room(server_socket, "0000000001.spam", "--spam");
113                 do_room(server_socket, "0000000001.ham", "--ham");
114         }
115
116         sock_puts(server_socket, "QUIT");
117         sock_getln(server_socket, buf, sizeof buf);
118         if (verbose) printf("%s\n", &buf[4]);
119         close(server_socket);
120         exit(0);
121 }