]> code.citadel.org Git - citadel.git/blob - citadel/aidepost.c
aidepost.c: add -r flag to allow posting to rooms other than Aide>
[citadel.git] / citadel / aidepost.c
1 /* aidepost.c
2  * This is just a little hack to copy standard input to a message in Aide>
3  * v2.0
4  * $Id$
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)
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, "ACitadel");
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 tempbase[32];
55         char temptmp[64];
56         char tempspool[64];
57         char target_room[ROOMNAMELEN];
58         FILE *tempfp, *spoolfp;
59         int ch;
60         int i;
61
62         get_config();
63
64         strcpy(target_room, "Aide");
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                 } else {
70                         fprintf(stderr, "%s: usage: %s [-rTargetRoom]\n",
71                                 argv[0], argv[0]);
72                         exit(1);
73                 }
74         }
75
76
77         snprintf(tempbase, sizeof tempbase, "ap.%d", getpid());
78         snprintf(temptmp, sizeof temptmp, "/tmp/%s", tempbase);
79         snprintf(tempspool, sizeof tempspool, "./network/spoolin/%s", tempbase);
80
81         tempfp = fopen(temptmp, "wb+");
82         if (tempfp == NULL) {
83                 perror("cannot open temp file");
84                 exit(errno);
85         }
86         /* Unlink the temp file, so it automatically gets deleted by the OS if
87          * this program is interrupted or crashes.
88          */ unlink(temptmp);
89
90         /* Generate a message from stdin */
91         make_message(tempfp, target_room);
92
93         /* Copy it to a new temp file in the spool directory */
94         rewind(tempfp);
95
96         spoolfp = fopen(tempspool, "wb");
97         if (spoolfp == NULL) {
98                 perror("cannot open spool file");
99                 exit(errno);
100         }
101         while (ch = getc(tempfp), (ch >= 0))
102                 putc(ch, spoolfp);
103
104         fclose(tempfp);
105         fclose(spoolfp);
106
107         execlp("./netproc", "netproc", "-i", NULL);
108         perror("cannot run netproc");
109         exit(errno);
110 }