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