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