Added verbose and non-verbose runtime options to ctdlsalearn
[citadel.git] / ctdlsalearn / src / main.c
1 /*
2  * (c) 2009 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 int discover_ipgm_secret(char *dirname) {
18         int fd;
19         struct partial_config ccc;
20         char configfile[1024];
21
22         sprintf(configfile, "%s/citadel.config", dirname);
23         fd = open(configfile, O_RDONLY);
24         if (fd < 0) {
25                 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
26                 return(-1);
27         }
28
29         if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
30                 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
31                 return(-1);
32         }
33         if (close(fd) != 0) {
34                 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
35                 return(-1);
36         }
37         return(ccc.c_ipgm_secret);
38 }
39
40
41 void do_room(int sock, char *roomname, char *salearnargs)
42 {
43 #define MAXMSGS 1000
44         char buf[1024];
45         long msgs[MAXMSGS];
46         int num_msgs = 0;
47         FILE *fp;
48         int i;
49
50         if (verbose) printf("%s: ", roomname);
51         fflush(stdout);
52         sock_printf(sock, "GOTO %s\n", roomname);
53         sock_getln(sock, buf, sizeof buf);
54         if (buf[0] != '2') {
55                 if (verbose) printf("%s\n", &buf[4]);
56                 return;
57         }
58
59         /* Only fetch enough message pointers to fill our buffer.
60          * Since we're going to delete them, if there are more we will get them on the next run.
61          */
62         sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
63         sock_getln(sock, buf, sizeof buf);
64         if (buf[0] != '1') {
65                 if (verbose) printf("%s\n", &buf[4]);
66                 return;
67         }
68         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
69                 msgs[num_msgs++] = atol(buf);
70         }
71         if (verbose) printf("%d messages\n", num_msgs);
72
73         if (num_msgs == 0) return;
74         for (i=0; i<num_msgs; ++i) {
75                 snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
76                 if (!verbose) strcat(buf, " >/dev/null");
77                 fp = popen(buf, "w");
78                 if (fp == NULL) return;
79                 if (verbose) printf("Submitting message %ld\n", msgs[i]);
80                 sock_printf(sock, "MSG2 %ld\n", msgs[i]);
81                 sock_getln(sock, buf, sizeof buf);
82                 if (buf[0] == '1') {
83                         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
84                                 fprintf(fp, "%s\n", buf);
85                         }
86                 }
87                 if (pclose(fp) == 0) {
88                         sock_printf(sock, "DELE %ld\n", msgs[i]);
89                         sock_getln(sock, buf, sizeof buf);
90                         if (verbose) printf("%s\n", &buf[4]);
91                 }
92         }
93 }
94
95
96 int main(int argc, char **argv)
97 {
98         int server_socket = 0;
99         char buf[1024];
100         int ipgm_secret = (-1);
101         int c;
102         char ctdldir[256];
103
104         if ( (argc >= 2) && (!strcmp(argv[1], "-v")) ) {
105                 verbose = 1;
106         }
107
108         if (verbose) {
109                 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
110                 printf("(c) 2009-2011 citadel.org GPLv3\n");
111         }
112
113         strcpy(ctdldir, "/usr/local/citadel");
114         ipgm_secret = discover_ipgm_secret(ctdldir);
115         if (ipgm_secret < 0) {
116                 strcpy(ctdldir, "/appl/citadel");
117                 ipgm_secret = discover_ipgm_secret(ctdldir);
118         }
119         if (ipgm_secret < 0) {
120                 strcpy(ctdldir, "/root/ctdl/trunk/citadel");
121                 ipgm_secret = discover_ipgm_secret(ctdldir);
122         }
123         if (ipgm_secret < 0) {
124                 exit(1);
125         }
126
127         if (verbose) printf("Connecting to Citadel server...\n");
128         fflush(stdout);
129         sprintf(buf, "%s/citadel.socket", ctdldir);
130         server_socket = uds_connectsock(buf);
131         if (server_socket < 0) {
132                 exit(1);
133         }
134
135         sock_getln(server_socket, buf, sizeof buf);
136         if (verbose) printf("%s\n", &buf[4]);
137
138         sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
139         sock_getln(server_socket, buf, sizeof buf);
140         if (verbose) printf("%s\n", &buf[4]);
141
142         if (buf[0] == '2') {
143                 do_room(server_socket, "0000000001.spam", "--spam");
144                 do_room(server_socket, "0000000001.ham", "--ham");
145         }
146
147         sock_puts(server_socket, "QUIT");
148         sock_getln(server_socket, buf, sizeof buf);
149         if (verbose) printf("%s\n", &buf[4]);
150         close(server_socket);
151         exit(0);
152 }