2 * (c) 2009 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
13 #include "ctdlsalearn.h"
15 int discover_ipgm_secret(char *dirname) {
17 struct partial_config ccc;
18 char configfile[1024];
20 sprintf(configfile, "%s/citadel.config", dirname);
21 fd = open(configfile, O_RDONLY);
23 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
27 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
28 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
32 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
35 return(ccc.c_ipgm_secret);
39 void do_room(int sock, char *roomname, char *salearnargs)
48 printf("Trying <%s>\n", roomname);
49 sock_printf(sock, "GOTO %s\n", roomname);
50 sock_getln(sock, buf, sizeof buf);
52 if (buf[0] != '2') return;
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.
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);
63 printf("%d messages\n", num_msgs);
65 if (num_msgs == 0) return;
66 for (i=0; i<num_msgs; ++i) {
67 snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
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);
74 while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
75 fprintf(fp, "%s\n", buf);
78 if (pclose(fp) == 0) {
79 sock_printf(sock, "DELE %ld\n", msgs[i]);
80 sock_getln(sock, buf, sizeof buf);
87 int main(int argc, char **argv)
89 int server_socket = 0;
91 int ipgm_secret = (-1);
95 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
96 printf("(c) 2009 citadel.org GPLv3\n");
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);
104 if (ipgm_secret < 0) {
105 strcpy(ctdldir, "/root/ctdl/trunk/citadel");
106 ipgm_secret = discover_ipgm_secret(ctdldir);
108 if (ipgm_secret < 0) {
112 printf("Attaching to server...\n");
114 sprintf(buf, "%s/citadel.socket", ctdldir);
115 server_socket = uds_connectsock(buf);
116 if (server_socket < 0) {
120 sock_getln(server_socket, buf, sizeof buf);
123 sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
124 sock_getln(server_socket, buf, sizeof buf);
128 do_room(server_socket, "0000000001.spam", "--spam");
129 do_room(server_socket, "0000000001.ham", "--ham");
132 sock_puts(server_socket, "QUIT");
133 sock_getln(server_socket, buf, sizeof buf);
135 close(server_socket);