* Changed the comments at the beginning of each file to a consistent format
[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)
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 tempspool[64];
55         char target_room[ROOMNAMELEN];
56         FILE *tempfp, *spoolfp;
57         int ch;
58         int i;
59
60         get_config();
61
62         strcpy(target_room, "Aide");
63         for (i=1; i<argc; ++i) {
64                 if (!strncasecmp(argv[i], "-r", 2)) {
65                         strncpy(target_room, &argv[i][2], sizeof(target_room));
66                         target_room[sizeof(target_room)-1] = 0;
67                 } else {
68                         fprintf(stderr, "%s: usage: %s [-rTargetRoom]\n",
69                                 argv[0], argv[0]);
70                         exit(1);
71                 }
72         }
73
74
75         snprintf(tempspool, sizeof tempspool, "./network/spoolin/ap.%d",
76                 getpid());
77
78         tempfp = tmpfile();
79         if (tempfp == NULL) {
80                 perror("cannot open temp file");
81                 exit(errno);
82         }
83
84         /* Generate a message from stdin */
85         make_message(tempfp, target_room);
86
87         /* Copy it to a new temp file in the spool directory */
88         rewind(tempfp);
89
90         spoolfp = fopen(tempspool, "wb");
91         if (spoolfp == NULL) {
92                 perror("cannot open spool file");
93                 exit(errno);
94         }
95         while (ch = getc(tempfp), (ch >= 0))
96                 putc(ch, spoolfp);
97
98         fclose(tempfp);
99         fclose(spoolfp);
100
101         execlp("./netproc", "netproc", "-i", NULL);
102         perror("cannot run netproc");
103         exit(errno);
104 }