* Began (but did not finish) applying GPL3+ declarations to each source file. This...
[citadel.git] / citadel / aidepost.c
index cd5599a715ed1373306edf347c66af44f30727dc..7b637f548233be8400ce3e426b7dabbe526d8794 100644 (file)
@@ -1,19 +1,56 @@
-/* aidepost.c
- * This is just a little hack to copy standard input to a message in Aide>
- * v2.0
+/*
  * $Id$
+ *
+ * This is just a little hack to copy standard input to a message in Aide>
+ *
+ * Copyright (c) 1987-2009 by the citadel.org team
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
-#include <time.h>
+
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
+
 #include <limits.h>
 #include <errno.h>
+#include <string.h>
 #include "citadel.h"
+#include "citadel_dirs.h"
 #include "config.h"
 
-void make_message(FILE *fp)
+#ifndef HAVE_SNPRINTF
+#include "snprintf.h"
+#endif
+
+
+/*
+ * Simplified function to generate a message in our format
+ */
+static void ap_make_message(FILE *fp, char *target_room, char *author, char *subject)
 {
        int a;
        long bb, cc;
@@ -26,10 +63,13 @@ void make_message(FILE *fp)
        putc(0, fp);
        fprintf(fp, "T%ld", (long)now);
        putc(0, fp);
-       fprintf(fp, "ACitadel");
+       fprintf(fp, "A%s", author);
        putc(0, fp);
-       fprintf(fp, "OAide");
+       fprintf(fp, "O%s", target_room);
        putc(0, fp);
+       if (strlen(subject) > 0) {
+               fprintf(fp, "U%s%c", subject, 0);
+       }
        fprintf(fp, "N%s", NODENAME);
        putc(0, fp);
        putc('M', fp);
@@ -50,28 +90,64 @@ void make_message(FILE *fp)
 
 int main(int argc, char **argv)
 {
-       char tempbase[32];
-       char temptmp[64];
        char tempspool[64];
+       char target_room[ROOMNAMELEN];
+       char author[64];
+       char subject[256];
        FILE *tempfp, *spoolfp;
        int ch;
+       int i;
+
+       int relh=0;
+       int home=0;
+       char relhome[PATH_MAX]="";
+       char ctdldir[PATH_MAX]=CTDLDIR;
+
+       /* TODO: should we be able to calculate relative dirs? */
+       calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
+
 
        get_config();
-       snprintf(tempbase, sizeof tempbase, "ap.%d", getpid());
-       snprintf(temptmp, sizeof temptmp, "/tmp/%s", tempbase);
-       snprintf(tempspool, sizeof tempspool, "./network/spoolin/%s", tempbase);
 
-       tempfp = fopen(temptmp, "wb+");
+       strcpy(target_room, "Aide");
+       strcpy(author, "Citadel");
+       strcpy(subject, "");
+       for (i=1; i<argc; ++i) {
+               if (!strncasecmp(argv[i], "-r", 2)) {
+                       strncpy(target_room, &argv[i][2], sizeof(target_room));
+                       target_room[sizeof(target_room)-1] = 0;
+               }
+               else if (!strncasecmp(argv[i], "-a", 2)) {
+                       strncpy(author, &argv[i][2], sizeof(author));
+                       author[sizeof(author)-1] = 0;
+               }
+               else if (!strncasecmp(argv[i], "-s", 2)) {
+                       strncpy(subject, &argv[i][2], sizeof(subject));
+                       subject[sizeof(subject)-1] = 0;
+               } else {
+                       fprintf(stderr, "%s: usage: %s "
+                                       "[-rTargetRoom] [-aAuthor] [-sSubject]\n",
+                               argv[0], argv[0]);
+                       exit(1);
+               }
+       }
+
+       snprintf(tempspool, sizeof tempspool,
+                        "%s/ap.%04lx",
+                        ctdl_netin_dir,
+               (long)getpid());
+
+       unlink(tempspool);
+
+       tempfp = fopen(tempspool, "w+b");
+       unlink(tempspool);
        if (tempfp == NULL) {
                perror("cannot open temp file");
                exit(errno);
        }
-       /* Unlink the temp file, so it automatically gets deleted by the OS if
-        * this program is interrupted or crashes.
-        */ unlink(temptmp);
 
        /* Generate a message from stdin */
-       make_message(tempfp);
+       ap_make_message(tempfp, target_room, author, subject);
 
        /* Copy it to a new temp file in the spool directory */
        rewind(tempfp);
@@ -81,13 +157,12 @@ int main(int argc, char **argv)
                perror("cannot open spool file");
                exit(errno);
        }
-       while (ch = getc(tempfp), (ch >= 0))
+       while (ch = getc(tempfp), (ch >= 0)) {
                putc(ch, spoolfp);
+       }
 
        fclose(tempfp);
        fclose(spoolfp);
 
-       execlp("./netproc", "netproc", "-i", NULL);
-       perror("cannot run netproc");
-       exit(errno);
+       exit(0);
 }