1bc26dcd4b7790e7ba8280216d75895070043b5c
[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 #ifndef HAVE_SPOOL_DIR
102                          CTDLDIR
103 #else
104                          SPOOL_DIR
105 #endif
106                          "/network/spoolin/ap.%04lx",
107                 (long)getpid());
108
109         unlink(tempspool);
110
111         tempfp = fopen(tempspool, "w+b");
112         unlink(tempspool);
113         if (tempfp == NULL) {
114                 perror("cannot open temp file");
115                 exit(errno);
116         }
117
118         /* Generate a message from stdin */
119         ap_make_message(tempfp, target_room, author);
120
121         /* Copy it to a new temp file in the spool directory */
122         rewind(tempfp);
123
124         spoolfp = fopen(tempspool, "wb");
125         if (spoolfp == NULL) {
126                 perror("cannot open spool file");
127                 exit(errno);
128         }
129         while (ch = getc(tempfp), (ch >= 0)) {
130                 putc(ch, spoolfp);
131         }
132
133         fclose(tempfp);
134         fclose(spoolfp);
135
136         exit(0);
137 }