home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / net / amitcp_pop / source / pop_dopop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  6.3 KB  |  449 lines

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