home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC15B43.ZIP
/
POP.C
< prev
next >
Wrap
Text File
|
1997-09-30
|
33KB
|
1,126 lines
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <mem.h>
#include <conio.h>
#include <io.h>
#include <share.h>
#include <errno.h>
#include <malloc.h>
#include <dir.h>
#include "tcp.h"
#include "pop.h"
#include "version.h"
#include "retcode.h"
#define POP_PORT 110
#define SMTP_PORT 25
#define _TEMP_BUFFER_LEN 1000
#define SHARE_LEVEL 10
#define WAIT_TIME 10
#define TRIES 100
#define MT_DESQVIEW 0x01
#define MT_WINDOWS 0x02
#define MT_OS2 0x04
#define MT_NB 0x40
#define blankline(LINE) (strspn(LINE, " \n\t\r")==strlen(LINE))
#define free_Mail_Socket(SOCK) if (SOCK != NULL) { \
if ((SOCK->sock) != NULL) free(SOCK->sock); free(SOCK); SOCK=NULL; }
static int POP_Err_Cond;
static char *POP_Err_Msg = NULL;
static char _POP_err_msg_buf[100] = "";
static int SMTP_Err_Cond;
static char *SMTP_Err_Msg = NULL;
static char _SMTP_err_msg_buf[100] = "";
static char *POPLIB_Err_Msg = NULL;
char from_user[81], POPHOST[81];
int WatTCP_initialized = 0;
static char _temp_buffer[_TEMP_BUFFER_LEN];
int POP_stat;
int SMTP_stat;
int SOCK_DELAY = 60;
int multitasker = 0;
int DEBUG = 1;
char pktowner[21];
#define SOCK_READ_ERR(PROTOCOL) \
sock_err: \
if (DEBUG) { \
switch (PROTOCOL##_stat) { \
case 1 : /* foreign host closed */ \
sprintf(PROTOCOL##_Err_Msg, #PROTOCOL " Ok"); \
sprintf(POPLIB_Err_Msg, "Connection reset: %s.", \
sockerr(PROTOCOL##_sock->sock)); \
output(#PROTOCOL " > %s\n", POPLIB_Err_Msg); \
return 0; \
case -1: /* timeout */ \
sprintf(PROTOCOL##_Err_Msg, #PROTOCOL " Ok"); \
sprintf(POPLIB_Err_Msg, "Timeout: %s.", sockerr(PROTOCOL##_sock->sock));\
output(#PROTOCOL " > %s\n", POPLIB_Err_Msg); \
return 0; \
} \
}
#define SOCK_GETS(PROTOCOL) \
sock_wait_input( PROTOCOL##_sock->sock, SOCK_DELAY, NULL, \
&PROTOCOL##_stat ); \
sock_gets( PROTOCOL##_sock->sock, _temp_buffer, sizeof( _temp_buffer )); \
if (DEBUG) \
output(#PROTOCOL"> %s\n", _temp_buffer); \
strncpy(PROTOCOL##_Err_Msg, &_temp_buffer[5], 100);
#define SMTP_FAIL_ON(NUM, SHUTDOWN) \
if ( SMTP_Err_Cond == NUM ) { \
if (DEBUG) \
output("SMTP Failure > '" #NUM "'\n"); \
sock_puts( SMTP_sock->sock, "QUIT" ); \
sock_close( SMTP_sock->sock ); \
free_Mail_Socket(SMTP_sock); \
if (DEBUG) \
output("SMTP Error - '" #NUM "'", 100); \
SHUTDOWN; \
return(0); \
}
#define SMTP_FAIL_IF(COND, SHUTDOWN) \
if ( COND ) { \
if (DEBUG) \
output("SMTP Failure > (" #COND ")\n"); \
sock_puts( SMTP_sock->sock, "QUIT" ); \
sock_close( SMTP_sock->sock ); \
free_Mail_Socket(SMTP_sock); \
output("SMTP Error - (" #COND ")", 100); \
SHUTDOWN; \
return(0); \
}
#define SMTP_RESET_ON(NUM, SHUTDOWN) \
if ( SMTP_Err_Cond == NUM ) { \
if (DEBUG) \
output("SMTP Problem > '" #NUM "'\n"); \
sock_puts( SMTP_sock->sock, "RSET" ); \
sock_wait_input( SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat ); \
sock_gets( SMTP_sock->sock, _temp_buffer, sizeof( _temp_buffer )); \
if (DEBUG) \
output("SMTP Problem - '" #NUM "'", 100); \
SHUTDOWN; \
return(0); \
}
void output(char *fmt, ...)
{
va_list v;
char s[255];
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, stderr);
}
void backline(void)
{
int i;
i = wherex();
if (i < 80)
output(" ");
else
i--;
while (i-- > 0)
output("\b \b");
}
int get_dos_version(void)
{
_AX = 0x3000;
geninterrupt(0x21);
if (_AX % 256 >= 10) {
multitasker |= MT_OS2;
}
return (_AX);
}
int get_dv_version(void)
{
int v;
if (multitasker & MT_OS2)
return 0;
_AX = 0x2b01;
_CX = 0x4445;
_DX = 0x5351;
geninterrupt(0x21);
if (_AL == 0xff) {
return 0;
} else {
v = _BX;
multitasker |= MT_DESQVIEW;
return v;
}
}
int get_win_version(void)
{
int v = 0;
__emit__(0x55, 0x06, 0x53);
_AX = 0x352f;
geninterrupt(0x21);
_AX = _ES;
if (_AX | _BX) {
_AX = 0x1600;
geninterrupt(0x2f);
v = _AX;
if (v % 256 <= 1)
v = 0;
}
__emit__(0x5b, 0x07, 0x5d);
if (v != 0)
multitasker |= MT_WINDOWS;
return (v);
}
int get_nb_version(void)
{
_AX = 0;
geninterrupt(0x2A);
return (_AH);
}
void detect_multitask(void)
{
get_dos_version();
get_win_version();
get_dv_version();
if (multitasker < 2)
if (get_nb_version())
multitasker = MT_NB;
}
unsigned char *trim(char *str)
{
int i;
if (str == NULL)
return (str);
for (i = strlen(str) - 1; (i >= 0) && isspace(str[i]); str[i--] = '\0');
while (isspace(str[0]))
strcpy(str, str + 1);
return (str);
}
unsigned char *strrep(char *str, char old, char New)
{
int i;
for (i = 0; str[i]; i++)
if (str[i] == old)
str[i] = New;
return (str);
}
int getnumbers(char *ascii, unsigned int *d1, unsigned long *d2)
{
char *p;
if ((p = (char *) strchr(ascii, ' ')) == NULL)
return 0;
while (*p == ' ')
p++;
*d1 = atoi(p);
if ((p = (char *) strchr(p, ' ')) == NULL)
return 1;
while (*p == ' ')
p++;
*d2 = atol(p);
return 2;
}
char *fix_quoted_commas(char *string)
{
char *ptr;
int quoted = 0;
ptr = string;
if (ptr) {
while (*ptr != 0) {
if (*ptr == '\"')
quoted = (!quoted);
if (*ptr == ',' && quoted)
*ptr = '│';
ptr = &ptr[1];
}
}
return (string);
}
long sh_lseek(int handle, long offset, int fromwhere)
{
if (handle == -1) {
return (-1L);
}
return (lseek(handle, offset, fromwhere));
}
FILE *fsh_open(char *path, char *fmode)
{
FILE *f;
int count, share, md, fd;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
share = SH_DENYWR;
md = 0;
if (((char *) strchr(fmode, 'w')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT | O_TRUNC;
} else
if (((char *) strchr(fmode, 'a')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT;
} else {
md = O_RDONLY;
}
if (((char *) strchr(fmode, 'b')) != NULL) {
md |= O_BINARY;
}
if (((char *) strchr(fmode, '+')) != NULL) {
md &= ~O_RDONLY;
md |= O_RDWR;
share = SH_DENYRD;
}
fd = open(path, md | share, S_IREAD | S_IWRITE);
if (fd < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if ((access(path, 0)) != -1) {
delay(WAIT_TIME);
fd = open(path, md | share, S_IREAD | S_IWRITE);
while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
delay(WAIT_TIME);
count++;
fd = open(path, md | share, S_IREAD | S_IWRITE);
}
}
}
if (fd > 0) {
if (((char *) strchr(fmode, 'a')) != NULL)
sh_lseek(fd, 0L, SEEK_END);
f = fdopen(fd, fmode);
if (!f) {
close(fd);
}
} else
f = 0;
return (f);
}
Mail_Socket *smtp_start(char *host, char *dom)
{
longword h;
Mail_Socket *SMTP_sock = NULL;
if (!SMTP_Err_Msg)
SMTP_Err_Msg = _SMTP_err_msg_buf;
if (!WatTCP_initialized) {
sock_init();
WatTCP_initialized = 1;
}
if (!(h = resolve(host))) {
SMTP_Err_Cond = SMTP_FAILED;
sprintf(SMTP_Err_Msg, "Unable to resolve host %s", host);
output("\n %s", SMTP_Err_Msg);
return 0;
}
SMTP_sock = (Mail_Socket *) malloc(sizeof(Mail_Socket));
SMTP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket));
if (!tcp_open(SMTP_sock->sock, 0, h, SMTP_PORT, NULL)) {
SMTP_Err_Cond = SMTP_FAILED;
sprintf(SMTP_Err_Msg, "Unable to connect to %s", host);
if (DEBUG)
output("\nSMTP> %s", SMTP_Err_Msg);
return 0;
}
sock_mode(SMTP_sock->sock, TCP_MODE_ASCII);
sock_wait_established(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
SMTP_FAIL_ON(SMTP_OOPS,);
}
sock_printf(SMTP_sock->sock, "HELO %s", dom);
sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
SMTP_FAIL_ON(SMTP_OOPS,);
SMTP_FAIL_ON(SMTP_SYNTAX,);
SMTP_FAIL_ON(SMTP_PARAM,);
SMTP_FAIL_ON(SMTP_BAD_PARM,);
}
SOCK_READ_ERR(SMTP);
return (SMTP_sock);
}
char *smtp_parse_from_line(FILE * f)
{
int found = 0, done = 0;
rewind(f);
while (!feof(f) && !done) {
fgets(_temp_buffer, sizeof(_temp_buffer), f);
if (*_temp_buffer == '\n')
done = 1;
else
if (strncmpi(_temp_buffer, "from:", 5) == 0 &&
strchr(_temp_buffer, '@') != 0)
found = 1, done = 1;
}
if (found)
return (trim(strdup(&_temp_buffer[5])));
return 0;
}
char **smtp_parse_to_line(FILE * f)
{
int done = 0, current = 0;
char **list = NULL;
char *addr;
rewind(f);
while (!feof(f) && !done) {
fgets(_temp_buffer, sizeof(_temp_buffer), f);
if (*_temp_buffer == '\n')
done = 1;
else if ((strncmpi(_temp_buffer, "to:", 3) == 0) ||
(strncmpi(_temp_buffer, "cc:", 3) == 0) ||
(strncmpi(_temp_buffer, "bcc:", 4) == 0)) {
fix_quoted_commas(_temp_buffer);
addr = strtok(_temp_buffer, ":");
while ((addr = strtok(NULL, ",\n")) != NULL) {
strrep(addr, '│', ',');
list = (char **) realloc(list, sizeof(char *) * ((current) + 2));
list[current] = strdup(addr);
list[current + 1] = NULL;
current++;
}
}
}
return (list);
}
int smtp_send_MAIL_FROM_line(Mail_Socket * SMTP_sock, FILE * f)
{
char *from;
from = smtp_parse_from_line(f);
if (from) {
if (DEBUG)
output("\nSMTP> Mail From:<%s>\n", from);
sock_printf(SMTP_sock->sock, "MAIL FROM:<%s>", from);
free(from);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
SMTP_FAIL_ON(SMTP_OOPS,);
}
}
return 1;
SOCK_READ_ERR(SMTP);
return 0;
}
#define FREE_ALL for (i=0; to_list[i]!=NULL; i++) if (to_list[i]) free(to_list[i]); if (to_list) free(to_list);
int smtp_send_RCPT_TO_line(Mail_Socket * SMTP_sock, FILE * f)
{
char **to_list;
int i;
to_list = smtp_parse_to_line(f);
for (i = 0; to_list[i] != NULL; i++) {
if (DEBUG)
output("SMTP> Rcpt To:<%s>\n", to_list[i]);
sock_printf(SMTP_sock->sock, "RCPT TO:<%s>", to_list[i]);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
SMTP_FAIL_ON(SMTP_OOPS, FREE_ALL);
SMTP_RESET_ON(SMTP_SYNTAX, FREE_ALL);
SMTP_RESET_ON(SMTP_PARAM, FREE_ALL);
SMTP_RESET_ON(SMTP_BAD_SEQ, FREE_ALL);
}
}
FREE_ALL;
return 1;
SOCK_READ_ERR(SMTP);
return 0;
}
void go_back(int from, int to)
{
int i;
for (i = from; i > to; i--)
output("\b \b");
}
#undef FREE_ALL
int smtp_sendf(Mail_Socket * SMTP_sock, FILE * f)
{
int in_header = 1, in_bcc = 0, pos;
long nbytes, obytes, rbytes;
char *temp, ch;
if (smtp_send_MAIL_FROM_line(SMTP_sock, f) == 0)
return 0;
if (smtp_send_RCPT_TO_line(SMTP_sock, f) == 0)
return 0;
fseek(f, 0L, SEEK_END);
obytes = ftell(f);
rewind(f);
sock_puts(SMTP_sock->sock, "DATA");
sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
if (DEBUG)
output("\nSMTP> %s", _temp_buffer);
SMTP_FAIL_ON(SMTP_OOPS,);
SMTP_RESET_ON(SMTP_SYNTAX,);
SMTP_RESET_ON(SMTP_PARAM,);
SMTP_RESET_ON(SMTP_COM_NI,);
SMTP_RESET_ON(SMTP_FAILED,);
SMTP_RESET_ON(SMTP_ERROR,);
}
nbytes = 0L;
rbytes = 1024L;
output(" - ");
pos = wherex();
while (!feof(f)) {
if (kbhit()) {
ch = (getch());
switch (ch) {
case 27:
case 32:
go_back(wherex(), pos);
return -1;
default:
break;
}
}
fgets(_temp_buffer, sizeof(_temp_buffer), f);
strrep(_temp_buffer, '\n', '\0');
strrep(_temp_buffer, '\r', '\0');
trim(temp = strdup(_temp_buffer));
if (strlen(temp) == 0)
in_header = 0;
free(temp);
if (in_header && !in_bcc && strncmpi(_temp_buffer, "bcc:", 4) == 0) {
in_bcc = 1;
continue;
}
if (in_bcc)
in_bcc = isspace(*_temp_buffer);
if (in_bcc)
continue;
if (*_temp_buffer == '.') {
movmem(_temp_buffer, _temp_buffer + 1, sizeof(_temp_buffer) - 1);
*_temp_buffer = '.';
if (DEBUG)
output("\nSMTP> %s", _temp_buffer);
}
nbytes += sock_puts(SMTP_sock->sock, _temp_buffer);
nbytes += 2;
if (nbytes > rbytes) {
go_back(wherex(), pos);
output("%ld/%ld", nbytes, obytes);
rbytes += 512L;
}
}
sock_puts(SMTP_sock->sock, ".");
sock_wait_input(SMTP_sock->sock, SOCK_DELAY, NULL, &SMTP_stat);
while (sock_tbused(SMTP_sock->sock) > 0) {
SOCK_GETS(SMTP);
SMTP_FAIL_ON(SMTP_OOPS,);
SMTP_RESET_ON(SMTP_ERROR,);
SMTP_RESET_ON(SMTP_SQUEEZED,);
SMTP_RESET_ON(SMTP_FULL,);
SMTP_RESET_ON(SMTP_FAILED,);
}
go_back(wherex(), pos);
return 1;
SOCK_READ_ERR(SMTP);
go_back(wherex(), pos);
return 0;
}
int smtp_shutdown(Mail_Socket * SMTP_sock)
{
if (SMTP_sock->sock) {
sock_puts(SMTP_sock->sock, "QUIT");
sock_close(SMTP_sock->sock);
return 1;
}
free_Mail_Socket(SMTP_sock);
return 0;
}
Mail_Socket *pop_init(char *host)
{
longword h;
Mail_Socket *POP_sock = NULL;
if (!POP_Err_Msg)
POP_Err_Msg = _POP_err_msg_buf;
if (!WatTCP_initialized) {
sock_init();
WatTCP_initialized = 1;
}
if (!(h = resolve(host))) {
sprintf(POP_Err_Msg, "Unable to resolve %s.", host);
POP_Err_Cond = POP_BAD_HOST;
output("\n %s.", POP_Err_Msg);
return 0;
}
POP_sock = (Mail_Socket *) farmalloc(sizeof(Mail_Socket));
POP_sock->sock = (tcp_Socket *) farmalloc(sizeof(tcp_Socket));
if (!tcp_open(POP_sock->sock, 0, h, POP_PORT, NULL)) {
sprintf(POP_Err_Msg, "Unable to connect to host %s", host);
output("\n ■ %s.", POP_Err_Msg);
POP_Err_Cond = POP_BAD_HOST;
return 0;
}
sock_mode(POP_sock->sock, TCP_MODE_ASCII);
sock_wait_established(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
sprintf(POP_Err_Msg, "%s unavailable", host);
output("\n ■ %s.", POP_Err_Msg);
POP_Err_Cond = POP_HOST_UNAVAILABLE;
sock_puts(POP_sock->sock, "QUIT");
sock_close(POP_sock->sock);
return 0;
}
SOCK_READ_ERR(POP);
return (POP_sock);
}
int pop_login(Mail_Socket * POP_sock, char *userid, char *password)
{
sprintf(_temp_buffer, "USER %s", userid);
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
output("\n ■ Mailbox \"%s\" does not exist!", userid);
POP_Err_Cond = POP_BAD_MBOX;
if (DEBUG)
sprintf(POP_Err_Msg, "Mailbox %s does not exist", userid);
return 0;
}
sprintf(_temp_buffer, "PASS %s", password);
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
output("\n ■ Password \"%s\" incorrect or account locked.", password);
POP_Err_Cond = POP_BAD_PASS;
if (DEBUG)
sprintf(POP_Err_Msg, "Incorrect password");
return 0;
}
return 1;
SOCK_READ_ERR(POP);
return 0;
}
int pop_status(Mail_Socket * POP_sock, unsigned int *count, unsigned long *totallength)
{
sock_puts(POP_sock->sock, "STAT");
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if ((*_temp_buffer != '+') ||
(getnumbers(_temp_buffer, count, totallength) < 2)) {
POP_Err_Cond = POP_UNKNOWN;
if (DEBUG) {
sprintf(POP_Err_Msg, "Unknown POP error");
output("\n%s", POP_Err_Msg);
}
return 0;
}
return 1;
SOCK_READ_ERR(POP);
return 0;
}
long pop_length(Mail_Socket * POP_sock, unsigned int msg_num, unsigned long *size)
{
unsigned int dummy;
sprintf(_temp_buffer, "LIST %u", msg_num);
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if ((*_temp_buffer != '+') ||
(getnumbers(_temp_buffer, &dummy, size) < 2)) {
POP_Err_Cond = POP_NOT_MSG;
if (DEBUG) {
sprintf(POP_Err_Msg, "No message #%u", msg_num);
output("\n%s", POP_Err_Msg);
}
return 0;
}
return (*size);
SOCK_READ_ERR(POP);
return 0;
}
char *stristr(char *String, char *Pattern)
{
char *pptr, *sptr, *start;
unsigned int slen, plen;
for (start = String, pptr = Pattern, slen = strlen(String),
plen = strlen(Pattern); slen >= plen; start++, slen--) {
while (toupper(*start) != toupper(*Pattern)) {
start++;
slen--;
if (slen < plen)
return (NULL);
}
sptr = start;
pptr = Pattern;
while (toupper(*sptr) == toupper(*pptr)) {
sptr++;
pptr++;
if ('\0' == *pptr)
return (start);
}
}
return (NULL);
}
int pop_top(Mail_Socket * POP_sock, unsigned int msg_num)
{
int okpkt;
sprintf(_temp_buffer, "TOP %u 50", msg_num);
/* sock_printf(POP_sock->sock, "TOP %u 50\n", msg_num); */
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
POP_Err_Cond = POP_NOT_MSG;
if (DEBUG) {
sprintf(POP_Err_Msg, "No message #%u.", msg_num);
output("\n%s", POP_Err_Msg);
}
return -1;
}
okpkt = 0;
while (1) {
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
break;
if ((strnicmp(_temp_buffer, "begin 6", 7) == 0) &&
(stristr(_temp_buffer, "WINMAIL") == NULL)) {
if (okpkt != 4)
okpkt = 1;
if ((stristr(_temp_buffer, ".ZIP") != NULL) ||
(stristr(_temp_buffer, ".ARJ") != NULL) ||
(stristr(_temp_buffer, ".LZH") != NULL))
okpkt = 2;
if ((stristr(_temp_buffer, ".GIF") != NULL) ||
(stristr(_temp_buffer, ".JPG") != NULL))
okpkt = 3;
}
if (strnicmp(_temp_buffer, "from:", 5) == 0) {
if ((stristr(_temp_buffer, "mailer-daemon") != NULL) ||
(stristr(_temp_buffer, "mail delivery") != NULL) ||
(stristr(_temp_buffer, "administrator") != NULL) ||
(stristr(_temp_buffer, from_user) != NULL))
okpkt = 4;
else
strncpy(pktowner, &_temp_buffer[6], 20);
}
}
return okpkt;
SOCK_READ_ERR(POP);
return -1;
}
int pop_getf(Mail_Socket * POP_sock, char *fn, unsigned int msg_num)
{
unsigned long size;
long nbytes, rbytes;
int pos, ctld, len;
FILE *fp;
if (!pop_length(POP_sock, msg_num, &size)) {
output("\n ■ Unable to retrieve message number %d", msg_num);
return 0;
}
sprintf(_temp_buffer, "RETR %u", msg_num);
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
POP_Err_Cond = POP_NOT_MSG;
if (DEBUG) {
sprintf(POP_Err_Msg, "No message #%u", msg_num);
output("\n%s", POP_Err_Msg);
}
return 0;
}
nbytes = 0L;
rbytes = 1024L;
output(" - ");
pos = wherex();
if ((fp = fsh_open(fn, "w")) == NULL) {
output("\n ■ Unable to create %s... aborting!", fn);
return 0;
}
ctld = 1;
while (1) {
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
len = sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if ((ctld == 1) && (len == 0))
ctld = 0;
if ((strnicmp(_temp_buffer, "begin ", 6) == 0) &&
(stristr(_temp_buffer, "WINMAIL") != NULL))
ctld = 2;
if ((ctld == 2) && (strnicmp(_temp_buffer, "end", 3) == 0))
ctld = 0;
if (_temp_buffer[0] == '.' && _temp_buffer[1] == 0)
break;
if (EOF == (nbytes += fprintf(fp, "%s%s\n", ctld ? "0R" : "", _temp_buffer))) {
if (fp != NULL)
fclose(fp);
return 0;
}
if (nbytes > rbytes) {
go_back(wherex(), pos);
output("%ld/%ld", nbytes, size);
rbytes += 512L;
}
}
if (fp != NULL)
fclose(fp);
go_back(wherex(), pos);
output("message received!");
return 1;
SOCK_READ_ERR(POP);
if (fp != NULL)
fclose(fp);
return 0;
}
int pop_delete(Mail_Socket * POP_sock, unsigned int msg_num)
{
sprintf(_temp_buffer, "DELE %u", msg_num);
sock_puts(POP_sock->sock, _temp_buffer);
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (DEBUG)
output("\nPOP> %s", _temp_buffer);
if (*_temp_buffer != '+') {
POP_Err_Cond = POP_NOT_MSG;
if (DEBUG) {
sprintf(POP_Err_Msg, "No message #%u", msg_num);
output("\n%s", POP_Err_Msg);
}
return 2;
}
return 1;
SOCK_READ_ERR(POP);
return 0;
}
int pop_shutdown(Mail_Socket * POP_sock)
{
if (POP_sock->sock) {
sock_puts(POP_sock->sock, "QUIT");
sock_wait_input(POP_sock->sock, SOCK_DELAY, NULL, &POP_stat);
sock_gets(POP_sock->sock, _temp_buffer, sizeof(_temp_buffer));
if (*_temp_buffer != '+') {
POP_Err_Cond = POP_UNKNOWN;
if (DEBUG) {
sprintf(POP_Err_Msg, "Unable to update mailbox!");
output("\n%s", POP_Err_Msg);
}
return 0;
} else
output("\n ■ Closed and updated mailbox on %s.", POPHOST);
sock_close(POP_sock->sock);
return 1;
}
sock_err:
free_Mail_Socket(POP_sock);
return 0;
}
int pop_get_nextf(Mail_Socket * POP_sock, char *fn, int msgnum)
{
if (!pop_getf(POP_sock, fn, msgnum))
return 0;
return (pop_delete(POP_sock, msgnum));
}
int exist(char *s)
{
int i;
struct ffblk ff;
i = findfirst(s, &ff, FA_HIDDEN);
if (i)
return 0;
else
return 1;
}
void main(int argc, char *argv[])
{
unsigned int count;
unsigned long size;
int i, i1, okpkt;
char temp[181], s[21], s1[21];
FILE *fp;
Mail_Socket *pop_sock = NULL;
Mail_Socket *smtp_sock = NULL;
detect_multitask();
if (strncmpi(argv[1], "-send", strlen(argv[1])) == 0) {
if (argc < 5)
exit(EXIT_FAILURE);
strcpy(_SMTP_err_msg_buf, "Unknown error");
output("- %s - [Space] skips", argv[2]);
if (argc == 6)
DEBUG = atoi(argv[5]);
if ((smtp_sock = smtp_start(argv[2], argv[3])) != NULL) {
if ((fp = fsh_open(argv[4], "r")) != NULL) {
switch (smtp_sendf(smtp_sock, fp)) {
case 1:
output("message sent!");
smtp_shutdown(smtp_sock);
if (fp != NULL)
fclose(fp);
exit(EXIT_SUCCESS);
case 0:
output(" - SMTP failure!");
smtp_shutdown(smtp_sock);
if (fp != NULL)
fclose(fp);
break;
case -1:
output("SMTP aborted!");
smtp_shutdown(smtp_sock);
if (fp != NULL)
fclose(fp);
break;
}
} else {
smtp_shutdown(smtp_sock);
output("\n ■ Error accessing file %s.", argv[4]);
}
} else {
output("\n ■ SMTP socket connect failed.");
exit(EXIT_FAILURE);
}
} else if (strncmpi(argv[1], "-receive", strlen(argv[1])) == 0) {
if (argc < 8) {
output("Received: ");
for (i = 0; i < argc; i++)
output("%s ", argv[i]);
exit(EXIT_FAILURE);
}
if (argc == 9)
DEBUG = atoi(argv[8]);
sprintf(from_user, "%s@%s", argv[3], argv[7]);
strcpy(POPHOST, argv[2]);
strcpy(_POP_err_msg_buf, "Unknown error");
if ((pop_sock = pop_init(argv[2])) != NULL) {
if (pop_login(pop_sock, argv[3], argv[4])) {
if (pop_status(pop_sock, &count, &size)) {
okpkt = 0;
output("%u message%s (%ldK).\n", count, count == 1 ? "" : "s", ((size + 1023)/ 1024));
i1 = 1;
while (i1 <= count) {
okpkt = -1;
okpkt = pop_top(pop_sock, i1);
switch (okpkt) {
case -1:
output("\n ■ Error accessing message %d", i1);
break;
case 0:
i = atoi(argv[6]);
if (i == 0)
output("\n ■ Non-network message %d left on server.", i1);
else {
i = 0;
sprintf(temp, "%sUNK-%03d.MSG", argv[5], i);
while (exist(temp))
sprintf(temp, "%sUNK-%03d.MSG", argv[5], ++i);
fnsplit(temp, NULL, NULL, s, s1);
go_back(wherex(), 0);
output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "non-network packet", s, s1);
switch (pop_get_nextf(pop_sock, temp, i1)) {
case 0:
output("\n ■ Unable to retrieve message %d.", i1);
unlink(temp);
break;
case 1:
break;
case 2:
output("\n ■ Unable to delete message %d from host!", i1);
break;
}
}
break;
case 1:
i = 0;
sprintf(temp, "%sPKT-%03d.UUE", argv[5], i);
while (exist(temp))
sprintf(temp, "%sPKT-%03d.UUE", argv[5], ++i);
fnsplit(temp, NULL, NULL, s, s1);
go_back(wherex(), 0);
output(" ■ RCV : %3.3d - %-20s - %s%s", i1, pktowner, s, s1);
switch (pop_get_nextf(pop_sock, temp, i1)) {
case 0:
output("\n ■ Unable to retrieve message %d.", i1);
unlink(temp);
break;
case 1:
break;
case 2:
output("\n ■ Unable to delete message %d on host!", i1);
break;
}
break;
case 2:
i = 0;
sprintf(temp, "%sARC-%03d.UUE", argv[5], i);
while (exist(temp))
sprintf(temp, "%sARC-%03d.UUE", argv[5], ++i);
fnsplit(temp, NULL, NULL, s, s1);
go_back(wherex(), 0);
output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "archived file", s, s1);
switch (pop_get_nextf(pop_sock, temp, i1)) {
case 0:
output("\n ■ Unable to retrieve message %d.", i1);
unlink(temp);
break;
case 1:
break;
case 2:
output("\n ■ Unable to delete message %d on host!", i1);
break;
}
break;
case 3:
i = 0;
sprintf(temp, "%sGIF-%03d.UUE", argv[5], i);
while (exist(temp))
sprintf(temp, "%sGIF-%03d.UUE", argv[5], ++i);
fnsplit(temp, NULL, NULL, s, s1);
go_back(wherex(), 0);
output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "graphic/image file", s, s1);
switch (pop_get_nextf(pop_sock, temp, i1)) {
case 0:
output("\n ■ Unable to retrieve message %d.", i1);
unlink(temp);
break;
case 1:
break;
case 2:
output("\n ■ Unable to delete message %d from host!", i1);
break;
}
break;
case 4:
i = 0;
sprintf(temp, "%sBAD-%03d.UUE", argv[5], i);
while (exist(temp))
sprintf(temp, "%sBAD-%03d.UUE", argv[5], ++i);
fnsplit(temp, NULL, NULL, s, s1);
go_back(wherex(), 0);
output(" ■ RCV : %3.3d - %-20s - %s%s", i1, "mailer-daemon", s, s1);
switch (pop_get_nextf(pop_sock, temp, i1)) {
case 0:
output("\n ■ Unable to retrieve message %d.", i1);
unlink(temp);
break;
case 1:
break;
case 2:
output("\n ■ Unable to delete message %d from host!", i1);
break;
}
break;
}
i1++;
}
backline();
output("\r ■ Mailbox scan of %d message%s completed.", count,
count == 1 ? "" : "s");
} else
output("\n ■ Unknown POP access error - try again later.");
pop_shutdown(pop_sock);
fcloseall();
exit(EXIT_SUCCESS);
} else {
output("\n ■ Unable to log into POP server!");
pop_shutdown(pop_sock);
fcloseall();
}
} else
output("\n ■ POP socket connect failed.");
}
exit(EXIT_FAILURE);
}