home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 3 / Meeting_Pearls_III.iso / Pearls / comm / Mail+News / UMS11 / Developer / C / examples / SumNew.c < prev   
C/C++ Source or Header  |  1994-04-24  |  7KB  |  264 lines

  1. /************************************************************************/
  2. /*         SumNew:  Print a summary of new messages to stdout.          */
  3. /************************************************************************/
  4.  
  5. /*
  6.  * Demo sent by: SteveX@omx.OCUnix.On.Ca (Steve Tibbett)
  7.  *
  8.  * Adapted to DICE by stefanb
  9.  *
  10.  */
  11.  
  12. #include <clib/exec_protos.h>
  13. #include <proto/ums.h>
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <exec/types.h>
  19. #include <dos/dos.h>
  20.  
  21. #ifdef __SASC
  22. #include <dos.h>
  23. #endif
  24.  
  25. UMSUserAccount Handle;
  26. struct Library *UMSBase;
  27. extern struct Library *SysBase;
  28.  
  29. struct Subject
  30.         {
  31.         struct Subject *Next;
  32.         long Count;
  33.         char *Text;
  34.         } *Subjects;
  35.  
  36. /************************************************************************/
  37. /*           This gets called just before the program exits.            */
  38. /************************************************************************/
  39. void
  40. cleanup(void)
  41. {
  42. if (Handle) UMSLogout(Handle);
  43. if (UMSBase) CloseLibrary(UMSBase);
  44. }
  45.  
  46. /************************************************************************/
  47. /*             This gets jumped to whenever Ctrl-C is hit.              */
  48. /************************************************************************/
  49. int
  50. breaker(void)
  51. {
  52. printf("^C\n");
  53. fflush(stdout);
  54. return(0);
  55. }
  56.  
  57. /************************************************************************/
  58. /*               If there is an error, print it and exit.               */
  59. /************************************************************************/
  60. void
  61. CheckErr(void)
  62. {
  63. UMSError err;
  64.  
  65. err=UMSErrNum(Handle);
  66.  
  67. if (err != UMSERR_OK)
  68.         {
  69.         fprintf(stderr,"UMS-Error: %3d, '%s'\n",err,UMSErrTxt(Handle));
  70.         exit(20);
  71.         }
  72. }
  73.  
  74. /************************************************************************/
  75. /*                Add this subject to the subject list.                 */
  76. /************************************************************************/
  77. void
  78. AddSubjectToList(char *SubText)
  79. {
  80. struct Subject *Sub;
  81. struct Subject *NewSub;
  82.  
  83. Sub=Subjects;
  84. while (Sub)
  85.         {
  86.         if (stricmp(SubText, Sub->Text)==0)
  87.                 {
  88.                 Sub->Count++;
  89.                 return;
  90.                 };
  91.  
  92.         Sub=Sub->Next;
  93.         };
  94.  
  95. NewSub=malloc(strlen(SubText)+1+sizeof(struct Subject));
  96. if (NewSub==0) return;
  97.  
  98. NewSub->Text=(char *)(NewSub+1);
  99. strcpy(NewSub->Text, SubText);
  100. NewSub->Count=1;
  101.  
  102. if (Subjects==0)
  103.         {
  104.         Subjects=NewSub;
  105.         NewSub->Next=0;
  106.         } else
  107.         {
  108.         if (stricmp(NewSub->Text, Subjects->Text)<0)
  109.                 {
  110.                 NewSub->Next=Subjects;
  111.                 Subjects=NewSub;
  112.                 } else
  113.                 {
  114.                 Sub=Subjects;
  115.                 while (Sub->Next)
  116.                         {
  117.                         if (stricmp(NewSub->Text, Sub->Next->Text)<0)
  118.                                 {
  119.                                 NewSub->Next=Sub->Next;
  120.                                 Sub->Next=NewSub;
  121.                                 break;
  122.                                 };
  123.  
  124.                         Sub=Sub->Next;
  125.                         };
  126.  
  127.                 if (Sub->Next==0)
  128.                         {
  129.                         Sub->Next=NewSub;
  130.                         NewSub->Next=0;
  131.                         };
  132.                 };
  133.         };
  134. }
  135.  
  136. /************************************************************************/
  137. /*                      Main entry/exit and loop                        */
  138. /************************************************************************/
  139. int
  140. main(int argc, char *argv[])
  141. {
  142. char *login,*passwd;
  143. UMSMsgNum result;
  144. char SubjLine[256];
  145.  
  146. if (argc!=3)
  147.         {
  148.         printf("Usage: %s \"User Name\" \"Password\"\n", argv[0]);
  149.         exit(0);
  150.         };
  151.  
  152. onbreak(breaker);
  153.  
  154. UMSBase=OpenLibrary("ums.library", 9);
  155. if (UMSBase==0)
  156.         {
  157.         fprintf(stderr, "Error: Can't open ums.library.\n");
  158.         exit(0);
  159.         };
  160.  
  161. login=argv[1];
  162. passwd=argv[2];
  163.  
  164. if ((Handle=UMSLogin(login,passwd))==NULL)
  165.         {
  166.         fprintf(stderr,"UMS login failed.\n");
  167.         exit(20);
  168.         }
  169.  
  170. /****************************************************************************
  171.  
  172.    1. Flag all new messages.
  173.  
  174.       If: (GlobalFlags & (ViewAccess|PostPoned|Old)) == ViewAccess
  175.  
  176.       Set the local flag bit 0.
  177.  
  178.       These are all NEW messages, which can be seen by the user.
  179.  
  180. *****************************************************************************/
  181.  
  182. printf("Searching...");
  183. fflush(stdout);
  184.  
  185. result=UMSSelectTags(Handle,
  186.         UMSTAG_SelMask,         UMSUSTATF_ViewAccess|UMSUSTATF_PostPoned|UMSUSTATF_Old,
  187.         UMSTAG_SelMatch,        UMSUSTATF_ViewAccess,
  188.         UMSTAG_SelWriteLocal,   TRUE,
  189.         UMSTAG_SelSet,          (1L<<0),
  190.         UMSTAG_SelUnset,        0,
  191.         TAG_DONE);
  192.  
  193. CheckErr();
  194. if (result==0)
  195.         printf("No new messages.");
  196.         else printf("%d new messages found.  Summarizing...\n", result);
  197.  
  198.  
  199. /****************************************************************************
  200.  
  201.    2. Read in all the subjects.
  202.  
  203. *****************************************************************************/
  204.  
  205. if (result)
  206.         {
  207.         long Succ;
  208.         long HdrLength;
  209.         long TextLength;
  210.         char *Subject;
  211.         char *Group;
  212.         struct Subject *Sub;
  213.  
  214.         result=0;
  215.  
  216.         while (TRUE)
  217.                 {
  218.                 result=UMSSearchTags(Handle,
  219.                         UMSTAG_SearchLast, result,
  220.                         UMSTAG_SearchDirection, 1,
  221.                         UMSTAG_SearchLocal, TRUE,
  222.                         UMSTAG_SearchMask, (1L<<0),
  223.                         UMSTAG_SearchMatch, (1L<<0),
  224.                         TAG_DONE);
  225.  
  226.                 if (result==0)
  227.                         break;
  228.  
  229.                 Succ=ReadUMSMsgTags(Handle,
  230.                         UMSTAG_RMsgNum, result,
  231.                         UMSTAG_RHeaderLength, &HdrLength,
  232.                         UMSTAG_RTextLength, &TextLength,
  233.                         UMSTAG_RNoUpdate, 1,
  234.                         UMSTAG_RSubject, &Subject,
  235.                         UMSTAG_RGroup, &Group,
  236.                         TAG_DONE);
  237.  
  238.  
  239.                 if (Succ)
  240.                         {
  241.                         sprintf(SubjLine, "%-33s  In: %s", Subject, Group?Group:":::Mail:::");
  242.                         AddSubjectToList(SubjLine);
  243.                         } else
  244.                         printf("UMS-Error: %3d, '%s'\n",UMSErrNum(Handle),UMSErrTxt(Handle));
  245.  
  246.                 if (SetSignal(0,0)&SIGBREAKF_CTRL_C)
  247.                         {
  248.                         cleanup();
  249.                         exit(0);
  250.                         }
  251.                 };
  252.  
  253.         Sub=Subjects;
  254.         while (Sub)
  255.                 {
  256.                 printf("%4d: %s\n", Sub->Count, Sub->Text);
  257.                 Sub=Sub->Next;
  258.                 };
  259.         };
  260.  
  261. cleanup();
  262. return(0);
  263. }
  264.