fixed time_t handling (have to cast it to long for printf/scanf)
[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 "citadel.h"
14 #include "config.h"
15
16 void make_message(FILE *fp)
17 {
18         int a;
19         long bb, cc;
20         time_t now;
21         time(&now);
22         putc(255, fp);
23         putc(MES_NORMAL, fp);
24         putc(1, fp);
25         fprintf(fp, "Proom_aide");
26         putc(0, fp);
27         fprintf(fp, "T%ld", (long)now);
28         putc(0, fp);
29         fprintf(fp, "ACitadel");
30         putc(0, fp);
31         fprintf(fp, "OAide");
32         putc(0, fp);
33         fprintf(fp, "N%s", NODENAME);
34         putc(0, fp);
35         putc('M', fp);
36         bb = ftell(fp);
37         while (a = getc(stdin), a > 0) {
38                 if (a != 8)
39                         putc(a, fp);
40                 else {
41                         cc = ftell(fp);
42                         if (cc != bb)
43                                 fseek(fp, (-1L), 1);
44                 }
45         }
46         putc(0, fp);
47         putc(0, fp);
48         putc(0, fp);
49 }
50
51 int main(int argc, char **argv)
52 {
53         char tempbase[32];
54         char temptmp[64];
55         char tempspool[64];
56         FILE *tempfp, *spoolfp;
57         int ch;
58
59         get_config();
60         snprintf(tempbase, sizeof tempbase, "ap.%d", getpid());
61         snprintf(temptmp, sizeof temptmp, "/tmp/%s", tempbase);
62         snprintf(tempspool, sizeof tempspool, "./network/spoolin/%s", tempbase);
63
64         tempfp = fopen(temptmp, "wb+");
65         if (tempfp == NULL) {
66                 perror("cannot open temp file");
67                 exit(errno);
68         }
69         /* Unlink the temp file, so it automatically gets deleted by the OS if
70          * this program is interrupted or crashes.
71          */ unlink(temptmp);
72
73         /* Generate a message from stdin */
74         make_message(tempfp);
75
76         /* Copy it to a new temp file in the spool directory */
77         rewind(tempfp);
78
79         spoolfp = fopen(tempspool, "wb");
80         if (spoolfp == NULL) {
81                 perror("cannot open spool file");
82                 exit(errno);
83         }
84         while (ch = getc(tempfp), (ch >= 0))
85                 putc(ch, spoolfp);
86
87         fclose(tempfp);
88         fclose(spoolfp);
89
90         execlp("./netproc", "netproc", "-i", NULL);
91         perror("cannot run netproc");
92         exit(errno);
93 }