/* Libpop3 - (C)opyright 2000-2001 Kristian Van Der Vliet This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA */ #include /* sprintf() */ #include #include #include #include #include #include #include #include #include #include #include #ifdef __POP3_DEBUG # include #endif /* __POP3_DEBUG */ /* Static functions */ static char *itoa(int nInteger); static int extract_header (char **header, char *source, char *name); static char *stripltwhite(char *nString); static char *strstrcase(const char *cs, const char *ct); /* Socket *has* to be a global */ int __pop3GlobalSocket; /* Timeout must also be global */ unsigned long __pop3Timeout=30; unsigned char __pop3TimedOut=0; /* 0==FALSE 1==TRUE */ #define __POP3_ALARM_SET \ signal(SIGALRM, pop3_alarm); \ (void) alarm(__pop3Timeout); #define __POP3_ALARM_CLEAR \ signal(SIGALRM, SIG_DFL); \ (void) alarm(0); int pop3_clear_buf(int nSocket); void pop3_alarm(int unused); int pop3_connect(const char* zServer, int nPort) { /* Set up the socket structures */ struct sockaddr_in sSocketAddr; /* Socket connection details */ struct hostent* sServer; /* Host details */ char nRecvBuf[3]; /* Receive buffer */ int nBytesRecv; /* Number of bytes read by recv */ #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Server is \"%s\", port %i\n",zServer,nPort); #endif /* __POP3_DEBUG */ /* Lookup the hostname of the server */ sServer=gethostbyname(zServer); if(sServer==NULL) return(-POP3_UNKNOWN_SERVER); #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: IP address %s\n",inet_ntoa(*((struct in_addr *)sServer->h_addr))); #endif /* __POP3_DEBUG */ /* Now attempt to open a socket to the server */ __pop3GlobalSocket=socket(PF_INET,SOCK_STREAM,0); sSocketAddr.sin_family=AF_INET; sSocketAddr.sin_port=htons(nPort); /* FIXME: Don't even ask. The code is ugly. It does it ass */ /* backwards. It sucks. But it works. Want to fix it? */ /* Please do :) */ if(inet_aton(inet_ntoa(*((struct in_addr *)sServer->h_addr)),&(sSocketAddr.sin_addr))==0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Connect failed at inet_ntoa()\n"); #endif /* __POP3_DEBUG */ return(-POP3_CONNECT_FAILED); } memset(&(sSocketAddr.sin_zero),'\0',8); /* Set the timeout alarm */ __POP3_ALARM_SET if(connect(__pop3GlobalSocket, (struct sockaddr*)&sSocketAddr, sizeof(struct sockaddr))==-1) { #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Connect failed at connect()\n"); #endif /* __POP3_DEBUG */ return(-POP3_CONNECT_FAILED); } /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Connected\n"); #endif /* __POP3_DEBUG */ /* We should now have a socket connection to the server, on the requested port The POP3 server should send a +OK response */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) { #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Server did not respond correctly.\n"); #endif /* __POP3_DEBUG */ return(-POP3_CONNECT_FAILED); } /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_connect: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_CONNECT_FAILED); } /* Clear out the rest of the buffer */ pop3_clear_buf(__pop3GlobalSocket); /* Thats all we have to do here */ return(POP3_OK); } int pop3_login(const char* zUsername, const char* zPassword) { /* Send the user & pass combo to actually login to the server */ int nLength; /* Used to calculate line lengths etc */ int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ char nRecvBuf[3]; /* Return code recieve buffer */ char *nUserMsg, *nPassMsg; /* User & Pass messages */ #ifdef __POP3_DEBUG printf("libpop3: pop3_login: Logging in\n"); #endif /* __POP3_DEBUG */ /* Build the User line */ nLength=strlen(zUsername); nLength+=8; /* 8 is the length of the string "USER " + \r\n\0 */ nUserMsg=(char*)malloc(nLength); nUserMsg=strcpy(nUserMsg,"USER "); nUserMsg=strcat(nUserMsg,zUsername); nUserMsg=strcat(nUserMsg,"\r\n\0"); /* Send the USER line */ nBytesSend=send(__pop3GlobalSocket,nUserMsg,strlen(nUserMsg),0); /* If we're O.K, server should respond with a +OK */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_login: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_login: USER failed\n"); printf("libpop3: pop3_login: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_INVALID_USER_PASS); } /* Clear out the rest of the buffer */ pop3_clear_buf(__pop3GlobalSocket); #ifdef __POP3_DEBUG printf("libpop3: pop3_login: User OK\n"); #endif /* __POP3_DEBUG */ /* Now send the password */ /* Build the Pass line */ nLength=strlen(zPassword); nLength+=8; /* 8 is the length of the string "PASS " + \r\n\0 */ nPassMsg=(char*)malloc(nLength); nPassMsg=strcpy(nPassMsg,"PASS "); nPassMsg=strcat(nPassMsg,zPassword); nPassMsg=strcat(nPassMsg,"\r\n\0"); /* Send the PASS line */ nBytesSend=send(__pop3GlobalSocket,nPassMsg,strlen(nPassMsg),0); /* If we're O.K, server should respond with a +OK */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_login: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_login: PASS failed\n"); printf("libpop3: pop3_login: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_INVALID_USER_PASS); } /* Clear out the rest of the buffer */ pop3_clear_buf(__pop3GlobalSocket); #ifdef __POP3_DEBUG printf("libpop3: pop3_login: Pass OK\n"); printf("libpop3: pop3_login: Logged in\n"); #endif /* __POP3_DEBUG */ /* We are now logged in & ready to use the server */ return(POP3_OK); } int pop3_getmailcount(void) { /* Get the mail count from the server with the STAT command */ int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ int nByteCount,nMailCount; /* Loop counter & message count */ char nRecvBuf[5]; /* Return code recieve buffer */ char* nMailCountBuf; /* Mail count buffer. */ /* Send the STAT command first */ nBytesSend=send(__pop3GlobalSocket,"STAT\r\n",6,0); /* Now get the response. First part should be normal +OK, followed by the mail count, then the total mail size in bytes */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_getmailcount: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_getmailcount: STAT failed\n"); printf("libpop3: pop3_getmailcount: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_GENERAL_FAILURE); } /* Now get the mail count */ /* Goble the leading space */ nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,1,0); /* Now keep getting a byte at the time until the next whitespace */ /* Note that we only go upto 4 characters here. Honestly, if your POP3 mailbox has more than 9999 messages waiting, you have bigger problems! */ nMailCountBuf=(char*)malloc(4); nMailCountBuf=(char*)memset(nMailCountBuf,0,4); for(nByteCount=0;nByteCount<3;nByteCount++) { nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,1,0); if(nRecvBuf[0]==32) /* If we've hit the whitespace */ break; /* Come out of the loop */ nMailCountBuf[nByteCount]=nRecvBuf[0]; /* Otherwise, append the character to the buffer */ } /* Terminate the mailcount buffer */ nMailCountBuf=strcat(nMailCountBuf,"\0"); /* Clear the rest of the buffer upto the CRLF*/ for(;;) { recv(__pop3GlobalSocket,(void*)&nRecvBuf,1,0); if(nRecvBuf[0]=='\n') { break; } } /* Convert the string into a proper integer */ nMailCount=atoi(nMailCountBuf); #ifdef __POP3_DEBUG printf("libpop3: pop3_getmailcount: Mailcount is %i\n",nMailCount); #endif /* __POP3_DEBUG */ /* Done, return the mail count integer */ return(nMailCount); } int pop3_getmailsize(int nMessageNo) { /* Get the message size in bytes from the LIST command */ int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ int nCurrentMsgLine,nByteCount; /* Loop counters */ int nMailSize; /* Int for size of mail in bytes */ char* nMailSizeBuf; /* Mail size buffer. */ char nRecvBuf[5]; /* Return code recieve buffer */ /* Send the LIST command first */ nBytesSend=send(__pop3GlobalSocket,"LIST\r\n",6,0); /* Now get the response. First part should be normal +OK, followed by the mail count, then the total mail size in bytes. Each line after that is the size of each mail in bytes */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_getmailsize: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_getmailsize: LIST failed\n"); printf("libpop3: pop3_getmailsize: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_GENERAL_FAILURE); } /* Clear the rest of the line from the buffer */ pop3_clear_buf(__pop3GlobalSocket); /* Now get each line until we get the line we want, bearing in mind that the messages start at 1, not 0!*/ if(nMessageNo<1) return(-POP3_INVALID_MESSAGE_NUM); for(nCurrentMsgLine=1;nCurrentMsgLinenMessageSize) { #ifdef __POP3_DEBUG printf("libpop3: pop3_getmail: Reallocating"); #endif /* __POP3_DEBUG */ nMessageSize+=32; /* Just a guess */ nMsgBuf=(char*)realloc((char*)nMsgBuf,nMessageSize); } }while(nEndFound==0); #ifdef __POP3_DEBUG printf("libpop3: pop3_getmail: Got mail, %i bytes\n",nBytesDone); #endif /* __POP3_DEBUG */ /* Terminate the message buffer & free the part buffer */ nMsgBuf[nBytesDone]='\0'; /* Process the mail */ #ifdef __POP3_DEBUG printf("libpop3: pop3_getmail: Extracting headers\n"); #endif /* __POP3_DEBUG */ /* Set each header in the pop3_mail structure to be NULL */ mail->zTo=0; mail->zCc=0; mail->zBcc=0; mail->zReplyTo=0; mail->zSubject=0; mail->zMessageId=0; mail->zFrom=0; mail->zSender=0; mail->zDate=0; mail->zBody=0; mail->zContent=0; mail->zEncoding=0; mail->zInReplyTo=0; /* Now parse the message headers into the pop3_mail struct */ /* Get the following headers: To: Cc: Bcc: ReplyTo: Subject: MessageID: From: Sender: Date: Content-Type: Content-Transfer-Encoding: In-Reply-To: Body: - This is actually the whole message */ /* Properly terminate the buffer */ nMsgBuf=strcat(nMsgBuf,"\0"); extract_header(&mail->zTo,nMsgBuf,"\nTo:"); extract_header(&mail->zCc,nMsgBuf,"\nCc:"); extract_header(&mail->zBcc,nMsgBuf,"\nBcc:"); extract_header(&mail->zReplyTo,nMsgBuf,"\nReply-To:"); extract_header(&mail->zSubject,nMsgBuf,"\nSubject:"); extract_header(&mail->zMessageId,nMsgBuf,"\nMessage-ID:"); extract_header(&mail->zFrom,nMsgBuf,"\nFrom:"); extract_header(&mail->zSender,nMsgBuf,"\nSender:"); extract_header(&mail->zDate,nMsgBuf,"\nDate:"); extract_header(&mail->zContent,nMsgBuf,"\nContent-Type:"); extract_header(&mail->zEncoding,nMsgBuf,"\nContent-Transfer-Encoding:"); extract_header(&mail->zInReplyTo,nMsgBuf,"\nIn-Reply-To:"); mail->zBody=nMsgBuf; /* For debugging purposes, print out the headers */ #ifdef __POP3_DEBUG printf("libpop3: pop3_getmail: To=%s\n",mail->zTo); printf("libpop3: pop3_getmail: Cc=%s\n",mail->zCc); printf("libpop3: pop3_getmail: Bcc=%s\n",mail->zBcc); printf("libpop3: pop3_getmail: ReplyTo=%s\n",mail->zReplyTo); printf("libpop3: pop3_getmail: Subject=%s\n",mail->zSubject); printf("libpop3: pop3_getmail: MessageID=%s\n",mail->zMessageId); printf("libpop3: pop3_getmail: From=%s\n",mail->zFrom); printf("libpop3: pop3_getmail: Sender=%s\n",mail->zSender); printf("libpop3: pop3_getmail: Date=%s\n",mail->zDate); printf("libpop3: pop3_getmail: Content-Type=%s\n",mail->zContent); printf("libpop3: pop3_getmail: Content-Transfer-Encoding=%s\n", mail->zEncoding); printf("libpop3: pop3_getmail: In-Reply-To=%s\n",mail->zInReplyTo); #endif /* __POP3_DEBUG */ /* Return the completed message */ /* return(mail); */ return POP3_OK; /* Nepto [21/10/2002] */ } /* * Free the data from the mail structure */ void pop3_freemail(struct pop3_mail *mail) { if(mail->zTo!=0) free(mail->zTo); if(mail->zCc!=0) free(mail->zCc); if(mail->zBcc!=0) free(mail->zBcc); if(mail->zReplyTo!=0) free(mail->zReplyTo); if(mail->zSubject!=0) free(mail->zSubject); if(mail->zBody!=0) free(mail->zBody); if(mail->zMessageId!=0) free(mail->zMessageId); if(mail->zFrom!=0) free(mail->zFrom); if(mail->zSender!=0) free(mail->zSender); if(mail->zDate!=0) free(mail->zDate); if(mail->zContent!=0) free(mail->zContent); if(mail->zEncoding!=0) free(mail->zEncoding); if(mail->zInReplyTo!=0) free(mail->zInReplyTo); /* Not implemented? if(mail->zHeaders!=0) free(mail->zHeaders); */ } int pop3_deletemail(int nMessageNo) { /* Delete the mail with the DELE command */ int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ char nRecvBuf[3]; /* Return code recieve buffer */ char nDeleMsg[25]; /* DELE message to send */ char* nIntString; /* Integer converted to char[] */ /* Allocate the IntegerString */ nIntString=(char*)malloc(3); nIntString=(char*)memset(nIntString,'\0',3); /* Build the message */ nIntString=itoa(nMessageNo); sprintf(nDeleMsg, "DELE %u\r\n", nMessageNo); /* Send the DELE command */ nBytesSend=send(__pop3GlobalSocket,nDeleMsg,strlen(nDeleMsg),0); /* Now get the response. First part should be normal +OK, followed by the mail count, then the total mail size in bytes */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_deletemail: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_deletemail: DELE failed\n"); printf("libpop3: pop3_deletemail: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ return(-POP3_DELE_FAILED); } /* Clear the rest of the buffer */ pop3_clear_buf(__pop3GlobalSocket); /* Message is deleted */ #ifdef __POP3_DEBUG printf("libpop3: pop3_deletemail: Deleted mail %i\n",nMessageNo); #endif /* __POP3_DEBUG */ return(POP3_OK); } int pop3_disconnect(void) { /* Disconnect from the POP3 server with QUIT */ int nBytesRecv,nBytesSend; /* Returns from send() & recv() */ char nRecvBuf[3]; /* Return code recieve buffer */ /* Send the QUIT message */ nBytesSend=send(__pop3GlobalSocket,"QUIT\r\n",6,0); /* Server should respond with a +OK again */ /* Set the timeout alarm */ __POP3_ALARM_SET nBytesRecv=recv(__pop3GlobalSocket,(void*)&nRecvBuf,3,0); if(nBytesRecv<3) #ifdef __POP3_DEBUG printf("libpop3: pop3_disconnect: Recieved less than 3 bytes\n"); #endif /* __POP3_DEBUG */ /* Clear the timeout alarm & see if we timedout */ __POP3_ALARM_CLEAR if(__pop3TimedOut==1) return(-POP3_TIMED_OUT); nRecvBuf[3]='\0'; /* Terminate after the first 3 bytes, which will be just the return code, as we use strcmp on the buffer */ if(strcmp(nRecvBuf,"+OK")!=0) { #ifdef __POP3_DEBUG printf("libpop3: pop3_disconnect: QUIT failed\n"); printf("libpop3: pop3_disconnect: Server responded %s\n",nRecvBuf); #endif /* __POP3_DEBUG */ /* Close the socket our end anyway */ close(__pop3GlobalSocket); return(-POP3_GENERAL_FAILURE); } /* Clear out the rest of the buffer */ pop3_clear_buf(__pop3GlobalSocket); /* Close the socket */ close(__pop3GlobalSocket); /* We are now disconnected */ #ifdef __POP3_DEBUG printf("libpop3: pop3_disconnect: QUIT succeded\n"); #endif /* __POP3_DEBUG */ return(POP3_OK); } int pop3_set_timeout(unsigned long nTimeout) /* Set a new timeout value for libpop3 */ { __pop3Timeout=nTimeout; return(POP3_OK); } int pop3_clear_buf(int nSocket) /* Read through the buffer one byte at a time until we hit a CRLF */ { char nBufByte; /* Set the timeout alarm */ __POP3_ALARM_SET while(recv(nSocket,(void*)&nBufByte,1,0)>0) { if(nBufByte=='\r') { recv(nSocket,(void*)&nBufByte,1,0); __POP3_ALARM_CLEAR if(__pop3TimedOut==1) /* See if we timed out */ break; return(POP3_OK); } } /* The only time this could occure is if the alarm hit in, so */ return(-1); } void pop3_alarm(int unused) /* Callback function for alarm() */ { #ifdef __POP3_DEBUG printf("libpop3: pop3_alarm: Timed out!\n"); #endif __pop3TimedOut=1; return; } /* * STATIC FUNCTIONS */ char *itoa(int nInteger) { char *nString=(char*)malloc(7); nString=(char*)memset(nString,'\0',7); sprintf(nString,"%i",nInteger); return(nString); } int extract_header (char **header, char *source, char *name) /* int extract_header (char **header, char *source, char *name); Extracts the first occurance of the header labeled 'name' in 'source' and puts it in 'header'. NOTE: You need to free() 'header' yourself. RETURNS: 0 - Success 1 - 'name' not found 2 - Header error 3 - Grave error */ { char *start = NULL; unsigned int i = 0; /* Find the first location of 'name' */ /* KV: Written own strstrcase() function, below */ start = strstrcase (source, name); if (!start) return (1); /* Advance 'start' to point at the actual text itself instead of 'name' */ start += strlen (name) + 1; /* Allocate some mem for header */ *header = malloc (strlen (source) + 1); if (!(*header)) return (3); while (*start) { if (*start == '\r') { (*header)[i] = ' '; i++; start++; continue; } if ((*start == '\n') && (*++start != ' ')) { (*header)[i] = '\0'; break; } (*header)[i] = *start; i++; start++; } /* KV: Replaced g_strstrip with stripltwhite() below, avioid glib depend */ *header = stripltwhite(*header); /* KV: Terminate the string. This should really be done in stripltwhite, but i'm lazy & can do it here */ (*header)[i]='\0'; if (i == (strlen (start) - 1)) { /* We reached the end of the header string */ /* This should only occur if we are viewing a message in the Outbox/Sent Items */ /* because they don't have complete headers in Treehouse */ return (2); } return (0); } char *stripltwhite(char *nString) /* Strip the leading & trailing whitespace from a string */ { /* Start at the top first */ int nCurrent,nLength,nStart,nEnd,nStripedLength; char* pzOutputBuffer; /* First of all, find the start of the string after the initial whitespace */ nLength=strlen(nString); nCurrent=0; while(isspace(nString[nCurrent])!=0 && nCurrent!=nLength) nCurrent++; nStart=nCurrent; /* Now find the "end" of the string */ nCurrent=nLength; while(isspace(nString[nCurrent])!=0 && nCurrent!=nStart) --nCurrent; nEnd=nCurrent; /* Calculate the length of the stripped string */ nStripedLength=nEnd-nStart; /* Now copy out the middle of the string into the output string */ pzOutputBuffer=(char*)malloc(nStripedLength+1); for(nCurrent=nStart;nCurrent int main(int argc, char *argv[]) { register int port = POP3_PORT; char *server = "localhost"; char *username = NULL; char *password = NULL; int nMailCount=0; /* Number of mails on server */ int nMailSize=0; /* Size of first email in bytes */ if (argc < 2) { fprintf(stderr, "Usage: %s " " [ [ ]]\n", argv[0]); return 1; } if (argc > 2) { server = argv[2]; } if (argc > 3) { port = atoi(argv[3]); if (port == 0) port = POP3_PORT; } printf("Connecting to server %s:%d\n", server, port); if (pop3_connect(server, port) != 0) { printf("Error: Could not connect to server\n"); return(1); } username = argv[1]; if ((password = strchr(username, ':')) == NULL) { password = getpass("POP3 password: "); } else { *password = '\0'; password++; } printf("Logging in\n"); if (pop3_login(username, password) != 0) { printf("Error: Could not login to server\n"); /* Attempt to disconnect */ pop3_disconnect(); return 1; } printf("Logged in\nGetting number of mails\n"); nMailCount = pop3_getmailcount(); if (nMailCount < 0) { printf("Error: Could not get mail count\n"); /* Attempt to disconnect */ pop3_disconnect(); return(1); } printf("There are %i messages on the server %s:%d\n", nMailCount, server, port); if (nMailCount == 0) { printf("No mail to download\n"); } else { struct pop3_mail sMailMessage; /* The mail we download */ /* Get mail 1 */ nMailSize = pop3_getmailsize(1); if (pop3_getmail(1, nMailSize, &sMailMessage)) { printf("Error: Could not get mail no. 1\n"); /* Attempt to disconnect */ pop3_disconnect(); return(1); } /* Print some headers */ printf("To: %s\n",sMailMessage.zTo); printf("From: %s\n",sMailMessage.zFrom); printf("Subject: %s\n",sMailMessage.zSubject); pop3_freemail(&sMailMessage); /* Disconnect from the server */ printf("Disconnecting from server\n"); } pop3_disconnect(); return 0; } #endif