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