/* Copyright 1999 Scott Auge */ /* http://www.amduus.com */ /* Released under Revised BSD License */ /* Read pop3 mail boxes from command line */ #include #include #include #include #include #include #include #include #include #include #ifdef AIX #include #else #include #endif /* #define DEBUG */ #ifdef DEBUG #define DBUG(x) puts ((x)); #else #define DBUG(x) #endif /* Reads server reply from the socket connection established in main () */ int readserver (int sfd, int out) { char bptr [255]; int readreturn; int gotdata = 0; while (1) { memset (bptr, 0x00, sizeof(bptr)); readreturn = read (sfd, &bptr, sizeof(bptr) ); if (readreturn <= 0 && gotdata == 1) break; if (readreturn > 0) gotdata = 1; if (out) puts (bptr); } return 0; } /* readserver () */ int main (int argc, char **argv) { int sfd; struct hostent *hostaddr; struct sockaddr_in sin; unsigned short port = 110; extern int errno; char *Machine = argv[1]; char *User = argv[2]; char *PassWord = argv[3]; char *Command = argv[4]; char *Message = argv[5]; char *Length = argv[6]; char Protocol [255]; if (argc < 4) { puts ("pop3 [machine name] [user] [password] [command] [msg]"); puts ("Copyright 1999 Scott Auge http://www.sauge.com"); puts ("Code placed in the public domain"); puts ("machine name - name of host and domain"); puts ("user - account holder"); puts ("password - password of acct holder"); puts ("command - list for list of messages"); puts (" - retr to retrieve message [msg]"); puts (" - dele to delete message [msg]"); puts (" - top [Length] top Length lines of [msg]"); return 0; } sfd = socket (PF_INET, SOCK_STREAM, 0); if (sfd < 0) { DBUG ("Socket Creation Failed...\n") return 0; } else DBUG ("Socket Successful\n") DBUG ("Looking up \n") if (( hostaddr = gethostbyname (Machine) ) == NULL ) { DBUG ("Host name lookup failed...\n") return 0; } else DBUG ( "Host successful\n") bzero((char *)&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(port); bcopy (hostaddr->h_addr, (char *)&sin.sin_addr, hostaddr->h_length); if ( connect ( sfd, (struct sockaddr *)&sin, sizeof(sin) ) < 0) { DBUG ("Connection failed...\n") return errno; } else DBUG ("Connection successful\n") DBUG ("Gonna login to server\n") if (fcntl (sfd, F_SETFL, O_NONBLOCK | O_SYNC) == -1) puts ("fcntl failed"); memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "USER "); strcat (Protocol, User); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); readserver (sfd, 0); memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "PASS "); strcat (Protocol, PassWord); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); readserver (sfd, 0); DBUG ("Gonna issue command to server\n") if (strcmp (Command, "list") == 0) { memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "LIST "); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); } else if (strcmp(Command, "retr") == 0) { memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "RETR "); strcat (Protocol, Message); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); } else if (strcmp(Command, "dele") == 0) { memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "DELE "); strcat (Protocol, Message); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); } else if (strcmp(Command, "top") == 0) { memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "TOP "); strcat (Protocol, Message); strcat (Protocol, " "); strcat (Protocol, Length); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); } DBUG ("Gonna receive results\n") sleep (5); readserver (sfd, 1); DBUG ("Gonna logout results\n") memset (Protocol, 0x00, sizeof(Protocol)); strcat (Protocol, "QUIT "); strcat (Protocol, "\n"); DBUG(Protocol) write (sfd, Protocol, strlen(Protocol)); close (sfd); return 0; }