home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / comm / Mail+News / UMS11 / Tools / SUMSTools / Source / sumswrite.c < prev    next >
C/C++ Source or Header  |  1995-08-03  |  7KB  |  266 lines

  1.  
  2. #include "sumstl.h"
  3.  
  4. #include <stdio.h>
  5. #include <ctype.h>
  6.  
  7. #include "date.h"
  8.  
  9. /* SMAKE */
  10.  
  11. // Version String
  12. // --------------
  13.  
  14. static char VersionString[] = "$VER: sumswrite "VERSION;
  15.  
  16. static char UsageString[] = "\
  17.   U=User      : user name.\n\
  18.   P=Password  : user's password.\n\
  19.   S=Server    : server name.\n\n\
  20. Message will be read from standard input. See docs for file format.\n\n\
  21. ";
  22.  
  23.  
  24. // Template
  25. // --------
  26.  
  27. static char *TemplateString = "U=User=Name/A,P=Password/A,S=Server/K";
  28. enum opts {
  29.     OPT_USER, OPT_PASSWORD, OPT_SERVER,
  30.     OPT_COUNT};
  31. static LONG opts[OPT_COUNT];
  32.  
  33.  
  34. // Globals
  35. // -------
  36.  
  37. extern struct DosLibrary *DOSBase;
  38.  
  39. struct Library *UMSBase = NULL;
  40. UMSAccount acc = NULL;
  41.  
  42. #define KW_STRING  0
  43. #define KW_NUMBER  1
  44. #define KW_BOOLEAN 2
  45. #define KW_DATE    3
  46.  
  47. struct Keyword
  48. {
  49.     char *name;
  50.     LONG type;
  51.     LONG tag;
  52.     APTR data;
  53. } Keywords[]=
  54. {
  55.     { "MsgNum"       , KW_NUMBER , UMSTAG_WMsgNum       , NULL },
  56.     { "MsgDate"      , KW_DATE   , UMSTAG_WMsgDate      , NULL },
  57.     { "ChainUp"      , KW_NUMBER , UMSTAG_WChainUp      , NULL },
  58.     { "HardLink"     , KW_NUMBER , UMSTAG_WHardLink     , NULL },
  59.     { "SoftLink"     , KW_NUMBER , UMSTAG_WSoftLink     , NULL },
  60.     { "AutoBounce"   , KW_BOOLEAN, UMSTAG_WAutoBounce   , NULL },
  61.     { "HdrFill"      , KW_NUMBER , UMSTAG_WHdrFill      , NULL },
  62.     { "TxtFill"      , KW_NUMBER , UMSTAG_WTxtFill      , NULL },
  63.     { "NoUpdate"     , KW_BOOLEAN, UMSTAG_WNoUpdate     , NULL },
  64.     { "FromName"     , KW_STRING , UMSTAG_WFromName     , NULL },
  65.     { "FromAddr"     , KW_STRING , UMSTAG_WFromAddr     , NULL },
  66.     { "ToName"       , KW_STRING , UMSTAG_WToName       , NULL },
  67.     { "ToAddr"       , KW_STRING , UMSTAG_WToAddr       , NULL },
  68.     { "MsgID"        , KW_STRING , UMSTAG_WMsgID        , NULL },
  69.     { "CreationDate" , KW_STRING , UMSTAG_WCreationDate , NULL },
  70.     { "ReceiveDate"  , KW_STRING , UMSTAG_WReceiveDate  , NULL },
  71.     { "ReferID"      , KW_STRING , UMSTAG_WReferID      , NULL },
  72.     { "Group"        , KW_STRING , UMSTAG_WGroup        , NULL },
  73.     { "Subject"      , KW_STRING , UMSTAG_WSubject      , NULL },
  74.     { "Attributes"   , KW_STRING , UMSTAG_WAttributes   , NULL },
  75.     { "Comments"     , KW_STRING , UMSTAG_WComments     , NULL },
  76.     { "Organization" , KW_STRING , UMSTAG_WOrganization , NULL },
  77.     { "Distribution" , KW_STRING , UMSTAG_WDistribution , NULL },
  78.     { "Folder"       , KW_STRING , UMSTAG_WFolder       , NULL },
  79.     { "FidoID"       , KW_STRING , UMSTAG_WFidoID       , NULL },
  80.     { "MausID"       , KW_STRING , UMSTAG_WMausID       , NULL },
  81.     { "ReplyGroup"   , KW_STRING , UMSTAG_WReplyGroup   , NULL },
  82.     { "ReplyName"    , KW_STRING , UMSTAG_WReplyName    , NULL },
  83.     { "ReplyAddr"    , KW_STRING , UMSTAG_WReplyAddr    , NULL },
  84.     { "LogicalToName", KW_STRING , UMSTAG_WLogicalToName, NULL },
  85.     { "LogicalToAddr", KW_STRING , UMSTAG_WLogicalToAddr, NULL },
  86.     { "RFCMsgNum"    , KW_STRING , UMSTAG_WRFCMsgNum    , NULL },
  87.     { "FidoText"     , KW_STRING , UMSTAG_WFidoText     , NULL },
  88.     { "ErrorText"    , KW_STRING , UMSTAG_WErrorText    , NULL },
  89.     { "Newsreader"   , KW_STRING , UMSTAG_WNewsreader   , NULL },
  90.     { "RFCAttr"      , KW_STRING , UMSTAG_WRfcAttr      , NULL },
  91.     { "FTNAttr"      , KW_STRING , UMSTAG_WFtnAttr      , NULL },
  92.     { "ZerAttr"      , KW_STRING , UMSTAG_WZerAttr      , NULL },
  93.     { "MausAttr"     , KW_STRING , UMSTAG_WMausAttr     , NULL },
  94.     { NULL,0,0,0 }
  95. };
  96.  
  97.  
  98. // CTRL-C Stuff
  99. // ------------
  100.  
  101. VOID chkabort(VOID) {}
  102. #define ABORTED (SetSignal(0,0) & SIGBREAKF_CTRL_C)
  103.  
  104.  
  105. struct Keyword *FindKeyword(UBYTE *name)
  106. {
  107.     struct Keyword *k;
  108.     for (k=Keywords;k->name;k++)
  109.         if (!strnicmp(name,k->name,strlen(k->name))) return(k);
  110.     return(NULL);
  111. }
  112.  
  113.  
  114. BOOL GetTrueFalse(char *str)
  115. {
  116.     if (!stricmp(str,"true") || !stricmp(str,"on") || !stricmp(str,"yes") || !stricmp(str,"ein") || !stricmp(str,"an") || !stricmp(str,"ja"))
  117.         return(TRUE);
  118.     else
  119.         return(FALSE);
  120. }
  121.  
  122.  
  123. BOOL HandleLine(char *line)
  124. {
  125.     struct Keyword *k;
  126.     int len=strlen(line);
  127.     if (!len) return(TRUE);
  128.     if (line[len-1]=='\n') line[len-1]=0;
  129.     if (!(k=FindKeyword(line))) return(FALSE);
  130.     line+=strlen(k->name);
  131.     while (isspace(*line)) line++;
  132.     if (*line=='=') line++;
  133.     while (isspace(*line)) line++;
  134.     if (!*line && k->type==KW_BOOLEAN) line="true";
  135.     if (!*line) return(TRUE);
  136.     switch (k->type)
  137.     {
  138.         case KW_STRING:    k->data = strdup(line);
  139.                                 break;
  140.         case KW_NUMBER:    k->data = (APTR)atol(line);
  141.                                 break;
  142.         case KW_BOOLEAN:    k->data = (APTR)(*line ? GetTrueFalse(line) : TRUE);
  143.                                 break;
  144.         case KW_DATE:        k->data = (APTR)DateToSeconds(line);
  145.                                 break;
  146.     }
  147.     return(TRUE);
  148. }
  149.  
  150.  
  151. // Main Function
  152. // -------------
  153.  
  154. int main(int argc,char *argv[])
  155. {
  156.     static char buffer[256];
  157.     static struct TagItem Tags[30];
  158.     int erg = RETURN_FAIL;
  159.     BOOL text=FALSE;
  160.     char *textbuf=NULL,*newbuf,*tptr;
  161.     int textbufsize=0,textlen=0,c;
  162.     struct RDArgs *args_ptr;
  163.  
  164.     if (argc<2 || *argv[1] == '?')
  165.         fprintf(stderr,"\33[1m%s\33[0m, written by Stefan Stuntz, Public Domain\n\nTemplate: %s\n%s",&VersionString[6],TemplateString,UsageString);
  166.  
  167.     {
  168.         int i;
  169.  
  170.         for (i=0; i<OPT_COUNT; i++)
  171.         opts[i]=NULL;
  172.     }
  173.  
  174.     if (args_ptr = ReadArgs(TemplateString, opts, NULL))
  175.     {
  176.         if (UMSBase = OpenLibrary(UMSNAME,UMSVERSION))
  177.         {
  178.             if (acc = UMSRLogin((char *)opts[OPT_SERVER],(char *)opts[OPT_USER],(char *)opts[OPT_PASSWORD]))
  179.             {
  180.                 while (!feof(stdin))
  181.                 {
  182.                     if (text)
  183.                     {
  184.                         if ((c=fgetc(stdin))==EOF) break;
  185.                         if (textlen==textbufsize)
  186.                         {
  187.                             if (!(newbuf=malloc(textbufsize+10001))) { text=FALSE; break; }
  188.                             if (textbuf)
  189.                             {
  190.                                 CopyMem(textbuf,newbuf,textbufsize);
  191.                                 free(textbuf);
  192.                             }
  193.                             textbuf=newbuf;
  194.                             textbufsize+=10000;
  195.                             tptr=textbuf+textlen;
  196.                         }
  197.                         *tptr++=c;
  198.                         textlen++;
  199.                     }
  200.                     else
  201.                     {
  202.                         if (fgets(buffer,255,stdin))
  203.                         {
  204.                             if (buffer[0]=='-')
  205.                             {
  206.                                 text=TRUE;
  207.                             }
  208.                             else if (!HandleLine(buffer))
  209.                             {
  210.                                 fprintf(stderr,"\7Invalid line: \"%s\"\n",buffer);
  211.                             }
  212.                         }
  213.                     }
  214.                 }
  215.  
  216.                 if (text)
  217.                 {
  218.                     struct Keyword *k;
  219.                     struct TagItem *t=Tags;
  220.                     for (k=Keywords;k->name;k++)
  221.                     {
  222.                         if (k->data)
  223.                         {
  224.                             t->ti_Tag  = k->tag;
  225.                             t->ti_Data = (LONG)((k->tag==UMSTAG_WGroup && k->data && !stricmp(k->data,"Netmail") ? NULL : (LONG)k->data));
  226.                             t++;
  227.                         }
  228.                     }
  229.                     t->ti_Tag  = UMSTAG_WMsgText;
  230.                     t->ti_Data = (LONG)textbuf;
  231.                     t++;
  232.                     t->ti_Tag = TAG_DONE;
  233.  
  234.                     *tptr=0;
  235.  
  236.                     if (UMSWriteMsg(acc,Tags))
  237.                     {
  238.                         printf("Message written.\n");
  239.                         erg=0;
  240.                     }
  241.                     else
  242.                         fprintf(stderr,"\7UMS-Error %ld: %s\n",UMSErrNum(acc),UMSErrTxt(acc));
  243.                 }
  244.                 else
  245.                     fprintf(stderr,"\7Missing message text.\n");
  246.  
  247.                 UMSLogout(acc);
  248.             }
  249.             else printf("\7UMS-Login failed.\n");
  250.  
  251.  
  252.             CloseLibrary(UMSBase);
  253.         }
  254.         else printf("\7Could not open ums.library V10.\n");
  255.  
  256.         FreeArgs(args_ptr);
  257.     }
  258.     else
  259.     {
  260.         PrintFault(IoErr(), NULL);
  261.         return(RETURN_ERROR);
  262.     }
  263.  
  264.     return(erg);
  265. }
  266.