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