#include <pwd.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include <string.h>
#include <ctype.h>
#include <sm/bitops.h>
#include "conf.h"
typedef int bool;
#define FALSE 0
#define TRUE 1
bool Debug = FALSE;
char *myname;
char *homedir;
char *AliasList[MAXLINE];
char *fromp;
char *fromuser;
int AliasCount = 0;
static char *newstr();
int ask(char *);
int sendmessage(char *);
void AutoInstall(void);
void usrerr(const char *, ...);
int
main(argc, argv)
int argc;
char **argv;
{
register char *p;
struct passwd *pw;
extern char *getfrom();
while (--argc > 0 && (p = *++argv) != NULL && *p == '-')
{
switch (*++p)
{
case 'd':
Debug = TRUE;
break;
default:
usrerr("Unknown flag -%s", p);
exit(EX_USAGE);
}
}
if (argc != 1)
{
if (argc == 0)
AutoInstall();
else
usrerr("Usage: mailcompat username (or) mailcompat -r");
exit(EX_USAGE);
}
myname = p;
pw = getpwnam(myname);
if (pw == NULL)
{
usrerr("user: %s look up failed, name services outage ?", myname);
exit(EX_TEMPFAIL);
}
homedir = newstr(pw->pw_dir);
fromuser = getfrom(&fromp);
return (sendmessage(fromuser));
}
#define L_HEADER "Content-Length:"
#define LL_HEADER 15
int
sendmessage(from)
char *from;
{
static char line[MAXLINE];
static char command[MAXLINE];
bool in_body = FALSE;
FILE *mail_fp;
static char user_name[L_cuserid];
if (from == NULL)
from = cuserid(user_name);
snprintf(command, sizeof (command), "/bin/mail -f %s -d %s", from,
myname);
mail_fp = popen(command, "w");
while (fgets(line, sizeof line, stdin) != NULL)
{
if (line[0] == (char)'\n')
in_body = TRUE;
if (in_body && (strncmp(line, "From ", 5) == 0))
fprintf(mail_fp, ">");
if (in_body || (strncasecmp(line, L_HEADER, LL_HEADER) != 0))
fputs(line, mail_fp);
}
return (pclose(mail_fp));
}
char *
getfrom(shortp)
char **shortp;
{
static char line[MAXLINE];
register char *p, *start, *at, *bang;
char saveat;
if (fgets(line, sizeof line, stdin) == NULL ||
strncmp(line, "From ", 5) != 0)
{
usrerr("No initial From line");
exit(EX_USAGE);
}
start = &line[5];
p = strchr(start, ' ');
if (p == NULL)
{
usrerr("Funny From line '%s'", line);
exit(EX_USAGE);
}
*p = '\0';
#ifdef VDEBUG
printf("start='%s'\n", start);
#endif
*shortp = start;
if ((at = strchr(start, '@')) == NULL)
at = p;
saveat = *at;
*at = '\0';
if ((bang = strrchr(start, '!')) != NULL) {
char *bang2;
*bang = '\0';
if ((bang2 = strrchr(start, '!')) != NULL)
*shortp = bang2 + 1;
*bang = '!';
}
*at = saveat;
#ifdef VDEBUG
printf("place='%s'\n", *shortp);
#endif
return newstr(start);
}
void
usrerr(const char *f, ...)
{
va_list alist;
va_start(alist, f);
(void) fprintf(stderr, "mailcompat: ");
(void) vfprintf(stderr, f, alist);
(void) fprintf(stderr, "\n");
va_end(alist);
}
char *
newstr(s)
char *s;
{
char *p;
size_t psize = strlen(s) + 1;
p = malloc(psize);
if (p == NULL)
{
usrerr("newstr: cannot alloc memory");
exit(EX_OSERR);
}
strlcpy(p, s, psize);
return (p);
}
void
AutoInstall()
{
char forward[MAXLINE];
char line[MAXLINE];
static char user_name[L_cuserid];
FILE *f;
myname = cuserid(user_name);
homedir = getenv("HOME");
if (homedir == NULL) {
usrerr("Home directory unknown");
exit(EX_CONFIG);
}
printf("This program can be used to store your mail in a format\n");
printf("that you can read with SunOS 4.X based mail readers\n");
(void) strlcpy(forward, homedir, sizeof (forward));
(void) strlcat(forward, "/.forward", sizeof (forward));
f = fopen(forward, "r");
if (f) {
printf("You have a .forward file in your home directory");
printf(" containing:\n");
while (fgets(line, MAXLINE, f))
printf(" %s", line);
fclose(f);
if (!ask("Would you like to remove it and disable the mailcompat feature"))
exit(0);
if (unlink(forward))
perror("Error removing .forward file:");
else
printf("Back to normal reception of mail.\n");
exit(0);
}
printf("To enable the mailcompat feature a \".forward\" ");
printf("file is created.\n");
if (!ask("Would you like to enable the mailcompat feature")) {
printf("OK, mailcompat feature NOT enabled.\n");
exit(0);
}
f = fopen(forward, "w");
if (f == NULL) {
perror("Error opening .forward file");
exit(EX_USAGE);
}
fprintf(f, "\"|/usr/bin/mailcompat %s\"\n", myname);
fclose(f);
printf("Mailcompat feature ENABLED.");
printf("Run mailcompat with no arguments to remove it\n");
}
int
ask(prompt)
char *prompt;
{
char line[MAXLINE];
for (;;) {
printf("%s? ", prompt);
fflush(stdout);
fgets(line, sizeof (line), stdin);
if (line[0] == 'y' || line[0] == 'Y')
return (TRUE);
if (line[0] == 'n' || line[0] == 'N')
return (FALSE);
printf("Please reply \"yes\" or \"no\" (\'y\' or \'n\')\n");
}
}