home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / comm / ums / SUMSTools.lha / UMS / Tools / SUMSTools / Source / sumswrite.c < prev    next >
C/C++ Source or Header  |  1997-01-09  |  11KB  |  268 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.         { "FileName"     , KW_STRING , UMSTAG_WFileName     , NULL },
  87.         { "RFCMsgNum"    , KW_STRING , UMSTAG_WRFCMsgNum    , NULL },
  88.         { "FidoText"     , KW_STRING , UMSTAG_WFidoText     , NULL },
  89.         { "ErrorText"    , KW_STRING , UMSTAG_WErrorText    , NULL },
  90.         { "Newsreader"   , KW_STRING , UMSTAG_WNewsreader   , NULL },
  91.         { "RFCAttr"      , KW_STRING , UMSTAG_WRfcAttr      , NULL },
  92.         { "FTNAttr"      , KW_STRING , UMSTAG_WFtnAttr      , NULL },
  93.         { "ZerAttr"      , KW_STRING , UMSTAG_WZerAttr      , NULL },
  94.         { "MausAttr"     , KW_STRING , UMSTAG_WMausAttr     , NULL },
  95.         { "TempFileName" , KW_STRING , UMSTAG_WTempFileName , NULL },
  96.         { NULL,0,0,0 }
  97. };
  98.  
  99.  
  100. // CTRL-C Stuff
  101. // ------------
  102.  
  103. VOID chkabort(VOID) {}
  104. #define ABORTED (SetSignal(0,0) & SIGBREAKF_CTRL_C)
  105.  
  106.  
  107. struct Keyword *FindKeyword(UBYTE *name)
  108. {
  109.         struct Keyword *k;
  110.         for (k=Keywords;k->name;k++)
  111.                 if (!strnicmp(name,k->name,strlen(k->name))) return(k);
  112.         return(NULL);
  113. }
  114.  
  115.  
  116. BOOL GetTrueFalse(char *str)
  117. {
  118.         if (!stricmp(str,"true") || !stricmp(str,"on") || !stricmp(str,"yes") || !stricmp(str,"ein") || !stricmp(str,"an") || !stricmp(str,"ja"))
  119.                 return(TRUE);
  120.         else
  121.                 return(FALSE);
  122. }
  123.  
  124.  
  125. BOOL HandleLine(char *line)
  126. {
  127.         struct Keyword *k;
  128.         int len=strlen(line);
  129.         if (!len) return(TRUE);
  130.         if (line[len-1]=='\n') line[len-1]=0;
  131.         if (!(k=FindKeyword(line))) return(FALSE);
  132.         line+=strlen(k->name);
  133.         while (isspace(*line)) line++;
  134.         if (*line=='=') line++;
  135.         while (isspace(*line)) line++;
  136.         if (!*line && k->type==KW_BOOLEAN) line="true";
  137.         if (!*line) return(TRUE);
  138.         switch (k->type)
  139.         {
  140.                 case KW_STRING: k->data = strdup(line);
  141.                                                                 break;
  142.                 case KW_NUMBER: k->data = (APTR)atol(line);
  143.                                                                 break;
  144.                 case KW_BOOLEAN:        k->data = (APTR)(*line ? GetTrueFalse(line) : TRUE);
  145.                                                                 break;
  146.                 case KW_DATE:           k->data = (APTR)DateToSeconds(line);
  147.                                                                 break;
  148.         }
  149.         return(TRUE);
  150. }
  151.  
  152.  
  153. // Main Function
  154. // -------------
  155.  
  156. int main(int argc,char *argv[])
  157. {
  158.         static char buffer[256];
  159.         static struct TagItem Tags[30];
  160.         int erg = RETURN_FAIL;
  161.         BOOL text=FALSE;
  162.         char *textbuf=NULL,*newbuf,*tptr;
  163.         int textbufsize=0,textlen=0,c;
  164.         struct RDArgs *args_ptr;
  165.  
  166.         if (argc<2 || *argv[1] == '?')
  167.                 fprintf(stderr,"\33[1m%s\33[0m, written by Stefan Stuntz, Public Domain\n\nTemplate: %s\n%s",&VersionString[6],TemplateString,UsageString);
  168.  
  169.         {
  170.                 int i;
  171.  
  172.                 for (i=0; i<OPT_COUNT; i++)
  173.                 opts[i]=NULL;
  174.         }
  175.  
  176.         if (args_ptr = ReadArgs(TemplateString, opts, NULL))
  177.         {
  178.                 if (UMSBase = OpenLibrary(UMSNAME,UMSVERSION))
  179.                 {
  180.                         if (acc = UMSRLogin((char *)opts[OPT_SERVER],(char *)opts[OPT_USER],(char *)opts[OPT_PASSWORD]))
  181.                         {
  182.                                 while (!feof(stdin))
  183.                                 {
  184.                                         if (text)
  185.                                         {
  186.                                                 if ((c=fgetc(stdin))==EOF) break;
  187.                                                 if (textlen==textbufsize)
  188.                                                 {
  189.                                                         if (!(newbuf=malloc(textbufsize+10001))) { text=FALSE; break; }
  190.                                                         if (textbuf)
  191.                                                         {
  192.                                                                 CopyMem(textbuf,newbuf,textbufsize);
  193.                                                                 free(textbuf);
  194.                                                         }
  195.                                                         textbuf=newbuf;
  196.                                                         textbufsize+=10000;
  197.                                                         tptr=textbuf+textlen;
  198.                                                 }
  199.                                                 *tptr++=c;
  200.                                                 textlen++;
  201.                                         }
  202.                                         else
  203.                                         {
  204.                                                 if (fgets(buffer,255,stdin))
  205.                                                 {
  206.                                                         if (buffer[0]=='-')
  207.                                                         {
  208.                                                                 text=TRUE;
  209.                                                         }
  210.                                                         else if (!HandleLine(buffer))
  211.                                                         {
  212.                                                                 fprintf(stderr,"\7Invalid line: \"%s\"\n",buffer);
  213.                                                         }
  214.                                                 }
  215.                                         }
  216.                                 }
  217.  
  218.                                 if (text)
  219.                                 {
  220.                                         struct Keyword *k;
  221.                                         struct TagItem *t=Tags;
  222.                                         for (k=Keywords;k->name;k++)
  223.                                         {
  224.                                                 if (k->data)
  225.                                                 {
  226.                                                         t->ti_Tag  = k->tag;
  227.                                                         t->ti_Data = (LONG)((k->tag==UMSTAG_WGroup && k->data && !stricmp(k->data,"Netmail") ? NULL : (LONG)k->data));
  228.                                                         t++;
  229.                                                 }
  230.                                         }
  231.                                         t->ti_Tag  = UMSTAG_WMsgText;
  232.                                         t->ti_Data = (LONG)textbuf;
  233.                                         t++;
  234.                                         t->ti_Tag = TAG_DONE;
  235.  
  236.                                         *tptr=0;
  237.  
  238.                                         if (UMSWriteMsg(acc,Tags))
  239.                                         {
  240.                                                 printf("Message written.\n");
  241.                                                 erg=0;
  242.                                         }
  243.                                         else
  244.                                                 fprintf(stderr,"\7UMS-Error %ld: %s\n",UMSErrNum(acc),UMSErrTxt(acc));
  245.                                 }
  246.                                 else
  247.                                         fprintf(stderr,"\7Missing message text.\n");
  248.  
  249.                                 UMSLogout(acc);
  250.                         }
  251.                         else printf("\7UMS-Login failed.\n");
  252.  
  253.  
  254.                         CloseLibrary(UMSBase);
  255.                 }
  256.                 else printf("\7Could not open ums.library V10.\n");
  257.  
  258.                 FreeArgs(args_ptr);
  259.         }
  260.         else
  261.         {
  262.                 PrintFault(IoErr(), NULL);
  263.                 return(RETURN_ERROR);
  264.         }
  265.  
  266.         return(erg);
  267. }
  268.