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 ...@@ -2,7 +2,6 @@ AUTOMAKE_OPTIONS = gnu 1.4
2 ACLOCAL_AMFLAGS = -I m4 2 ACLOCAL_AMFLAGS = -I m4
3 3
4 # imap4d 4 # imap4d
5 SUBDIRS = include doc m4 lib mailbox from mail pop3d 5 SUBDIRS = include doc m4 lib mailbox from mail pop3d imap4d
6 #SUBDIRS = imap4d
7 6
8 EXTRA_DIST = mailutils.spec mailutils.spec.in 7 EXTRA_DIST = mailutils.spec mailutils.spec.in
......
...@@ -72,6 +72,6 @@ AC_CHECK_FUNC(socket, [true], ...@@ -72,6 +72,6 @@ AC_CHECK_FUNC(socket, [true],
72 72
73 dnl Output Makefiles 73 dnl Output Makefiles
74 AC_OUTPUT(Makefile mailutils.spec include/Makefile include/mailutils/Makefile 74 AC_OUTPUT(Makefile mailutils.spec include/Makefile include/mailutils/Makefile
75 m4/Makefile doc/Makefile lib/Makefile mailbox/Makefile 75 m4/Makefile doc/Makefile lib/Makefile mailbox/Makefile imap4d/Makefile
76 mailbox/include/Makefile from/Makefile mail/Makefile pop3d/Makefile) 76 mailbox/include/Makefile from/Makefile mail/Makefile pop3d/Makefile)
77 77
......
1 INCLUDES =-I$(srcdir) -I$(top_srcdir)/lib -I$(top_srcdir)/libmailbox 1 INCLUDES = -I$(top_srcdir)/lib -I$(top_srcdir)/include
2 CPPFLAGS = -Wall -pedantic -ansi 2 CPPFLAGS = -Wall -pedantic -ansi
3 3
4 sbin_PROGRAMS = imap4d 4 sbin_PROGRAMS = imap4d
5
6 imap4d_SOURCES = capability.c \
7 login.c \
8 logout.c \
9 main.c \
10 noop.c
11
12 imap4d_LDADD = ../libmailbox/libmailbox.la \
13 ../lib/libmailutils.a \
14 @AUTHLIBS@
15
16 noinst_HEADERS = capability.h \
17 imap.h \
18 imap_commands.h \
19 imap_types.h \
20 login.h \
21 logout.h \
22 noop.h
23 5
6 imap4d_LDADD = ../mailbox/libmailbox.la ../lib/libmailutils.a @AUTHLIBS@
7 imap4d_SOURCES = imap4d.c imap4d.h capability.c noop.c logout.c authenticate.c \
8 login.c select.c examine.c create.c delete.c rename.c subscribe.c \
9 unsubscribe.c list.c lsub.c status.c append.c check.c close.c \
10 expunge.c search.c fetch.c store.c copy.c uid.c util.c commands.c
......
1 - finish command retrieval restructuring
2 - see how many globals I can kill
3 - make it compile
4 - make it work
5 - use libmailbox and friends
6 - testing
7 - testing
8 - testing
9 - optimize
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_append (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_authenticate (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 #include "capability.h"
2
3 STATUS imap_capability(Command *command) {
4
5 if( command->args )
6 return BAD;
7
8 fprintf(output, "* CAPABILITY IMAP4rev1\r\n");
9
10 return OK;
11 }
1 #ifndef _MY_IMAP_CAPABILITY
2 #define _MY_IMAP_CAPABILITY
3
4 #include "imap.h"
5
6 STATUS imap_capability(Command *command);
7
8 #endif
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_check (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_close (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 const struct imap4d_command imap4d_command_table [] = {
21 { "capability", imap4d_capability, STATE_ALL },
22 { "noop", imap4d_noop, STATE_ALL },
23 { "logout", imap4d_logout, STATE_ALL },
24 { "authenticate", imap4d_authenticate, STATE_NONAUTH },
25 { "login", imap4d_login, STATE_NONAUTH },
26 { "select", imap4d_select, STATE_AUTH | STATE_SEL },
27 { "examine", imap4d_examine, STATE_AUTH | STATE_SEL },
28 { "create", imap4d_create, STATE_AUTH | STATE_SEL },
29 { "delete", imap4d_delete, STATE_AUTH | STATE_SEL },
30 { "rename", imap4d_rename, STATE_AUTH | STATE_SEL },
31 { "subscribe", imap4d_subscribe, STATE_AUTH | STATE_SEL },
32 { "unsubscribe", imap4d_unsubscribe, STATE_AUTH | STATE_SEL },
33 { "list", imap4d_list, STATE_AUTH | STATE_SEL },
34 { "lsub", imap4d_lsub, STATE_AUTH | STATE_SEL },
35 { "status", imap4d_status, STATE_AUTH | STATE_SEL },
36 { "append", imap4d_append, STATE_AUTH | STATE_SEL },
37 { "check", imap4d_check, STATE_SEL },
38 { "close", imap4d_close, STATE_SEL },
39 { "expunge", imap4d_expunge, STATE_SEL },
40 { "search", imap4d_search, STATE_SEL },
41 { "fetch", imap4d_fetch, STATE_SEL },
42 { "store", imap4d_store, STATE_SEL },
43 { "copy", imap4d_copy, STATE_SEL },
44 { "uid", imap4d_uid, STATE_SEL },
45 { 0, 0, 0}
46 };
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_copy (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_create (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_delete (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_examine (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_expunge (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_fetch (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 #ifndef _MY_IMAP
2 #define _MY_IMAP
3
4 #include <stdio.h>
5 #include <syslog.h>
6
7 #include "imap_types.h"
8
9 FILE *input;
10 FILE *output;
11
12 /* current server state */
13 STATES state;
14
15 #endif
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 int ifile;
21 int ofile;
22
23 int
24 main (int argc, char **argv)
25 {
26 chdir ("/");
27 openlog ("imap4d", LOG_PID, LOG_MAIL);
28
29 /* for each incoming connection */
30 {
31 char *remote_host = "";
32 ifile = fileno (stdin);
33 ofile = fileno (stdout);
34
35 syslog (LOG_INFO, "Incoming connection from %s", remote_host);
36 util_out (NULL, TAG_NONE, "IMAP4rev1 GNU " PACKAGE " " VERSION);
37 while (1)
38 {
39 char *cmd = util_getline ();
40 /* check for updates */
41 util_do_command (cmd);
42 free (cmd);
43 }
44 }
45
46 closelog ();
47 return 0;
48 }
1 #ifndef _MY_IMAP_COMMANDS
2 #define _MY_IMAP_COMMANDS
3
4 #include "capability.h"
5 #include "login.h"
6 #include "logout.h"
7 #include "noop.h"
8
9 #endif
1 #ifndef _MY_IMAP_TYPES
2 #define _MY_IMAP_TYPES
3
4 /* possible server states */
5 enum _STATES {NON_AUTH, AUTH, SELECTED, LOGOUT};
6
7 /* imap command status codes */
8 enum _STATUS {BAD, NO, OK};
9
10 typedef enum _STATES STATES;
11 typedef enum _STATUS STATUS;
12
13 typedef struct _Command {
14 char tag[16];
15 char cmd[16];
16 char *args;
17 } Command;
18
19 #endif
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_list (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 #define _XOPEN_SOURCE
2 #include <unistd.h>
3 #include <ctype.h>
4 #include <pwd.h>
5 #include <shadow.h>
6 #include <string.h>
7 #include <sys/types.h>
8
9 #include "login.h"
10
11 STATUS imap_login(Command *command) {
12
13 char user[256];
14 char *pass;
15 struct passwd *pw;
16 struct spwd *shadow;
17 size_t len;
18
19 if( state != NON_AUTH )
20 return BAD;
21 if( command->args == NULL )
22 return BAD;
23
24 len = strcspn(command->args, " ");
25 if( len == 0 || len > 255 )
26 return BAD;
27 memset(user, '\0', 256);
28 strncpy(user, command->args, len);
29 pass = command->args + len;
30 ++pass; /* skip the space */
31 if( isspace(pass[0]) )
32 return BAD;
33 pw = getpwnam(user);
34 if ( pw == NULL ) {
35 syslog(LOG_WARNING, "invalid user: %s\n", user);
36 return NO;
37 }
38 if( strcmp(pw->pw_passwd, crypt(pass, pw->pw_passwd)) ) {
39 shadow = getspnam(user);
40 if( shadow == NULL ) {
41 syslog(LOG_WARNING, "invalid user: %s\n", user);
42 return NO;
43 }
44 if( strcmp(shadow->sp_pwdp, crypt(pass, shadow->sp_pwdp)) ) {
45 syslog(LOG_WARNING, "invalid user: %s\n", user);
46 return NO;
47 }
48 }
49 memset(pass, '\0', sizeof(pass)); /* paranoia */
50 state = AUTH;
51 syslog(LOG_INFO, "user: %s, logged in\n", user);
52 return OK;
53 }
1 #ifndef _MY_IMAP_LOGIN
2 #define _MY_IMAP_LOGIN
3
4 #include "imap.h"
5
6 STATUS imap_login(Command *command);
7
8 #endif
1 #include "logout.h"
2
3 STATUS imap_logout(Command *command) {
4
5 if( command->args )
6 return BAD;
7
8 /* finish everything */
9
10 fprintf(output, "* BYE IMAP4rev1 terminating connection\r\n");
11 #ifdef STATISTICS
12 statistics();
13 #endif
14
15 state = LOGOUT;
16
17 return OK;
18 }
1 #ifndef _MY_IMAP_LOGOUT
2 #define _MY_IMAP_LOGOUT
3
4 #include "imap.h"
5
6 STATUS imap_logout(Command *command);
7
8 #endif
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_lsub (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 #include <grp.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #include "imap.h"
9 #include "imap_commands.h"
10 #include "command.h"
11
12 static void init(void);
13 static int mainloop(void);
14 static void cleanup(void);
15 static void greeting(void);
16 static Command* parse_command(char *string);
17
18 #define action(cmd) cmd->info->action ? cmd->info->action(cmd) : BAD;
19
20 #define free_command(cmd) free(cmd);
21
22 /* state the server is in */
23 STATES state = NON_AUTH;
24
25 FILE *input = NULL;
26 FILE *output = NULL;
27
28 /* output strings, maps STATUS to strings */
29 static char *status_word[3] = {"BAD", "NO", "OK"};
30 static char *status_code[3] = {"invalid", "failed", "completed"};
31
32 static imap4_cmd_t COMMANDS[] = {
33 { "capability", imap_capability, , , },
34 { "noop" , imap_noop, , , },
35 { "logout" , imap_logout, , , },
36 { "login" , imap_login, , , },
37 { "", , NULL, , , }
38 };
39
40
41 int main(int argc, char *argv[]) {
42
43 input = stdin;
44 output = stdout;
45
46 init();
47
48 greeting();
49
50 mainloop();
51
52 cleanup();
53
54 fclose(input);
55 fclose(output);
56
57 return 0;
58 }
59
60 static void init(void) {
61
62 struct group *gr;
63
64 gr = getgrnam("mail");
65 if( gr == NULL )
66 exit(-1);
67 if( setgid(gr->gr_gid) == -1 )
68 exit(-1);
69 openlog("GNU IMAP4", LOG_PID, LOG_MAIL);
70 if( output == stdout )
71 setvbuf(output, NULL, _IOLBF, 0);
72 }
73
74 static void cleanup(void) {
75 closelog();
76 }
77
78 static void greeting(void) {
79
80 fprintf(output, "* OK IMAP4rev1 Service Ready\r\n");
81 }
82
83 static int mainloop(void) {
84
85 char *client_string = NULL;
86 size_t len = 0;
87 Command *command;
88 STATUS status;
89
90 while( getline(&client_string, &len, input) != -1 ) {
91 command = parse_command(client_string);
92 if(command) {
93 status = action(command);
94 fprintf(output, "%s %s %s %s\r\n", command->tag, status_word[status],
95 command->cmd, status_code[status]);
96 free_command(command);
97 }
98 else {
99 /* FIXME: properly handle this */
100 fprintf(output, "");
101 }
102
103 if( state == LOGOUT ) /* all done, let's go */
104 break;
105 }
106 free(client_string);
107
108 return 1;
109 }
110
111 imap4_action_t check_command(const char *command) {
112
113 for(i = 0; COMMANDS[i]; ++i) {
114 if(strcasecmp(command, COMMANDS[i].cmd) == 0)
115 return COMMANDS[i];
116 }
117 return NULL;
118 }
119
120 Command* parse_command(char *string) {
121
122 size_t len, tmp;
123 Command *command;
124
125 if( string == NULL )
126 return NULL;
127
128 command = malloc(sizeof(Command));
129 memset(command, '\0', sizeof(Command));
130
131 len = strcspn(string, " ");
132 if( len > 0 ) {
133 tmp = strcspn(string, "\r\n");
134 string[tmp] = '\0';
135 strncpy(command->tag, string, len > 15 ? 15 : len);
136 } else {
137 strcpy(command->tag, "gibberish");
138 return command;
139 }
140
141 tmp = len;
142 ++tmp; /* skip space char */
143 len = strcspn(string + tmp, " ");
144 if( len > 0 )
145 strncpy(command->cmd, string + tmp, len > 15 ? 15 : len);
146 else {
147 strcpy(command->cmd, "gibberish");
148 return command;
149 }
150 /* args is the rest of the string */
151 command->args = string + len + tmp + 1;
152
153 if( command->args[0] == '\0' ) /* no args */
154 command->args = NULL;
155
156 command->info = check_command(command->cmd);
157
158 return command;
159 }
1 #include "noop.h"
2
3 STATUS imap_noop(Command *command) {
4
5 if( command->args )
6 return BAD;
7
8 fprintf(output, "* OK XSTATE %d\r\n", state);
9
10 return OK;
11 }
1 #ifndef _MY_IMAP_NOOP
2 #define _MY_IMAP_NOOP
3
4 #include "imap.h"
5
6 STATUS imap_noop(Command *command);
7
8 #endif
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_rename (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_search (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_select (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_status (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_store (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_subscribe (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_uid (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 /*
21 *
22 */
23
24 int
25 imap4d_unsubscribe (int argc, char **argv)
26 {
27 util_out (argv[0], TAG_NONE, "BAD %s Command not implemented", argv[1]);
28 return util_finish (argc, argv, RESP_BAD, "Command not implemented");
29 }
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #include "imap4d.h"
19
20 extern int ifile;
21 extern int ofile;
22
23 int
24 util_out (char *seq, int tag, char *f, ...)
25 {
26 char *buf = NULL;
27 int len = 0;
28 va_list ap;
29 va_start (ap, f);
30 len = vasprintf (&buf, f, ap);
31
32 if (tag == TAG_SEQ)
33 {
34 write (ofile, seq, strlen (seq));
35 write (ofile, " ", 1);
36 }
37 else if (tag == TAG_NONE)
38 write (ofile, "* ", 2);
39
40 write (ofile, buf, len);
41 write (ofile, "\r\n", 2);
42 free (buf);
43
44 return 0;
45 }
46
47 int
48 util_finish (int argc, char **argv, int resp, char *f, ...)
49 {
50 char *buf = NULL, *buf2 = NULL, *code = NULL;
51 int len = 0;
52 va_list ap;
53 va_start (ap, f);
54
55 switch (resp)
56 {
57 case RESP_OK:
58 code = strdup ("OK");
59 break;
60 case RESP_BAD:
61 code = strdup ("BAD");
62 break;
63 case RESP_NO:
64 code = strdup ("NO");
65 break;
66 default:
67 code = strdup ("X-BUG");
68 }
69
70 vasprintf (&buf, f, ap);
71 len = asprintf (&buf2, "%s %s %s %s\r\n", argv[0], code, argv[1], buf);
72
73 write (ofile, buf2, len);
74
75 free (buf);
76 free (buf2);
77 free (code);
78
79 return resp;
80 }
81
82 char *
83 util_getline (void)
84 {
85 fd_set rfds;
86 struct timeval tv;
87 char buf[1024], *ret = NULL;
88 int fd = ifile, len = 0;
89
90 FD_ZERO (&rfds);
91 FD_SET (fd, &rfds);
92 tv.tv_sec = 30;
93 tv.tv_usec = 0;
94 memset (buf, '\0', 1024);
95
96 do
97 {
98 if (!select (fd + 1, &rfds, NULL, NULL, &tv))
99 return NULL;
100 else if (read (fd, buf, 1024) < 1)
101 exit (1); /* FIXME: dead socket */
102 else if (ret == NULL)
103 {
104 ret = malloc ((strlen (buf + 1) * sizeof (char)));
105 strcpy (ret, buf);
106 }
107 else
108 {
109 ret = realloc (ret, (strlen (ret) + strlen (buf) + 1) *
110 sizeof (char));
111 strcat (ret, buf);
112 }
113 }
114 while (strchr (buf, '\n') == NULL);
115
116 for (len = strlen (ret); len > 0; len--)
117 if (ret[len] == '\r' || ret[len] == '\n')
118 ret[len] = '\0';
119
120 return ret;
121 }
122
123 int
124 util_do_command (char *cmd)
125 {
126 if (cmd == NULL)
127 return 0;
128 else
129 {
130 int argc = 0, status = 0, i = 0, len;
131 char **argv = NULL;
132 struct imap4d_command *command = NULL;
133
134 if (argcv_get (cmd, &argc, &argv) != 0)
135 {
136 argcv_free (argc, argv);
137 return 1;
138 }
139
140
141 util_start (argv[0]);
142
143 if (argc < 2)
144 {
145 util_finish (argc, argv, RESP_BAD,
146 "Client did not send space after tag");
147 argcv_free (argc, argv);
148 return 1;
149 }
150
151 len = strlen (argv[1]);
152 for (i=0; i < len; i++)
153 argv[1][i] = tolower (argv[1][i]);
154
155 for (i=0; command == NULL && imap4d_command_table[i].name != 0; i++)
156 {
157 if (strlen (imap4d_command_table[i].name) == len &&
158 !strcmp (imap4d_command_table[i].name, argv[1]))
159 command = &imap4d_command_table[i];
160 }
161
162 for (i=0; i < len; i++)
163 argv[1][i] = toupper (argv[1][i]);
164
165 if (command == NULL)
166 {
167 util_finish (argc, argv, RESP_BAD, "Invalid command");
168 argcv_free (argc, argv);
169 return 1;
170 }
171 else if (! (command->states & util_getstate ()))
172 {
173 util_finish (argc, argv, RESP_BAD, "Incorrect state");
174 argcv_free (argc, argv);
175 return 1;
176 }
177 else
178 {
179 status = command->func (argc, argv);
180 argcv_free (argc, argv);
181 return status;
182 }
183 }
184 return 1;
185 }
186
187 int
188 util_start (char *seq)
189 {
190 return 0;
191 }
192
193 int
194 util_getstate (void)
195 {
196 return STATE_NONAUTH;
197 }
1 noinst_LIBRARIES = libmailutils.a 1 noinst_LIBRARIES = libmailutils.a
2 2
3 libmailutils_a_SOURCES = getopt.c getopt1.c md5.c getline.c snprintf.c \ 3 libmailutils_a_SOURCES = getopt.c getopt1.c md5.c getline.c snprintf.c \
4 xstrdup.c xstrtol.c xmalloc.c 4 xstrdup.c xstrtol.c xmalloc.c argcv.c
5 5
6 noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h 6 noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h argcv.h
7 7
8 CFLAGS = -Wall -pedantic -g -DTESTING 8 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 \ ...@@ -10,4 +10,4 @@ mail_SOURCES = alias.c alt.c bang.c cd.c copy.c delete.c discard.c dp.c \
10 headers.c help.c hold.c if.c list.c mail.c mail.h mbox.c next.c pipe.c \ 10 headers.c help.c hold.c if.c list.c mail.c mail.h mbox.c next.c pipe.c \
11 previous.c print.c printall.c quit.c relist.c reply.c retain.c save.c \ 11 previous.c print.c printall.c quit.c relist.c reply.c retain.c save.c \
12 send.c set.c shell.c size.c source.c top.c touch.c unalias.c \ 12 send.c set.c shell.c size.c source.c top.c touch.c unalias.c \
13 undelete.c unset.c util.c visual.c write.c z.c table.h 13 undelete.c unset.c util.c visual.c write.c z.c table.c
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #include "mail.h" 18 #include "mail.h"
19 #include "table.h"
20 19
21 /* 20 /*
22 * hel[p] [command] 21 * hel[p] [command]
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #include "mail.h" 18 #include "mail.h"
19 #include "table.h"
20 19
21 /* 20 /*
22 * l[ist] 21 * l[ist]
......
...@@ -52,11 +52,24 @@ extern "C" { ...@@ -52,11 +52,24 @@ extern "C" {
52 # endif 52 # endif
53 #endif /*__P */ 53 #endif /*__P */
54 54
55 /* Global variables */ 55 /* Type definitions */
56 #ifndef Function
57 typedef int Function ();
58 #endif
59
60 struct mail_command_entry {
61 char *shortname;
62 char *longname;
63 Function *func;
64 char *synopsis;
65 };
66
67 /* Global variables and constants*/
56 mailbox_t mbox; 68 mailbox_t mbox;
57 unsigned int cursor; 69 unsigned int cursor;
58 unsigned int realcursor; 70 unsigned int realcursor;
59 unsigned int total; 71 unsigned int total;
72 extern const struct mail_command_entry mail_command_table[];
60 73
61 /* Functions */ 74 /* Functions */
62 int mail_alias __P((int argc, char **argv)); 75 int mail_alias __P((int argc, char **argv));
...@@ -106,15 +119,14 @@ int mail_z __P((int argc, char **argv)); ...@@ -106,15 +119,14 @@ int mail_z __P((int argc, char **argv));
106 int mail_bang __P((int argc, char **argv)); /* command ! */ 119 int mail_bang __P((int argc, char **argv)); /* command ! */
107 int mail_eq __P((int argc, char **argv)); /* command = */ 120 int mail_eq __P((int argc, char **argv)); /* command = */
108 121
109 int util_get_argcv __P((const char *command, int *argc, char ***argv));
110 int util_expand_msglist __P((const int argc, char **argv, int **list)); 122 int util_expand_msglist __P((const int argc, char **argv, int **list));
111 int util_do_command __P((const char *cmd)); 123 int util_do_command __P((const char *cmd));
112 int util_msglist_command __P((int (*func)(int, char**), int argc, char **argv)); 124 int util_msglist_command __P((int (*func)(int, char**), int argc, char **argv));
113 Function* util_command_get __P((char *cmd)); 125 Function* util_command_get __P((char *cmd));
114 int util_free_argv __P((int argc, char **argv));
115 char **util_command_completion __P((char *cmd, int start, int end)); 126 char **util_command_completion __P((char *cmd, int start, int end));
116 char *util_command_generator __P((char *text, int state)); 127 char *util_command_generator __P((char *text, int state));
117 char *util_stripwhite __P((char *string)); 128 char *util_stripwhite __P((char *string));
129 struct mail_command_entry util_find_entry __P((char *cmd));
118 130
119 #ifdef __cplusplus 131 #ifdef __cplusplus
120 } 132 }
......
...@@ -15,33 +15,9 @@ ...@@ -15,33 +15,9 @@
15 along with this program; if not, write to the Free Software 15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #ifndef _TABLE_H
19 #define _TABLE_H 1
20
21 #include "mail.h" 18 #include "mail.h"
22 19
23 #ifdef __cplusplus 20 const struct mail_command_entry mail_command_table[] = {
24 extern "C" {
25 #endif
26
27 #ifndef __P
28 # ifdef __STDC__
29 # define __P(args) args
30 # else
31 # define __P(args) ()
32 # endif
33 #endif /*__P */
34
35 struct mail_command_entry {
36 char *shortname;
37 char *longname;
38 Function *func;
39 char *synopsis;
40 };
41
42 struct mail_command_entry util_find_entry __P((char *cmd));
43
44 static struct mail_command_entry mail_command_table[] = {
45 { "a", "alias", mail_alias, 21 { "a", "alias", mail_alias,
46 "a[lias] [alias [address...]]" }, 22 "a[lias] [alias [address...]]" },
47 { "g", "group", mail_alias, 23 { "g", "group", mail_alias,
...@@ -122,10 +98,3 @@ static struct mail_command_entry mail_command_table[] = { ...@@ -122,10 +98,3 @@ static struct mail_command_entry mail_command_table[] = {
122 { "#", "#", NULL, "# comment" }, 98 { "#", "#", NULL, "# comment" },
123 { 0, 0, 0, 0,} 99 { 0, 0, 0, 0,}
124 }; 100 };
125
126 #ifdef __cplusplus
127 }
128 #endif
129
130 #endif /* _TABLE_H */
131
......
...@@ -16,49 +16,6 @@ ...@@ -16,49 +16,6 @@
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #include "mail.h" 18 #include "mail.h"
19 #include "table.h"
20
21 /*
22 * takes a string and splits it into several strings, breaking at ' '
23 * command is the string to split
24 * the number of strings is placed into argc
25 * the split strings are put into argv
26 * returns 0 on success, nonzero on failure
27 */
28
29 int
30 util_get_argcv (const char *command, int *argc, char ***argv)
31 {
32 int len = strlen (command);
33 int i = 0, j = 0;
34 int start = 0;
35
36 if (len < 1)
37 return 1;
38
39 *argc = 1;
40
41 for (i = 0; i < len; i++)
42 if (command[i] == ' ')
43 (*argc)++;
44
45 *argv = malloc ((*argc + 1) * sizeof (char *));
46
47 for (i = 0; i <= len; i++)
48 {
49 if (command[i] == ' ' || command[i] == '\0')
50 {
51 (*argv)[j] = malloc ((i-start) * sizeof (char));
52 if (argv[j] == NULL && (i-start > 0))
53 return 1;
54 strncpy ((*argv)[j], &command[start], i-start);
55 (*argv)[j][i-start] = '\0';
56 j++;
57 start = i+1;
58 }
59 }
60 return 0;
61 }
62 19
63 /* 20 /*
64 * expands a standard message list into an array of numbers 21 * expands a standard message list into an array of numbers
...@@ -142,20 +99,6 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -142,20 +99,6 @@ util_expand_msglist (const int argc, char **argv, int **list)
142 } 99 }
143 100
144 /* 101 /*
145 * frees all elements of an argv array
146 * argc is the number of elements
147 * argv is the array
148 */
149 int
150 util_free_argv (int argc, char **argv)
151 {
152 while (--argc >= 0)
153 free (argv[argc]);
154 free (argv);
155 return 1;
156 }
157
158 /*
159 * expands command into its command and arguments, then runs command 102 * expands command into its command and arguments, then runs command
160 * cmd is the command to parse and run 103 * cmd is the command to parse and run
161 * returns exit status of the command 104 * returns exit status of the command
...@@ -173,8 +116,8 @@ util_do_command (const char *cmd) ...@@ -173,8 +116,8 @@ util_do_command (const char *cmd)
173 116
174 if (cmd) 117 if (cmd)
175 { 118 {
176 if (util_get_argcv (cmd, &argc, &argv) != 0) 119 if (argcv_get (cmd, &argc, &argv) != 0)
177 return util_free_argv (argc, argv); 120 return argcv_free (argc, argv);
178 command = util_command_get (argv[0]); 121 command = util_command_get (argv[0]);
179 } 122 }
180 else 123 else
...@@ -188,7 +131,7 @@ util_do_command (const char *cmd) ...@@ -188,7 +131,7 @@ util_do_command (const char *cmd)
188 status = 1; 131 status = 1;
189 } 132 }
190 133
191 util_free_argv (argc, argv); 134 argcv_free (argc, argv);
192 return status; 135 return status;
193 } 136 }
194 137
...@@ -242,7 +185,9 @@ util_find_entry (char *cmd) ...@@ -242,7 +185,9 @@ util_find_entry (char *cmd)
242 { 185 {
243 sl = strlen (mail_command_table[i].shortname); 186 sl = strlen (mail_command_table[i].shortname);
244 ll = strlen (mail_command_table[i].longname); 187 ll = strlen (mail_command_table[i].longname);
245 if (sl == len && !strcmp (mail_command_table[i].shortname, cmd)) 188 if (sl > ll && !strncmp (mail_command_table[i].shortname, cmd, sl))
189 return mail_command_table[i];
190 else if (sl == len && !strcmp (mail_command_table[i].shortname, cmd))
246 return mail_command_table[i]; 191 return mail_command_table[i];
247 else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len)) 192 else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len))
248 return mail_command_table[i]; 193 return mail_command_table[i];
...@@ -279,9 +224,9 @@ util_command_generator (char *text, int state) ...@@ -279,9 +224,9 @@ util_command_generator (char *text, int state)
279 224
280 while ((name = mail_command_table[i].longname)) 225 while ((name = mail_command_table[i].longname))
281 { 226 {
227 if (strlen (mail_command_table[i].shortname) > strlen(name))
228 name = mail_command_table[i].shortname;
282 i++; 229 i++;
283 /*if (strlen (mail_command_table[i].shortname) > strlen(name))
284 name = mail_command_table[i].shortname; */
285 if (strncmp (name, text, len) == 0) 230 if (strncmp (name, text, len) == 0)
286 return (strdup(name)); 231 return (strdup(name));
287 } 232 }
......
...@@ -110,11 +110,7 @@ main (int argc, char **argv) ...@@ -110,11 +110,7 @@ main (int argc, char **argv)
110 pop3_daemon_init (); 110 pop3_daemon_init ();
111 111
112 /* change directories */ 112 /* change directories */
113 #ifdef MAILSPOOLHOME
114 chdir ("/"); 113 chdir ("/");
115 #else
116 chdir (_PATH_MAILDIR);
117 #endif
118 114
119 /* Set up for syslog */ 115 /* Set up for syslog */
120 openlog ("gnu-pop3d", LOG_PID, LOG_MAIL); 116 openlog ("gnu-pop3d", LOG_PID, LOG_MAIL);
......