Back to index

citadel  8.12
msgform.c
Go to the documentation of this file.
00001 /*
00002  * This is simply a filter that converts Citadel binary message format
00003  * to readable, formatted output.
00004  * 
00005  * If the -q (quiet or qwk) flag is used, only the message text prints, and
00006  * then it stops at the end of the first message it prints.
00007  * 
00008  * This utility isn't very useful anymore.
00009  *
00010  */
00011 
00012 #include "sysdep.h"
00013 #include <stdlib.h>
00014 #include <unistd.h>
00015 #include <fcntl.h>
00016 #include <stdio.h>
00017 #include <ctype.h>
00018 #include <string.h>
00019 
00020 #if TIME_WITH_SYS_TIME
00021 # include <sys/time.h>
00022 # include <time.h>
00023 #else
00024 # if HAVE_SYS_TIME_H
00025 #  include <sys/time.h>
00026 # else
00027 #  include <time.h>
00028 # endif
00029 #endif
00030 
00031 #include <errno.h>
00032 #include <libcitadel.h>
00033 
00034 int qwk = 0;
00035 
00036 int fpgetfield(FILE * fp, char *string);
00037 int fmout(int width, FILE * fp);
00038 
00039 
00040 #ifndef HAVE_STRERROR
00041 /*
00042  * replacement strerror() for systems that don't have it
00043  */
00044 char *strerror(int e)
00045 {
00046        static char buf[32];
00047 
00048        snprintf(buf, sizeof buf, "errno = %d", e);
00049        return (buf);
00050 }
00051 #endif
00052 
00053 int main(int argc, char **argv)
00054 {
00055        struct tm tm;
00056        int a, b, e, mtype, aflag;
00057        char bbb[1024];
00058        char subject[1024];
00059        FILE *fp;
00060        time_t now;
00061 
00062        if (argc == 2)
00063               if (!strcmp(argv[1], "-q"))
00064                      qwk = 1;
00065        fp = stdin;
00066        if (argc == 2)
00067               if (strcmp(argv[1], "-q")) {
00068                      fp = fopen(argv[1], "r");
00069                      if (fp == NULL) {
00070                             fprintf(stderr, "%s: cannot open %s: %s\n",
00071                                    argv[0], argv[1], strerror(errno));
00072                             exit(errno);
00073                      }
00074               }
00075 
00076 TOP:   do {
00077               e = getc(fp);
00078               if (e < 0)
00079                      exit(0);
00080        } while (e != 255);
00081        strcpy(subject, "");
00082        mtype = getc(fp);
00083        aflag = getc(fp);
00084        if (qwk == 0)
00085               printf(" ");
00086 
00087        do {
00088               b = getc(fp);
00089               if (b == 'M') {
00090                      if (qwk == 0) {
00091                             printf("\n");
00092                             if (!IsEmptyStr(subject))
00093                                    printf("Subject: %s\n", subject);
00094                      }
00095                      if (aflag != 1)
00096                             fmout(80, fp);
00097                      else
00098                             while (a = getc(fp), a > 0) {
00099                                    if (a == 13)
00100                                           putc(10, stdout);
00101                                    else
00102                                           putc(a, stdout);
00103                             }
00104               }
00105               if ((b != 'M') && (b > 0))
00106                      fpgetfield(fp, bbb);
00107               if (b == 'U')
00108                      strcpy(subject, bbb);
00109               if (qwk == 0) {
00110                      if (b == 'A')
00111                             printf("from %s ", bbb);
00112                      if (b == 'N')
00113                             printf("@%s ", bbb);
00114                      if (b == 'O')
00115                             printf("in %s> ", bbb);
00116                      if (b == 'R')
00117                             printf("to %s ", bbb);
00118                      if (b == 'T') {
00119                             now = atol(bbb);
00120                             localtime_r(&now, &tm);
00121                             strcpy(bbb, asctime(&tm));
00122                             bbb[strlen(bbb) - 1] = 0;
00123                             printf("%s ", &bbb[4]);
00124                      }
00125               }
00126        } while ((b != 'M') && (b > 0));
00127        if (qwk == 0)
00128               printf("\n");
00129        if (qwk == 1)
00130               exit(0);
00131        goto TOP;
00132 }
00133 
00134 int fpgetfield(FILE * fp, char *string)
00135 
00136 {                           /* level-2 break out next null-terminated string */
00137        int a, b;
00138        strcpy(string, "");
00139        a = 0;
00140        do {
00141               b = getc(fp);
00142               if (b < 1) {
00143                      string[a] = 0;
00144                      return (0);
00145               }
00146               string[a] = b;
00147               ++a;
00148        } while (b != 0);
00149        return (0);
00150 }
00151 
00152 int fmout(int width, FILE * fp)
00153 {
00154        int a, b, c;
00155        int real = 0;
00156        int old = 0;
00157        char aaa[140];
00158 
00159        strcpy(aaa, "");
00160        old = 255;
00161        c = 1;               /* c is the current pos */
00162 FMTA:  old = real;
00163        a = getc(fp);
00164        real = a;
00165        if (a <= 0)
00166               goto FMTEND;
00167 
00168        if (((a == 13) || (a == 10)) && (old != 13) && (old != 10))
00169               a = 32;
00170        if (((old == 13) || (old == 10)) && (isspace(real))) {
00171               printf("\n");
00172               c = 1;
00173        }
00174        if (a > 126)
00175               goto FMTA;
00176 
00177        if (a > 32) {
00178               if (((strlen(aaa) + c) > (width - 5))
00179                   && (strlen(aaa) > (width - 5))) {
00180                      printf("\n%s", aaa);
00181                      c = strlen(aaa);
00182                      aaa[0] = 0;
00183               }
00184               b = strlen(aaa);
00185               aaa[b] = a;
00186               aaa[b + 1] = 0;
00187        }
00188        if (a == 32) {
00189               if ((strlen(aaa) + c) > (width - 5)) {
00190                      printf("\n");
00191                      c = 1;
00192               }
00193               printf("%s ", aaa);
00194               ++c;
00195               c = c + strlen(aaa);
00196               strcpy(aaa, "");
00197               goto FMTA;
00198        }
00199        if ((a == 13) || (a == 10)) {
00200               printf("%s\n", aaa);
00201               c = 1;
00202               strcpy(aaa, "");
00203               goto FMTA;
00204        }
00205        goto FMTA;
00206 
00207 FMTEND:       printf("\n");
00208        return (0);
00209 }