calculate the directories in a central manner.
[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 #ifndef HAVE_SNPRINTF
29 #include "snprintf.h"
30 #endif
31
32
33 /*
34  * Simplified function to generate a message in our format
35  */
36 static void ap_make_message(FILE *fp, char *target_room, char *author)
37 {
38         int a;
39         long bb, cc;
40         time_t now;
41         time(&now);
42         putc(255, fp);
43         putc(MES_NORMAL, fp);
44         putc(1, fp);
45         fprintf(fp, "Proom_aide");
46         putc(0, fp);
47         fprintf(fp, "T%ld", (long)now);
48         putc(0, fp);
49         fprintf(fp, "A%s", author);
50         putc(0, fp);
51         fprintf(fp, "O%s", target_room);
52         putc(0, fp);
53         fprintf(fp, "N%s", NODENAME);
54         putc(0, fp);
55         putc('M', fp);
56         bb = ftell(fp);
57         while (a = getc(stdin), a > 0) {
58                 if (a != 8)
59                         putc(a, fp);
60                 else {
61                         cc = ftell(fp);
62                         if (cc != bb)
63                                 fseek(fp, (-1L), 1);
64                 }
65         }
66         putc(0, fp);
67         putc(0, fp);
68         putc(0, fp);
69 }
70
71 int main(int argc, char **argv)
72 {
73         char tempspool[64];
74         char target_room[ROOMNAMELEN];
75         char author[64];
76         FILE *tempfp, *spoolfp;
77         int ch;
78         int i;
79
80         get_config();
81
82         strcpy(target_room, "Aide");
83         strcpy(author, "Citadel");
84         for (i=1; i<argc; ++i) {
85                 if (!strncasecmp(argv[i], "-r", 2)) {
86                         strncpy(target_room, &argv[i][2], sizeof(target_room));
87                         target_room[sizeof(target_room)-1] = 0;
88                 }
89                 else if (!strncasecmp(argv[i], "-a", 2)) {
90                         strncpy(author, &argv[i][2], sizeof(author));
91                         author[sizeof(author)-1] = 0;
92                 } else {
93                         fprintf(stderr, "%s: usage: %s "
94                                         "[-rTargetRoom] [-aAuthor]\n",
95                                 argv[0], argv[0]);
96                         exit(1);
97                 }
98         }
99
100         snprintf(tempspool, sizeof tempspool,
101                          "%s/ap.%04lx",
102                          ctdl_netin_dir,
103                 (long)getpid());
104
105         unlink(tempspool);
106
107         tempfp = fopen(tempspool, "w+b");
108         unlink(tempspool);
109         if (tempfp == NULL) {
110                 perror("cannot open temp file");
111                 exit(errno);
112         }
113
114         /* Generate a message from stdin */
115         ap_make_message(tempfp, target_room, author);
116
117         /* Copy it to a new temp file in the spool directory */
118         rewind(tempfp);
119
120         spoolfp = fopen(tempspool, "wb");
121         if (spoolfp == NULL) {
122                 perror("cannot open spool file");
123                 exit(errno);
124         }
125         while (ch = getc(tempfp), (ch >= 0)) {
126                 putc(ch, spoolfp);
127         }
128
129         fclose(tempfp);
130         fclose(spoolfp);
131
132         exit(0);
133 }