]> code.citadel.org Git - citadel.git/blobdiff - ctdlsalearn/src/main.c
Change copyright date in banner to 2015
[citadel.git] / ctdlsalearn / src / main.c
index f48300df3ed974fd9055d216a5c8b71713e922ff..a1d8495367c4cd5b50cd14d237319f3cbf1839ce 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * (c) 2009 by Art Cancro and citadel.org
+ * (c) 2009-2012 by Art Cancro and citadel.org
  * This program is released under the terms of the GNU General Public License v3.
  */
 
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
-#include "ctdlsalearn.h"
-
-int discover_ipgm_secret(char *dirname) {
-       int fd;
-       struct partial_config ccc;
-       char configfile[1024];
-
-       sprintf(configfile, "%s/citadel.config", dirname);
-       fd = open(configfile, O_RDONLY);
-       if (fd < 0) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
-       }
-
-       if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
-       }
-       if (close(fd) != 0) {
-               fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
-               return(-1);
-       }
-       return(ccc.c_ipgm_secret);
-}
+#include <getopt.h>
 
+int verbose = 0;
 
 void do_room(int sock, char *roomname, char *salearnargs)
 {
@@ -45,29 +23,36 @@ void do_room(int sock, char *roomname, char *salearnargs)
        FILE *fp;
        int i;
 
-       printf("Trying <%s>\n", roomname);
+       if (verbose) printf("%s: ", roomname);
+       fflush(stdout);
        sock_printf(sock, "GOTO %s\n", roomname);
        sock_getln(sock, buf, sizeof buf);
-       printf("%s\n", buf);
-       if (buf[0] != '2') return;
+       if (buf[0] != '2') {
+               if (verbose) printf("%s\n", &buf[4]);
+               return;
+       }
 
        /* Only fetch enough message pointers to fill our buffer.
         * Since we're going to delete them, if there are more we will get them on the next run.
         */
        sock_printf(sock, "MSGS LAST|%d\n", MAXMSGS);
        sock_getln(sock, buf, sizeof buf);
-       if (buf[0] != '1') return;
+       if (buf[0] != '1') {
+               if (verbose) printf("%s\n", &buf[4]);
+               return;
+       }
        while (sock_getln(sock, buf, sizeof buf), strcmp(buf, "000")) {
                msgs[num_msgs++] = atol(buf);
        }
-       printf("%d messages\n", num_msgs);
+       if (verbose) printf("%d messages\n", num_msgs);
 
        if (num_msgs == 0) return;
        for (i=0; i<num_msgs; ++i) {
                snprintf(buf, sizeof buf, "sa-learn %s", salearnargs);
+               if (!verbose) strcat(buf, " >/dev/null");
                fp = popen(buf, "w");
                if (fp == NULL) return;
-               printf("Submitting message %ld\n", msgs[i]);
+               if (verbose) printf("Submitting message %ld\n", msgs[i]);
                sock_printf(sock, "MSG2 %ld\n", msgs[i]);
                sock_getln(sock, buf, sizeof buf);
                if (buf[0] == '1') {
@@ -78,7 +63,7 @@ void do_room(int sock, char *roomname, char *salearnargs)
                if (pclose(fp) == 0) {
                        sock_printf(sock, "DELE %ld\n", msgs[i]);
                        sock_getln(sock, buf, sizeof buf);
-                       printf("%s\n", buf);
+                       if (verbose) printf("%s\n", &buf[4]);
                }
        }
 }
@@ -89,49 +74,55 @@ int main(int argc, char **argv)
        int server_socket = 0;
        char buf[1024];
        int ipgm_secret = (-1);
-       int c;
-       char ctdldir[256];
-
-       printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
-       printf("(c) 2009 citadel.org GPLv3\n");
+       int a, c, i = 0;
+       char *ctdldir = "/usr/local/citadel" ;
+
+       while ((a = getopt(argc, argv, "vh:")) != EOF) switch(a) {
+               case 'v':
+                       verbose = 1;
+                       break;
+               case 'h':
+                       ctdldir = strdup(optarg);
+                       break;
+               default:
+                       fprintf(stderr, "%s: usage: %s [-v]\n", argv[0], argv[0]);
+                       return(1);
+       }
 
-       strcpy(ctdldir, "/usr/local/citadel");
-       ipgm_secret = discover_ipgm_secret(ctdldir);
-       if (ipgm_secret < 0) {
-               strcpy(ctdldir, "/appl/citadel");
-               ipgm_secret = discover_ipgm_secret(ctdldir);
+       if (verbose) {
+               printf("\nAuto-submit spam and ham to sa-learn for Citadel " PACKAGE_VERSION "\n");
+               printf("(c) 2009-2011 citadel.org GPLv3\n");
        }
-       if (ipgm_secret < 0) {
-               strcpy(ctdldir, "/root/ctdl/trunk/citadel");
-               ipgm_secret = discover_ipgm_secret(ctdldir);
+
+       if (chdir(ctdldir) != 0) {
+               fprintf(stderr, "%s: cannot change directory to %s: %s\n", argv[0], ctdldir, strerror(errno));
+               return(errno);
        }
-       if (ipgm_secret < 0) {
-               exit(1);
+       else if (verbose) {
+               fprintf(stderr, "Changed directory to %s\n", ctdldir);
        }
 
-       printf("Attaching to server...\n");
-       fflush(stdout);
-       sprintf(buf, "%s/citadel.socket", ctdldir);
-       server_socket = uds_connectsock(buf);
+       server_socket = (-1);
+
+       if (verbose) fprintf(stderr, "Connecting to server...\n");
+       server_socket = uds_connectsock("citadel-admin.socket");
+
        if (server_socket < 0) {
+               if (verbose) fprintf(stderr, "Could not connect to Citadel server.\n");
                exit(1);
        }
 
        sock_getln(server_socket, buf, sizeof buf);
-       printf("%s\n", buf);
-
-       sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
-       sock_getln(server_socket, buf, sizeof buf);
-       printf("%s\n", buf);
+       if (verbose) printf("%s\n", &buf[4]);
 
        if (buf[0] == '2') {
-               do_room(server_socket, "0000000001.spam", "--spam");
-               do_room(server_socket, "0000000001.ham", "--ham");
+               do_room(server_socket, "0000000001.spam", "--dbpath /home/spam/.spamassassin --spam");
+               do_room(server_socket, "0000000001.ham", "--dbpath /home/spam/.spamassassin --ham");
        }
 
        sock_puts(server_socket, "QUIT");
        sock_getln(server_socket, buf, sizeof buf);
-       printf("%s\n", buf);
+       if (verbose) printf("%s\n", &buf[4]);
        close(server_socket);
        exit(0);
 }