* ctdlsalearn: experimental program to submit contents of spam and ham rooms into...
[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("Trying <%s>\n", roomname);
49         sock_printf(sock, "GOTO %s\n", roomname);
50         sock_getln(sock, buf, sizeof buf);
51         printf("%s\n", buf);
52         if (buf[0] != '2') return;
53
54         /* Only fetch enough message pointers to fill our buffer.
55          * Since we're going to delete them, if there are more we will get them on the next run.
56          */
57         sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
58         sock_getln(sock, buf, sizeof buf);
59         if (buf[0] != '1') return;
60         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
61                 msgs[num_msgs++] = atol(buf);
62         }
63         printf("%d messages\n", num_msgs);
64
65         if (num_msgs == 0) return;
66         for (i=0; i<num_msgs; ++i) {
67                 snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
68                 fp = popen(buf, "w");
69                 if (fp == NULL) return;
70                 printf("Submitting message %ld\n", msgs[i]);
71                 sock_printf(sock, "MSG2 %ld\n", msgs[i]);
72                 sock_getln(sock, buf, sizeof buf);
73                 if (buf[0] == '1') {
74                         while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
75                                 fprintf(fp, "%s\n", buf);
76                         }
77                 }
78                 if (pclose(fp) == 0) {
79                         sock_printf(sock, "DELE %ld\n", msgs[i]);
80                         sock_getln(sock, buf, sizeof buf);
81                         printf("%s\n", buf);
82                 }
83         }
84 }
85
86
87 int main(int argc, char **argv)
88 {
89         int server_socket = 0;
90         char buf[1024];
91         int ipgm_secret = (-1);
92         int c;
93         char ctdldir[256];
94
95         printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
96         printf("(c) 2009 citadel.org GPLv3\n");
97
98         strcpy(ctdldir, "/usr/local/citadel");
99         ipgm_secret = discover_ipgm_secret(ctdldir);
100         if (ipgm_secret < 0) {
101                 strcpy(ctdldir, "/appl/citadel");
102                 ipgm_secret = discover_ipgm_secret(ctdldir);
103         }
104         if (ipgm_secret < 0) {
105                 strcpy(ctdldir, "/root/ctdl/trunk/citadel");
106                 ipgm_secret = discover_ipgm_secret(ctdldir);
107         }
108         if (ipgm_secret < 0) {
109                 exit(1);
110         }
111
112         printf("Attaching to server...\n");
113         fflush(stdout);
114         sprintf(buf, "%s/citadel.socket", ctdldir);
115         server_socket = uds_connectsock(buf);
116         if (server_socket < 0) {
117                 exit(1);
118         }
119
120         sock_getln(server_socket, buf, sizeof buf);
121         printf("%s\n", buf);
122
123         sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
124         sock_getln(server_socket, buf, sizeof buf);
125         printf("%s\n", buf);
126
127         if (buf[0] == '2') {
128                 do_room(server_socket, "0000000001.spam", "--spam");
129                 do_room(server_socket, "0000000001.ham", "--ham");
130         }
131
132         sock_puts(server_socket, "QUIT");
133         sock_getln(server_socket, buf, sizeof buf);
134         printf("%s\n", buf);
135         close(server_socket);
136         exit(0);
137 }