]> code.citadel.org Git - citadel.git/blob - citadel/aidepost.c
* Added 'author' command-line arg to aidepost. Closes enhancement request
[citadel.git] / citadel / aidepost.c
1 /*
2  * $Id$
3  *
4  * This is just a little hack to copy standard input to a message in Aide>
5  */
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <string.h>
14 #include "citadel.h"
15 #include "config.h"
16
17 void make_message(FILE *fp, char *target_room, char *author)
18 {
19         int a;
20         long bb, cc;
21         time_t now;
22         time(&now);
23         putc(255, fp);
24         putc(MES_NORMAL, fp);
25         putc(1, fp);
26         fprintf(fp, "Proom_aide");
27         putc(0, fp);
28         fprintf(fp, "T%ld", (long)now);
29         putc(0, fp);
30         fprintf(fp, "A%s", author);
31         putc(0, fp);
32         fprintf(fp, "O%s", target_room);
33         putc(0, fp);
34         fprintf(fp, "N%s", NODENAME);
35         putc(0, fp);
36         putc('M', fp);
37         bb = ftell(fp);
38         while (a = getc(stdin), a > 0) {
39                 if (a != 8)
40                         putc(a, fp);
41                 else {
42                         cc = ftell(fp);
43                         if (cc != bb)
44                                 fseek(fp, (-1L), 1);
45                 }
46         }
47         putc(0, fp);
48         putc(0, fp);
49         putc(0, fp);
50 }
51
52 int main(int argc, char **argv)
53 {
54         char tempspool[64];
55         char target_room[ROOMNAMELEN];
56         char author[64];
57         FILE *tempfp, *spoolfp;
58         int ch;
59         int i;
60
61         get_config();
62
63         strcpy(target_room, "Aide");
64         strcpy(author, "Citadel");
65         for (i=1; i<argc; ++i) {
66                 if (!strncasecmp(argv[i], "-r", 2)) {
67                         strncpy(target_room, &argv[i][2], sizeof(target_room));
68                         target_room[sizeof(target_room)-1] = 0;
69                 }
70                 else if (!strncasecmp(argv[i], "-a", 2)) {
71                         strncpy(author, &argv[i][2], sizeof(author));
72                         author[sizeof(author)-1] = 0;
73                 } else {
74                         fprintf(stderr, "%s: usage: %s "
75                                         "[-rTargetRoom] [-aAuthor]\n",
76                                 argv[0], argv[0]);
77                         exit(1);
78                 }
79         }
80
81
82         snprintf(tempspool, sizeof tempspool, "./network/spoolin/ap.%d",
83                 getpid());
84
85         tempfp = tmpfile();
86         if (tempfp == NULL) {
87                 perror("cannot open temp file");
88                 exit(errno);
89         }
90
91         /* Generate a message from stdin */
92         make_message(tempfp, target_room, author);
93
94         /* Copy it to a new temp file in the spool directory */
95         rewind(tempfp);
96
97         spoolfp = fopen(tempspool, "wb");
98         if (spoolfp == NULL) {
99                 perror("cannot open spool file");
100                 exit(errno);
101         }
102         while (ch = getc(tempfp), (ch >= 0))
103                 putc(ch, spoolfp);
104
105         fclose(tempfp);
106         fclose(spoolfp);
107
108         execlp("./netproc", "netproc", "-i", NULL);
109         perror("cannot run netproc");
110         exit(errno);
111 }