Updated ctdlsalearn to work with citadel-admin.socket
[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 "ctdlsalearn.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 c;
78         int i;
79         char ctdldir[256];
80
81         if ( (argc >= 2) && (!strcmp(argv[1], "-v")) ) {
82                 verbose = 1;
83         }
84
85         if (verbose) {
86                 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
87                 printf("(c) 2009-2011 citadel.org GPLv3\n");
88         }
89
90         char *socket_locations[] = {
91                 "/usr/local/citadel/citadel-admin.socket",
92                 "/appl/citadel/citadel-admin.socket",
93                 "/root/citadel/citadel/citadel-admin.socket"
94         };
95
96         server_socket = (-1);
97
98         for (i=0; i<(sizeof socket_locations / sizeof(char *)); ++i) {
99                 if (server_socket < 0) {
100                         if (verbose) printf("Trying %s...\n", socket_locations[i]);
101                         server_socket = uds_connectsock(socket_locations[i]);
102                 }
103         }
104
105         if (server_socket < 0) {
106                 exit(1);
107         }
108
109         sock_getln(server_socket, buf, sizeof buf);
110         if (verbose) printf("%s\n", &buf[4]);
111
112         if (buf[0] == '2') {
113                 do_room(server_socket, "0000000001.spam", "--spam");
114                 do_room(server_socket, "0000000001.ham", "--ham");
115         }
116
117         sock_puts(server_socket, "QUIT");
118         sock_getln(server_socket, buf, sizeof buf);
119         if (verbose) printf("%s\n", &buf[4]);
120         close(server_socket);
121         exit(0);
122 }