* Began (but did not finish) applying GPL3+ declarations to each source file. This...
[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  * Copyright (c) 1987-2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <limits.h>
39 #include <errno.h>
40 #include <string.h>
41 #include "citadel.h"
42 #include "citadel_dirs.h"
43 #include "config.h"
44
45 #ifndef HAVE_SNPRINTF
46 #include "snprintf.h"
47 #endif
48
49
50 /*
51  * Simplified function to generate a message in our format
52  */
53 static void ap_make_message(FILE *fp, char *target_room, char *author, char *subject)
54 {
55         int a;
56         long bb, cc;
57         time_t now;
58         time(&now);
59         putc(255, fp);
60         putc(MES_NORMAL, fp);
61         putc(1, fp);
62         fprintf(fp, "Proom_aide");
63         putc(0, fp);
64         fprintf(fp, "T%ld", (long)now);
65         putc(0, fp);
66         fprintf(fp, "A%s", author);
67         putc(0, fp);
68         fprintf(fp, "O%s", target_room);
69         putc(0, fp);
70         if (strlen(subject) > 0) {
71                 fprintf(fp, "U%s%c", subject, 0);
72         }
73         fprintf(fp, "N%s", NODENAME);
74         putc(0, fp);
75         putc('M', fp);
76         bb = ftell(fp);
77         while (a = getc(stdin), a > 0) {
78                 if (a != 8)
79                         putc(a, fp);
80                 else {
81                         cc = ftell(fp);
82                         if (cc != bb)
83                                 fseek(fp, (-1L), 1);
84                 }
85         }
86         putc(0, fp);
87         putc(0, fp);
88         putc(0, fp);
89 }
90
91 int main(int argc, char **argv)
92 {
93         char tempspool[64];
94         char target_room[ROOMNAMELEN];
95         char author[64];
96         char subject[256];
97         FILE *tempfp, *spoolfp;
98         int ch;
99         int i;
100
101         int relh=0;
102         int home=0;
103         char relhome[PATH_MAX]="";
104         char ctdldir[PATH_MAX]=CTDLDIR;
105
106         /* TODO: should we be able to calculate relative dirs? */
107         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
108
109
110         get_config();
111
112         strcpy(target_room, "Aide");
113         strcpy(author, "Citadel");
114         strcpy(subject, "");
115         for (i=1; i<argc; ++i) {
116                 if (!strncasecmp(argv[i], "-r", 2)) {
117                         strncpy(target_room, &argv[i][2], sizeof(target_room));
118                         target_room[sizeof(target_room)-1] = 0;
119                 }
120                 else if (!strncasecmp(argv[i], "-a", 2)) {
121                         strncpy(author, &argv[i][2], sizeof(author));
122                         author[sizeof(author)-1] = 0;
123                 }
124                 else if (!strncasecmp(argv[i], "-s", 2)) {
125                         strncpy(subject, &argv[i][2], sizeof(subject));
126                         subject[sizeof(subject)-1] = 0;
127                 } else {
128                         fprintf(stderr, "%s: usage: %s "
129                                         "[-rTargetRoom] [-aAuthor] [-sSubject]\n",
130                                 argv[0], argv[0]);
131                         exit(1);
132                 }
133         }
134
135         snprintf(tempspool, sizeof tempspool,
136                          "%s/ap.%04lx",
137                          ctdl_netin_dir,
138                 (long)getpid());
139
140         unlink(tempspool);
141
142         tempfp = fopen(tempspool, "w+b");
143         unlink(tempspool);
144         if (tempfp == NULL) {
145                 perror("cannot open temp file");
146                 exit(errno);
147         }
148
149         /* Generate a message from stdin */
150         ap_make_message(tempfp, target_room, author, subject);
151
152         /* Copy it to a new temp file in the spool directory */
153         rewind(tempfp);
154
155         spoolfp = fopen(tempspool, "wb");
156         if (spoolfp == NULL) {
157                 perror("cannot open spool file");
158                 exit(errno);
159         }
160         while (ch = getc(tempfp), (ch >= 0)) {
161                 putc(ch, spoolfp);
162         }
163
164         fclose(tempfp);
165         fclose(spoolfp);
166
167         exit(0);
168 }