2020-06-23 02:40:32 +00:00
|
|
|
/////////////////////////////////////////////////////////////////
|
|
|
|
// POP65
|
|
|
|
// Post Office Protocol v3 (POP3) Client for IP65
|
2020-06-22 02:21:26 +00:00
|
|
|
// https://www.ietf.org/rfc/rfc1939.txt
|
2020-06-23 02:40:32 +00:00
|
|
|
// (Based on IP65's wget65.c)
|
|
|
|
// Bobbi June 2020
|
|
|
|
/////////////////////////////////////////////////////////////////
|
2020-06-22 02:21:26 +00:00
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
#include <dio.h>
|
2020-06-22 02:21:26 +00:00
|
|
|
#include <cc65.h>
|
2020-06-23 02:40:32 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2020-06-22 02:21:26 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2020-06-23 02:40:32 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <device.h>
|
|
|
|
|
2020-06-22 02:21:26 +00:00
|
|
|
#include "../inc/ip65.h"
|
2020-06-23 02:40:32 +00:00
|
|
|
#include "w5100.h"
|
|
|
|
#include "w5100_http.h"
|
|
|
|
#include "linenoise.h"
|
2020-06-22 02:21:26 +00:00
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
// Both pragmas are obligatory to have cc65 generate code
|
|
|
|
// suitable to access the W5100 auto-increment registers.
|
|
|
|
#pragma optimize (on)
|
|
|
|
#pragma static-locals (on)
|
2020-06-22 02:21:26 +00:00
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
#define FILENAME "/H1/DOCUMENTS/EMAIL.TXT"
|
2020-06-22 02:21:26 +00:00
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
unsigned char buf[1500+1]; // One extra byte for null terminator
|
2020-06-22 02:21:26 +00:00
|
|
|
int len;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
// Configuration params from POP65.CFG
|
|
|
|
char cfg_server[80];
|
|
|
|
char cfg_user[80];
|
|
|
|
char cfg_pass[80];
|
|
|
|
|
|
|
|
void confirm_exit(void)
|
|
|
|
{
|
|
|
|
printf("\nPress any key ");
|
|
|
|
cgetc();
|
2020-06-23 02:40:32 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void error_exit()
|
|
|
|
{
|
|
|
|
confirm_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ip65_error_exit(void)
|
|
|
|
{
|
|
|
|
printf("- %s\n", ip65_strerror(ip65_error));
|
|
|
|
confirm_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_strip_crlf(char *s) {
|
|
|
|
uint8_t i = 0;
|
|
|
|
while ((s[i] != '\0') && (s[i] != '\r') && (s[i] != '\n'))
|
|
|
|
putchar(s[i++]);
|
|
|
|
putchar('\n');
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
// Modified verson of w5100_http_open from w5100_http.c
|
|
|
|
// Sends a TCP message and receives a the first packet of the response.
|
|
|
|
// sendbuf is the buffer to send (null terminated)
|
|
|
|
// recvbuf is the buffer into which the received message will be written
|
|
|
|
// length is the length of recvbuf[]
|
|
|
|
// dosend Do the sending if true, otherwise skip
|
|
|
|
// binary Binary mode for received message, maybe first block of long message
|
|
|
|
bool w5100_tcp_send_recv(char* sendbuf, char* recvbuf, size_t length,
|
|
|
|
uint8_t dosend, uint8_t binary) {
|
|
|
|
|
|
|
|
uint8_t i;
|
|
|
|
|
|
|
|
if (dosend) {
|
|
|
|
uint16_t snd;
|
|
|
|
uint16_t pos = 0;
|
|
|
|
uint16_t len = strlen(sendbuf);
|
|
|
|
|
|
|
|
if (strncmp(sendbuf, "PASS", 4) == 0)
|
|
|
|
printf(">PASS ****\n");
|
|
|
|
else {
|
|
|
|
putchar('>');
|
|
|
|
print_strip_crlf(sendbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len) {
|
|
|
|
if (input_check_for_abort_key())
|
|
|
|
{
|
|
|
|
printf("- User abort\n");
|
|
|
|
w5100_disconnect();
|
|
|
|
return false;
|
2020-06-22 03:01:22 +00:00
|
|
|
}
|
2020-06-23 02:40:32 +00:00
|
|
|
|
|
|
|
snd = w5100_send_request();
|
|
|
|
if (!snd) {
|
|
|
|
if (!w5100_connected()) {
|
|
|
|
printf("- Connection lost\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
continue;
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
2020-06-23 02:40:32 +00:00
|
|
|
|
|
|
|
if (len < snd)
|
|
|
|
snd = len;
|
|
|
|
|
|
|
|
{
|
|
|
|
// One less to allow for faster pre-increment below
|
|
|
|
const char *dataptr = sendbuf + pos - 1;
|
|
|
|
uint16_t i;
|
|
|
|
for (i = 0; i < snd; ++i) {
|
|
|
|
// The variable is necessary to have cc65 generate code
|
|
|
|
// suitable to access the W5100 auto-increment register.
|
|
|
|
char data = *++dataptr;
|
|
|
|
*w5100_data = data;
|
|
|
|
}
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
2020-06-23 02:40:32 +00:00
|
|
|
|
|
|
|
w5100_send_commit(snd);
|
|
|
|
len -= snd;
|
|
|
|
pos += snd;
|
|
|
|
}
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
if (binary) {
|
|
|
|
//
|
|
|
|
// Handle a sequence of data packets
|
|
|
|
//
|
|
|
|
uint16_t rcv;
|
|
|
|
uint16_t len = 0;
|
|
|
|
uint32_t size = 0;
|
|
|
|
uint8_t cont = 1;
|
|
|
|
|
|
|
|
while(cont) {
|
|
|
|
if (input_check_for_abort_key()) {
|
|
|
|
printf("- User abort\n");
|
|
|
|
w5100_disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcv = w5100_receive_request();
|
|
|
|
if (!rcv) {
|
|
|
|
cont = w5100_connected();
|
|
|
|
if (cont)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rcv > length - len) {
|
|
|
|
rcv = length - len;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// One less to allow for faster pre-increment below
|
|
|
|
char *dataptr = recvbuf + len - 1;
|
|
|
|
uint16_t i;
|
|
|
|
for (i = 0; i < rcv; ++i) {
|
|
|
|
// The variable is necessary to have cc65 generate code
|
|
|
|
// suitable to access the W5100 auto-increment register.
|
|
|
|
char data = *w5100_data;
|
|
|
|
*++dataptr = data;
|
|
|
|
|
|
|
|
if (!memcmp(dataptr - 4, "\r\n.\r\n", 5))
|
|
|
|
cont = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w5100_receive_commit(rcv);
|
|
|
|
len += rcv;
|
|
|
|
|
|
|
|
// TODO WRITE TO DISK HERE
|
|
|
|
size += len;
|
|
|
|
printf("Size is %ld\n", size);
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Handle short single packet ASCII text responses
|
|
|
|
//
|
|
|
|
uint16_t rcv;
|
|
|
|
uint16_t len = 0;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if (input_check_for_abort_key()) {
|
|
|
|
printf("- User abort\n");
|
|
|
|
w5100_disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcv = w5100_receive_request();
|
|
|
|
if (rcv)
|
|
|
|
break;
|
|
|
|
if (!w5100_connected()) {
|
|
|
|
printf("Connection lost\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rcv > length - len) {
|
|
|
|
rcv = length - len;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// One less to allow for faster pre-increment below
|
|
|
|
char *dataptr = recvbuf + len - 1;
|
|
|
|
uint16_t i;
|
|
|
|
for (i = 0; i < rcv; ++i) {
|
|
|
|
// The variable is necessary to have cc65 generate code
|
|
|
|
// suitable to access the W5100 auto-increment register.
|
|
|
|
char data = *w5100_data;
|
|
|
|
*++dataptr = data;
|
|
|
|
}
|
|
|
|
w5100_receive_commit(rcv);
|
|
|
|
len += rcv;
|
|
|
|
}
|
|
|
|
putchar('<');
|
|
|
|
print_strip_crlf(recvbuf);
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
2020-06-23 02:40:32 +00:00
|
|
|
return true;
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
void expect(char *buf, char *s) {
|
|
|
|
if (strncmp(buf, s, strlen(s)) != 0) {
|
|
|
|
printf("\nExpected '%s' got '%s\n", s, buf);
|
2020-06-22 02:21:26 +00:00
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void readconfigfile(void) {
|
|
|
|
fp = fopen("POP65.CFG", "r");
|
|
|
|
if (!fp) {
|
|
|
|
puts("Can't open config file POP65.CFG");
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
fscanf(fp, "%s", cfg_server);
|
|
|
|
fscanf(fp, "%s", cfg_user);
|
|
|
|
fscanf(fp, "%s", cfg_pass);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
2020-06-23 02:40:32 +00:00
|
|
|
char *arg;
|
2020-06-22 02:21:26 +00:00
|
|
|
uint8_t eth_init = ETH_INIT_DEFAULT;
|
|
|
|
char sendbuf[80];
|
|
|
|
uint16_t msg, nummsgs;
|
|
|
|
uint32_t bytes;
|
|
|
|
|
|
|
|
if (doesclrscrafterexit())
|
|
|
|
{
|
|
|
|
atexit(confirm_exit);
|
|
|
|
}
|
|
|
|
|
|
|
|
videomode(VIDEOMODE_80COL);
|
|
|
|
printf("\nReading POP65.CFG -");
|
|
|
|
readconfigfile();
|
|
|
|
printf(" Ok");
|
|
|
|
|
|
|
|
{
|
|
|
|
int file;
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
printf("\nSetting slot - ");
|
2020-06-22 02:21:26 +00:00
|
|
|
file = open("ethernet.slot", O_RDONLY);
|
|
|
|
if (file != -1)
|
|
|
|
{
|
|
|
|
read(file, ð_init, 1);
|
|
|
|
close(file);
|
|
|
|
eth_init &= ~'0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
printf("%d\n\nInitializing %s - ", eth_init, eth_name);
|
2020-06-22 02:21:26 +00:00
|
|
|
if (ip65_init(eth_init))
|
|
|
|
{
|
2020-06-23 02:40:32 +00:00
|
|
|
ip65_error_exit();
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
// Abort on Ctrl-C to be consistent with Linenoise
|
|
|
|
abort_key = 0x83;
|
|
|
|
|
|
|
|
printf("Ok\n\nObtaining IP address - ");
|
2020-06-22 02:21:26 +00:00
|
|
|
if (dhcp_init())
|
|
|
|
{
|
2020-06-23 02:40:32 +00:00
|
|
|
ip65_error_exit();
|
2020-06-22 02:21:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
// Copy IP config from IP65 to W5100
|
|
|
|
w5100_config(eth_init);
|
|
|
|
|
2020-06-22 02:21:26 +00:00
|
|
|
printf(" Ok\nConnecting to %s -", cfg_server);
|
2020-06-23 02:40:32 +00:00
|
|
|
|
|
|
|
if (!w5100_connect(parse_dotted_quad(cfg_server), 110))
|
|
|
|
{
|
|
|
|
printf("failed\n");
|
2020-06-22 02:21:26 +00:00
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Ok\n\n");
|
2020-06-23 02:40:32 +00:00
|
|
|
|
|
|
|
if (!w5100_tcp_send_recv(sendbuf, buf, 1500, 0, 0)) {
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
expect(buf, "+OK");
|
|
|
|
|
|
|
|
sprintf(sendbuf, "USER %s\r\n", cfg_user);
|
|
|
|
if (!w5100_tcp_send_recv(sendbuf, buf, 1500, 1, 0)) {
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
expect(buf, "+OK");
|
|
|
|
|
|
|
|
sprintf(sendbuf, "PASS %s\r\n", cfg_pass);
|
|
|
|
if (!w5100_tcp_send_recv(sendbuf, buf, 1500, 1, 0)) {
|
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
expect(buf, "+OK Logged in.");
|
|
|
|
|
|
|
|
if (!w5100_tcp_send_recv("STAT\r\n", buf, 1500, 1, 0)) {
|
|
|
|
error_exit();
|
|
|
|
}
|
2020-06-22 02:21:26 +00:00
|
|
|
sscanf(buf, "+OK %u %lu", &nummsgs, &bytes);
|
|
|
|
printf(" %u message(s), %lu total bytes\n", nummsgs, bytes);
|
2020-06-23 02:40:32 +00:00
|
|
|
|
2020-06-22 02:21:26 +00:00
|
|
|
for (msg = 1; msg <= nummsgs; ++msg) {
|
2020-06-23 02:40:32 +00:00
|
|
|
sprintf(sendbuf, "RETR %u\r\n", msg);
|
|
|
|
if (!w5100_tcp_send_recv(sendbuf, buf, 1500, 1, 1)) {
|
2020-06-22 02:21:26 +00:00
|
|
|
error_exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 02:40:32 +00:00
|
|
|
if (!w5100_tcp_send_recv("QUIT\r\n", buf, 1500, 1, 0)) {
|
2020-06-22 02:21:26 +00:00
|
|
|
error_exit();
|
|
|
|
}
|
2020-06-23 02:40:32 +00:00
|
|
|
expect(buf, "+OK Logging out.");
|
|
|
|
|
|
|
|
printf("- Ok\n\nDisconnecting ");
|
|
|
|
w5100_disconnect();
|
2020-06-22 02:21:26 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|