home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / net / amitcp_pop / source / pop_dopop.c.old < prev    next >
Encoding:
Text File  |  1993-12-21  |  5.9 KB  |  423 lines

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