Back to index

citadel  8.12
aidepost.c
Go to the documentation of this file.
00001 /*
00002  * This is just a little hack to copy standard input to a message in Aide>
00003  *
00004  * Copyright (c) 1987-2012 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <stdio.h>
00018 #include <limits.h>
00019 #include <errno.h>
00020 #include <string.h>
00021 #include <time.h>
00022 #include "citadel.h"
00023 #include "citadel_dirs.h"
00024 
00025 #ifndef HAVE_SNPRINTF
00026 #include "snprintf.h"
00027 #endif
00028 
00029 
00030 /*
00031  * Simplified function to generate a message in our format
00032  */
00033 static void ap_make_message(FILE *fp, char *target_room, char *author, char *subject)
00034 {
00035        int a;
00036        long bb, cc;
00037        time_t now;
00038        time(&now);
00039        putc(255, fp);
00040        putc(MES_NORMAL, fp);
00041        putc(1, fp);
00042        fprintf(fp, "Proom_aide");
00043        putc(0, fp);
00044        fprintf(fp, "T%ld", (long)now);
00045        putc(0, fp);
00046        fprintf(fp, "A%s", author);
00047        putc(0, fp);
00048        fprintf(fp, "O%s", target_room);
00049        putc(0, fp);
00050        if (strlen(subject) > 0) {
00051               fprintf(fp, "U%s%c", subject, 0);
00052        }
00053        putc('M', fp);
00054        bb = ftell(fp);
00055        while (a = getc(stdin), a > 0) {
00056               if (a != 8)
00057                      putc(a, fp);
00058               else {
00059                      cc = ftell(fp);
00060                      if (cc != bb)
00061                             fseek(fp, (-1L), 1);
00062               }
00063        }
00064        putc(0, fp);
00065        putc(0, fp);
00066        putc(0, fp);
00067 }
00068 
00069 
00070 int main(int argc, char **argv)
00071 {
00072        char tempspool[64];
00073        char target_room[ROOMNAMELEN];
00074        char author[64];
00075        char subject[256];
00076        FILE *tempfp, *spoolfp;
00077        int ch;
00078        int i;
00079        int relh=0;
00080        int home=0;
00081        char relhome[PATH_MAX]="";
00082        char ctdldir[PATH_MAX]=CTDLDIR;
00083 
00084        calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
00085 
00086        strcpy(target_room, "Aide");
00087        strcpy(author, "Citadel");
00088        strcpy(subject, "");
00089        for (i=1; i<argc; ++i) {
00090               if (!strncasecmp(argv[i], "-r", 2)) {
00091                      strncpy(target_room, &argv[i][2], sizeof(target_room));
00092                      target_room[sizeof(target_room)-1] = 0;
00093               }
00094               else if (!strncasecmp(argv[i], "-a", 2)) {
00095                      strncpy(author, &argv[i][2], sizeof(author));
00096                      author[sizeof(author)-1] = 0;
00097               }
00098               else if (!strncasecmp(argv[i], "-s", 2)) {
00099                      strncpy(subject, &argv[i][2], sizeof(subject));
00100                      subject[sizeof(subject)-1] = 0;
00101               } else {
00102                      fprintf(stderr, "%s: usage: %s "
00103                                    "[-rTargetRoom] [-aAuthor] [-sSubject]\n",
00104                             argv[0], argv[0]);
00105                      exit(1);
00106               }
00107        }
00108 
00109        snprintf(tempspool, sizeof tempspool, "%s/ap.%04x.%08lx", ctdl_netin_dir, getpid(), time(NULL));
00110        unlink(tempspool);   /* unlinking it for now, keeps it from being posted prematurely */
00111 
00112        tempfp = fopen(tempspool, "w+b");
00113        unlink(tempspool);
00114        if (tempfp == NULL) {
00115               perror("cannot open temp file");
00116               exit(errno);
00117        }
00118 
00119        /* Generate a message from stdin */
00120        ap_make_message(tempfp, target_room, author, subject);
00121 
00122        /* Copy it to a new temp file in the spool directory */
00123        rewind(tempfp);
00124 
00125        spoolfp = fopen(tempspool, "wb");
00126        if (spoolfp == NULL) {
00127               perror("cannot open spool file");
00128               exit(errno);
00129        }
00130        while (ch = getc(tempfp), (ch >= 0)) {
00131               putc(ch, spoolfp);
00132        }
00133 
00134        fclose(tempfp);
00135        return(fclose(spoolfp));
00136 }