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"
17 int discover_ipgm_secret(char *dirname) {
19 struct partial_config ccc;
20 char configfile[1024];
22 sprintf(configfile, "%s/citadel.config", dirname);
23 fd = open(configfile, O_RDONLY);
25 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
29 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
30 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
34 if (verbose) fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
37 return(ccc.c_ipgm_secret);
41 void do_room(int sock, char *roomname, char *salearnargs)
50 if (verbose) printf("%s: ", roomname);
52 sock_printf(sock, "GOTO %s\n", roomname);
53 sock_getln(sock, buf, sizeof buf);
55 if (verbose) printf("%s\n", &buf[4]);
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.
62 sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
63 sock_getln(sock, buf, sizeof buf);
65 if (verbose) printf("%s\n", &buf[4]);
68 while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
69 msgs[num_msgs++] = atol(buf);
71 if (verbose) printf("%d messages\n", num_msgs);
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");
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);
83 while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
84 fprintf(fp, "%s\n", buf);
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]);
96 int main(int argc, char **argv)
98 int server_socket = 0;
100 int ipgm_secret = (-1);
104 if ( (argc >= 2) && (!strcmp(argv[1], "-v")) ) {
109 printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
110 printf("(c) 2009-2011 citadel.org GPLv3\n");
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);
119 if (ipgm_secret < 0) {
120 strcpy(ctdldir, "/root/ctdl/trunk/citadel");
121 ipgm_secret = discover_ipgm_secret(ctdldir);
123 if (ipgm_secret < 0) {
127 if (verbose) printf("Connecting to Citadel server...\n");
129 sprintf(buf, "%s/citadel.socket", ctdldir);
130 server_socket = uds_connectsock(buf);
131 if (server_socket < 0) {
135 sock_getln(server_socket, buf, sizeof buf);
136 if (verbose) printf("%s\n", &buf[4]);
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]);
143 do_room(server_socket, "0000000001.spam", "--spam");
144 do_room(server_socket, "0000000001.ham", "--ham");
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);