]> code.citadel.org Git - citadel.git/blobdiff - citadel/aidepost.c
fix all the <time.h> vs. <sys/time.h> issues, hopefully
[citadel.git] / citadel / aidepost.c
index a54bc1e18d3751795ec1dbc7c7799e12c71d0422..0ed777b66f457ef428de76c2a86f84248f69eca7 100644 (file)
-/* aidepost.c
- * This is just a little hack to copy standard input to a message in Aide>
- * v1.6
+/*
  * $Id$
+ *
+ * This is just a little hack to copy standard input to a message in Aide>
  */
 
 #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 "config.h"
 
-void make_message(char *filename)
+void make_message(FILE *fp, char *target_room, char *author)
 {
-       FILE *fp;
        int a;
-       long bb,cc;
+       long bb, cc;
        time_t now;
        time(&now);
-       fp=fopen(filename,"wb"); if (fp==NULL) exit(22);
-       putc(255,fp);
-       putc(MES_NORMAL,fp);
-       putc(1,fp);
-       fprintf(fp,"Proom_aide"); putc(0,fp);
-       fprintf(fp,"T%ld",now); putc(0,fp);
-       fprintf(fp,"ACitadel"); putc(0,fp);
-       fprintf(fp,"OAide"); putc(0,fp);
-       fprintf(fp,"N%s",NODENAME); putc(0,fp);
-       putc('M',fp);
-       bb=ftell(fp);
-       while (a=getc(stdin), a>0) {
-               if (a!=8) putc(a,fp);
+       putc(255, fp);
+       putc(MES_NORMAL, fp);
+       putc(1, fp);
+       fprintf(fp, "Proom_aide");
+       putc(0, fp);
+       fprintf(fp, "T%ld", (long)now);
+       putc(0, fp);
+       fprintf(fp, "A%s", author);
+       putc(0, fp);
+       fprintf(fp, "O%s", target_room);
+       putc(0, fp);
+       fprintf(fp, "N%s", NODENAME);
+       putc(0, fp);
+       putc('M', fp);
+       bb = ftell(fp);
+       while (a = getc(stdin), a > 0) {
+               if (a != 8)
+                       putc(a, fp);
                else {
-                       cc=ftell(fp);
-                       if (cc!=bb) fseek(fp,(-1L),1);
-                       }
+                       cc = ftell(fp);
+                       if (cc != bb)
+                               fseek(fp, (-1L), 1);
                }
-       putc(0,fp);
-       putc(0,fp);
-       putc(0,fp);
-       fclose(fp);
        }
+       putc(0, fp);
+       putc(0, fp);
+       putc(0, fp);
+}
 
 int main(int argc, char **argv)
 {
-       char tempbase[32];
-       char temptmp[64];
        char tempspool[64];
-       char movecmd[256];
-       
+       char target_room[ROOMNAMELEN];
+       char author[64];
+       FILE *tempfp, *spoolfp;
+       int ch;
+       int i;
+
        get_config();
-       snprintf(tempbase,sizeof tempbase,"ap.%d",getpid());
-       snprintf(temptmp,sizeof temptmp,"/tmp/%s", tempbase);
-       snprintf(tempspool,sizeof tempspool,"./network/spoolin/%s", tempbase);
-       make_message(temptmp);
 
-       snprintf(movecmd, sizeof movecmd, "/bin/mv %s %s", temptmp, tempspool);
-       system(movecmd);
+       strcpy(target_room, "Aide");
+       strcpy(author, "Citadel");
+       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 {
+                       fprintf(stderr, "%s: usage: %s "
+                                       "[-rTargetRoom] [-aAuthor]\n",
+                               argv[0], argv[0]);
+                       exit(1);
+               }
+       }
+
+
+       snprintf(tempspool, sizeof tempspool, "./network/spoolin/ap.%d",
+               getpid());
+
+       tempfp = tmpfile();
+       if (tempfp == NULL) {
+               perror("cannot open temp file");
+               exit(errno);
+       }
+
+       /* Generate a message from stdin */
+       make_message(tempfp, target_room, author);
+
+       /* Copy it to a new temp file in the spool directory */
+       rewind(tempfp);
 
-       execlp("./netproc","netproc",NULL);
-       fprintf(stderr,"aidepost: could not run netproc\n");
-       exit(1);
+       spoolfp = fopen(tempspool, "wb");
+       if (spoolfp == NULL) {
+               perror("cannot open spool file");
+               exit(errno);
        }
+       while (ch = getc(tempfp), (ch >= 0))
+               putc(ch, spoolfp);
+
+       fclose(tempfp);
+       fclose(spoolfp);
+
+       execlp("./netproc", "netproc", "-i", NULL);
+       perror("cannot run netproc");
+       exit(errno);
+}