Commit ae4073c4 ae4073c42137f3b823d81b95ec3f7df62a525dd0 by Jakob Kaivo

More mail(x) tweaking

Begin the great IMAP4d rewrite - most of the infrastructure is there, now
	just fill in the blanks
1 parent 4e0913bf
......@@ -2,7 +2,6 @@ AUTOMAKE_OPTIONS = gnu 1.4
ACLOCAL_AMFLAGS = -I m4
# imap4d
SUBDIRS = include doc m4 lib mailbox from mail pop3d
#SUBDIRS = imap4d
SUBDIRS = include doc m4 lib mailbox from mail pop3d imap4d
EXTRA_DIST = mailutils.spec mailutils.spec.in
......
......@@ -72,6 +72,6 @@ AC_CHECK_FUNC(socket, [true],
dnl Output Makefiles
AC_OUTPUT(Makefile mailutils.spec include/Makefile include/mailutils/Makefile
m4/Makefile doc/Makefile lib/Makefile mailbox/Makefile
m4/Makefile doc/Makefile lib/Makefile mailbox/Makefile imap4d/Makefile
mailbox/include/Makefile from/Makefile mail/Makefile pop3d/Makefile)
......
INCLUDES =-I$(srcdir) -I$(top_srcdir)/lib -I$(top_srcdir)/libmailbox
INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/include
CPPFLAGS = -Wall -pedantic -ansi
sbin_PROGRAMS = imap4d
imap4d_SOURCES = capability.c \
login.c \
logout.c \
main.c \
noop.c
imap4d_LDADD = ../libmailbox/libmailbox.la \
../lib/libmailutils.a \
@AUTHLIBS@
noinst_HEADERS = capability.h \
imap.h \
imap_commands.h \
imap_types.h \
login.h \
logout.h \
noop.h
imap4d_LDADD = ../mailbox/libmailbox.la ../lib/libmailutils.a @AUTHLIBS@
imap4d_SOURCES = imap4d.c imap4d.h capability.c noop.c logout.c authenticate.c \
login.c select.c examine.c create.c delete.c rename.c subscribe.c \
unsubscribe.c list.c lsub.c status.c append.c check.c close.c \
expunge.c search.c fetch.c store.c copy.c uid.c util.c commands.c
......
- finish command retrieval restructuring
- see how many globals I can kill
- make it compile
- make it work
- use libmailbox and friends
- testing
- testing
- testing
- optimize
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_append (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_authenticate (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
#include "capability.h"
STATUS imap_capability(Command *command) {
if( command->args )
return BAD;
fprintf(output, "* CAPABILITY IMAP4rev1\r\n");
return OK;
}
#ifndef _MY_IMAP_CAPABILITY
#define _MY_IMAP_CAPABILITY
#include "imap.h"
STATUS imap_capability(Command *command);
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_check (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_close (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
const struct imap4d_command imap4d_command_table [] = {
{ "capability", imap4d_capability, STATE_ALL },
{ "noop", imap4d_noop, STATE_ALL },
{ "logout", imap4d_logout, STATE_ALL },
{ "authenticate", imap4d_authenticate, STATE_NONAUTH },
{ "login", imap4d_login, STATE_NONAUTH },
{ "select", imap4d_select, STATE_AUTH | STATE_SEL },
{ "examine", imap4d_examine, STATE_AUTH | STATE_SEL },
{ "create", imap4d_create, STATE_AUTH | STATE_SEL },
{ "delete", imap4d_delete, STATE_AUTH | STATE_SEL },
{ "rename", imap4d_rename, STATE_AUTH | STATE_SEL },
{ "subscribe", imap4d_subscribe, STATE_AUTH | STATE_SEL },
{ "unsubscribe", imap4d_unsubscribe, STATE_AUTH | STATE_SEL },
{ "list", imap4d_list, STATE_AUTH | STATE_SEL },
{ "lsub", imap4d_lsub, STATE_AUTH | STATE_SEL },
{ "status", imap4d_status, STATE_AUTH | STATE_SEL },
{ "append", imap4d_append, STATE_AUTH | STATE_SEL },
{ "check", imap4d_check, STATE_SEL },
{ "close", imap4d_close, STATE_SEL },
{ "expunge", imap4d_expunge, STATE_SEL },
{ "search", imap4d_search, STATE_SEL },
{ "fetch", imap4d_fetch, STATE_SEL },
{ "store", imap4d_store, STATE_SEL },
{ "copy", imap4d_copy, STATE_SEL },
{ "uid", imap4d_uid, STATE_SEL },
{ 0, 0, 0}
};
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_copy (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_create (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_delete (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_examine (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_expunge (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_fetch (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
#ifndef _MY_IMAP
#define _MY_IMAP
#include <stdio.h>
#include <syslog.h>
#include "imap_types.h"
FILE *input;
FILE *output;
/* current server state */
STATES state;
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
int ifile;
int ofile;
int
main (int argc, char **argv)
{
chdir ("/");
openlog ("imap4d", LOG_PID, LOG_MAIL);
/* for each incoming connection */
{
char *remote_host = "";
ifile = fileno (stdin);
ofile = fileno (stdout);
syslog (LOG_INFO, "Incoming connection from %s", remote_host);
util_out (NULL, TAG_NONE, "IMAP4rev1 GNU " PACKAGE " " VERSION);
while (1)
{
char *cmd = util_getline ();
/* check for updates */
util_do_command (cmd);
free (cmd);
}
}
closelog ();
return 0;
}
#ifndef _MY_IMAP_COMMANDS
#define _MY_IMAP_COMMANDS
#include "capability.h"
#include "login.h"
#include "logout.h"
#include "noop.h"
#endif
#ifndef _MY_IMAP_TYPES
#define _MY_IMAP_TYPES
/* possible server states */
enum _STATES {NON_AUTH, AUTH, SELECTED, LOGOUT};
/* imap command status codes */
enum _STATUS {BAD, NO, OK};
typedef enum _STATES STATES;
typedef enum _STATUS STATUS;
typedef struct _Command {
char tag[16];
char cmd[16];
char *args;
} Command;
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_list (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
#define _XOPEN_SOURCE
#include <unistd.h>
#include <ctype.h>
#include <pwd.h>
#include <shadow.h>
#include <string.h>
#include <sys/types.h>
#include "login.h"
STATUS imap_login(Command *command) {
char user[256];
char *pass;
struct passwd *pw;
struct spwd *shadow;
size_t len;
if( state != NON_AUTH )
return BAD;
if( command->args == NULL )
return BAD;
len = strcspn(command->args, " ");
if( len == 0 || len > 255 )
return BAD;
memset(user, '\0', 256);
strncpy(user, command->args, len);
pass = command->args + len;
++pass; /* skip the space */
if( isspace(pass[0]) )
return BAD;
pw = getpwnam(user);
if ( pw == NULL ) {
syslog(LOG_WARNING, "invalid user: %s\n", user);
return NO;
}
if( strcmp(pw->pw_passwd, crypt(pass, pw->pw_passwd)) ) {
shadow = getspnam(user);
if( shadow == NULL ) {
syslog(LOG_WARNING, "invalid user: %s\n", user);
return NO;
}
if( strcmp(shadow->sp_pwdp, crypt(pass, shadow->sp_pwdp)) ) {
syslog(LOG_WARNING, "invalid user: %s\n", user);
return NO;
}
}
memset(pass, '\0', sizeof(pass)); /* paranoia */
state = AUTH;
syslog(LOG_INFO, "user: %s, logged in\n", user);
return OK;
}
#ifndef _MY_IMAP_LOGIN
#define _MY_IMAP_LOGIN
#include "imap.h"
STATUS imap_login(Command *command);
#endif
#include "logout.h"
STATUS imap_logout(Command *command) {
if( command->args )
return BAD;
/* finish everything */
fprintf(output, "* BYE IMAP4rev1 terminating connection\r\n");
#ifdef STATISTICS
statistics();
#endif
state = LOGOUT;
return OK;
}
#ifndef _MY_IMAP_LOGOUT
#define _MY_IMAP_LOGOUT
#include "imap.h"
STATUS imap_logout(Command *command);
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_lsub (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "imap.h"
#include "imap_commands.h"
#include "command.h"
static void init(void);
static int mainloop(void);
static void cleanup(void);
static void greeting(void);
static Command* parse_command(char *string);
#define action(cmd) cmd->info->action ? cmd->info->action(cmd) : BAD;
#define free_command(cmd) free(cmd);
/* state the server is in */
STATES state = NON_AUTH;
FILE *input = NULL;
FILE *output = NULL;
/* output strings, maps STATUS to strings */
static char *status_word[3] = {"BAD", "NO", "OK"};
static char *status_code[3] = {"invalid", "failed", "completed"};
static imap4_cmd_t COMMANDS[] = {
{ "capability", imap_capability, , , },
{ "noop" , imap_noop, , , },
{ "logout" , imap_logout, , , },
{ "login" , imap_login, , , },
{ "", , NULL, , , }
};
int main(int argc, char *argv[]) {
input = stdin;
output = stdout;
init();
greeting();
mainloop();
cleanup();
fclose(input);
fclose(output);
return 0;
}
static void init(void) {
struct group *gr;
gr = getgrnam("mail");
if( gr == NULL )
exit(-1);
if( setgid(gr->gr_gid) == -1 )
exit(-1);
openlog("GNU IMAP4", LOG_PID, LOG_MAIL);
if( output == stdout )
setvbuf(output, NULL, _IOLBF, 0);
}
static void cleanup(void) {
closelog();
}
static void greeting(void) {
fprintf(output, "* OK IMAP4rev1 Service Ready\r\n");
}
static int mainloop(void) {
char *client_string = NULL;
size_t len = 0;
Command *command;
STATUS status;
while( getline(&client_string, &len, input) != -1 ) {
command = parse_command(client_string);
if(command) {
status = action(command);
fprintf(output, "%s %s %s %s\r\n", command->tag, status_word[status],
command->cmd, status_code[status]);
free_command(command);
}
else {
/* FIXME: properly handle this */
fprintf(output, "");
}
if( state == LOGOUT ) /* all done, let's go */
break;
}
free(client_string);
return 1;
}
imap4_action_t check_command(const char *command) {
for(i = 0; COMMANDS[i]; ++i) {
if(strcasecmp(command, COMMANDS[i].cmd) == 0)
return COMMANDS[i];
}
return NULL;
}
Command* parse_command(char *string) {
size_t len, tmp;
Command *command;
if( string == NULL )
return NULL;
command = malloc(sizeof(Command));
memset(command, '\0', sizeof(Command));
len = strcspn(string, " ");
if( len > 0 ) {
tmp = strcspn(string, "\r\n");
string[tmp] = '\0';
strncpy(command->tag, string, len > 15 ? 15 : len);
} else {
strcpy(command->tag, "gibberish");
return command;
}
tmp = len;
++tmp; /* skip space char */
len = strcspn(string + tmp, " ");
if( len > 0 )
strncpy(command->cmd, string + tmp, len > 15 ? 15 : len);
else {
strcpy(command->cmd, "gibberish");
return command;
}
/* args is the rest of the string */
command->args = string + len + tmp + 1;
if( command->args[0] == '\0' ) /* no args */
command->args = NULL;
command->info = check_command(command->cmd);
return command;
}
#include "noop.h"
STATUS imap_noop(Command *command) {
if( command->args )
return BAD;
fprintf(output, "* OK XSTATE %d\r\n", state);
return OK;
}
#ifndef _MY_IMAP_NOOP
#define _MY_IMAP_NOOP
#include "imap.h"
STATUS imap_noop(Command *command);
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_rename (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_search (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_select (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_status (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_store (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_subscribe (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_uid (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
/*
*
*/
int
imap4d_unsubscribe (int argc, char **argv)
{
util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
return util_finish (argc, argv, RESP_BAD, "Command not implemented");
}
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "imap4d.h"
extern int ifile;
extern int ofile;
int
util_out (char *seq, int tag, char *f, ...)
{
char *buf = NULL;
int len = 0;
va_list ap;
va_start (ap, f);
len = vasprintf (&buf, f, ap);
if (tag == TAG_SEQ)
{
write (ofile, seq, strlen (seq));
write (ofile, " ", 1);
}
else if (tag == TAG_NONE)
write (ofile, "* ", 2);
write (ofile, buf, len);
write (ofile, "\r\n", 2);
free (buf);
return 0;
}
int
util_finish (int argc, char **argv, int resp, char *f, ...)
{
char *buf = NULL, *buf2 = NULL, *code = NULL;
int len = 0;
va_list ap;
va_start (ap, f);
switch (resp)
{
case RESP_OK:
code = strdup ("OK");
break;
case RESP_BAD:
code = strdup ("BAD");
break;
case RESP_NO:
code = strdup ("NO");
break;
default:
code = strdup ("X-BUG");
}
vasprintf (&buf, f, ap);
len = asprintf (&buf2, "%s %s %s %s\r\n", argv[0], code, argv[1], buf);
write (ofile, buf2, len);
free (buf);
free (buf2);
free (code);
return resp;
}
char *
util_getline (void)
{
fd_set rfds;
struct timeval tv;
char buf[1024], *ret = NULL;
int fd = ifile, len = 0;
FD_ZERO (&rfds);
FD_SET (fd, &rfds);
tv.tv_sec = 30;
tv.tv_usec = 0;
memset (buf, '\0', 1024);
do
{
if (!select (fd + 1, &rfds, NULL, NULL, &tv))
return NULL;
else if (read (fd, buf, 1024) < 1)
exit (1); /* FIXME: dead socket */
else if (ret == NULL)
{
ret = malloc ((strlen (buf + 1) * sizeof (char)));
strcpy (ret, buf);
}
else
{
ret = realloc (ret, (strlen (ret) + strlen (buf) + 1) *
sizeof (char));
strcat (ret, buf);
}
}
while (strchr (buf, '\n') == NULL);
for (len = strlen (ret); len > 0; len--)
if (ret[len] == '\r' || ret[len] == '\n')
ret[len] = '\0';
return ret;
}
int
util_do_command (char *cmd)
{
if (cmd == NULL)
return 0;
else
{
int argc = 0, status = 0, i = 0, len;
char **argv = NULL;
struct imap4d_command *command = NULL;
if (argcv_get (cmd, &argc, &argv) != 0)
{
argcv_free (argc, argv);
return 1;
}
util_start (argv[0]);
if (argc < 2)
{
util_finish (argc, argv, RESP_BAD,
"Client did not send space after tag");
argcv_free (argc, argv);
return 1;
}
len = strlen (argv[1]);
for (i=0; i < len; i++)
argv[1][i] = tolower (argv[1][i]);
for (i=0; command == NULL && imap4d_command_table[i].name != 0; i++)
{
if (strlen (imap4d_command_table[i].name) == len &&
!strcmp (imap4d_command_table[i].name, argv[1]))
command = &imap4d_command_table[i];
}
for (i=0; i < len; i++)
argv[1][i] = toupper (argv[1][i]);
if (command == NULL)
{
util_finish (argc, argv, RESP_BAD, "Invalid command");
argcv_free (argc, argv);
return 1;
}
else if (! (command->states & util_getstate ()))
{
util_finish (argc, argv, RESP_BAD, "Incorrect state");
argcv_free (argc, argv);
return 1;
}
else
{
status = command->func (argc, argv);
argcv_free (argc, argv);
return status;
}
}
return 1;
}
int
util_start (char *seq)
{
return 0;
}
int
util_getstate (void)
{
return STATE_NONAUTH;
}
noinst_LIBRARIES = libmailutils.a
libmailutils_a_SOURCES = getopt.c getopt1.c md5.c getline.c snprintf.c \
xstrdup.c xstrtol.c xmalloc.c
xstrdup.c xstrtol.c xmalloc.c argcv.c
noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h
noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h argcv.h
CFLAGS = -Wall -pedantic -g -DTESTING
......
......@@ -10,4 +10,4 @@ mail_SOURCES = alias.c alt.c bang.c cd.c copy.c delete.c discard.c dp.c \
headers.c help.c hold.c if.c list.c mail.c mail.h mbox.c next.c pipe.c \
previous.c print.c printall.c quit.c relist.c reply.c retain.c save.c \
send.c set.c shell.c size.c source.c top.c touch.c unalias.c \
undelete.c unset.c util.c visual.c write.c z.c table.h
undelete.c unset.c util.c visual.c write.c z.c table.c
......
......@@ -16,7 +16,6 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
#include "table.h"
/*
* hel[p] [command]
......
......@@ -16,7 +16,6 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
#include "table.h"
/*
* l[ist]
......
......@@ -52,11 +52,24 @@ extern "C" {
# endif
#endif /*__P */
/* Global variables */
/* Type definitions */
#ifndef Function
typedef int Function ();
#endif
struct mail_command_entry {
char *shortname;
char *longname;
Function *func;
char *synopsis;
};
/* Global variables and constants*/
mailbox_t mbox;
unsigned int cursor;
unsigned int realcursor;
unsigned int total;
extern const struct mail_command_entry mail_command_table[];
/* Functions */
int mail_alias __P((int argc, char **argv));
......@@ -106,15 +119,14 @@ int mail_z __P((int argc, char **argv));
int mail_bang __P((int argc, char **argv)); /* command ! */
int mail_eq __P((int argc, char **argv)); /* command = */
int util_get_argcv __P((const char *command, int *argc, char ***argv));
int util_expand_msglist __P((const int argc, char **argv, int **list));
int util_do_command __P((const char *cmd));
int util_msglist_command __P((int (*func)(int, char**), int argc, char **argv));
Function* util_command_get __P((char *cmd));
int util_free_argv __P((int argc, char **argv));
char **util_command_completion __P((char *cmd, int start, int end));
char *util_command_generator __P((char *text, int state));
char *util_stripwhite __P((char *string));
struct mail_command_entry util_find_entry __P((char *cmd));
#ifdef __cplusplus
}
......
......@@ -15,33 +15,9 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifndef _TABLE_H
#define _TABLE_H 1
#include "mail.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef __P
# ifdef __STDC__
# define __P(args) args
# else
# define __P(args) ()
# endif
#endif /*__P */
struct mail_command_entry {
char *shortname;
char *longname;
Function *func;
char *synopsis;
};
struct mail_command_entry util_find_entry __P((char *cmd));
static struct mail_command_entry mail_command_table[] = {
const struct mail_command_entry mail_command_table[] = {
{ "a", "alias", mail_alias,
"a[lias] [alias [address...]]" },
{ "g", "group", mail_alias,
......@@ -122,10 +98,3 @@ static struct mail_command_entry mail_command_table[] = {
{ "#", "#", NULL, "# comment" },
{ 0, 0, 0, 0,}
};
#ifdef __cplusplus
}
#endif
#endif /* _TABLE_H */
......
......@@ -16,49 +16,6 @@
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
#include "table.h"
/*
* takes a string and splits it into several strings, breaking at ' '
* command is the string to split
* the number of strings is placed into argc
* the split strings are put into argv
* returns 0 on success, nonzero on failure
*/
int
util_get_argcv (const char *command, int *argc, char ***argv)
{
int len = strlen (command);
int i = 0, j = 0;
int start = 0;
if (len < 1)
return 1;
*argc = 1;
for (i = 0; i < len; i++)
if (command[i] == ' ')
(*argc)++;
*argv = malloc ((*argc + 1) * sizeof (char *));
for (i = 0; i <= len; i++)
{
if (command[i] == ' ' || command[i] == '\0')
{
(*argv)[j] = malloc ((i-start) * sizeof (char));
if (argv[j] == NULL && (i-start > 0))
return 1;
strncpy ((*argv)[j], &command[start], i-start);
(*argv)[j][i-start] = '\0';
j++;
start = i+1;
}
}
return 0;
}
/*
* expands a standard message list into an array of numbers
......@@ -142,20 +99,6 @@ util_expand_msglist (const int argc, char **argv, int **list)
}
/*
* frees all elements of an argv array
* argc is the number of elements
* argv is the array
*/
int
util_free_argv (int argc, char **argv)
{
while (--argc >= 0)
free (argv[argc]);
free (argv);
return 1;
}
/*
* expands command into its command and arguments, then runs command
* cmd is the command to parse and run
* returns exit status of the command
......@@ -173,8 +116,8 @@ util_do_command (const char *cmd)
if (cmd)
{
if (util_get_argcv (cmd, &argc, &argv) != 0)
return util_free_argv (argc, argv);
if (argcv_get (cmd, &argc, &argv) != 0)
return argcv_free (argc, argv);
command = util_command_get (argv[0]);
}
else
......@@ -188,7 +131,7 @@ util_do_command (const char *cmd)
status = 1;
}
util_free_argv (argc, argv);
argcv_free (argc, argv);
return status;
}
......@@ -242,7 +185,9 @@ util_find_entry (char *cmd)
{
sl = strlen (mail_command_table[i].shortname);
ll = strlen (mail_command_table[i].longname);
if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
if (sl > ll && !strncmp (mail_command_table[i].shortname, cmd, sl))
return mail_command_table[i];
else if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
return mail_command_table[i];
else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len))
return mail_command_table[i];
......@@ -279,9 +224,9 @@ util_command_generator (char *text, int state)
while ((name = mail_command_table[i].longname))
{
if (strlen (mail_command_table[i].shortname) > strlen(name))
name = mail_command_table[i].shortname;
i++;
/*if (strlen (mail_command_table[i].shortname) > strlen(name))
name = mail_command_table[i].shortname; */
if (strncmp (name, text, len) == 0)
return (strdup(name));
}
......
......@@ -110,11 +110,7 @@ main (int argc, char **argv)
pop3_daemon_init ();
/* change directories */
#ifdef MAILSPOOLHOME
chdir ("/");
#else
chdir (_PATH_MAILDIR);
#endif
/* Set up for syslog */
openlog ("gnu-pop3d", LOG_PID, LOG_MAIL);
......