Rework the aidepost utility to not need the config file
[citadel.git] / citadel / utils / aidepost.c
1 /*
2  * This is just a little hack to copy standard input to a message in Aide>
3  *
4  * Copyright (c) 1987-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <limits.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <time.h>
22 #include "citadel.h"
23 #include "citadel_dirs.h"
24
25 #ifndef HAVE_SNPRINTF
26 #include "snprintf.h"
27 #endif
28
29
30 /*
31  * Simplified function to generate a message in our format
32  */
33 static void ap_make_message(FILE *fp, char *target_room, char *author, char *subject)
34 {
35         int a;
36         long bb, cc;
37         time_t now;
38         time(&now);
39         putc(255, fp);
40         putc(MES_NORMAL, fp);
41         putc(1, fp);
42         fprintf(fp, "Proom_aide");
43         putc(0, fp);
44         fprintf(fp, "T%ld", (long)now);
45         putc(0, fp);
46         fprintf(fp, "A%s", author);
47         putc(0, fp);
48         fprintf(fp, "O%s", target_room);
49         putc(0, fp);
50         if (strlen(subject) > 0) {
51                 fprintf(fp, "U%s%c", subject, 0);
52         }
53         putc('M', fp);
54         bb = ftell(fp);
55         while (a = getc(stdin), a > 0) {
56                 if (a != 8)
57                         putc(a, fp);
58                 else {
59                         cc = ftell(fp);
60                         if (cc != bb)
61                                 fseek(fp, (-1L), 1);
62                 }
63         }
64         putc(0, fp);
65         putc(0, fp);
66         putc(0, fp);
67 }
68
69
70 int main(int argc, char **argv)
71 {
72         char tempspool[64];
73         char target_room[ROOMNAMELEN];
74         char author[64];
75         char subject[256];
76         FILE *tempfp, *spoolfp;
77         int ch;
78         int i;
79         int relh=0;
80         int home=0;
81         char relhome[PATH_MAX]="";
82         char ctdldir[PATH_MAX]=CTDLDIR;
83
84         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
85
86         strcpy(target_room, "Aide");
87         strcpy(author, "Citadel");
88         strcpy(subject, "");
89         for (i=1; i<argc; ++i) {
90                 if (!strncasecmp(argv[i], "-r", 2)) {
91                         strncpy(target_room, &argv[i][2], sizeof(target_room));
92                         target_room[sizeof(target_room)-1] = 0;
93                 }
94                 else if (!strncasecmp(argv[i], "-a", 2)) {
95                         strncpy(author, &argv[i][2], sizeof(author));
96                         author[sizeof(author)-1] = 0;
97                 }
98                 else if (!strncasecmp(argv[i], "-s", 2)) {
99                         strncpy(subject, &argv[i][2], sizeof(subject));
100                         subject[sizeof(subject)-1] = 0;
101                 } else {
102                         fprintf(stderr, "%s: usage: %s "
103                                         "[-rTargetRoom] [-aAuthor] [-sSubject]\n",
104                                 argv[0], argv[0]);
105                         exit(1);
106                 }
107         }
108
109         snprintf(tempspool, sizeof tempspool, "%s/ap.%04x.%08lx", ctdl_netin_dir, getpid(), time(NULL));
110         unlink(tempspool);      /* unlinking it for now, keeps it from being posted prematurely */
111
112         tempfp = fopen(tempspool, "w+b");
113         unlink(tempspool);
114         if (tempfp == NULL) {
115                 perror("cannot open temp file");
116                 exit(errno);
117         }
118
119         /* Generate a message from stdin */
120         ap_make_message(tempfp, target_room, author, subject);
121
122         /* Copy it to a new temp file in the spool directory */
123         rewind(tempfp);
124
125         spoolfp = fopen(tempspool, "wb");
126         if (spoolfp == NULL) {
127                 perror("cannot open spool file");
128                 exit(errno);
129         }
130         while (ch = getc(tempfp), (ch >= 0)) {
131                 putc(ch, spoolfp);
132         }
133
134         fclose(tempfp);
135         return(fclose(spoolfp));
136 }