home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / bbs / comm / inetutils-amitcp-1.3.lha / INetUtils-AmiTCP / contrib / AmiPOP18.lha / AmiPOP18 / source / pop_dopop.c < prev    next >
C/C++ Source or Header  |  1994-02-28  |  6KB  |  437 lines

  1. #include "pop.h"
  2.  
  3. #define ASYNCBUFSIZE 8192
  4.  
  5. /* Variables global to this file */
  6.  
  7. struct Library *SockBase ;
  8. int havemail;
  9.  
  10. /* Functions */
  11.  
  12. int dopop(void)
  13. {
  14.     int s;
  15.     int count;
  16.     int okay=1;
  17.     struct hostent *hp;
  18.     struct sockaddr_in sa;
  19.  
  20.     sprintf(title,"Connecting to %s",pophost);
  21.     settitle();
  22.  
  23.     if((SockBase = OpenLibrary( SOCKLIBNAME, SOCKLIBVERSION )) == NULL)
  24.     {
  25.         doreq("Error opening socket.library\n",bum);
  26.         return(1);
  27.     }
  28. #ifdef AMITCP
  29.     SetErrnoPtr(&errno, sizeof errno);
  30. #else
  31.     setup_sockets( MAXSOCKS, &errno );
  32. #endif
  33.  
  34.     if((hp=gethostbyname(pophost))==NULL)
  35.     {
  36. #ifndef AMITCP
  37.         cleanup_sockets();
  38. #endif
  39.         CloseLibrary( SockBase ) ;
  40.         doreq("Connection Refused.",bum);
  41.         return(1);
  42.     }
  43.  
  44.     bzero(&sa, sizeof(sa));
  45.     bcopy(hp->h_addr, (char *)&sa.sin_addr, hp->h_length);
  46.     sa.sin_family = hp->h_addrtype;
  47.     sa.sin_port = htons((u_short)port);
  48.     if ((s=socket(hp->h_addrtype,SOCK_STREAM,0)) < 0)
  49.     {
  50. #ifndef AMITCP
  51.         cleanup_sockets();
  52. #endif
  53.         CloseLibrary( SockBase ) ;
  54.         doreq("Something bad\nhas happened.",bum);
  55.         return(1);
  56.     }
  57.  
  58.     if (connect(s,(struct sockaddr *) &sa,sizeof(sa))< 0)
  59.     {
  60.         doreq("No POP3 daemon\nrunning on this\nmachine or port.",bum);
  61.         s_close(s);
  62. #ifndef AMITCP
  63.         cleanup_sockets();
  64. #endif
  65.         CloseLibrary( SockBase ) ;
  66.         return(1);
  67.     }
  68.  
  69. /* Put actual code here */
  70.  
  71.     recv(s,buf,BUFSIZE-1,0);
  72.     sprintf(title,"Got Connection");
  73.     settitle();
  74.  
  75.     sendgreet(s);
  76.  
  77.     if (senduser(s))
  78.     {
  79. #ifndef AMITCP
  80.         cleanup_sockets();
  81. #endif
  82.         CloseLibrary( SockBase ) ;
  83.         return(1);
  84.     }
  85.  
  86.     count=havemail=sendstat(s);
  87.  
  88.     while (count && okay)
  89.     {
  90.         sprintf(title,"Retrieving %lu",count);
  91.         settitle();
  92.  
  93.         okay=retrieve(s,count);
  94.  
  95.         if (delmail && okay)
  96.         {
  97.             sprintf(title,"Deleting %lu",count);
  98.             settitle();
  99.  
  100.             okay=delmessage(s,count);
  101.         }
  102.         --count;
  103.     }
  104.  
  105.     if(sendquit(s))
  106.     {
  107. #ifndef AMITCP
  108.         cleanup_sockets();
  109. #endif
  110.         CloseLibrary( SockBase ) ;
  111.         return(1);
  112.     }
  113.  
  114.     if (Project0Wnd)
  115.     {
  116.         SetWindowTitles(Project0Wnd,Project0Wdt, (UBYTE *) ~0);
  117.     }
  118.  
  119.     if (havemail && notify) /* removed sendstat() after Nofify */
  120.     {
  121.         doreq("You have new mail.","Cool");
  122.     }
  123.  
  124. /* End actual code */
  125.  
  126.     s_close(s);
  127. #ifndef AMITCP
  128.     cleanup_sockets();
  129. #endif
  130.     CloseLibrary( SockBase ) ;
  131.     return(0);
  132. }
  133.  
  134. int sendgreet(int s)
  135. {
  136.     sprintf(buf,"HELLO\r\n");
  137.     trans(s);
  138.  
  139.     return(0);
  140. }
  141.  
  142. int senduser(int s)
  143. {
  144.     int t;
  145.  
  146.     sprintf(title,"Sending Username");
  147.     settitle();
  148.  
  149.     sprintf(buf,"USER %s\r\n",username);
  150.  
  151.     if ( !trans(s) ) return(1);
  152.  
  153.     t=sscanf(buf,"%s",temp);
  154.     if ( valcheck(t,temp) ) return(1);
  155.  
  156. /* Password */
  157.  
  158.     sprintf(title,"Sending Password");
  159.     settitle();
  160.  
  161.     sprintf(buf,"PASS %s\r\n",password);
  162.  
  163.     if ( !trans(s) ) return(1);
  164.  
  165.     t=sscanf(buf,"%s",temp);
  166.  
  167.     if ( valcheck(t,temp) ) return(1);
  168.  
  169.     return(0);
  170. }
  171.  
  172. int sendquit(int s)
  173. {
  174.     int t;
  175.  
  176.     sprintf(title,"Sending QUIT");
  177.     settitle();
  178.  
  179.     sprintf(buf,"QUIT\r\n");
  180.  
  181.     if ( !trans(s) ) return(1);
  182.  
  183.     t=sscanf(buf,"%s",temp);
  184.  
  185.     if ( valcheck(t,temp) ) return(1);
  186.  
  187.     sprintf(title,"Quit Acknowledged");
  188.     settitle();
  189.  
  190.     return(0);
  191. }
  192.  
  193. int sendstat( int s )
  194. {
  195.     int t;
  196.     int count=0;
  197.  
  198.     sprintf(buf,"STAT\r\n");
  199.  
  200.     if ( !trans(s) ) return(0);
  201.  
  202.     t=sscanf(buf,"%s %lu",temp,&count);
  203.  
  204.     if ( valcheck(t,temp) ) return(0);
  205.  
  206.     return(count);
  207. }
  208.  
  209. int retrieve(int s,int count)
  210. {
  211.     int foo;
  212.     int c;
  213.     int goon=1;
  214.  
  215.     char *havefrom;
  216.     BOOL begin=TRUE;
  217.     struct AsyncFile *ofp;
  218.  
  219.     if (havefrom=AllocVec(512,MEMF_CLEAR))
  220.     {
  221.         if ( (count == havemail) && (!appfile) )
  222.         {
  223.             ofp = OpenAsync(maildir, MODE_WRITE,ASYNCBUFSIZE);
  224.         }
  225.         else
  226.         {
  227.             ofp = OpenAsync(maildir, MODE_APPEND,ASYNCBUFSIZE);
  228.         }
  229.  
  230.         if ( ofp == NULL)
  231.         {
  232.             doreq("Open() failed\n",bum);
  233.             FreeVec(havefrom);
  234.             return(0);
  235.         }
  236.  
  237.         sprintf(buf,"RETR %lu\r\n",count);
  238.  
  239.         if ( !trans(s) )
  240.         {
  241.             CloseAsync(ofp);
  242.             FreeVec(havefrom);
  243.             return(0);
  244.         }
  245.  
  246.         if ( valcheck(1,buf) )
  247.         {
  248.             CloseAsync(ofp);
  249.             FreeVec(havefrom);
  250.             return(0);
  251.         }
  252.  
  253.         if (strstr(buf,"\r\n"))
  254.         {
  255.             WriteAsync(ofp,strstr(buf,"\r\n")+2,strlen(strstr(buf,"\r\n")+2));
  256.         }
  257.  
  258.         while ( goon )
  259.         {
  260.             foo=recv(s,buf,BUFSIZE-1,0);
  261.             buf[foo]='\0';
  262.  
  263.             goon=lastblock(buf);
  264.  
  265.             if (foo < 1)
  266.             {
  267.                 goon=0;
  268.             }
  269.  
  270.             /* Magic to add "From " header */
  271.             if (begin && goon)
  272.             {
  273.                 if (buf[0] != 'F')
  274.                 {
  275.                     struct DateTime dt;
  276.                     char day[20]="";
  277.                     char date[20]="";
  278.                     char time[20]="";
  279.                     char *newtemp;
  280.  
  281.                     if (newtemp=AllocVec(2*BUFSIZE,MEMF_CLEAR))
  282.                     {
  283.                         DateStamp(&dt.dat_Stamp);
  284.  
  285.                         dt.dat_Format = FORMAT_USA;
  286.                         dt.dat_Flags = 0;
  287.                         dt.dat_StrDay = day;
  288.                         dt.dat_StrDate = date;
  289.                         dt.dat_StrTime = time;
  290.  
  291.                         DateToStr(&dt);
  292.  
  293.                         sprintf(newtemp,"From %s@%s %s %s %s\n", username,pophost, day, date, time);
  294.                         WriteAsync(ofp,newtemp,strlen(newtemp));
  295.                         FreeVec(newtemp);
  296.                     }
  297.                 }
  298.             }
  299.             begin=FALSE;
  300.  
  301.             strip();
  302.  
  303.             c=WriteAsync(ofp,buf,strlen(buf));
  304.  
  305.             if (c == -1)
  306.             {
  307.                 doreq("FPuts() failed!",bum);
  308.                 goon=0;
  309.             }
  310.         }
  311.  
  312.         WriteAsync(ofp,"\n",1);
  313.         CloseAsync(ofp);
  314.  
  315.         FreeVec(havefrom);
  316.         return(1);
  317.     }
  318.     else
  319.     {
  320.         return(0);
  321.     }
  322.  
  323. }
  324.  
  325. int delmessage( int s, int count )
  326. {
  327.     sprintf(buf,"DELE %lu\r\n",count);
  328.     trans(s);
  329.  
  330.     if ( valcheck(1,buf) ) return(0);
  331.  
  332.     return(1);
  333. }
  334.  
  335. int valcheck(int t, char *localtemp)
  336. {
  337.     if (t == 0)
  338.     {
  339.         doreq("scanf() failed\n",bum);
  340.         return(1);
  341.     }
  342.  
  343.     if ( !strstr(localtemp,"+OK") )
  344.     {
  345.         doreq("Didn't get +OK",bum);
  346.  
  347.         if (localtemp[0] != '\0')
  348.         {
  349.             doreq(localtemp,bum);
  350.         }
  351.         return(1);
  352.     }
  353.     return(0);
  354. }
  355.  
  356. int trans( int s )
  357. {
  358.     ULONG foo;
  359.  
  360.     send(s,buf,strlen(buf),0);
  361.  
  362.     foo=recv(s,buf,BUFSIZE-1,0);
  363.     buf[foo]='\0';
  364.  
  365.     if ( !foo )
  366.     {
  367.         return(0);
  368.     }
  369.  
  370.     return (1);
  371. }
  372.  
  373. void settitle( void )
  374. {
  375.     if (winop)
  376.     {
  377.         SetWindowTitles(Project0Wnd,title, (UBYTE *) ~0);
  378.     }
  379. }
  380.  
  381. void strip( void )
  382. {
  383.     char out[BUFSIZE]="";
  384.     ULONG x1=0;
  385.     ULONG x2=0;
  386.     ULONG len;
  387.  
  388.     len=strlen(buf);
  389.  
  390.     while (buf[x1] != '\0' )
  391.     {
  392.         while (buf[x1] == '\r')
  393.         {
  394.             ++x1;
  395.         }
  396.         out[x2]=buf[x1];
  397.         ++x2;
  398.         if (x1 < len)
  399.         {
  400.             ++x1;
  401.         }
  402.     }
  403.     out[x2]='\0';
  404.  
  405.     strcpy(buf,out);
  406. }
  407.  
  408. int lastblock (char *segment)
  409. {
  410.     char *found;
  411.     int len = strlen(segment);
  412.  
  413.     if ( (found=strstr(segment,"\r\n.\r\n")))
  414.     {
  415.         buf[(found-segment)-1]='\n';
  416.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  417.         return(0);
  418.     }
  419.  
  420.     if ( (len==2) && (found=strstr(segment,"\r\n")) )
  421.     {
  422.         return(0);
  423.     }
  424.  
  425.     if ( len==1 ) /* Kludge of the century */
  426.     {
  427.         return(0);
  428.     }
  429.  
  430.     if ( (len <= 4) && (found=strstr(segment,".\r\n")) )
  431.     {
  432.         buf[found-segment]='\0'; /* Kill '.' at end of message */
  433.         return(0);
  434.     }
  435.  
  436.     return(1);
  437. }