warning fixes and cleanups for 64-bit machines
[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 "config.h"
27
28 #ifndef HAVE_SNPRINTF
29 #include "snprintf.h"
30 #endif
31
32
33 static void make_message(FILE *fp, char *target_room, char *author)
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         fprintf(fp, "N%s", NODENAME);
51         putc(0, fp);
52         putc('M', fp);
53         bb = ftell(fp);
54         while (a = getc(stdin), a > 0) {
55                 if (a != 8)
56                         putc(a, fp);
57                 else {
58                         cc = ftell(fp);
59                         if (cc != bb)
60                                 fseek(fp, (-1L), 1);
61                 }
62         }
63         putc(0, fp);
64         putc(0, fp);
65         putc(0, fp);
66 }
67
68 int main(int argc, char **argv)
69 {
70         char tempspool[64];
71         char target_room[ROOMNAMELEN];
72         char author[64];
73         FILE *tempfp, *spoolfp;
74         int ch;
75         int i;
76
77         get_config();
78
79         strcpy(target_room, "Aide");
80         strcpy(author, "Citadel");
81         for (i=1; i<argc; ++i) {
82                 if (!strncasecmp(argv[i], "-r", 2)) {
83                         strncpy(target_room, &argv[i][2], sizeof(target_room));
84                         target_room[sizeof(target_room)-1] = 0;
85                 }
86                 else if (!strncasecmp(argv[i], "-a", 2)) {
87                         strncpy(author, &argv[i][2], sizeof(author));
88                         author[sizeof(author)-1] = 0;
89                 } else {
90                         fprintf(stderr, "%s: usage: %s "
91                                         "[-rTargetRoom] [-aAuthor]\n",
92                                 argv[0], argv[0]);
93                         exit(1);
94                 }
95         }
96
97
98         snprintf(tempspool, sizeof tempspool,
99                 "./network/spoolin/ap.%04x",
100                 getpid());
101
102         tempfp = tmpfile();
103         if (tempfp == NULL) {
104                 perror("cannot open temp file");
105                 exit(errno);
106         }
107
108         /* Generate a message from stdin */
109         make_message(tempfp, target_room, author);
110
111         /* Copy it to a new temp file in the spool directory */
112         rewind(tempfp);
113
114         spoolfp = fopen(tempspool, "wb");
115         if (spoolfp == NULL) {
116                 perror("cannot open spool file");
117                 exit(errno);
118         }
119         while (ch = getc(tempfp), (ch >= 0))
120                 putc(ch, spoolfp);
121
122         fclose(tempfp);
123         fclose(spoolfp);
124
125         exit(0);
126 }