4634f809500487ad74b498c6cc09f011c3abd039
[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, char *subject)
38 {
39         int a;
40         long bb, cc;
41         time_t now;
42         time(&now);
43         putc(255, fp);
44         putc(MES_NORMAL, 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         if (strlen(subject) > 0) {
55                 fprintf(fp, "U%s%c", subject, 0);
56         }
57         fprintf(fp, "N%s", NODENAME);
58         putc(0, fp);
59         putc('M', fp);
60         bb = ftell(fp);
61         while (a = getc(stdin), a > 0) {
62                 if (a != 8)
63                         putc(a, fp);
64                 else {
65                         cc = ftell(fp);
66                         if (cc != bb)
67                                 fseek(fp, (-1L), 1);
68                 }
69         }
70         putc(0, fp);
71         putc(0, fp);
72         putc(0, fp);
73 }
74
75 int main(int argc, char **argv)
76 {
77         char tempspool[64];
78         char target_room[ROOMNAMELEN];
79         char author[64];
80         char subject[256];
81         FILE *tempfp, *spoolfp;
82         int ch;
83         int i;
84
85         int relh=0;
86         int home=0;
87         char relhome[PATH_MAX]="";
88         char ctdldir[PATH_MAX]=CTDLDIR;
89
90         /* TODO: should we be able to calculate relative dirs? */
91         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
92
93
94         get_config();
95
96         strcpy(target_room, "Aide");
97         strcpy(author, "Citadel");
98         strcpy(subject, "");
99         for (i=1; i<argc; ++i) {
100                 if (!strncasecmp(argv[i], "-r", 2)) {
101                         strncpy(target_room, &argv[i][2], sizeof(target_room));
102                         target_room[sizeof(target_room)-1] = 0;
103                 }
104                 else if (!strncasecmp(argv[i], "-a", 2)) {
105                         strncpy(author, &argv[i][2], sizeof(author));
106                         author[sizeof(author)-1] = 0;
107                 }
108                 else if (!strncasecmp(argv[i], "-s", 2)) {
109                         strncpy(subject, &argv[i][2], sizeof(subject));
110                         subject[sizeof(subject)-1] = 0;
111                 } else {
112                         fprintf(stderr, "%s: usage: %s "
113                                         "[-rTargetRoom] [-aAuthor] [-sSubject]\n",
114                                 argv[0], argv[0]);
115                         exit(1);
116                 }
117         }
118
119         snprintf(tempspool, sizeof tempspool,
120                          "%s/ap.%04lx",
121                          ctdl_netin_dir,
122                 (long)getpid());
123
124         unlink(tempspool);
125
126         tempfp = fopen(tempspool, "w+b");
127         unlink(tempspool);
128         if (tempfp == NULL) {
129                 perror("cannot open temp file");
130                 exit(errno);
131         }
132
133         /* Generate a message from stdin */
134         ap_make_message(tempfp, target_room, author, subject);
135
136         /* Copy it to a new temp file in the spool directory */
137         rewind(tempfp);
138
139         spoolfp = fopen(tempspool, "wb");
140         if (spoolfp == NULL) {
141                 perror("cannot open spool file");
142                 exit(errno);
143         }
144         while (ch = getc(tempfp), (ch >= 0)) {
145                 putc(ch, spoolfp);
146         }
147
148         fclose(tempfp);
149         fclose(spoolfp);
150
151         exit(0);
152 }