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