2020-06-25 18:23:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////
|
2020-06-27 03:42:03 +00:00
|
|
|
// emai//er - Simple Email User Agent vaguely inspired by Elm
|
2020-06-24 21:22:35 +00:00
|
|
|
// Handles INBOX in the format created by POP65
|
2020-07-03 08:12:12 +00:00
|
|
|
// Bobbi June, July 2020
|
2020-06-25 18:23:38 +00:00
|
|
|
/////////////////////////////////////////////////////////////////
|
2020-06-24 21:22:35 +00:00
|
|
|
|
2020-07-07 23:23:16 +00:00
|
|
|
// - TODO: Get rid of all uses of malloc(). Don't need it.
|
2020-06-28 04:25:49 +00:00
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2020-06-26 23:17:05 +00:00
|
|
|
#include <stdarg.h>
|
2020-06-26 15:29:45 +00:00
|
|
|
#include <unistd.h>
|
2020-06-24 21:22:35 +00:00
|
|
|
#include <conio.h>
|
2020-06-24 21:50:19 +00:00
|
|
|
#include <string.h>
|
2020-06-26 16:09:20 +00:00
|
|
|
#include <ctype.h>
|
2020-06-29 19:58:16 +00:00
|
|
|
#include <apple2_filetype.h>
|
2020-06-24 21:22:35 +00:00
|
|
|
|
2020-07-26 23:06:21 +00:00
|
|
|
#include <errno.h> // DEBUG
|
|
|
|
|
2020-06-25 18:23:38 +00:00
|
|
|
#define EMAIL_C
|
|
|
|
#include "email_common.h"
|
|
|
|
|
2020-06-29 23:54:57 +00:00
|
|
|
// Program constants
|
2020-06-26 23:30:06 +00:00
|
|
|
#define MSGS_PER_PAGE 18 // Number of messages shown on summary screen
|
2020-06-26 18:12:58 +00:00
|
|
|
#define MENU_ROW 22 // Row that the menu appears on
|
|
|
|
#define PROMPT_ROW 24 // Row that data entry prompt appears on
|
|
|
|
#define READSZ 1024 // Size of buffer for copying files
|
|
|
|
|
2020-06-29 23:54:57 +00:00
|
|
|
// Characters
|
2020-06-29 23:23:53 +00:00
|
|
|
#define BELL 0x07
|
|
|
|
#define BACKSPACE 0x08
|
|
|
|
#define INVERSE 0x0f
|
2020-06-29 23:54:57 +00:00
|
|
|
#define DOWNARROW 0x0a
|
|
|
|
#define UPARROW 0x0b
|
2020-06-29 23:23:53 +00:00
|
|
|
#define RETURN 0x0d
|
|
|
|
#define NORMAL 0x0e
|
|
|
|
#define CURDOWN 0x0a
|
|
|
|
#define HOME 0x19
|
|
|
|
#define CLRLINE 0x1a
|
2020-07-02 03:26:39 +00:00
|
|
|
#define CURUP 0x1f
|
2020-06-29 23:23:53 +00:00
|
|
|
#define DELETE 0x7f
|
|
|
|
|
2020-06-29 23:54:57 +00:00
|
|
|
// Addresses
|
|
|
|
#define CURSORROW 0x0025
|
2020-06-29 23:51:34 +00:00
|
|
|
#define SYSTEMTIME 0xbf90
|
|
|
|
|
2020-06-29 23:23:53 +00:00
|
|
|
/*
|
|
|
|
* Represents a date and time
|
|
|
|
*/
|
|
|
|
struct datetime {
|
|
|
|
unsigned int year;
|
|
|
|
unsigned char month;
|
|
|
|
unsigned char day;
|
|
|
|
unsigned char hour;
|
|
|
|
unsigned char minute;
|
|
|
|
unsigned char ispd25format;
|
|
|
|
unsigned char nodatetime;
|
|
|
|
};
|
2020-06-26 18:12:58 +00:00
|
|
|
|
2020-06-26 21:57:38 +00:00
|
|
|
char filename[80];
|
|
|
|
char userentry[80];
|
2020-07-01 21:01:54 +00:00
|
|
|
char linebuf[998+2]; // According to RFC2822 Section 2.1.1 (998+CRLF)
|
2020-07-06 16:10:47 +00:00
|
|
|
char halfscreen[0x0400];
|
2020-06-26 21:57:38 +00:00
|
|
|
FILE *fp;
|
|
|
|
struct emailhdrs *headers;
|
|
|
|
uint16_t selection, prevselection;
|
|
|
|
uint16_t num_msgs; // Num of msgs shown in current page
|
|
|
|
uint16_t total_msgs; // Total number of message in mailbox
|
|
|
|
uint16_t total_new; // Total number of new messages
|
2020-06-27 03:27:59 +00:00
|
|
|
uint16_t total_tag; // Total number of tagged messages
|
2020-06-26 21:57:38 +00:00
|
|
|
uint16_t first_msg; // Msg numr: first message current page
|
2020-07-07 06:23:07 +00:00
|
|
|
uint8_t reverse; // 0 normal, 1 reverse order
|
2020-06-26 21:57:38 +00:00
|
|
|
char curr_mbox[80] = "INBOX";
|
|
|
|
static unsigned char buf[READSZ];
|
2020-06-24 21:22:35 +00:00
|
|
|
|
2020-06-26 23:17:05 +00:00
|
|
|
#define ERR_NONFATAL 0
|
|
|
|
#define ERR_FATAL 1
|
2020-06-24 21:22:35 +00:00
|
|
|
|
2020-07-16 00:15:57 +00:00
|
|
|
// Shove this up in the Language Card out of an abundance of caution
|
|
|
|
#pragma code-name (push, "LC")
|
|
|
|
void load_editor(void) {
|
|
|
|
sprintf(userentry, "%s/EDIT.SYSTEM", cfg_instdir);
|
|
|
|
exec(userentry, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_pop65(void) {
|
|
|
|
sprintf(filename, "%s/POP65.SYSTEM", cfg_instdir);
|
|
|
|
exec(filename, "EMAIL");
|
|
|
|
}
|
|
|
|
|
|
|
|
void load_smtp65(void) {
|
|
|
|
sprintf(filename, "%s/SMTP65.SYSTEM", cfg_instdir);
|
|
|
|
exec(filename, "EMAIL");
|
|
|
|
}
|
|
|
|
#pragma code-name (pop)
|
|
|
|
|
2020-06-29 22:29:05 +00:00
|
|
|
/*
|
|
|
|
* Put cursor at beginning of PROMPT_ROW
|
|
|
|
*/
|
|
|
|
void goto_prompt_row(void) {
|
|
|
|
uint8_t i;
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(HOME);
|
2020-06-29 22:29:05 +00:00
|
|
|
for (i = 0; i < PROMPT_ROW - 1; ++i)
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CURDOWN);
|
2020-06-29 22:29:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 03:52:35 +00:00
|
|
|
void clrscr2(void) {
|
|
|
|
videomode(VIDEOMODE_80COL);
|
|
|
|
clrscr();
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
/*
|
2020-06-26 23:17:05 +00:00
|
|
|
* Show non fatal error in PROMPT_ROW
|
|
|
|
* Fatal errors are shown on a blank screen
|
2020-06-24 21:22:35 +00:00
|
|
|
*/
|
2020-06-26 23:17:05 +00:00
|
|
|
void error(uint8_t fatal, const char *fmt, ...) {
|
|
|
|
va_list v;
|
|
|
|
if (fatal) {
|
2020-07-06 03:52:35 +00:00
|
|
|
clrscr2();
|
2020-06-29 23:23:53 +00:00
|
|
|
printf("\n\n%cFATAL ERROR:%c\n\n", INVERSE, NORMAL);
|
2020-06-26 23:17:05 +00:00
|
|
|
va_start(v, fmt);
|
|
|
|
vprintf(fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
printf("\n\n\n\n[Press Any Key To Quit]");
|
|
|
|
cgetc();
|
|
|
|
exit(1);
|
|
|
|
} else {
|
2020-06-29 22:29:05 +00:00
|
|
|
goto_prompt_row();
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CLRLINE);
|
2020-06-26 23:17:05 +00:00
|
|
|
va_start(v, fmt);
|
|
|
|
vprintf(fmt, v);
|
|
|
|
va_end(v);
|
|
|
|
printf(" - [Press Any Key]");
|
|
|
|
cgetc();
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CLRLINE);
|
2020-06-26 23:17:05 +00:00
|
|
|
}
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 02:04:56 +00:00
|
|
|
/*
|
|
|
|
* Print spaces
|
|
|
|
*/
|
|
|
|
void pr_spc(uint8_t n) {
|
|
|
|
while (n--)
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Print ASCII-art envelope
|
|
|
|
*/
|
|
|
|
void envelope(void) {
|
|
|
|
uint8_t i;
|
|
|
|
putchar(NORMAL);
|
|
|
|
putchar(HOME);
|
|
|
|
for (i = 0; i < 8 - 1; ++i)
|
|
|
|
putchar(CURDOWN);
|
2020-07-08 02:12:28 +00:00
|
|
|
pr_spc(30); puts("+------------------+");
|
|
|
|
pr_spc(30); puts("|\\ \"Inbox Zero\" /|");
|
|
|
|
pr_spc(30); puts("| \\ / |");
|
|
|
|
pr_spc(30); puts("| \\ / |");
|
|
|
|
pr_spc(30); puts("| +----------+ |");
|
|
|
|
pr_spc(30); puts("| Bobbi |");
|
|
|
|
pr_spc(30); puts("+------------------+");
|
|
|
|
pr_spc(30); puts(" ... No messages in this mailbox");
|
2020-07-08 02:04:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:57:38 +00:00
|
|
|
/*
|
|
|
|
* Busy spinner
|
|
|
|
*/
|
|
|
|
void spinner(void) {
|
|
|
|
static char chars[] = "|/-\\";
|
|
|
|
static uint8_t i = 0;
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(BACKSPACE);
|
2020-06-26 21:57:38 +00:00
|
|
|
putchar(chars[(i++) % 4]);
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
/*
|
2020-07-15 21:20:13 +00:00
|
|
|
* Read parms from EMAIL.CFG
|
2020-06-24 21:22:35 +00:00
|
|
|
*/
|
|
|
|
void readconfigfile(void) {
|
2020-07-15 21:20:13 +00:00
|
|
|
fp = fopen("EMAIL.CFG", "r");
|
2020-06-26 23:17:05 +00:00
|
|
|
if (!fp)
|
2020-07-15 21:20:13 +00:00
|
|
|
error(ERR_FATAL, "Can't open config file EMAIL.CFG");
|
2020-06-26 21:57:38 +00:00
|
|
|
fscanf(fp, "%s", cfg_server);
|
|
|
|
fscanf(fp, "%s", cfg_user);
|
|
|
|
fscanf(fp, "%s", cfg_pass);
|
2020-06-29 21:51:12 +00:00
|
|
|
fscanf(fp, "%s", cfg_pop_delete);
|
2020-06-28 17:14:29 +00:00
|
|
|
fscanf(fp, "%s", cfg_smtp_server);
|
|
|
|
fscanf(fp, "%s", cfg_smtp_domain);
|
2020-07-15 23:45:25 +00:00
|
|
|
fscanf(fp, "%s", cfg_instdir);
|
2020-06-26 21:57:38 +00:00
|
|
|
fscanf(fp, "%s", cfg_emaildir);
|
2020-06-28 01:00:50 +00:00
|
|
|
fscanf(fp, "%s", cfg_emailaddr);
|
2020-06-26 21:57:38 +00:00
|
|
|
fclose(fp);
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 23:23:16 +00:00
|
|
|
/*
|
|
|
|
* Convert date/time bytes into struct datetime format.
|
|
|
|
*/
|
2020-06-29 23:23:53 +00:00
|
|
|
void readdatetime(unsigned char time[4], struct datetime *dt) {
|
|
|
|
unsigned int d = time[0] + 256U * time[1];
|
|
|
|
unsigned int t = time[2] + 256U * time[3];
|
|
|
|
if ((d == 0) && (t == 0)) {
|
|
|
|
dt->nodatetime = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dt->nodatetime = 0;
|
|
|
|
if (!(t & 0xe000)) {
|
|
|
|
/* ProDOS 1.0 to 2.4.2 date format */
|
|
|
|
dt->year = (d & 0xfe00) >> 9;
|
|
|
|
dt->month = (d & 0x01e0) >> 5;
|
|
|
|
dt->day = d & 0x001f;
|
|
|
|
dt->hour = (t & 0x1f00) >> 8;
|
|
|
|
dt->minute = t & 0x003f;
|
|
|
|
dt->ispd25format = 0;
|
|
|
|
if (dt->year < 40) /* See ProDOS-8 Tech Note 48 */
|
|
|
|
dt->year += 2000;
|
|
|
|
else
|
|
|
|
dt->year += 1900;
|
|
|
|
} else {
|
|
|
|
/* ProDOS 2.5.0+ */
|
|
|
|
dt->year = t & 0x0fff;
|
|
|
|
dt->month = ((t & 0xf000) >> 12) - 1;
|
|
|
|
dt->day = (d & 0xf800) >> 11;
|
|
|
|
dt->hour = (d & 0x07c0) >> 6;
|
|
|
|
dt->minute = d & 0x003f;
|
|
|
|
dt->ispd25format = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:51:34 +00:00
|
|
|
/*
|
|
|
|
* Print a date/time value in short format for the status bar
|
|
|
|
*/
|
2020-06-29 23:23:53 +00:00
|
|
|
void printdatetime(struct datetime *dt) {
|
|
|
|
if (dt->nodatetime)
|
|
|
|
fputs("????-??-?? ??:??", stdout);
|
|
|
|
else {
|
|
|
|
printf("%04d-%02d-%02d %02d:%02d",
|
|
|
|
dt->year, dt->month, dt->day, dt->hour, dt->minute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:51:34 +00:00
|
|
|
/*
|
|
|
|
* Format a date/time in format suitable for an email
|
|
|
|
* dt - structure representing date/time
|
|
|
|
* s - result is returned through this pointer
|
|
|
|
*/
|
|
|
|
void datetimelong(struct datetime *dt, char *s) {
|
|
|
|
static char *months[] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
|
|
if (dt->nodatetime)
|
|
|
|
sprintf(s, "No system clock");
|
|
|
|
else {
|
|
|
|
// eg: "20 Jun 2020 hh:mm:00"
|
|
|
|
sprintf(s, "%2d %s %04d %02d:%02d:00",
|
|
|
|
dt->day, months[dt->month], dt->year, dt->hour, dt->minute);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtain the system date and time for the status bar
|
|
|
|
*/
|
2020-06-29 23:23:53 +00:00
|
|
|
void printsystemdate(void) {
|
|
|
|
struct datetime dt;
|
2020-06-29 23:51:34 +00:00
|
|
|
readdatetime((unsigned char*)(SYSTEMTIME), &dt);
|
2020-06-29 23:23:53 +00:00
|
|
|
printdatetime(&dt);
|
|
|
|
}
|
|
|
|
|
2020-06-26 01:47:03 +00:00
|
|
|
/*
|
|
|
|
* Free linked list rooted at headers
|
|
|
|
*/
|
|
|
|
void free_headers_list(void) {
|
|
|
|
struct emailhdrs *h = headers;
|
|
|
|
while (h) {
|
|
|
|
free(h);
|
|
|
|
h = h->next; // Not strictly legal, but will work
|
|
|
|
}
|
|
|
|
headers = NULL;
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
/*
|
2020-06-25 18:23:38 +00:00
|
|
|
* Read EMAIL.DB and populate linked list rooted at headers
|
2020-06-26 01:47:03 +00:00
|
|
|
* startnum - number of the first message to load (1 is the first)
|
2020-06-26 23:30:06 +00:00
|
|
|
* initialize - if 1, then total_new and total_msgs are calculated
|
2020-06-27 00:28:41 +00:00
|
|
|
* switchmbox - if 1, then errors are treated as non-fatal (for S)witch command)
|
2020-06-26 23:30:06 +00:00
|
|
|
* Returns 0 if okay, 1 on non-fatal error.
|
2020-06-24 21:22:35 +00:00
|
|
|
*/
|
2020-06-27 00:28:41 +00:00
|
|
|
uint8_t read_email_db(uint16_t startnum, uint8_t initialize, uint8_t switchmbox) {
|
2020-06-24 21:22:35 +00:00
|
|
|
struct emailhdrs *curr = NULL, *prev = NULL;
|
2020-06-26 01:47:03 +00:00
|
|
|
uint16_t count = 0;
|
2020-07-07 06:23:07 +00:00
|
|
|
uint8_t done_visible = 0;
|
|
|
|
int32_t pos;
|
2020-06-24 21:22:35 +00:00
|
|
|
uint16_t l;
|
2020-06-26 02:35:14 +00:00
|
|
|
if (initialize) {
|
2020-06-27 03:27:59 +00:00
|
|
|
total_new = total_msgs = total_tag = 0;
|
2020-06-26 02:35:14 +00:00
|
|
|
}
|
2020-06-26 01:47:03 +00:00
|
|
|
free_headers_list();
|
2020-06-26 15:00:11 +00:00
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, curr_mbox);
|
2020-06-24 21:22:35 +00:00
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
2020-06-27 00:28:41 +00:00
|
|
|
error(switchmbox ? ERR_NONFATAL : ERR_FATAL, "Can't open %s", filename);
|
|
|
|
if (switchmbox)
|
2020-06-26 23:30:06 +00:00
|
|
|
return 1;
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
2020-07-07 05:36:49 +00:00
|
|
|
if (reverse) {
|
2020-07-07 06:23:07 +00:00
|
|
|
// TODO Streamline this once it works
|
2020-07-07 05:36:49 +00:00
|
|
|
if (fseek(fp, 0, SEEK_END)) {
|
2020-07-08 02:52:36 +00:00
|
|
|
fclose(fp);
|
2020-07-07 06:23:07 +00:00
|
|
|
error(switchmbox ? ERR_NONFATAL : ERR_FATAL, "Can't seek in %s", filename);
|
2020-07-07 05:36:49 +00:00
|
|
|
if (switchmbox)
|
|
|
|
return 1;
|
|
|
|
}
|
2020-07-08 02:52:36 +00:00
|
|
|
// If the mailbox is empty this seek will fail
|
2020-07-07 05:36:49 +00:00
|
|
|
if (fseek(fp, ftell(fp) - startnum * EMAILHDRS_SZ_ON_DISK, SEEK_SET)) {
|
2020-07-08 02:52:36 +00:00
|
|
|
fclose(fp);
|
|
|
|
num_msgs = total_new = total_msgs = total_tag = 0;
|
|
|
|
return 0;
|
2020-07-07 05:36:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (fseek(fp, (startnum - 1) * EMAILHDRS_SZ_ON_DISK, SEEK_SET)) {
|
2020-07-08 02:52:36 +00:00
|
|
|
fclose(fp);
|
2020-07-07 05:36:49 +00:00
|
|
|
error(switchmbox ? ERR_NONFATAL : ERR_FATAL, "Can't seek in %s", filename);
|
|
|
|
if (switchmbox)
|
|
|
|
return 1;
|
|
|
|
}
|
2020-06-26 01:47:03 +00:00
|
|
|
}
|
2020-06-26 02:35:14 +00:00
|
|
|
num_msgs = 0;
|
2020-06-24 21:22:35 +00:00
|
|
|
while (1) {
|
|
|
|
curr = (struct emailhdrs*)malloc(sizeof(struct emailhdrs));
|
2020-06-27 03:27:59 +00:00
|
|
|
if (!curr)
|
|
|
|
error(ERR_FATAL, "Can't malloc()");
|
2020-06-24 21:22:35 +00:00
|
|
|
curr->next = NULL;
|
2020-06-27 00:28:41 +00:00
|
|
|
curr->tag = ' ';
|
|
|
|
l = fread(curr, 1, EMAILHDRS_SZ_ON_DISK, fp);
|
2020-06-26 01:47:03 +00:00
|
|
|
++count;
|
2020-06-27 00:28:41 +00:00
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK) {
|
2020-06-24 21:22:35 +00:00
|
|
|
free(curr);
|
|
|
|
fclose(fp);
|
2020-06-26 23:30:06 +00:00
|
|
|
return 0;
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
2020-06-26 02:35:14 +00:00
|
|
|
if (count <= MSGS_PER_PAGE) {
|
2020-06-26 01:47:03 +00:00
|
|
|
if (!prev)
|
|
|
|
headers = curr;
|
|
|
|
else
|
|
|
|
prev->next = curr;
|
|
|
|
prev = curr;
|
|
|
|
++num_msgs;
|
2020-07-07 05:36:49 +00:00
|
|
|
} else {
|
2020-06-26 02:35:14 +00:00
|
|
|
if (!initialize) {
|
2020-07-07 05:36:49 +00:00
|
|
|
free(curr);
|
2020-06-26 02:35:14 +00:00
|
|
|
fclose(fp);
|
2020-06-26 23:30:06 +00:00
|
|
|
return 0;
|
2020-06-26 02:35:14 +00:00
|
|
|
}
|
2020-07-07 05:36:49 +00:00
|
|
|
done_visible = 1;
|
|
|
|
}
|
2020-06-26 02:35:14 +00:00
|
|
|
if (initialize) {
|
|
|
|
++total_msgs;
|
|
|
|
if (curr->status == 'N')
|
|
|
|
++total_new;
|
2020-06-27 03:27:59 +00:00
|
|
|
if (curr->tag == 'T')
|
|
|
|
++total_tag;
|
2020-07-07 05:36:49 +00:00
|
|
|
if (done_visible)
|
|
|
|
free(curr);
|
2020-06-26 01:47:03 +00:00
|
|
|
}
|
2020-07-07 06:23:07 +00:00
|
|
|
if (reverse) {
|
|
|
|
// TODO Streamline this once it works
|
|
|
|
pos = ftell(fp) - 2 * EMAILHDRS_SZ_ON_DISK;
|
|
|
|
if (pos == -1 * EMAILHDRS_SZ_ON_DISK) {
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (fseek(fp, pos, SEEK_SET)) {
|
|
|
|
error(switchmbox ? ERR_NONFATAL : ERR_FATAL, "Can't seek in %s", filename);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
2020-06-26 02:35:14 +00:00
|
|
|
fclose(fp);
|
2020-06-26 23:30:06 +00:00
|
|
|
return 0;
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 21:50:19 +00:00
|
|
|
/*
|
|
|
|
* Print a header field from char postion start to end,
|
|
|
|
* padding with spaces as needed
|
|
|
|
*/
|
|
|
|
void printfield(char *s, uint8_t start, uint8_t end) {
|
|
|
|
uint8_t i;
|
|
|
|
uint8_t l = strlen(s);
|
|
|
|
for (i = start; i < end; i++)
|
|
|
|
putchar(i < l ? s[i] : ' ');
|
|
|
|
}
|
|
|
|
|
2020-06-25 00:25:31 +00:00
|
|
|
/*
|
|
|
|
* Print one line summary of email headers for one message
|
|
|
|
*/
|
2020-06-25 18:23:38 +00:00
|
|
|
void print_one_email_summary(struct emailhdrs *h, uint8_t inverse) {
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(inverse ? INVERSE : NORMAL);
|
2020-06-27 00:28:41 +00:00
|
|
|
putchar(h->tag == 'T' ? 'T' : ' ');
|
2020-06-26 01:47:03 +00:00
|
|
|
switch(h->status) {
|
|
|
|
case 'N':
|
|
|
|
putchar('*'); // New
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
putchar(' '); // Read
|
|
|
|
break;
|
|
|
|
case 'D':
|
|
|
|
putchar('D'); // Deleted
|
|
|
|
break;
|
|
|
|
}
|
2020-06-28 00:02:09 +00:00
|
|
|
//printf("%02d|", h->emailnum);
|
|
|
|
putchar('|');
|
2020-06-25 00:25:31 +00:00
|
|
|
printfield(h->date, 0, 16);
|
|
|
|
putchar('|');
|
|
|
|
printfield(h->from, 0, 20);
|
|
|
|
putchar('|');
|
2020-06-28 00:02:09 +00:00
|
|
|
printfield(h->subject, 0, 39);
|
2020-06-25 00:25:31 +00:00
|
|
|
//putchar('\r');
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(NORMAL);
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 02:05:55 +00:00
|
|
|
/*
|
2020-06-25 18:23:38 +00:00
|
|
|
* Get emailhdrs for nth email in list of headers
|
2020-06-25 02:05:55 +00:00
|
|
|
*/
|
|
|
|
struct emailhdrs *get_headers(uint16_t n) {
|
|
|
|
uint16_t i = 1;
|
|
|
|
struct emailhdrs *h = headers;
|
2020-06-26 01:47:03 +00:00
|
|
|
while (h && (i < n)) {
|
2020-06-25 02:05:55 +00:00
|
|
|
++i;
|
|
|
|
h = h->next;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2020-06-29 23:23:53 +00:00
|
|
|
/*
|
|
|
|
* Print status bar at the top
|
|
|
|
*/
|
2020-06-27 03:27:59 +00:00
|
|
|
void status_bar(void) {
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(HOME);
|
|
|
|
putchar(INVERSE);
|
|
|
|
fputs(" ", stdout);
|
|
|
|
printsystemdate();
|
|
|
|
putchar(HOME);
|
2020-07-08 02:04:56 +00:00
|
|
|
if (num_msgs == 0) {
|
2020-06-29 23:23:53 +00:00
|
|
|
printf("%c%s [%s] No messages ", INVERSE, PROGNAME, curr_mbox);
|
2020-07-08 02:04:56 +00:00
|
|
|
envelope();
|
|
|
|
} else
|
2020-07-07 06:23:07 +00:00
|
|
|
printf("%c[%s] %u msgs, %u new, %u tagged. Showing %u-%u. %c ",
|
2020-06-29 23:23:53 +00:00
|
|
|
INVERSE, curr_mbox, total_msgs, total_new, total_tag, first_msg,
|
2020-07-07 06:23:07 +00:00
|
|
|
first_msg + num_msgs - 1, (reverse ? '<' : '>'));
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(NORMAL);
|
|
|
|
putchar(CURDOWN);
|
|
|
|
printf("\n");
|
2020-06-27 03:27:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
/*
|
|
|
|
* Show email summary
|
|
|
|
*/
|
|
|
|
void email_summary(void) {
|
2020-06-26 01:47:03 +00:00
|
|
|
uint8_t i = 1;
|
2020-06-24 21:22:35 +00:00
|
|
|
struct emailhdrs *h = headers;
|
2020-07-06 03:52:35 +00:00
|
|
|
clrscr2();
|
2020-06-27 03:27:59 +00:00
|
|
|
status_bar();
|
2020-06-24 21:22:35 +00:00
|
|
|
while (h) {
|
2020-06-25 18:23:38 +00:00
|
|
|
print_one_email_summary(h, (i == selection));
|
2020-06-25 00:25:31 +00:00
|
|
|
++i;
|
|
|
|
h = h->next;
|
|
|
|
}
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(HOME);
|
2020-06-26 15:00:11 +00:00
|
|
|
for (i = 0; i < MENU_ROW - 1; ++i)
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CURDOWN);
|
|
|
|
printf("%cUp/K Prev | SPC/RET Read | A)rchive | C)opy | M)ove | D)el | U)ndel | P)urge %c", INVERSE, NORMAL);
|
|
|
|
printf("%cDn/J Next | S)witch mbox | N)ew mbox| T)ag | W)rite | R)eply | F)wd | Q)uit %c", INVERSE, NORMAL);
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-06-25 18:23:38 +00:00
|
|
|
* Show email summary for nth email message in list of headers
|
2020-06-25 00:25:31 +00:00
|
|
|
*/
|
|
|
|
void email_summary_for(uint16_t n) {
|
|
|
|
struct emailhdrs *h = headers;
|
|
|
|
uint16_t j;
|
2020-06-25 02:05:55 +00:00
|
|
|
h = get_headers(n);
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(HOME);
|
2020-06-26 01:47:03 +00:00
|
|
|
for (j = 0; j < n + 1; ++j)
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CURDOWN);
|
2020-06-25 18:23:38 +00:00
|
|
|
print_one_email_summary(h, (n == selection));
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move the highlight bar when user selects different message
|
|
|
|
*/
|
|
|
|
void update_highlighted(void) {
|
|
|
|
email_summary_for(prevselection);
|
|
|
|
email_summary_for(selection);
|
|
|
|
}
|
|
|
|
|
2020-07-01 21:01:54 +00:00
|
|
|
/*
|
2020-07-20 21:20:44 +00:00
|
|
|
* Read a text file a line at a time
|
2020-07-01 21:01:54 +00:00
|
|
|
* Returns number of chars in the line, or -1 if EOF.
|
|
|
|
* Expects Apple ][ style line endings (CR) and does no conversion
|
|
|
|
* fp - file to read from
|
|
|
|
* reset - if 1 then just reset the buffer and return
|
2020-07-20 21:20:44 +00:00
|
|
|
* writep - Pointer to buffer into which line will be written
|
|
|
|
* pos - position in file is updated via this pointer
|
2020-07-01 21:01:54 +00:00
|
|
|
*/
|
2020-07-20 21:20:44 +00:00
|
|
|
int16_t get_line(FILE *fp, uint8_t reset, char *writep, uint32_t *pos) {
|
2020-07-08 21:05:20 +00:00
|
|
|
static uint16_t rd = 0; // Read
|
2020-07-29 21:28:35 +00:00
|
|
|
static uint16_t end = 0; // End of valid data in buf
|
|
|
|
uint16_t i = 0;
|
2020-07-01 21:01:54 +00:00
|
|
|
if (reset) {
|
2020-07-29 21:28:35 +00:00
|
|
|
rd = end = 0;
|
2020-07-02 03:07:02 +00:00
|
|
|
*pos = 0;
|
2020-07-01 21:01:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
while (1) {
|
2020-07-29 21:28:35 +00:00
|
|
|
if (rd == end) {
|
|
|
|
end = fread(buf, 1, READSZ, fp);
|
|
|
|
rd = 0;
|
|
|
|
}
|
|
|
|
if (end == 0)
|
|
|
|
return -1; // EOF
|
|
|
|
writep[i++] = buf[rd++];
|
|
|
|
++(*pos);
|
|
|
|
if (writep[i - 1] == '\r') {
|
|
|
|
writep[i] = '\0';
|
|
|
|
return i;
|
2020-07-01 21:01:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert hex char to value
|
|
|
|
*/
|
|
|
|
uint8_t hexdigit(char c) {
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
return c - '0';
|
|
|
|
else
|
|
|
|
return c - 'A' + 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-05 20:33:16 +00:00
|
|
|
* Decode linebuf[] from quoted-printable format in place
|
2020-07-20 21:30:30 +00:00
|
|
|
* p - Pointer to buffer to decode. Results written in place.
|
|
|
|
* Returns number of bytes decoded
|
2020-07-01 21:01:54 +00:00
|
|
|
*/
|
2020-07-20 21:30:30 +00:00
|
|
|
uint16_t decode_quoted_printable(char *p) {
|
2020-07-05 20:33:16 +00:00
|
|
|
uint16_t i = 0, j = 0;
|
2020-07-01 21:01:54 +00:00
|
|
|
char c;
|
2020-07-20 21:30:30 +00:00
|
|
|
while (c = p[i]) {
|
2020-07-01 21:01:54 +00:00
|
|
|
if (c == '=') {
|
2020-07-20 21:30:30 +00:00
|
|
|
if (p[i + 1] == '\r') { // Trailing '=' is a soft '\r'
|
|
|
|
p[j] = '\0';
|
2020-07-05 23:01:31 +00:00
|
|
|
return j;
|
2020-07-05 20:33:16 +00:00
|
|
|
}
|
2020-07-01 21:01:54 +00:00
|
|
|
// Otherwise '=xx' where x is a hex digit
|
|
|
|
c = 16 * hexdigit(linebuf[i + 1]) + hexdigit(linebuf[i + 2]);
|
|
|
|
if ((c >= 0x20) && (c <= 0x7e))
|
2020-07-20 21:30:30 +00:00
|
|
|
p[j++] = c;
|
2020-07-01 21:01:54 +00:00
|
|
|
i += 3;
|
|
|
|
} else {
|
2020-07-20 21:30:30 +00:00
|
|
|
p[j++] = c;
|
2020-07-01 21:01:54 +00:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2020-07-20 21:30:30 +00:00
|
|
|
p[j] = '\0';
|
2020-07-05 23:01:31 +00:00
|
|
|
return j;
|
2020-07-01 21:01:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 23:48:22 +00:00
|
|
|
/*
|
2020-07-02 04:19:16 +00:00
|
|
|
* Base64 decode table
|
2020-07-01 23:48:22 +00:00
|
|
|
*/
|
2020-07-03 03:28:16 +00:00
|
|
|
static const int8_t b64dec[] =
|
2020-07-02 23:02:24 +00:00
|
|
|
{62,-1,-1,-1,63,52,53,54,55,56,
|
|
|
|
57,58,59,60,61,-1,-1,-1,-2,-1,
|
|
|
|
-1,-1, 0, 1, 2, 3, 4, 5, 6, 7,
|
|
|
|
8, 9,10,11,12,13,14,15,16,17,
|
|
|
|
18,19,20,21,22,23,24,25,-1,-1,
|
|
|
|
-1,-1,-1,-1,26,27,28,29,30,31,
|
|
|
|
32,33,34,35,36,37,38,39,40,41,
|
|
|
|
42,43,44,45,46,47,48,49,50,51};
|
2020-07-01 23:48:22 +00:00
|
|
|
|
2020-07-29 03:45:56 +00:00
|
|
|
|
2020-07-01 21:01:54 +00:00
|
|
|
/*
|
2020-07-22 02:26:32 +00:00
|
|
|
* Decode Base64 format in place
|
2020-07-05 23:01:31 +00:00
|
|
|
* Each line of base64 has up to 76 chars, which decodes to up to 57 bytes
|
2020-07-20 21:30:30 +00:00
|
|
|
* p - Pointer to buffer to decode. Results written in place.
|
|
|
|
* Returns number of bytes decoded
|
2020-07-01 21:01:54 +00:00
|
|
|
*/
|
2020-07-29 03:45:56 +00:00
|
|
|
#if 0
|
2020-07-20 21:30:30 +00:00
|
|
|
uint16_t decode_base64(char *p) {
|
2020-07-05 23:01:31 +00:00
|
|
|
uint16_t i = 0, j = 0;
|
2020-07-29 03:03:27 +00:00
|
|
|
const int8_t *b = b64dec - 43;
|
2020-07-20 21:30:30 +00:00
|
|
|
while (p[i] != '\r') {
|
2020-07-29 03:03:27 +00:00
|
|
|
p[j++] = b[p[i]] << 2 | b[p[i + 1]] >> 4;
|
2020-07-20 21:30:30 +00:00
|
|
|
if (p[i + 2] != '=')
|
2020-07-29 03:03:27 +00:00
|
|
|
p[j++] = b[p[i + 1]] << 4 | b[p[i + 2]] >> 2;
|
2020-07-02 23:02:24 +00:00
|
|
|
if (linebuf[i + 3] != '=')
|
2020-07-29 03:03:27 +00:00
|
|
|
p[j++] = b[p[i + 2]] << 6 | b[p[i + 3]];
|
2020-07-01 23:48:22 +00:00
|
|
|
i += 4;
|
|
|
|
}
|
2020-07-05 23:01:31 +00:00
|
|
|
return j;
|
2020-07-01 21:01:54 +00:00
|
|
|
}
|
2020-07-29 03:45:56 +00:00
|
|
|
#endif
|
2020-07-01 21:01:54 +00:00
|
|
|
|
2020-07-05 03:22:23 +00:00
|
|
|
/*
|
2020-07-05 20:33:16 +00:00
|
|
|
* Print line up to first '\r' or '\0'
|
|
|
|
*/
|
|
|
|
void putline(FILE *fp, char *s) {
|
|
|
|
char *ret = strchr(s, '\r');
|
|
|
|
if (ret)
|
|
|
|
*ret = '\0';
|
|
|
|
fputs(s, fp);
|
|
|
|
if (ret)
|
|
|
|
*ret = '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-21 15:30:45 +00:00
|
|
|
* Perform word wrapping, for a line of text, which may contain multiple
|
|
|
|
* embedded '\r' carriage returns, or no carriage return at all.
|
2020-07-21 02:20:40 +00:00
|
|
|
* fp - File handle to use for output.
|
|
|
|
* s - Pointer to pointer to input buffer. If all text is consumed, this is
|
|
|
|
* set to NULL. If there is text left in the buffer to be consumed then
|
|
|
|
* the pointer will be advanced to point to the next text to process.
|
|
|
|
* Returns 1 if the caller should invoke the routine again before obtaining
|
|
|
|
* more input, or 0 if there is nothing more to do or caller needs to get more
|
|
|
|
* input before next call.
|
|
|
|
*/
|
2020-07-21 15:30:45 +00:00
|
|
|
uint8_t word_wrap_line(FILE *fp, char **s) {
|
2020-07-21 03:00:55 +00:00
|
|
|
static uint8_t col = 0; // Keeps track of screen column
|
2020-07-21 02:20:40 +00:00
|
|
|
char *ss = *s;
|
|
|
|
char *ret = strchr(ss, '\r');
|
|
|
|
uint16_t l = strlen(ss);
|
|
|
|
char *nextline = NULL;
|
|
|
|
uint16_t i;
|
|
|
|
if (l == 0)
|
2020-07-21 03:00:55 +00:00
|
|
|
return 0; // Need more input to proceed
|
2020-07-21 02:20:40 +00:00
|
|
|
if (ret) {
|
2020-07-21 03:00:55 +00:00
|
|
|
if (l > (ret - ss) + 1) // If '\r' is not at the end ...
|
2020-07-21 02:20:40 +00:00
|
|
|
nextline = ss + (ret - ss) + 1; // Keep track of next line(s)
|
|
|
|
l = ret - ss;
|
|
|
|
}
|
|
|
|
if (ret) {
|
|
|
|
if ((col + l) <= 80) { // Fits on this line
|
|
|
|
col += l;
|
|
|
|
putline(fp, ss);
|
|
|
|
if (ret) {
|
|
|
|
col = 0;
|
|
|
|
if (col + l != 80)
|
|
|
|
fputc('\r', fp);
|
|
|
|
}
|
|
|
|
*s = nextline;
|
|
|
|
return (*s ? 1 : 0); // Caller should invoke again
|
|
|
|
}
|
|
|
|
i = 80 - col; // Doesn't fit, need to break
|
|
|
|
if (i > l)
|
|
|
|
i = l;
|
|
|
|
while ((ss[--i] != ' ') && (i > 0));
|
|
|
|
if (i == 0) { // No space character found
|
|
|
|
if (col == 0) // Doesn't fit on full line
|
|
|
|
for (i = 0; i < 80; ++i) { // Truncate @80 chars
|
|
|
|
fputc(ss[i], fp);
|
|
|
|
*s = ss + l + 1;
|
|
|
|
} else // There is stuff on this line already
|
|
|
|
fputc('\r', fp); // Try a blank line
|
|
|
|
col = 0;
|
|
|
|
return (ret ? (*s ? 0 : 1) : 0); // If EOL, caller should invoke again
|
|
|
|
}
|
2020-07-21 03:00:55 +00:00
|
|
|
} else { // No ret
|
2020-07-21 02:41:30 +00:00
|
|
|
i = 80 - col; // Space left on line
|
2020-07-21 02:20:40 +00:00
|
|
|
if (i > l)
|
2020-07-21 02:41:30 +00:00
|
|
|
return 0; // Need more input to proceed
|
2020-07-21 02:20:40 +00:00
|
|
|
while ((ss[--i] != ' ') && (i > 0));
|
2020-07-21 03:00:55 +00:00
|
|
|
if (i == 0) // No space character found
|
2020-07-21 02:20:40 +00:00
|
|
|
return 0; // Need more input to proceed
|
|
|
|
}
|
2020-07-21 03:00:55 +00:00
|
|
|
ss[i] = '\0'; // Space was found, split line
|
2020-07-21 02:20:40 +00:00
|
|
|
putline(fp, ss);
|
|
|
|
fputc('\r', fp);
|
|
|
|
col = 0;
|
|
|
|
*s = ss + i + 1;
|
2020-07-21 03:00:55 +00:00
|
|
|
return (*s ? 1 : 0); // Caller should invoke again
|
2020-07-21 02:20:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-02 03:26:39 +00:00
|
|
|
/*
|
|
|
|
* OK to d/l attachment?
|
|
|
|
*/
|
|
|
|
char prompt_okay_attachment(char *filename) {
|
|
|
|
char c;
|
|
|
|
printf("Okay to download %s? (y/n) >", filename);
|
|
|
|
while (1) {
|
|
|
|
c = cgetc();
|
|
|
|
if ((c == 'y') || (c == 'Y') || (c == 'n') || (c == 'N'))
|
|
|
|
break;
|
|
|
|
putchar(BELL);
|
|
|
|
}
|
|
|
|
putchar(RETURN); // Go to col 0
|
|
|
|
putchar(CURUP);
|
|
|
|
putchar(CLRLINE);
|
|
|
|
if ((c == 'y') || (c == 'Y'))
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2020-07-03 08:12:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanitize filename for ProDOS
|
|
|
|
* Modifies s in place
|
|
|
|
*/
|
|
|
|
void sanitize_filename(char *s) {
|
|
|
|
uint8_t i = 0, j = 0;
|
|
|
|
char c;
|
|
|
|
while (1) {
|
|
|
|
c = s[i++];
|
|
|
|
if (c == '\r') {
|
|
|
|
s[j] = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isalnum(c) || c == '.' || c == '/')
|
|
|
|
s[j++] = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 03:31:38 +00:00
|
|
|
#define FROMAUX 0
|
|
|
|
#define TOAUX 1
|
|
|
|
/*
|
|
|
|
* Aux memory copy routine
|
|
|
|
*/
|
|
|
|
void copyaux(char *src, char *dst, uint16_t len, uint8_t dir) {
|
|
|
|
char **a1 = (char**)0x3c;
|
|
|
|
char **a2 = (char**)0x3e;
|
|
|
|
char **a4 = (char**)0x42;
|
|
|
|
*a1 = src;
|
|
|
|
*a2 = src + len - 1; // AUXMOVE moves length+1 bytes!!
|
|
|
|
*a4 = dst;
|
|
|
|
if (dir == TOAUX) {
|
2020-07-26 23:06:21 +00:00
|
|
|
__asm__("sta $c000"); // Turn off 80STORE
|
2020-07-06 03:31:38 +00:00
|
|
|
__asm__("sec"); // Copy main->aux
|
|
|
|
__asm__("jsr $c311"); // AUXMOVE
|
2020-07-26 23:06:21 +00:00
|
|
|
__asm__("sta $c001"); // Turn on 80STORE
|
2020-07-06 03:31:38 +00:00
|
|
|
} else {
|
2020-07-26 23:06:21 +00:00
|
|
|
__asm__("sta $c000"); // Turn off 80STORE
|
2020-07-06 03:31:38 +00:00
|
|
|
__asm__("clc"); // Copy aux->main
|
|
|
|
__asm__("jsr $c311"); // AUXMOVE
|
2020-07-26 23:06:21 +00:00
|
|
|
__asm__("sta $c001"); // Turn on 80STORE
|
2020-07-06 03:31:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save the current screen to the scrollback file
|
|
|
|
*/
|
|
|
|
void save_screen_to_scrollback(FILE *fp) {
|
2020-07-26 23:06:21 +00:00
|
|
|
if (fwrite((void*)0x0400, 0x0400, 1, fp) != 1) { // Even cols
|
|
|
|
error(ERR_NONFATAL, "Can't write scrollback");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-06 16:10:47 +00:00
|
|
|
copyaux((void*)0x400, halfscreen, 0x400, FROMAUX);
|
2020-07-26 23:06:21 +00:00
|
|
|
if (fwrite(halfscreen, 0x0400, 1, fp) != 1) { // Odd cols
|
|
|
|
error(ERR_NONFATAL, "Can't write scrollback");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-06 03:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load a screen from the scrollback file
|
|
|
|
* Screens are numbered 1, 2, 3 ...
|
2020-07-06 16:10:47 +00:00
|
|
|
* Does not trash the screen holes, which must be preserved!
|
2020-07-06 03:31:38 +00:00
|
|
|
*/
|
|
|
|
void load_screen_from_scrollback(FILE *fp, uint8_t screen) {
|
2020-07-26 23:06:21 +00:00
|
|
|
if (fseek(fp, (screen - 1) * 0x0800, SEEK_SET)) {
|
|
|
|
error(ERR_NONFATAL, "Can't seek scrollback");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fread(halfscreen, 0x0400, 1, fp) != 1) { // Even cols
|
|
|
|
error(ERR_NONFATAL, "Can't read scrollback");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy((void*)0x400, halfscreen + 0x000, 0x078);
|
|
|
|
memcpy((void*)0x480, halfscreen + 0x080, 0x078);
|
|
|
|
memcpy((void*)0x500, halfscreen + 0x100, 0x078);
|
|
|
|
memcpy((void*)0x580, halfscreen + 0x180, 0x078);
|
|
|
|
memcpy((void*)0x600, halfscreen + 0x200, 0x078);
|
|
|
|
memcpy((void*)0x680, halfscreen + 0x280, 0x078);
|
|
|
|
memcpy((void*)0x700, halfscreen + 0x300, 0x078);
|
|
|
|
memcpy((void*)0x780, halfscreen + 0x380, 0x078);
|
|
|
|
if (fread(halfscreen, 0x0400, 1, fp) != 1) { // Odd cols
|
|
|
|
error(ERR_NONFATAL, "Can't read scrollback");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
copyaux(halfscreen + 0x000, (void*)0x400, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x080, (void*)0x480, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x100, (void*)0x500, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x180, (void*)0x580, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x200, (void*)0x600, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x280, (void*)0x680, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x300, (void*)0x700, 0x078, TOAUX);
|
|
|
|
copyaux(halfscreen + 0x380, (void*)0x780, 0x078, TOAUX);
|
|
|
|
if (fseek(fp, 0, SEEK_END)) {
|
|
|
|
error(ERR_NONFATAL, "Can't seek scrollback");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-06 03:31:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 21:29:36 +00:00
|
|
|
#define ENC_7BIT 0 // 7bit
|
|
|
|
#define ENC_QP 1 // Quoted-Printable
|
|
|
|
#define ENC_B64 2 // Base64
|
|
|
|
#define ENC_SKIP 255 // Do nothing
|
|
|
|
|
2020-06-25 02:05:55 +00:00
|
|
|
/*
|
|
|
|
* Display email with simple pager functionality
|
2020-07-01 23:48:22 +00:00
|
|
|
* Includes support for decoding MIME headers
|
2020-06-25 02:05:55 +00:00
|
|
|
*/
|
2020-07-08 02:52:36 +00:00
|
|
|
void email_pager(struct emailhdrs *h) {
|
2020-06-25 03:08:40 +00:00
|
|
|
uint32_t pos = 0;
|
2020-07-04 23:48:59 +00:00
|
|
|
uint8_t *cursorrow = (uint8_t*)CURSORROW, mime = 0;
|
2020-07-06 03:31:38 +00:00
|
|
|
FILE *sbackfp = NULL;
|
2020-07-29 03:45:56 +00:00
|
|
|
const int8_t *b = b64dec - 43;
|
2020-07-06 03:31:38 +00:00
|
|
|
FILE *attachfp;
|
2020-07-05 23:01:31 +00:00
|
|
|
uint16_t linecount, chars;
|
2020-07-06 03:31:38 +00:00
|
|
|
uint8_t mime_enc, mime_binary, eof, screennum, maxscreennum;
|
2020-07-21 02:20:40 +00:00
|
|
|
char c, *readp, *writep;
|
2020-07-06 03:52:35 +00:00
|
|
|
clrscr2();
|
2020-06-26 15:00:11 +00:00
|
|
|
sprintf(filename, "%s/%s/EMAIL.%u", cfg_emaildir, curr_mbox, h->emailnum);
|
2020-06-25 02:05:55 +00:00
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
2020-07-06 03:31:38 +00:00
|
|
|
if (sbackfp)
|
|
|
|
fclose(sbackfp);
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
2020-06-25 03:08:40 +00:00
|
|
|
return;
|
2020-06-25 02:05:55 +00:00
|
|
|
}
|
2020-06-25 18:23:38 +00:00
|
|
|
pos = h->skipbytes;
|
|
|
|
fseek(fp, pos, SEEK_SET); // Skip over headers
|
2020-06-25 03:08:40 +00:00
|
|
|
restart:
|
2020-07-01 23:48:22 +00:00
|
|
|
eof = 0;
|
2020-07-02 03:07:02 +00:00
|
|
|
linecount = 0;
|
2020-07-20 22:07:12 +00:00
|
|
|
readp = linebuf;
|
2020-07-21 02:20:40 +00:00
|
|
|
writep = linebuf;
|
2020-07-01 23:48:22 +00:00
|
|
|
attachfp = NULL;
|
2020-07-06 03:31:38 +00:00
|
|
|
if (sbackfp)
|
|
|
|
fclose(sbackfp);
|
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
|
|
|
sprintf(filename, "%s/SCROLLBACK", cfg_emaildir);
|
|
|
|
unlink(filename);
|
|
|
|
sbackfp = fopen(filename, "wb+");
|
|
|
|
if (!sbackfp) {
|
|
|
|
error(ERR_NONFATAL, "No scrollback");
|
|
|
|
}
|
|
|
|
maxscreennum = screennum = 0;
|
2020-07-06 03:52:35 +00:00
|
|
|
clrscr2();
|
2020-06-25 02:05:55 +00:00
|
|
|
fputs("Date: ", stdout);
|
2020-06-26 03:18:23 +00:00
|
|
|
printfield(h->date, 0, 39);
|
2020-06-25 02:05:55 +00:00
|
|
|
fputs("\nFrom: ", stdout);
|
|
|
|
printfield(h->from, 0, 70);
|
|
|
|
fputs("\nTo: ", stdout);
|
|
|
|
printfield(h->to, 0, 70);
|
|
|
|
if (h->cc[0] != '\0') {
|
|
|
|
fputs("\nCC: ", stdout);
|
|
|
|
printfield(h->cc, 0, 70);
|
|
|
|
}
|
|
|
|
fputs("\nSubject: ", stdout);
|
|
|
|
printfield(h->subject, 0, 70);
|
|
|
|
fputs("\n\n", stdout);
|
2020-07-20 21:20:44 +00:00
|
|
|
get_line(fp, 1, linebuf, &pos); // Reset buffer
|
2020-06-25 03:08:40 +00:00
|
|
|
while (1) {
|
2020-07-21 02:20:40 +00:00
|
|
|
if (!readp)
|
|
|
|
readp = linebuf;
|
|
|
|
if (!writep)
|
|
|
|
writep = linebuf;
|
2020-07-29 22:21:10 +00:00
|
|
|
if (get_line(fp, 0, writep, &pos) == -1) {
|
2020-07-20 22:07:12 +00:00
|
|
|
eof = 1;
|
2020-07-29 22:21:10 +00:00
|
|
|
goto endscreen;
|
|
|
|
}
|
2020-07-20 21:53:22 +00:00
|
|
|
++linecount;
|
2020-07-21 02:20:40 +00:00
|
|
|
if ((mime >= 1) && (!strncmp(writep, "--", 2))) {
|
2020-07-20 21:53:22 +00:00
|
|
|
if (attachfp)
|
|
|
|
fclose(attachfp);
|
|
|
|
if ((mime == 4) && mime_binary) {
|
|
|
|
putchar(BACKSPACE); // Erase spinner
|
|
|
|
puts("[OK]");
|
|
|
|
}
|
|
|
|
attachfp = NULL;
|
|
|
|
mime = 2;
|
|
|
|
mime_enc = ENC_7BIT;
|
|
|
|
mime_binary = 0;
|
2020-07-21 02:20:40 +00:00
|
|
|
readp = writep = NULL;
|
2020-07-20 21:53:22 +00:00
|
|
|
} else if ((mime < 4) && (mime >= 2)) {
|
2020-07-21 02:20:40 +00:00
|
|
|
if (!strncasecmp(writep, "Content-Type: ", 14)) {
|
|
|
|
if (!strncmp(writep + 14, "text/plain", 10)) {
|
2020-07-20 21:53:22 +00:00
|
|
|
mime = 3;
|
2020-07-21 02:20:40 +00:00
|
|
|
} else if (!strncmp(writep + 14, "text/html", 9)) {
|
2020-07-20 21:53:22 +00:00
|
|
|
printf("\n<Not showing HTML>\n");
|
|
|
|
mime = 1;
|
|
|
|
} else {
|
|
|
|
mime_binary = 1;
|
2020-07-01 23:48:22 +00:00
|
|
|
mime = 3;
|
2020-07-01 21:01:54 +00:00
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
} else if (!strncasecmp(writep, "Content-Transfer-Encoding: ", 27)) {
|
2020-07-20 21:53:22 +00:00
|
|
|
mime = 3;
|
2020-07-21 02:20:40 +00:00
|
|
|
if (!strncmp(writep + 27, "7bit", 4))
|
2020-07-20 21:53:22 +00:00
|
|
|
mime_enc = ENC_7BIT;
|
2020-07-21 02:20:40 +00:00
|
|
|
else if (!strncmp(writep + 27, "quoted-printable", 16))
|
2020-07-20 21:53:22 +00:00
|
|
|
mime_enc = ENC_QP;
|
2020-07-21 02:20:40 +00:00
|
|
|
else if (!strncmp(writep + 27, "base64", 6))
|
2020-07-20 21:53:22 +00:00
|
|
|
mime_enc = ENC_B64;
|
|
|
|
else {
|
2020-07-21 02:20:40 +00:00
|
|
|
printf("** Unsupp encoding %s\n", writep + 27);
|
2020-07-20 21:53:22 +00:00
|
|
|
mime = 1;
|
2020-07-05 20:33:16 +00:00
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
} else if (strstr(writep, "filename=")) {
|
2020-07-20 21:53:22 +00:00
|
|
|
sprintf(filename, "%s/ATTACHMENTS/%s",
|
2020-07-21 02:20:40 +00:00
|
|
|
cfg_emaildir, strstr(writep, "filename=") + 9);
|
2020-07-20 21:53:22 +00:00
|
|
|
sanitize_filename(filename);
|
|
|
|
if (prompt_okay_attachment(filename)) {
|
|
|
|
printf("** Attachment -> %s ", filename);
|
|
|
|
attachfp = fopen(filename, "wb");
|
|
|
|
if (!attachfp)
|
|
|
|
printf("\n** Can't open %s ", filename);
|
|
|
|
} else
|
|
|
|
attachfp = NULL;
|
2020-07-21 02:20:40 +00:00
|
|
|
} else if ((mime == 3) && (!strncmp(writep, "\r", 1))) {
|
2020-07-20 21:53:22 +00:00
|
|
|
mime = 4;
|
|
|
|
if (!attachfp && mime_binary) {
|
|
|
|
mime_enc = ENC_SKIP; // Skip over binary MIME parts with no filename
|
|
|
|
fputs("Skipping ", stdout);
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
readp = writep = NULL;
|
2020-07-20 21:53:22 +00:00
|
|
|
} else if (mime == 4) {
|
|
|
|
switch (mime_enc) {
|
|
|
|
case ENC_QP:
|
2020-07-21 02:20:40 +00:00
|
|
|
chars = decode_quoted_printable(writep);
|
2020-07-20 21:53:22 +00:00
|
|
|
break;
|
|
|
|
case ENC_B64:
|
2020-07-29 03:45:56 +00:00
|
|
|
//chars = decode_base64(writep);
|
|
|
|
{
|
2020-07-29 21:28:35 +00:00
|
|
|
uint8_t i = 0, j = 0;
|
2020-07-29 03:45:56 +00:00
|
|
|
while (writep[i] != '\r') {
|
|
|
|
writep[j++] = b[writep[i]] << 2 | b[writep[i + 1]] >> 4;
|
|
|
|
if (writep[i + 2] != '=')
|
|
|
|
writep[j++] = b[writep[i + 1]] << 4 | b[writep[i + 2]] >> 2;
|
|
|
|
if (linebuf[i + 3] != '=')
|
|
|
|
writep[j++] = b[writep[i + 2]] << 6 | b[writep[i + 3]];
|
|
|
|
i += 4;
|
|
|
|
}
|
|
|
|
chars = j;
|
|
|
|
}
|
2020-07-20 21:53:22 +00:00
|
|
|
break;
|
|
|
|
case ENC_SKIP:
|
2020-07-21 02:20:40 +00:00
|
|
|
readp = writep = NULL;
|
2020-07-20 21:53:22 +00:00
|
|
|
break;
|
2020-07-02 03:07:02 +00:00
|
|
|
}
|
2020-07-20 21:53:22 +00:00
|
|
|
if (mime_binary && !(linecount % 10))
|
|
|
|
spinner();
|
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
if (readp) {
|
2020-07-22 02:54:56 +00:00
|
|
|
if ((mime == 0) || ((mime == 4) && !mime_binary)) {
|
2020-07-22 03:01:02 +00:00
|
|
|
do {
|
|
|
|
c = word_wrap_line(stdout, &readp);
|
|
|
|
if (*cursorrow == 22)
|
|
|
|
break;
|
|
|
|
} while (c == 1);
|
2020-07-22 02:54:56 +00:00
|
|
|
if (readp) {
|
|
|
|
chars = strlen(readp);
|
|
|
|
memmove(linebuf, readp, strlen(readp));
|
|
|
|
readp = linebuf;
|
|
|
|
writep = linebuf + chars;
|
|
|
|
} else
|
|
|
|
writep = NULL;
|
2020-07-21 02:20:40 +00:00
|
|
|
}
|
2020-07-22 02:54:56 +00:00
|
|
|
if ((mime == 4) && mime_binary) {
|
|
|
|
if (attachfp)
|
|
|
|
fwrite(readp, 1, chars, attachfp);
|
2020-07-21 02:20:40 +00:00
|
|
|
readp = writep = NULL;
|
2020-07-20 21:53:22 +00:00
|
|
|
}
|
2020-07-22 02:54:56 +00:00
|
|
|
if (mime == 1) {
|
|
|
|
readp = writep = NULL;
|
2020-07-21 02:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 22:21:10 +00:00
|
|
|
endscreen:
|
2020-07-22 03:01:02 +00:00
|
|
|
if ((*cursorrow == 22) || eof) {
|
2020-07-29 22:21:10 +00:00
|
|
|
printf("\n%c[%07lu] %s | B)ack | T)op | H)drs | M)IME | Q)uit%c",
|
2020-07-21 02:20:40 +00:00
|
|
|
INVERSE,
|
|
|
|
pos,
|
|
|
|
(eof ? " ** END ** " : "SPACE continue reading"),
|
|
|
|
NORMAL);
|
|
|
|
if (sbackfp) {
|
|
|
|
save_screen_to_scrollback(sbackfp);
|
|
|
|
++screennum;
|
|
|
|
++maxscreennum;
|
|
|
|
}
|
2020-07-04 23:51:33 +00:00
|
|
|
retry:
|
2020-07-21 02:20:40 +00:00
|
|
|
c = cgetc();
|
|
|
|
switch (c) {
|
|
|
|
case ' ':
|
|
|
|
if (sbackfp && (screennum < maxscreennum)) {
|
|
|
|
load_screen_from_scrollback(sbackfp, ++screennum);
|
|
|
|
goto retry;
|
|
|
|
} else {
|
|
|
|
if (eof) {
|
2020-07-05 03:22:23 +00:00
|
|
|
putchar(BELL);
|
|
|
|
goto retry;
|
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
}
|
2020-07-20 21:53:22 +00:00
|
|
|
break;
|
2020-07-21 02:20:40 +00:00
|
|
|
case 'B':
|
|
|
|
case 'b':
|
|
|
|
if (sbackfp && (screennum > 1)) {
|
|
|
|
load_screen_from_scrollback(sbackfp, --screennum);
|
|
|
|
goto retry;
|
|
|
|
} else {
|
2020-07-20 21:53:22 +00:00
|
|
|
putchar(BELL);
|
|
|
|
goto retry;
|
2020-06-25 02:05:55 +00:00
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
case 't':
|
|
|
|
mime = 0;
|
|
|
|
pos = h->skipbytes;
|
|
|
|
fseek(fp, pos, SEEK_SET);
|
|
|
|
goto restart;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
case 'h':
|
|
|
|
mime = 0;
|
|
|
|
pos = 0;
|
|
|
|
fseek(fp, pos, SEEK_SET);
|
|
|
|
goto restart;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
case 'm':
|
|
|
|
mime = 1;
|
2020-07-22 01:16:39 +00:00
|
|
|
pos = 0;
|
|
|
|
fseek(fp, pos, SEEK_SET);
|
|
|
|
get_line(fp, 1, linebuf, &pos); // Reset buffer
|
|
|
|
do {
|
|
|
|
get_line(fp, 0, linebuf, &pos);
|
|
|
|
if (!strncasecmp(linebuf, "Content-Transfer-Encoding: ", 27)) {
|
|
|
|
mime = 4;
|
|
|
|
if (!strncmp(linebuf + 27, "7bit", 4))
|
|
|
|
mime_enc = ENC_7BIT;
|
|
|
|
else if (!strncmp(linebuf + 27, "quoted-printable", 16))
|
|
|
|
mime_enc = ENC_QP;
|
|
|
|
else if (!strncmp(linebuf + 27, "base64", 6))
|
|
|
|
mime_enc = ENC_B64;
|
|
|
|
else {
|
|
|
|
printf("** Unsupp encoding %s\n", linebuf + 27);
|
|
|
|
mime = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (linebuf[0] != '\r');
|
2020-07-21 02:20:40 +00:00
|
|
|
pos = h->skipbytes;
|
|
|
|
fseek(fp, pos, SEEK_SET);
|
|
|
|
goto restart;
|
|
|
|
case 'Q':
|
|
|
|
case 'q':
|
|
|
|
if (attachfp)
|
|
|
|
fclose(attachfp);
|
|
|
|
if (sbackfp)
|
|
|
|
fclose(sbackfp);
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
putchar(BELL);
|
|
|
|
goto retry;
|
2020-07-20 21:53:22 +00:00
|
|
|
}
|
2020-07-21 02:20:40 +00:00
|
|
|
clrscr2();
|
|
|
|
}
|
2020-06-25 02:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 03:18:23 +00:00
|
|
|
/*
|
|
|
|
* Write updated email headers to EMAIL.DB
|
|
|
|
*/
|
|
|
|
void write_updated_headers(struct emailhdrs *h, uint16_t pos) {
|
|
|
|
uint16_t l;
|
2020-06-26 15:00:11 +00:00
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, curr_mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
2020-06-26 03:18:23 +00:00
|
|
|
fp = fopen(filename, "rb+");
|
2020-06-26 23:17:05 +00:00
|
|
|
if (!fp)
|
|
|
|
error(ERR_FATAL, "Can't open %s", filename);
|
2020-06-27 00:28:41 +00:00
|
|
|
if (fseek(fp, (pos - 1) * EMAILHDRS_SZ_ON_DISK, SEEK_SET))
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_FATAL, "Can't seek in %s", filename);
|
2020-06-27 00:28:41 +00:00
|
|
|
l = fwrite(h, 1, EMAILHDRS_SZ_ON_DISK, fp);
|
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK)
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_FATAL, "Can't write to %s", filename);
|
2020-06-26 03:18:23 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:29:45 +00:00
|
|
|
/*
|
|
|
|
* Create new mailbox
|
|
|
|
* Create directory, EMAIL.DB and NEXT.EMAIL files
|
|
|
|
*/
|
|
|
|
void new_mailbox(char *mbox) {
|
|
|
|
sprintf(filename, "%s/%s", cfg_emaildir, mbox);
|
|
|
|
if (mkdir(filename)) {
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't create dir %s", filename);
|
2020-06-26 15:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
2020-06-26 15:29:45 +00:00
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (!fp) {
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't create EMAIL.DB");
|
2020-06-26 15:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
sprintf(filename, "%s/%s/NEXT.EMAIL", cfg_emaildir, mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_TXT;
|
|
|
|
_auxtype = 0;
|
2020-06-26 15:29:45 +00:00
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (!fp) {
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't create NEXT.EMAIL");
|
2020-06-26 15:29:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(fp, "1");
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2020-06-26 15:00:11 +00:00
|
|
|
/*
|
|
|
|
* Change current mailbox
|
|
|
|
*/
|
2020-06-27 00:28:41 +00:00
|
|
|
void switch_mailbox(char *mbox) {
|
2020-06-26 23:30:06 +00:00
|
|
|
char prev_mbox[80];
|
|
|
|
uint8_t err;
|
2020-06-29 23:23:53 +00:00
|
|
|
// Treat '.' as shortcut for INBOX
|
|
|
|
if (!strcmp(mbox, "."))
|
|
|
|
strcpy(mbox, "INBOX");
|
2020-06-26 23:30:06 +00:00
|
|
|
strcpy(prev_mbox, curr_mbox);
|
2020-06-26 15:00:11 +00:00
|
|
|
strcpy(curr_mbox, mbox);
|
|
|
|
first_msg = 1;
|
2020-06-26 23:30:06 +00:00
|
|
|
err = read_email_db(first_msg, 1, 1); // Errors non-fatal
|
|
|
|
if (err) {
|
|
|
|
strcpy(curr_mbox, prev_mbox);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-26 15:00:11 +00:00
|
|
|
selection = 1;
|
|
|
|
email_summary();
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:44:21 +00:00
|
|
|
/*
|
|
|
|
* Purge deleted messages from current mailbox
|
|
|
|
*/
|
|
|
|
void purge_deleted(void) {
|
2020-06-27 06:53:46 +00:00
|
|
|
uint16_t count = 0, delcount = 0;
|
|
|
|
struct emailhdrs *h;
|
|
|
|
FILE *fp2;
|
|
|
|
uint16_t l;
|
|
|
|
h = (struct emailhdrs*)malloc(sizeof(struct emailhdrs));
|
|
|
|
if (!h)
|
|
|
|
error(ERR_FATAL, "Can't malloc()");
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, curr_mbox);
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB.NEW", cfg_emaildir, curr_mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
2020-06-27 06:53:46 +00:00
|
|
|
fp2 = fopen(filename, "wb");
|
|
|
|
if (!fp2) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
l = fread(h, 1, EMAILHDRS_SZ_ON_DISK, fp);
|
|
|
|
++count;
|
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK)
|
|
|
|
goto done;
|
|
|
|
if (h->status == 'D') {
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.%u", cfg_emaildir, curr_mbox, h->emailnum);
|
|
|
|
if (unlink(filename)) {
|
|
|
|
error(ERR_NONFATAL, "Can't delete %s", filename);
|
|
|
|
}
|
2020-06-29 22:29:05 +00:00
|
|
|
goto_prompt_row();
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CLRLINE);
|
2020-06-27 06:53:46 +00:00
|
|
|
printf("%u msgs deleted", ++delcount);
|
|
|
|
} else {
|
|
|
|
l = fwrite(h, 1, EMAILHDRS_SZ_ON_DISK, fp2);
|
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK) {
|
|
|
|
error(ERR_NONFATAL, "Can't write to %s", filename);
|
|
|
|
free(h);
|
|
|
|
fclose(fp);
|
|
|
|
fclose(fp2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
free(h);
|
|
|
|
fclose(fp);
|
|
|
|
fclose(fp2);
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, curr_mbox);
|
|
|
|
if (unlink(filename)) {
|
|
|
|
error(ERR_NONFATAL, "Can't delete %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sprintf(userentry, "%s/%s/EMAIL.DB.NEW", cfg_emaildir, curr_mbox);
|
|
|
|
if (rename(userentry, filename)) {
|
|
|
|
error(ERR_NONFATAL, "Can't rename %s", userentry);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-26 17:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 01:00:50 +00:00
|
|
|
/*
|
|
|
|
* Get next email number from NEXT.EMAIL
|
|
|
|
* Returns 1 on error, 0 if all is good
|
|
|
|
*/
|
|
|
|
uint8_t get_next_email(char *mbox, uint16_t *num) {
|
|
|
|
sprintf(filename, "%s/%s/NEXT.EMAIL", cfg_emaildir, mbox);
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s/NEXT.EMAIL", mbox);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fscanf(fp, "%u", num);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update NEXT.EMAIL file
|
|
|
|
*/
|
|
|
|
uint8_t update_next_email(char *mbox, uint16_t num) {
|
|
|
|
sprintf(filename, "%s/%s/NEXT.EMAIL", cfg_emaildir, mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_TXT;
|
|
|
|
_auxtype = 0;
|
2020-06-28 01:00:50 +00:00
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (!fp) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s/NEXT.EMAIL", mbox);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fprintf(fp, "%u", num);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-28 04:25:49 +00:00
|
|
|
/*
|
|
|
|
* Copy string p to q truncating any trailing spaces.
|
|
|
|
*/
|
|
|
|
void truncate_header(char *p, char *q, uint8_t l) {
|
|
|
|
int16_t last_char = -1;
|
|
|
|
uint8_t i;
|
|
|
|
for (i = 0; i < l; ++i) {
|
|
|
|
q[i] = p[i];
|
|
|
|
if ((p[i] != ' ') && (p[i] != '\0'))
|
|
|
|
last_char = i;
|
|
|
|
}
|
|
|
|
q[last_char + 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2020-06-29 19:58:16 +00:00
|
|
|
/*
|
|
|
|
* Parse 'from' address, of the form 'Joe Smith <jsmith@example.org>'
|
|
|
|
* If < > angle brackets are present, just return the text within them,
|
|
|
|
* otherwise return the whole string.
|
|
|
|
* p - input buffer
|
|
|
|
* q - output buffer
|
|
|
|
* Returns 0 if okay, 1 on error
|
|
|
|
*/
|
|
|
|
uint8_t parse_from_addr(char *p, char *q) {
|
|
|
|
char *start = strchr(p, '<');
|
|
|
|
char *end = strchr(p, '>');
|
|
|
|
uint8_t l;
|
|
|
|
if (!start) {
|
|
|
|
strcpy(q, p);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (end)
|
|
|
|
l = end - start - 1;
|
|
|
|
else {
|
|
|
|
error(ERR_NONFATAL, "Bad address format '%s'", p);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strncpy(q, start + 1, l);
|
|
|
|
q[l] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:10:27 +00:00
|
|
|
/*
|
|
|
|
* Prompt for a name in the line below the menu, store it in userentry
|
|
|
|
* Returns number of chars read.
|
|
|
|
* prompt - Message to display before > prompt
|
|
|
|
* is_file - if 1, restrict chars to those allowed in ProDOS filename
|
|
|
|
* Returns number of chars read
|
|
|
|
*/
|
|
|
|
uint8_t prompt_for_name(char *prompt, uint8_t is_file) {
|
|
|
|
uint16_t i;
|
|
|
|
char c;
|
|
|
|
goto_prompt_row();
|
|
|
|
printf("%s>", prompt);
|
|
|
|
i = 0;
|
|
|
|
while (1) {
|
|
|
|
c = cgetc();
|
2020-07-26 23:06:21 +00:00
|
|
|
if (is_file && !isalnum(c) &&
|
|
|
|
(c != RETURN) && (c != BACKSPACE) && (c != DELETE) && (c != '.')) {
|
2020-07-01 04:10:27 +00:00
|
|
|
putchar(BELL);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case RETURN:
|
|
|
|
goto done;
|
|
|
|
case BACKSPACE:
|
|
|
|
case DELETE:
|
|
|
|
if (i > 0) {
|
|
|
|
putchar(BACKSPACE);
|
|
|
|
putchar(' ');
|
|
|
|
putchar(BACKSPACE);
|
|
|
|
--i;
|
|
|
|
} else
|
|
|
|
putchar(BELL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
putchar(c);
|
|
|
|
userentry[i++] = c;
|
|
|
|
}
|
|
|
|
if (i == 79)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
userentry[i] = '\0';
|
|
|
|
putchar(CLRLINE);
|
|
|
|
goto_prompt_row();
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2020-06-28 04:25:49 +00:00
|
|
|
/*
|
|
|
|
* Write email headers for replies and forwarded messages
|
2020-06-29 19:58:16 +00:00
|
|
|
* fp1 - File handle of the mail message being replied/forwarded
|
|
|
|
* fp2 - File handle of the destination mail message
|
|
|
|
* h - headers of the message being replied/forwarded
|
2020-06-28 04:25:49 +00:00
|
|
|
* mode - 'R' for reply, 'F' for forward
|
2020-07-08 04:50:20 +00:00
|
|
|
* fwd_to - Recipient (used for mode=='F' only)
|
2020-06-29 19:58:16 +00:00
|
|
|
* Returns 0 if okay, 1 on error
|
2020-06-28 04:25:49 +00:00
|
|
|
*/
|
2020-07-08 04:50:20 +00:00
|
|
|
uint8_t write_email_headers(FILE *fp1, FILE *fp2, struct emailhdrs *h,
|
|
|
|
char mode, char *fwd_to) {
|
2020-06-29 23:51:34 +00:00
|
|
|
struct datetime dt;
|
2020-06-28 04:25:49 +00:00
|
|
|
fprintf(fp2, "From: %s\r", cfg_emailaddr);
|
|
|
|
truncate_header(h->subject, buf, 80);
|
|
|
|
fprintf(fp2, "Subject: %s: %s\r", (mode == 'F' ? "Fwd" : "Re"), buf);
|
2020-06-29 23:51:34 +00:00
|
|
|
readdatetime((unsigned char*)(SYSTEMTIME), &dt);
|
|
|
|
datetimelong(&dt, buf);
|
|
|
|
fprintf(fp2, "Date: %s\r", buf);
|
2020-07-01 04:10:27 +00:00
|
|
|
if (mode == 'R') {
|
|
|
|
truncate_header(h->from, filename, 80);
|
|
|
|
if (parse_from_addr(filename, buf))
|
|
|
|
return 1;
|
|
|
|
fprintf(fp2, "To: %s\r", buf);
|
2020-07-08 04:50:20 +00:00
|
|
|
} else
|
|
|
|
fprintf(fp2, "To: %s\r", fwd_to);
|
2020-07-01 04:10:27 +00:00
|
|
|
prompt_for_name("cc", 0);
|
2020-07-08 02:04:56 +00:00
|
|
|
if (strlen(userentry) > 0)
|
|
|
|
fprintf(fp2, "cc: %s\r", userentry);
|
2020-07-28 21:14:43 +00:00
|
|
|
fprintf(fp2, "X-Mailer: %s - Apple II Forever!\r\r", PROGNAME);
|
2020-06-28 04:25:49 +00:00
|
|
|
if (mode == 'R') {
|
|
|
|
truncate_header(h->date, buf, 40);
|
|
|
|
fprintf(fp2, "On %s, ", buf);
|
|
|
|
truncate_header(h->from, buf, 80);
|
|
|
|
fprintf(fp2, "%s wrote:\r\r", buf);
|
|
|
|
} else {
|
2020-07-01 00:58:50 +00:00
|
|
|
fprintf(fp2, "-------- Forwarded Message --------\r");
|
2020-06-28 04:25:49 +00:00
|
|
|
truncate_header(h->subject, buf, 80);
|
|
|
|
fprintf(fp2, "Subject: %s\r", buf);
|
|
|
|
truncate_header(h->date, buf, 40);
|
|
|
|
fprintf(fp2, "Date: %s\r", buf);
|
|
|
|
truncate_header(h->from, buf, 80);
|
|
|
|
fprintf(fp2, "From: %s\r", buf);
|
|
|
|
truncate_header(h->to, buf, 80);
|
|
|
|
fprintf(fp2, "To: %s\r\r", buf);
|
|
|
|
}
|
|
|
|
fseek(fp1, h->skipbytes, SEEK_SET); // Skip headers when copying
|
2020-06-29 19:58:16 +00:00
|
|
|
return 0;
|
2020-06-28 04:25:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 00:15:57 +00:00
|
|
|
/*
|
|
|
|
* Prompt ok?
|
|
|
|
*/
|
|
|
|
char prompt_okay(char *msg) {
|
|
|
|
char c;
|
|
|
|
goto_prompt_row();
|
|
|
|
printf("%sSure? (y/n)", msg);
|
|
|
|
while (1) {
|
|
|
|
c = cgetc();
|
|
|
|
if ((c == 'y') || (c == 'Y') || (c == 'n') || (c == 'N'))
|
|
|
|
break;
|
|
|
|
putchar(BELL);
|
|
|
|
}
|
|
|
|
if ((c == 'y') || (c == 'Y'))
|
|
|
|
c = 1;
|
|
|
|
else
|
|
|
|
c = 0;
|
|
|
|
putchar(CLRLINE);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:44:21 +00:00
|
|
|
/*
|
2020-06-27 03:27:59 +00:00
|
|
|
* Copies the current message to mailbox mbox.
|
|
|
|
* h is a pointer to the emailheaders for the message to copy
|
|
|
|
* idx is the index of the message in EMAIL.DB in the source mailbox (1-based)
|
|
|
|
* mbox is the name of the destination mailbox
|
2020-06-28 04:25:49 +00:00
|
|
|
* delete - if set to 1 then the message will be marked as deleted in the
|
|
|
|
* source mbox
|
|
|
|
* mode - 'R' for reply, 'F' for forward, otherwise ' '
|
2020-06-26 17:44:21 +00:00
|
|
|
*/
|
2020-06-28 04:25:49 +00:00
|
|
|
void copy_to_mailbox(struct emailhdrs *h, uint16_t idx,
|
|
|
|
char *mbox, uint8_t delete, char mode) {
|
2020-06-27 06:53:46 +00:00
|
|
|
uint16_t num, buflen, written, l;
|
2020-06-26 17:44:21 +00:00
|
|
|
FILE *fp2;
|
|
|
|
|
2020-07-08 04:50:20 +00:00
|
|
|
if (mode == 'F') {
|
|
|
|
prompt_for_name("Fwd to", 0);
|
|
|
|
if (strlen(userentry) == 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:44:21 +00:00
|
|
|
// Read next number from dest/NEXT.EMAIL
|
2020-06-28 01:00:50 +00:00
|
|
|
if (get_next_email(mbox, &num))
|
2020-06-26 17:44:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Open source email file
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.%u", cfg_emaildir, curr_mbox, h->emailnum);
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp) {
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
2020-06-26 17:44:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open destination email file
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.%u", cfg_emaildir, mbox, num);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_TXT;
|
|
|
|
_auxtype = 0;
|
2020-06-26 17:44:21 +00:00
|
|
|
fp2 = fopen(filename, "wb");
|
|
|
|
if (!fp2) {
|
2020-06-27 03:27:59 +00:00
|
|
|
fclose(fp);
|
2020-06-26 23:17:05 +00:00
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
2020-06-26 17:44:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-28 04:25:49 +00:00
|
|
|
|
|
|
|
if (mode != ' ')
|
2020-07-08 04:50:20 +00:00
|
|
|
if (write_email_headers(fp, fp2, h, mode, userentry)) {
|
2020-06-29 19:58:16 +00:00
|
|
|
error(ERR_NONFATAL, "Invalid email header");
|
|
|
|
fclose(fp);
|
|
|
|
fclose(fp2);
|
|
|
|
return;
|
|
|
|
}
|
2020-06-28 04:25:49 +00:00
|
|
|
|
|
|
|
// Make sure spinner is in the right place
|
2020-06-29 22:29:05 +00:00
|
|
|
if ((mode == 'R') || (mode == 'F'))
|
|
|
|
goto_prompt_row();
|
2020-06-28 04:25:49 +00:00
|
|
|
|
2020-06-26 21:57:38 +00:00
|
|
|
// Copy email
|
|
|
|
putchar(' '); // For spinner
|
2020-06-26 18:12:58 +00:00
|
|
|
while (1) {
|
|
|
|
buflen = fread(buf, 1, READSZ, fp);
|
2020-06-26 21:57:38 +00:00
|
|
|
spinner();
|
2020-06-26 18:12:58 +00:00
|
|
|
if (buflen == 0)
|
|
|
|
break;
|
|
|
|
written = fwrite(buf, 1, buflen, fp2);
|
|
|
|
if (written != buflen) {
|
2020-06-28 04:25:49 +00:00
|
|
|
error(ERR_NONFATAL, "Write error during copy");
|
2020-06-26 18:12:58 +00:00
|
|
|
fclose(fp);
|
|
|
|
fclose(fp2);
|
|
|
|
return;
|
2020-06-26 17:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(BACKSPACE);
|
2020-06-26 21:57:38 +00:00
|
|
|
putchar(' ');
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(BACKSPACE);
|
2020-06-26 18:12:58 +00:00
|
|
|
|
2020-06-26 17:44:21 +00:00
|
|
|
fclose(fp);
|
|
|
|
fclose(fp2);
|
|
|
|
|
2020-07-01 04:10:27 +00:00
|
|
|
// Update dest/EMAIL.DB unless this is R)eply or F)orward
|
|
|
|
// The upshot of this is we never create EMAIL.DB in OUTBOX
|
|
|
|
if (mode == ' ') {
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, mbox);
|
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
|
|
|
fp = fopen(filename, "ab");
|
|
|
|
if (!fp) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s/EMAIL.DB", mbox);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buflen = h->emailnum; // Just reusing buflen as a temporary
|
|
|
|
h->emailnum = num;
|
|
|
|
l = fwrite(h, 1, EMAILHDRS_SZ_ON_DISK, fp);
|
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK) {
|
|
|
|
error(ERR_NONFATAL, "Can't write to %s/EMAIL.DB %u %u", mbox);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
h->emailnum = buflen ;
|
|
|
|
fclose(fp);
|
2020-06-27 06:53:46 +00:00
|
|
|
}
|
2020-06-26 17:44:21 +00:00
|
|
|
|
|
|
|
// Update dest/NEXT.EMAIL, incrementing count by 1
|
2020-06-28 01:00:50 +00:00
|
|
|
if (update_next_email(mbox, num + 1))
|
2020-06-26 17:44:21 +00:00
|
|
|
return;
|
|
|
|
|
2020-06-27 03:27:59 +00:00
|
|
|
if (delete)
|
2020-06-26 17:44:21 +00:00
|
|
|
h->status = 'D';
|
2020-06-27 03:27:59 +00:00
|
|
|
h->tag = ' '; // Untag files after copy or move
|
|
|
|
write_updated_headers(h, idx);
|
|
|
|
email_summary_for(selection);
|
2020-06-28 04:25:49 +00:00
|
|
|
|
|
|
|
if (mode != ' ') {
|
|
|
|
// Not really an error but useful to have an alert
|
|
|
|
sprintf(filename, "Created %s %s/OUTBOX/EMAIL.%u",
|
|
|
|
(mode == 'R' ? "reply" : "fwded msg"), cfg_emaildir, num);
|
|
|
|
error(ERR_NONFATAL, filename);
|
2020-07-16 00:15:57 +00:00
|
|
|
if (prompt_okay("Open in editor - ")) {
|
|
|
|
sprintf(filename, "%s/OUTBOX/EMAIL.%u", cfg_emaildir, num);
|
|
|
|
load_editor();
|
|
|
|
}
|
2020-06-28 04:25:49 +00:00
|
|
|
}
|
2020-06-26 17:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 08:15:04 +00:00
|
|
|
/*
|
|
|
|
* Return index into EMAIL.DB for current selection.
|
|
|
|
*/
|
|
|
|
uint16_t get_db_index(void) {
|
|
|
|
if (!reverse)
|
|
|
|
return first_msg + selection - 1;
|
|
|
|
else
|
2020-07-08 01:33:56 +00:00
|
|
|
return total_msgs - (first_msg + selection - 1) + 1;
|
2020-07-07 08:15:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-27 03:27:59 +00:00
|
|
|
/*
|
|
|
|
* Check if there are tagged messages. If not, just call copy_to_mailbox()
|
|
|
|
* on the current message. If they are, prompt the user and, if affirmative,
|
|
|
|
* iterate through the tagged messages calling copy_to_mailbox() on each.
|
|
|
|
*/
|
|
|
|
uint8_t copy_to_mailbox_tagged(char *mbox, uint8_t delete) {
|
2020-06-27 03:42:03 +00:00
|
|
|
uint16_t count = 0, tagcount = 0;
|
2020-06-27 03:27:59 +00:00
|
|
|
struct emailhdrs *h;
|
|
|
|
uint16_t l;
|
|
|
|
if (total_tag == 0) {
|
|
|
|
h = get_headers(selection);
|
2020-07-07 08:15:04 +00:00
|
|
|
copy_to_mailbox(h, get_db_index(), mbox, delete, ' ');
|
2020-06-27 03:27:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-27 06:53:46 +00:00
|
|
|
sprintf(filename, "%u tagged - ", total_tag);
|
2020-06-27 03:27:59 +00:00
|
|
|
if (!prompt_okay(filename))
|
|
|
|
return 0;
|
|
|
|
h = (struct emailhdrs*)malloc(sizeof(struct emailhdrs));
|
|
|
|
if (!h)
|
|
|
|
error(ERR_FATAL, "Can't malloc()");
|
|
|
|
while (1) {
|
|
|
|
sprintf(filename, "%s/%s/EMAIL.DB", cfg_emaildir, curr_mbox);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_BIN;
|
|
|
|
_auxtype = 0;
|
2020-06-27 03:27:59 +00:00
|
|
|
fp = fopen(filename, "rb+");
|
|
|
|
if (!fp) {
|
2020-07-07 05:36:49 +00:00
|
|
|
free(h);
|
2020-06-27 03:27:59 +00:00
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (fseek(fp, count * EMAILHDRS_SZ_ON_DISK, SEEK_SET)) {
|
|
|
|
error(ERR_NONFATAL, "Can't seek in %s", filename);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
l = fread(h, 1, EMAILHDRS_SZ_ON_DISK, fp);
|
|
|
|
fclose(fp);
|
|
|
|
++count;
|
|
|
|
if (l != EMAILHDRS_SZ_ON_DISK) {
|
|
|
|
free(h);
|
|
|
|
read_email_db(first_msg, 1, 0);
|
|
|
|
email_summary();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (h->tag == 'T') {
|
2020-06-27 03:47:05 +00:00
|
|
|
h->tag = ' '; // Don't want it tagged in the destination
|
2020-06-29 22:29:05 +00:00
|
|
|
goto_prompt_row();
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(CLRLINE);
|
2020-06-27 03:42:03 +00:00
|
|
|
printf("%u/%u:", ++tagcount, total_tag);
|
2020-06-28 04:25:49 +00:00
|
|
|
copy_to_mailbox(h, count, mbox, delete, ' ');
|
2020-06-27 03:27:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
err:
|
|
|
|
free(h);
|
|
|
|
fclose(fp);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-06-28 01:00:50 +00:00
|
|
|
/*
|
|
|
|
* Create a blank outgoing message and put it in OUTBOX.
|
|
|
|
* OUTBOX is not a 'proper' mailbox (no EMAIL.DB)
|
|
|
|
*/
|
|
|
|
void create_blank_outgoing() {
|
2020-06-29 23:51:34 +00:00
|
|
|
struct datetime dt;
|
2020-06-28 01:00:50 +00:00
|
|
|
uint16_t num;
|
|
|
|
|
|
|
|
// Read next number from dest/NEXT.EMAIL
|
2020-06-28 04:25:49 +00:00
|
|
|
if (get_next_email("OUTBOX", &num))
|
2020-06-28 01:00:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Open destination email file
|
2020-06-28 04:25:49 +00:00
|
|
|
sprintf(filename, "%s/OUTBOX/EMAIL.%u", cfg_emaildir, num);
|
2020-06-29 19:58:16 +00:00
|
|
|
_filetype = PRODOS_T_TXT;
|
|
|
|
_auxtype = 0;
|
2020-06-28 01:00:50 +00:00
|
|
|
fp = fopen(filename, "wb");
|
|
|
|
if (!fp) {
|
|
|
|
error(ERR_NONFATAL, "Can't open %s", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-30 17:28:00 +00:00
|
|
|
fprintf(fp, "From: %s\r", cfg_emailaddr);
|
2020-06-29 23:23:53 +00:00
|
|
|
prompt_for_name("To", 0);
|
|
|
|
if (strlen(userentry) == 0)
|
|
|
|
return;
|
2020-06-30 17:28:00 +00:00
|
|
|
fprintf(fp, "To: %s\r", userentry);
|
2020-06-29 22:29:05 +00:00
|
|
|
prompt_for_name("Subject", 0);
|
2020-06-30 17:28:00 +00:00
|
|
|
fprintf(fp, "Subject: %s\r", userentry);
|
2020-06-29 23:51:34 +00:00
|
|
|
readdatetime((unsigned char*)(SYSTEMTIME), &dt);
|
|
|
|
datetimelong(&dt, userentry);
|
2020-06-30 17:28:00 +00:00
|
|
|
fprintf(fp, "Date: %s\r", userentry);
|
2020-06-29 22:29:05 +00:00
|
|
|
prompt_for_name("cc", 0);
|
2020-07-08 04:50:20 +00:00
|
|
|
if (strlen(userentry) > 0)
|
|
|
|
fprintf(fp, "cc: %s\r", userentry);
|
2020-07-28 21:14:43 +00:00
|
|
|
fprintf(fp, "X-Mailer: %s - Apple II Forever!\r\r", PROGNAME);
|
2020-06-28 01:00:50 +00:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
// Update dest/NEXT.EMAIL, incrementing count by 1
|
2020-06-28 04:25:49 +00:00
|
|
|
if (update_next_email("OUTBOX", num + 1))
|
2020-06-28 01:00:50 +00:00
|
|
|
return;
|
|
|
|
|
2020-07-26 23:06:21 +00:00
|
|
|
sprintf(filename, "Open %s/OUTBOX/EMAIL.%u in editor - ", cfg_emaildir, num);
|
|
|
|
if (prompt_okay(filename)) {
|
2020-07-16 00:15:57 +00:00
|
|
|
sprintf(filename, "%s/OUTBOX/EMAIL.%u", cfg_emaildir, num);
|
|
|
|
load_editor();
|
|
|
|
}
|
2020-07-15 21:21:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 00:25:31 +00:00
|
|
|
/*
|
|
|
|
* Keyboard handler
|
|
|
|
*/
|
|
|
|
void keyboard_hdlr(void) {
|
2020-06-26 01:47:03 +00:00
|
|
|
struct emailhdrs *h;
|
2020-07-08 03:09:47 +00:00
|
|
|
char c;
|
2020-06-25 00:25:31 +00:00
|
|
|
while (1) {
|
2020-07-08 03:09:47 +00:00
|
|
|
h = get_headers(selection);
|
|
|
|
c = cgetc();
|
2020-06-25 00:25:31 +00:00
|
|
|
switch (c) {
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
2020-06-29 23:54:57 +00:00
|
|
|
case UPARROW:
|
2020-06-25 00:25:31 +00:00
|
|
|
if (selection > 1) {
|
|
|
|
prevselection = selection;
|
|
|
|
--selection;
|
|
|
|
update_highlighted();
|
2020-06-26 01:47:03 +00:00
|
|
|
} else if (first_msg > MSGS_PER_PAGE) {
|
|
|
|
first_msg -= MSGS_PER_PAGE;
|
2020-06-26 23:30:06 +00:00
|
|
|
read_email_db(first_msg, 0, 0);
|
2020-06-26 01:47:03 +00:00
|
|
|
selection = num_msgs;
|
|
|
|
email_summary();
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-06-27 03:47:05 +00:00
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
if (h) {
|
|
|
|
if (h->tag == 'T') {
|
|
|
|
h->tag = ' ';
|
|
|
|
--total_tag;
|
|
|
|
} else {
|
|
|
|
h->tag = 'T';
|
|
|
|
++total_tag;
|
|
|
|
}
|
2020-07-07 08:15:04 +00:00
|
|
|
write_updated_headers(h, get_db_index());
|
2020-06-27 03:47:05 +00:00
|
|
|
email_summary_for(selection);
|
|
|
|
status_bar();
|
|
|
|
}
|
|
|
|
// Fallthrough so tagging also moves down!!!
|
2020-06-25 00:25:31 +00:00
|
|
|
case 'j':
|
|
|
|
case 'J':
|
2020-06-29 23:54:57 +00:00
|
|
|
case DOWNARROW:
|
2020-06-25 00:25:31 +00:00
|
|
|
if (selection < num_msgs) {
|
|
|
|
prevselection = selection;
|
|
|
|
++selection;
|
|
|
|
update_highlighted();
|
2020-06-30 04:43:54 +00:00
|
|
|
} else if (first_msg + selection - 1 < total_msgs) {
|
2020-06-26 01:47:03 +00:00
|
|
|
first_msg += MSGS_PER_PAGE;
|
2020-06-26 23:30:06 +00:00
|
|
|
read_email_db(first_msg, 0, 0);
|
2020-06-26 01:47:03 +00:00
|
|
|
selection = 1;
|
|
|
|
email_summary();
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
break;
|
2020-06-29 23:23:53 +00:00
|
|
|
case RETURN:
|
2020-06-25 03:08:40 +00:00
|
|
|
case ' ':
|
2020-06-26 03:18:23 +00:00
|
|
|
if (h) {
|
|
|
|
if (h->status == 'N')
|
|
|
|
--total_new;
|
|
|
|
h->status = 'R'; // Mark email read
|
2020-07-07 08:15:04 +00:00
|
|
|
write_updated_headers(h, get_db_index());
|
2020-07-08 03:09:47 +00:00
|
|
|
email_pager(h);
|
|
|
|
email_summary();
|
2020-06-26 03:18:23 +00:00
|
|
|
}
|
2020-06-25 02:05:55 +00:00
|
|
|
break;
|
2020-06-26 01:47:03 +00:00
|
|
|
case 'd':
|
|
|
|
case 'D':
|
2020-06-26 03:18:23 +00:00
|
|
|
if (h) {
|
2020-06-26 01:47:03 +00:00
|
|
|
h->status = 'D';
|
2020-07-07 08:15:04 +00:00
|
|
|
write_updated_headers(h, get_db_index());
|
2020-06-26 03:18:23 +00:00
|
|
|
email_summary_for(selection);
|
|
|
|
}
|
2020-06-26 01:47:03 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
2020-06-26 03:18:23 +00:00
|
|
|
if (h) {
|
2020-06-26 01:47:03 +00:00
|
|
|
h->status = 'R';
|
2020-07-07 08:15:04 +00:00
|
|
|
write_updated_headers(h, get_db_index());
|
2020-06-26 03:18:23 +00:00
|
|
|
email_summary_for(selection);
|
|
|
|
}
|
2020-06-26 01:47:03 +00:00
|
|
|
break;
|
2020-06-26 17:44:21 +00:00
|
|
|
case 'c':
|
|
|
|
case 'C':
|
2020-07-08 03:09:47 +00:00
|
|
|
if (h)
|
|
|
|
if (prompt_for_name("Copy to mbox", 1))
|
|
|
|
copy_to_mailbox_tagged(userentry, 0);
|
2020-06-26 17:44:21 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2020-07-08 03:09:47 +00:00
|
|
|
if (h)
|
|
|
|
if (prompt_for_name("Move to mbox", 1))
|
|
|
|
copy_to_mailbox_tagged(userentry, 1);
|
2020-06-26 17:44:21 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
2020-07-08 03:09:47 +00:00
|
|
|
if (h) {
|
|
|
|
goto_prompt_row();
|
|
|
|
copy_to_mailbox_tagged("RECEIVED", 1);
|
|
|
|
}
|
2020-06-26 17:44:21 +00:00
|
|
|
break;
|
2020-06-26 01:47:03 +00:00
|
|
|
case 'p':
|
|
|
|
case 'P':
|
2020-07-08 03:09:47 +00:00
|
|
|
if (h) {
|
|
|
|
if (prompt_okay("Purge - ")) {
|
|
|
|
purge_deleted();
|
|
|
|
first_msg = 1;
|
|
|
|
read_email_db(first_msg, 1, 0);
|
|
|
|
selection = 1;
|
|
|
|
email_summary();
|
|
|
|
}
|
2020-06-30 14:56:38 +00:00
|
|
|
}
|
2020-06-26 01:47:03 +00:00
|
|
|
break;
|
2020-07-08 03:09:47 +00:00
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
if (h)
|
|
|
|
copy_to_mailbox(h, get_db_index(), "OUTBOX", 0, 'R');
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
if (h)
|
|
|
|
copy_to_mailbox(h, get_db_index(), "OUTBOX", 0, 'F');
|
|
|
|
break;
|
|
|
|
// Everything above here needs a selected message (h != NULL)
|
|
|
|
// Everything below here does NOT need a selected message
|
2020-06-26 15:29:45 +00:00
|
|
|
case 'n':
|
|
|
|
case 'N':
|
2020-06-29 22:29:05 +00:00
|
|
|
if (prompt_for_name("New mbox", 1))
|
2020-06-26 21:57:38 +00:00
|
|
|
new_mailbox(userentry);
|
2020-06-26 15:30:30 +00:00
|
|
|
break;
|
2020-06-27 00:28:41 +00:00
|
|
|
case 's':
|
|
|
|
case 'S':
|
2020-06-29 22:29:05 +00:00
|
|
|
if (prompt_for_name("Switch mbox", 1))
|
2020-06-27 00:28:41 +00:00
|
|
|
switch_mailbox(userentry);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
case 'W':
|
2020-06-28 01:00:50 +00:00
|
|
|
create_blank_outgoing();
|
2020-06-27 00:28:41 +00:00
|
|
|
break;
|
2020-07-07 06:23:07 +00:00
|
|
|
case ',':
|
|
|
|
case '<':
|
|
|
|
reverse = 1;
|
|
|
|
switch_mailbox(curr_mbox);
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
case '>':
|
|
|
|
reverse = 0;
|
|
|
|
switch_mailbox(curr_mbox);
|
|
|
|
break;
|
2020-07-19 22:03:01 +00:00
|
|
|
case 0x80 + 'e': // OA-E "Open message in editor"
|
|
|
|
case 0x80 + 'E':
|
2020-07-16 21:36:01 +00:00
|
|
|
sprintf(filename, "%s/%s/EMAIL.%u", cfg_emaildir, curr_mbox, h->emailnum);
|
|
|
|
load_editor();
|
|
|
|
break;
|
2020-07-19 22:03:01 +00:00
|
|
|
case 0x80 + 'r': // OA-R "Retrieve messages from server"
|
|
|
|
case 0x80 + 'R':
|
2020-07-15 23:45:25 +00:00
|
|
|
load_pop65();
|
|
|
|
break;
|
2020-07-19 22:03:01 +00:00
|
|
|
case 0x80 + 's': // OA-S "Send messages in Outbox to server"
|
|
|
|
case 0x80 + 'S':
|
2020-07-15 23:45:25 +00:00
|
|
|
load_smtp65();
|
|
|
|
break;
|
2020-06-25 00:25:31 +00:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
2020-06-27 06:53:46 +00:00
|
|
|
if (prompt_okay("Quit - ")) {
|
2020-07-06 03:52:35 +00:00
|
|
|
clrscr2();
|
2020-06-26 21:57:38 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
2020-06-25 00:25:31 +00:00
|
|
|
default:
|
2020-06-25 02:05:55 +00:00
|
|
|
//printf("[%02x]", c);
|
2020-06-29 23:23:53 +00:00
|
|
|
putchar(BELL);
|
2020-06-25 00:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-15 21:21:59 +00:00
|
|
|
|
2020-06-24 21:22:35 +00:00
|
|
|
void main(void) {
|
2020-07-06 03:31:38 +00:00
|
|
|
uint8_t *pp;
|
|
|
|
pp = (uint8_t*)0xbf98;
|
|
|
|
if (!(*pp & 0x02))
|
|
|
|
error(ERR_FATAL, "Need 80 cols");
|
|
|
|
if ((*pp & 0x30) != 0x30)
|
|
|
|
error(ERR_FATAL, "Need 128K");
|
2020-06-24 21:22:35 +00:00
|
|
|
videomode(VIDEOMODE_80COL);
|
|
|
|
readconfigfile();
|
2020-07-07 06:23:07 +00:00
|
|
|
reverse = 0;
|
2020-06-26 01:47:03 +00:00
|
|
|
first_msg = 1;
|
2020-06-26 23:30:06 +00:00
|
|
|
read_email_db(first_msg, 1, 0);
|
2020-06-25 00:25:31 +00:00
|
|
|
selection = 1;
|
2020-06-24 21:22:35 +00:00
|
|
|
email_summary();
|
2020-06-25 00:25:31 +00:00
|
|
|
keyboard_hdlr();
|
2020-06-24 21:22:35 +00:00
|
|
|
}
|
|
|
|
|