Commit 48b49232 48b492320e9b852dcdc7a637f6d7d9dfd1895019 by Alain Magloire

Ok, working from Jakob first draft, get things in

shape and ready.  The biggest thing will be the
awfull parsing done for FETCH/SEARCH etc ..
"This will be for another day" -- Scarlet O'hara
1 parent 8c2c480c
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,9 +22,7 @@ ...@@ -22,9 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_append (int argc, char **argv) 25 imap4d_append (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc < 4) 27 return util_finish (command, RESP_NO, "Not implemented");
28 return TOO_FEW;
29 return NOT_IMPL;
30 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_authenticate (int argc, char **argv) 25 imap4d_authenticate (struct imap4d_command *command, char *arg)
26 { 26 {
27 return util_finish (argc, argv, RESP_NO, NULL, "Type not supported"); 27 return util_finish (command, RESP_NO, "Command not supported");
28 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -18,10 +18,8 @@ ...@@ -18,10 +18,8 @@
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 int 20 int
21 imap4d_capability (int argc, char **argv) 21 imap4d_capability (struct imap4d_command *command, char *arg)
22 { 22 {
23 if (argc > 2) 23 util_out (RESP_NONE, "CAPABILITY IMAP4rev1");
24 return TOO_MANY; 24 return util_finish (command, RESP_OK, "Completed");
25 util_out (argv[0], TAG_NONE, "CAPABILITY IMAP4rev1");
26 return util_finish (argc, argv, RESP_OK, NULL, "Completed");
27 } 25 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -19,12 +19,12 @@ ...@@ -19,12 +19,12 @@
19 19
20 /* 20 /*
21 * Do we need to do anything here? 21 * Do we need to do anything here?
22 * FIXME: This is like noop we need to notify the client of
23 * new mails etc ... and do housekeeping.
22 */ 24 */
23 25
24 int 26 int
25 imap4d_check (int argc, char **argv) 27 imap4d_check (struct imap4d_command *command, char *arg)
26 { 28 {
27 if (argc > 2) 29 return util_finish (command, RESP_OK, "Completed");
28 return TOO_MANY;
29 return util_finish (argc, argc, RESP_OK, NULL, "Completed");
30 } 30 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -18,13 +18,21 @@ ...@@ -18,13 +18,21 @@
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 /* 20 /*
21 *
22 */ 21 */
23 22
24 int 23 int
25 imap4d_close (int argc, char **argv) 24 imap4d_close (struct imap4d_command *command, char *arg)
26 { 25 {
27 if (argc > 2) 26 /* FIXME: Check args. */
28 return TOO_MANY; 27 /* FIXME: Check state and if they selected. */
29 return NOT_IMPL; 28 /* FIXME: state = AUTHENTICATE. */
29
30 /* FIXME: Check and report errors. */
31 mailbox_expunge (mbox);
32 mailbox_close (mbox);
33 mailbox_destroy (&mbox);
34
35 /* FIXME: state = Not selected. */
36
37 return util_finish (command, RESP_OK, "Completed");
30 } 38 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -17,30 +17,31 @@ ...@@ -17,30 +17,31 @@
17 17
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 const struct imap4d_command imap4d_command_table [] = { 20 struct imap4d_command imap4d_command_table [] =
21 { "capability", imap4d_capability, STATE_ALL, STATE_NONE, STATE_NONE }, 21 {
22 { "noop", imap4d_noop, STATE_ALL, STATE_NONE, STATE_NONE }, 22 { "CAPABILITY", imap4d_capability, STATE_ALL, STATE_NONE, STATE_NONE },
23 { "logout", imap4d_logout, STATE_ALL, STATE_LOGOUT, STATE_NONE }, 23 { "NOOP", imap4d_noop, STATE_ALL, STATE_NONE, STATE_NONE },
24 { "authenticate", imap4d_authenticate, STATE_NONAUTH, STATE_NONE, STATE_AUTH }, 24 { "LOGOUT", imap4d_logout, STATE_ALL, STATE_LOGOUT, STATE_NONE },
25 { "login", imap4d_login, STATE_NONAUTH, STATE_NONE, STATE_AUTH }, 25 { "AUTHENTICATE", imap4d_authenticate, STATE_NONAUTH, STATE_NONE, STATE_AUTH },
26 { "select", imap4d_select, STATE_AUTH | STATE_SEL, STATE_AUTH, STATE_SEL }, 26 { "LOGIN", imap4d_login, STATE_NONAUTH, STATE_NONE, STATE_AUTH },
27 { "examine", imap4d_examine, STATE_AUTH | STATE_SEL, STATE_AUTH, STATE_SEL }, 27 { "SELECT", imap4d_select, STATE_AUTH | STATE_SEL, STATE_AUTH, STATE_SEL },
28 { "create", imap4d_create, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 28 { "EXAMINE", imap4d_examine, STATE_AUTH | STATE_SEL, STATE_AUTH, STATE_SEL },
29 { "delete", imap4d_delete, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 29 { "CREATE", imap4d_create, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
30 { "rename", imap4d_rename, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 30 { "DELETE", imap4d_delete, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
31 { "subscribe", imap4d_subscribe, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 31 { "RENAME", imap4d_rename, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
32 { "unsubscribe", imap4d_unsubscribe, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 32 { "SUBSCRIBE", imap4d_subscribe, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
33 { "list", imap4d_list, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 33 { "UNSUBSCRIBE", imap4d_unsubscribe, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
34 { "lsub", imap4d_lsub, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 34 { "LIST", imap4d_list, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
35 { "status", imap4d_status, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 35 { "LSUB", imap4d_lsub, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
36 { "append", imap4d_append, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE }, 36 { "STATUS", imap4d_status, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
37 { "check", imap4d_check, STATE_SEL, STATE_NONE, STATE_NONE }, 37 { "APPEND", imap4d_append, STATE_AUTH | STATE_SEL, STATE_NONE, STATE_NONE },
38 { "close", imap4d_close, STATE_SEL, STATE_AUTH, STATE_AUTH }, 38 { "CHECK", imap4d_check, STATE_SEL, STATE_NONE, STATE_NONE },
39 { "expunge", imap4d_expunge, STATE_SEL, STATE_NONE, STATE_NONE }, 39 { "CLOSE", imap4d_close, STATE_SEL, STATE_AUTH, STATE_AUTH },
40 { "search", imap4d_search, STATE_SEL, STATE_NONE, STATE_NONE }, 40 { "EXPUNGE", imap4d_expunge, STATE_SEL, STATE_NONE, STATE_NONE },
41 { "fetch", imap4d_fetch, STATE_SEL, STATE_NONE, STATE_NONE }, 41 { "SEARCH", imap4d_search, STATE_SEL, STATE_NONE, STATE_NONE },
42 { "store", imap4d_store, STATE_SEL, STATE_NONE, STATE_NONE }, 42 { "FETCH", imap4d_fetch, STATE_SEL, STATE_NONE, STATE_NONE },
43 { "copy", imap4d_copy, STATE_SEL, STATE_NONE, STATE_NONE }, 43 { "STORE", imap4d_store, STATE_SEL, STATE_NONE, STATE_NONE },
44 { "uid", imap4d_uid, STATE_SEL, STATE_NONE, STATE_NONE }, 44 { "COPY", imap4d_copy, STATE_SEL, STATE_NONE, STATE_NONE },
45 { "UID", imap4d_uid, STATE_SEL, STATE_NONE, STATE_NONE },
45 { 0, 0, 0} 46 { 0, 0, 0}
46 }; 47 };
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_copy (int argc, char **argv) 25 imap4d_copy (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 4) 27 return util_finish (command, RESP_NO, "Command not supported");
28 return TOO_MANY;
29 else if (argc < 4)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_create (int argc, char **argv) 25 imap4d_create (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 3) 27 return util_finish (command, RESP_NO, "Command not supported");
28 return TOO_MANY;
29 else if (argc < 3)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_delete (int argc, char **argv) 25 imap4d_delete (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 3) 27 return util_finish (command, RESP_NO, "Command not supported");;
28 return TOO_MANY;
29 if (argc < 3)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_examine (int argc, char **argv) 25 imap4d_examine (struct imap4d_command *command, char *arg)
26 { 26 {
27 return NOT_IMPL; 27 return imap4d_select0 (command, arg, MU_STREAM_READ);
28 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,9 +22,14 @@ ...@@ -22,9 +22,14 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_expunge (int argc, char **argv) 25 imap4d_expunge (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 2) 27 char *sp;
28 return TOO_MANY; 28
29 return NOT_IMPL; 29 if (util_getword (arg, &sp))
30 return util_finish (command, RESP_NO, "Too many args");
31
32 /* FIXME: Check state, check for errors. */
33 mailbox_expunge (mbox);
34 return util_finish (command, RESP_OK, "Completed");
30 } 35 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,10 +22,17 @@ ...@@ -22,10 +22,17 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_fetch (int argc, char **argv) 25 imap4d_fetch (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc < 4) 27 char *sp = NULL;
28 return TOO_FEW; 28 char *message_set;
29 char *item;
30
31 message_set = util_getword (arg, &sp);
32 item = util_getword (NULL, &sp);
33 if (!message_set || !item)
34 return util_finish (command, RESP_BAD, "Too few args");
35
29 /* check for paren list */ 36 /* check for paren list */
30 return NOT_IMPL; 37 return util_finish (command, RESP_NO, "Not supported");
31 } 38 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -17,8 +17,12 @@ ...@@ -17,8 +17,12 @@
17 17
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 int ifile; 20 int *ifile;
21 int ofile; 21 FILE *ofile;
22 unsigned int timeout = 1800; /* RFC2060: 30 minutes, if enable. */
23 mailbox_t mbox;
24
25 static int imap4_mainloop __P ((int, int));
22 26
23 int 27 int
24 main (int argc, char **argv) 28 main (int argc, char **argv)
...@@ -26,22 +30,41 @@ main (int argc, char **argv) ...@@ -26,22 +30,41 @@ main (int argc, char **argv)
26 chdir ("/"); 30 chdir ("/");
27 openlog ("imap4d", LOG_PID, LOG_MAIL); 31 openlog ("imap4d", LOG_PID, LOG_MAIL);
28 32
29 /* for each incoming connection */ 33 /* Register the desire formats. We only need Mbox mail format. */
30 { 34 {
31 char *remote_host = ""; 35 list_t bookie;
32 ifile = fileno (stdin); 36 registrar_get_list (&bookie);
33 ofile = fileno (stdout); 37 list_append (bookie, path_record);
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 } 38 }
39 /* FIXME: Incomplete, make it work for standalone, see pop3d. */
40 imap4_mainloop (fileno (stdin), fileno (stdout));
41 return 0;
42 }
43
44 static int
45 imap4_mainloop (int infile, int outfile)
46 {
47 const char *remote_host = "";
48
49 ofile = fdopen (outfile, "w");
50 if (ofile == NULL)
51 util_quit (1);
52
53 /* FIXME: Retreive hostname with getpeername() and log. */
54 syslog (LOG_INFO, "Incoming connection from %s", remote_host);
55
56 /* Greetings. */
57 util_out (RESP_OK, "IMAP4rev1 GNU " PACKAGE " " VERSION);
58 fflush (ofile);
59
60 while (1)
61 {
62 char *cmd = imap4d_readline (infile);
63 /* check for updates */
64 util_do_command (cmd);
65 free (cmd);
66 fflush (ofile);
67 }
45 68
46 closelog (); 69 closelog ();
47 return 0; 70 return 0;
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,6 +22,12 @@ ...@@ -22,6 +22,12 @@
22 #include <config.h> 22 #include <config.h>
23 #endif 23 #endif
24 24
25 #define _GNU_SOURCE
26
27 #ifdef HAVE_SHADOW_H
28 #include <shadow.h>
29 #endif
30
25 #include <errno.h> 31 #include <errno.h>
26 #include <stdio.h> 32 #include <stdio.h>
27 #include <stdlib.h> 33 #include <stdlib.h>
...@@ -36,14 +42,11 @@ ...@@ -36,14 +42,11 @@
36 #include <sys/time.h> 42 #include <sys/time.h>
37 #include <sys/stat.h> 43 #include <sys/stat.h>
38 44
39 #include <argp.h>
40
41 #include <mailutils/mailbox.h> 45 #include <mailutils/mailbox.h>
42 #include <mailutils/message.h> 46 #include <mailutils/message.h>
43 #include <mailutils/header.h> 47 #include <mailutils/header.h>
44 #include <mailutils/body.h> 48 #include <mailutils/body.h>
45 49 #include <mailutils/registrar.h>
46 #include <argcv.h>
47 50
48 #ifdef __cplusplus 51 #ifdef __cplusplus
49 extern "C" { 52 extern "C" {
...@@ -57,72 +60,74 @@ extern "C" { ...@@ -57,72 +60,74 @@ extern "C" {
57 # endif 60 # endif
58 #endif /*__P */ 61 #endif /*__P */
59 62
60 /* Type definitions */ 63 struct imap4d_command
61 #ifndef Function 64 {
62 typedef int Function (); 65 const char *name;
63 #endif 66 int (*func) __P ((struct imap4d_command *, char *));
64
65 struct imap4d_command {
66 char *name;
67 Function *func;
68 int states; 67 int states;
69 int success; 68 int success;
70 int failure; 69 int failure;
70 char *tag;
71 }; 71 };
72 72
73 /* Global variables and constants*/ 73 /* Global variables and constants*/
74 #define STATE_NONE 1<<0 74 #define STATE_NONE (1 << 0)
75 #define STATE_NONAUTH 1<<1 75 #define STATE_NONAUTH (1 << 1)
76 #define STATE_AUTH 1<<2 76 #define STATE_AUTH (1 << 2)
77 #define STATE_SEL 1<<3 77 #define STATE_SEL (1 << 3)
78 #define STATE_LOGOUT 1<<4 78 #define STATE_LOGOUT (1 << 4)
79 79
80 #define STATE_ALL (STATE_NONE | STATE_NONAUTH | STATE_AUTH | STATE_SEL \ 80 #define STATE_ALL (STATE_NONE | STATE_NONAUTH | STATE_AUTH | STATE_SEL \
81 | STATE_LOGOUT) 81 | STATE_LOGOUT)
82 82
83 #define TAG_NONE 0
84 #define TAG_SEQ 1
85
86 #define RESP_OK 0 83 #define RESP_OK 0
87 #define RESP_BAD 1 84 #define RESP_BAD 1
88 #define RESP_NO 2 85 #define RESP_NO 2
89 86 #define RESP_BYE 3
90 extern const struct imap4d_command imap4d_command_table[]; 87 #define RESP_NONE 4
91 88
92 #define TOO_MANY (util_finish (argc, argv, RESP_BAD, NULL, "Too many args")) 89 extern struct imap4d_command imap4d_command_table[];
93 #define TOO_FEW (util_finish (argc, argv, RESP_BAD, NULL, "Too few args")) 90 extern FILE *ofile;
94 #define NOT_IMPL (util_finish (argc, argv, RESP_BAD, NULL, "Not implemented")) 91 extern unsigned int timeout;
95 92 extern mailbox_t mbox;
96 /* Functions */ 93 extern unsigned int state;
97 int imap4d_capability __P ((int argc, char **argv)); 94
98 int imap4d_noop __P ((int argc, char **argv)); 95 /* Imap4 commands */
99 int imap4d_logout __P ((int argc, char **argv)); 96 int imap4d_capability __P ((struct imap4d_command *, char *));
100 int imap4d_authenticate __P ((int argc, char **argv)); 97 int imap4d_noop __P ((struct imap4d_command *, char *));
101 int imap4d_login __P ((int argc, char **argv)); 98 int imap4d_logout __P ((struct imap4d_command *, char *));
102 int imap4d_select __P ((int argc, char **argv)); 99 int imap4d_authenticate __P ((struct imap4d_command *, char *));
103 int imap4d_examine __P ((int argc, char **argv)); 100 int imap4d_login __P ((struct imap4d_command *, char *));
104 int imap4d_create __P ((int argc, char **argv)); 101 int imap4d_select __P ((struct imap4d_command *, char *));
105 int imap4d_delete __P ((int argc, char **argv)); 102 int imap4d_select0 __P ((struct imap4d_command *, char *, int));
106 int imap4d_rename __P ((int argc, char **argv)); 103 int imap4d_examine __P ((struct imap4d_command *, char *));
107 int imap4d_subscribe __P ((int argc, char **argv)); 104 int imap4d_create __P ((struct imap4d_command *, char *));
108 int imap4d_unsubscribe __P ((int argc, char **argv)); 105 int imap4d_delete __P ((struct imap4d_command *, char *));
109 int imap4d_list __P ((int argc, char **argv)); 106 int imap4d_rename __P ((struct imap4d_command *, char *));
110 int imap4d_lsub __P ((int argc, char **argv)); 107 int imap4d_subscribe __P ((struct imap4d_command *, char *));
111 int imap4d_status __P ((int argc, char **argv)); 108 int imap4d_unsubscribe __P ((struct imap4d_command *, char *));
112 int imap4d_append __P ((int argc, char **argv)); 109 int imap4d_list __P ((struct imap4d_command *, char *));
113 int imap4d_check __P ((int argc, char **argv)); 110 int imap4d_lsub __P ((struct imap4d_command *, char *));
114 int imap4d_close __P ((int argc, char **argv)); 111 int imap4d_status __P ((struct imap4d_command *, char *));
115 int imap4d_expunge __P ((int argc, char **argv)); 112 int imap4d_append __P ((struct imap4d_command *, char *));
116 int imap4d_search __P ((int argc, char **argv)); 113 int imap4d_check __P ((struct imap4d_command *, char *));
117 int imap4d_fetch __P ((int argc, char **argv)); 114 int imap4d_close __P ((struct imap4d_command *, char *));
118 int imap4d_store __P ((int argc, char **argv)); 115 int imap4d_expunge __P ((struct imap4d_command *, char *));
119 int imap4d_copy __P ((int argc, char **argv)); 116 int imap4d_search __P ((struct imap4d_command *, char *));
120 int imap4d_uid __P ((int argc, char **argv)); 117 int imap4d_fetch __P ((struct imap4d_command *, char *));
121 118 int imap4d_store __P ((struct imap4d_command *, char *));
122 int util_out __P ((char *seq, int tag, char *f, ...)); 119 int imap4d_copy __P ((struct imap4d_command *, char *));
123 int util_start __P ((char *seq)); 120 int imap4d_uid __P ((struct imap4d_command *, char *));
124 int util_finish __P ((int argc, char **argv, int resp, char *r, char *f, ...)); 121
122 /* Helper functions. */
123 int util_out __P ((int rc, const char *f, ...));
124 int util_start __P ((char *tag));
125 int util_finish __P ((struct imap4d_command *, int rc, const char *f, ...));
125 int util_getstate __P ((void)); 126 int util_getstate __P ((void));
127 int util_do_command __P ((char *prompt));
128 char *imap4d_readline __P ((int fd));
129 void util_quit __P ((int));
130 char *util_getword __P ((char *s, char **save_ptr));
126 struct imap4d_command *util_getcommand __P ((char *cmd)); 131 struct imap4d_command *util_getcommand __P ((char *cmd));
127 132
128 #ifdef __cplusplus 133 #ifdef __cplusplus
...@@ -130,4 +135,3 @@ struct imap4d_command *util_getcommand __P ((char *cmd)); ...@@ -130,4 +135,3 @@ struct imap4d_command *util_getcommand __P ((char *cmd));
130 #endif 135 #endif
131 136
132 #endif /* _IMAP4D_H */ 137 #endif /* _IMAP4D_H */
133
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_list (int argc, char **argv) 25 imap4d_list (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 4) 27 return util_finish (command, RESP_NO, "Not supported");
28 return TOO_MANY;
29 if (argc < 4)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -18,17 +18,37 @@ ...@@ -18,17 +18,37 @@
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 /* 20 /*
21 * argv[2] == username 21 * FIXME: this should support PAM, shadow, and normal password
22 * argv[3] == password
23 * this should support PAM, shadow, and normal password
24 */ 22 */
25 23
26 int 24 int
27 imap4d_login (int argc, char **argv) 25 imap4d_login (struct imap4d_command *command, char *arg)
28 { 26 {
29 if (argc > 4) 27 struct passwd *pw;
30 return TOO_MANY; 28 char *sp = NULL, *username, *pass;
31 else if (argc < 4) 29
32 return TOO_FEW; 30 username = util_getword (arg, &sp);
33 return NOT_IMPL; 31 pass = util_getword (NULL, &sp);
32
33 if (username == NULL || pass == NULL)
34 return util_finish (command, RESP_NO, "Too few args");
35 else if (util_getword (NULL, &sp))
36 return util_finish (command, RESP_NO, "Too many args");
37
38 pw = getpwnam (arg);
39 if (pw == NULL || pw->pw_uid < 1)
40 return util_finish (command, RESP_NO, "User name or passwd rejected");
41 if (strcmp (pw->pw_passwd, crypt (pass, pw->pw_passwd)))
42 {
43 #ifdef HAVE_SHADOW_H
44 struct spwd *spw;
45 spw = getspnam (arg);
46 if (spw == NULL || strcmp (spw->sp_pwdp, crypt (pass, spw->sp_pwdp)))
47 #endif /* HAVE_SHADOW_H */
48 return util_finish (command, RESP_NO, "User name or passwd rejected");
49 }
50
51 if (pw->pw_uid > 1)
52 setuid (pw->pw_uid);
53 return util_finish (command, RESP_OK, "Completed");
34 } 54 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,15 @@ ...@@ -22,11 +22,15 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_logout (int argc, char **argv) 25 imap4d_logout (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 2) 27 char *sp;
28 return TOO_MANY; 28 if (util_getword (arg, &sp))
29 util_out (argv[0], TAG_NONE, "BYE Logging out"); 29 return util_finish (command, RESP_BAD, "Too many args");
30 util_finish (argc, argv, RESP_OK, NULL, "Completed"); 30 util_out (RESP_BYE, "Logging out");
31 exit (0); 31 util_finish (command, RESP_OK, "Completed");
32 mailbox_close (mbox);
33 mailbox_destroy (&mbox);
34 util_quit (0);
35 return 0;
32 } 36 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_lsub (int argc, char **argv) 25 imap4d_lsub (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 4) 27 return util_finish (command, RESP_NO, "Not supported");
28 return TOO_MANY;
29 if (argc < 4)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -17,10 +17,12 @@ ...@@ -17,10 +17,12 @@
17 17
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 /* FIXME: Notify of new messages. */
20 int 21 int
21 imap4d_noop (int argc, char **argv) 22 imap4d_noop (struct imap4d_command *command, char *arg)
22 { 23 {
23 if (argc > 2) 24 char *sp;
24 return TOO_MANY; 25 if (util_getword (arg, &sp))
25 return util_finish (argc, argv, RESP_OK, NULL, "Completed"); 26 return util_finish (command, RESP_BAD, "Too many args");
27 return util_finish (command, RESP_OK, "Completed");
26 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_rename (int argc, char **argv) 25 imap4d_rename (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 4) 27 return util_finish (command, RESP_NO, "Not Supported");
28 return TOO_MANY;
29 if (argc < 4)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -19,12 +19,12 @@ ...@@ -19,12 +19,12 @@
19 19
20 /* 20 /*
21 * This will be a royal pain in the arse to implement 21 * This will be a royal pain in the arse to implement
22 * Alain: True, but the new lib mailbox should coming handy with
23 * some sort of query interface.
22 */ 24 */
23 25
24 int 26 int
25 imap4d_search (int argc, char **argv) 27 imap4d_search (struct imap4d_command *command, char *arg)
26 { 28 {
27 if (argc < 3) 29 return util_finish (command, RESP_NO, "Not supported");
28 return TOO_FEW;
29 return NOT_IMPL;
30 } 30 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -19,26 +19,57 @@ ...@@ -19,26 +19,57 @@
19 19
20 /* 20 /*
21 * argv[2] == mailbox 21 * argv[2] == mailbox
22 * this needs to share code with EXAMINE
23 */ 22 */
24 23
25 int 24 int
26 imap4d_select (int argc, char **argv) 25 imap4d_select (struct imap4d_command *command, char *arg)
27 { 26 {
28 if (argc > 3) 27 return imap4d_select0 (command, arg, MU_STREAM_RDWR);
29 return TOO_MANY; 28 }
30 else if (argc < 3) 29
31 return TOO_FEW; 30 /* This code is share with EXAMINE. */
31 int
32 imap4d_select0 (struct imap4d_command *command, char *arg, int flags)
33 {
34 char *mailbox_name, *sp = NULL;
35 struct passwd *pw;
32 36
33 /* close previous mailbox */ 37 /* FIXME: Check state. */
34 if ( /* open mailbox (argv[2]) == */ 0) 38
39 mailbox_name = util_getword (arg, &sp);
40 if (mailbox_name == NULL)
41 return util_finish (command, RESP_BAD, "Too few arguments");
42 if (util_getword (NULL, &sp))
43 return util_finish (command, RESP_BAD, "Too many arguments");
44
45 if (mbox)
35 { 46 {
36 char *flags = NULL; 47 mailbox_close (mbox);
37 int num_messages = 0, recent = 0, uid = 0; 48 mailbox_destroy (&mbox);
38 util_out (argv[0], TAG_NONE, "FLAGS %s", flags); 49 }
39 util_out (argv[0], TAG_NONE, "%d EXISTS", num_messages); 50
40 util_out (argv[0], TAG_NONE, "%d RECENT", recent); 51 if (strcasecmp (mailbox_name, "INBOX") == 0)
41 return util_finish (argc, argv, RESP_OK, NULL, "Complete"); 52 {
53 pw = getpwuid (getuid());
54 if (pw)
55 mailbox_name = pw->pw_name;
56 }
57
58 if (mailbox_create_default (&mbox, mailbox_name) == 0
59 && mailbox_open (mbox, flags) == 0)
60 {
61 const char *sflags = "\\Answered \\Flagged \\Deleted \\Seen \\Draft";
62 int num = 0, recent = 0, uid = 0;
63
64 mailbox_messages_count (mbox, &num);
65 mailbox_recent_count (mbox, &recent);
66 util_out (RESP_NONE, "%d EXISTS", num);
67 util_out (RESP_NONE, "%d RECENT", recent);
68 util_out (RESP_NONE, "FLAGS (%s)", sflags);
69 util_out (RESP_OK, "[UIDNEXT %d]", num + 1);
70 /*util_out (RESP_OK, "[UIDVALIDITY (%d)]", uid);*/
71 /*util_out (RESP_OK, "[PERMANENTFLAGS (%s)]", flags);*/
72 return util_finish (command, RESP_OK, "Complete");
42 } 73 }
43 return util_finish (argc, argv, RESP_NO, NULL, "Couldn't open %s", argv[2]); 74 return util_finish (command, RESP_NO, "Couldn't open %s", mailbox_name);
44 } 75 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,14 @@ ...@@ -22,11 +22,14 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_status (int argc, char **argv) 25 imap4d_status (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc < 4) 27 char *sp = NULL;
28 return TOO_FEW; 28 char *mailbox_name;
29 if (argv[3][0] != '(' || argv[argc-1][strlen(argv[argc-1])-1] != ')') 29 char *data;
30 return util_finish (argc, argv, RESP_BAD, NULL, "Invalid args"); 30 mailbox_name = util_getword (arg, &sp);
31 return NOT_IMPL; 31 data = util_getword (NULL, &sp);
32 if (!mailbox_name || !data)
33 return util_finish (command, RESP_BAD, "Too few args");
34 return util_finish (command, RESP_BAD, "Not supported");
32 } 35 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_store (int argc, char **argv) 25 imap4d_store (struct imap4d_command *command, char *arg)
26 { 26 {
27 return NOT_IMPL; 27 return util_finish (command, RESP_NO, "Not supported");
28 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_subscribe (int argc, char **argv) 25 imap4d_subscribe (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 3) 27 return util_finish (command, RESP_NO, "Not supported");
28 return TOO_MANY;
29 if (argc < 3)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
23 */ 23 */
24 24
25 int 25 int
26 imap4d_uid (int argc, char **argv) 26 imap4d_uid (struct imap4d_command *command, char *arg)
27 { 27 {
28 return NOT_IMPL; 28 return util_finish (command, RESP_NO, "Not supported");
29 } 29 }
......
1 /* GNU mailutils - a suite of utilities for electronic mail 1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2001 Free Software Foundation, Inc.
3 3
4 This program is free software; you can redistribute it and/or modify 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 5 it under the terms of the GNU General Public License as published by
...@@ -22,11 +22,7 @@ ...@@ -22,11 +22,7 @@
22 */ 22 */
23 23
24 int 24 int
25 imap4d_unsubscribe (int argc, char **argv) 25 imap4d_unsubscribe (struct imap4d_command *command, char *arg)
26 { 26 {
27 if (argc > 3) 27 return util_finish (command, RESP_NO, "Not supported");
28 return TOO_MANY;
29 if (argc < 3)
30 return TOO_FEW;
31 return NOT_IMPL;
32 } 28 }
......
...@@ -17,163 +17,165 @@ ...@@ -17,163 +17,165 @@
17 17
18 #include "imap4d.h" 18 #include "imap4d.h"
19 19
20 extern int ifile; 20 static const char *
21 extern int ofile; 21 rc2string (int rc)
22 {
23 switch (rc)
24 {
25 case RESP_OK:
26 return "OK ";
27
28 case RESP_BAD:
29 return "BAD ";
30
31 case RESP_NO:
32 return "NO ";
33
34 case RESP_BYE:
35 return "BYE ";
36 }
37 return "";
38 }
39
40 /* FIXME: Some words are:
41 between double quotes, consider like one word.
42 between parenthesis, consider line one word. */
43 char *
44 util_getword (char *s, char **save)
45 {
46 static char *sp;
47 return strtok_r (s, " \r\n", ((save) ? save : &sp));
48 }
22 49
23 int 50 int
24 util_out (char *seq, int tag, char *f, ...) 51 util_out (int rc, const char *format, ...)
25 { 52 {
26 char *buf = NULL; 53 char *buf = NULL;
27 int len = 0;
28 va_list ap; 54 va_list ap;
29 va_start (ap, f);
30 len = vasprintf (&buf, f, ap);
31 55
32 if (tag == TAG_SEQ) 56 va_start (ap, format);
33 { 57 vasprintf (&buf, format, ap);
34 write (ofile, seq, strlen (seq)); 58 va_end (ap);
35 write (ofile, " ", 1);
36 }
37 else if (tag == TAG_NONE)
38 write (ofile, "* ", 2);
39 59
40 write (ofile, buf, len); 60 fprintf (ofile, "* %s%s\r\n", rc2string (rc), buf);
41 write (ofile, "\r\n", 2);
42 free (buf); 61 free (buf);
43
44 return 0; 62 return 0;
45 } 63 }
46 64
47 int 65 int
48 util_finish (int argc, char **argv, int resp, char *rc, char *f, ...) 66 util_finish (struct imap4d_command *command, int rc, const char *format, ...)
49 { 67 {
50 char *buf = NULL, *buf2 = NULL, *code = NULL; 68 char *buf = NULL;
51 /* FIXME: No argc maybe 1, then it will sigsegv. */ 69 const char *resp;
52 /* struct imap4d_command *cmd = util_getcommand (argv[1]); */
53 int len = 0;
54 va_list ap; 70 va_list ap;
55 va_start (ap, f);
56
57 switch (resp)
58 {
59 case RESP_OK:
60 code = strdup ("OK");
61 /* set state (cmd->success); */
62 break;
63
64 case RESP_BAD:
65 code = strdup ("BAD");
66 break;
67 71
68 case RESP_NO: 72 va_start (ap, format);
69 code = strdup ("NO"); 73 vasprintf (&buf, format, ap);
70 /* set state (cmd->failure); */ 74 va_end(ap);
71 break;
72
73 default:
74 code = strdup ("X-BUG");
75 }
76
77 vasprintf (&buf, f, ap);
78 if (rc != NULL)
79 len = asprintf (&buf, "%s %s %s %s %s\r\n",
80 argv[0], code, rc, (argc > 1) ? argv[1] : "", buf);
81 else
82 len = asprintf (&buf2, "%s %s %s %s\r\n",
83 argv[0], code, (argc > 1) ? argv[1] : "", buf);
84
85 write (ofile, buf2, len);
86 75
76 resp = rc2string (rc);
77 fprintf (ofile, "%s %s%s %s\r\n", command->tag, resp, command->name, buf);
87 free (buf); 78 free (buf);
88 free (buf2); 79 return 0;
89 free (code);
90 free (rc);
91 argcv_free (argc, argv);
92
93 return resp;
94 } 80 }
95 81
96 char * 82 char *
97 util_getline (void) 83 imap4d_readline (int fd)
98 { 84 {
99 fd_set rfds; 85 fd_set rfds;
100 struct timeval tv; 86 struct timeval tv;
101 char buf[1024], *ret = NULL; 87 char buf[512], *ret = NULL;
102 int fd = ifile, len = 0; 88 int nread;
89 int available;
103 90
104 FD_ZERO (&rfds); 91 FD_ZERO (&rfds);
105 FD_SET (fd, &rfds); 92 FD_SET (fd, &rfds);
106 tv.tv_sec = 30; 93 tv.tv_sec = timeout;
107 tv.tv_usec = 0; 94 tv.tv_usec = 0;
108 memset (buf, '\0', 1024);
109 95
110 do 96 do
111 { 97 {
112 if (!select (fd + 1, &rfds, NULL, NULL, &tv)) 98 if (timeout > 0)
113 return NULL; 99 {
114 else if (read (fd, buf, 1024) < 1) 100 available = select (fd + 1, &rfds, NULL, NULL, &tv);
115 exit (1); /* FIXME: dead socket */ 101 if (!available)
116 else if (ret == NULL) 102 util_quit (1); /* FIXME: Timeout. */
103 }
104 nread = read (fd, buf, sizeof (buf) - 1);
105 if (nread < 1)
106 util_quit (1); /* FIXME: dead socket */
107
108 buf[nread] = '\0';
109
110 if (ret == NULL)
117 { 111 {
118 ret = malloc ((strlen (buf + 1) * sizeof (char))); 112 ret = malloc ((nread + 1) * sizeof (char));
119 strcpy (ret, buf); 113 strcpy (ret, buf);
120 } 114 }
121 else 115 else
122 { 116 {
123 ret = realloc (ret, (strlen (ret) + strlen (buf) + 1) * 117 ret = realloc (ret, (strlen (ret) + nread + 1) * sizeof (char));
124 sizeof (char));
125 strcat (ret, buf); 118 strcat (ret, buf);
126 } 119 }
120 /* FIXME: handle literal strings here. */
127 } 121 }
128 while (strchr (buf, '\n') == NULL); 122 while (strchr (buf, '\n') == NULL);
129 123
130 for (len = strlen (ret); len > 0; len--)
131 if (ret[len] == '\r' || ret[len] == '\n')
132 ret[len] = '\0';
133
134 return ret; 124 return ret;
135 } 125 }
136 126
137 int 127 int
138 util_do_command (char *cmd) 128 util_do_command (char *prompt)
139 { 129 {
140 int argc = 0, i = 0; 130 char *sp = NULL, *tag, *cmd, *arg;
141 char **argv = NULL; 131 struct imap4d_command *command;
142 struct imap4d_command *command = NULL; 132 static struct imap4d_command nullcommand;
143 133
144 if (cmd == NULL) 134 tag = util_getword (prompt, &sp);
145 return 0; 135 cmd = util_getword (NULL, &sp);
146 136 if (!tag)
147 if (argcv_get (cmd, &argc, &argv) != 0)
148 { 137 {
149 argcv_free (argc, argv); 138 nullcommand.name = "";
150 return 1; 139 nullcommand.tag = (char *)"*";
140 return util_finish (&nullcommand, RESP_BAD, "Null command");
141 }
142 else if (!cmd)
143 {
144 nullcommand.name = "";
145 nullcommand.tag = tag;
146 return util_finish (&nullcommand, RESP_BAD, "Missing arguments");
151 } 147 }
152 148
153 util_start (argv[0]); 149 util_start (tag);
154
155 if (argc < 2)
156 return util_finish (argc, argv, RESP_BAD, NULL, "No space after tag");
157
158 command = util_getcommand (argv[1]);
159
160 for (i=0; i < strlen (argv[1]); i++)
161 argv[1][i] = toupper (argv[1][i]);
162 150
151 command = util_getcommand (cmd);
163 if (command == NULL) 152 if (command == NULL)
164 return util_finish (argc, argv, RESP_BAD, NULL, "Invalid command"); 153 {
165 else if (! (command->states & util_getstate ())) 154 nullcommand.name = "";
166 return util_finish (argc, argv, RESP_BAD, NULL, "Incorrect state"); 155 nullcommand.tag = tag;
156 return util_finish (&nullcommand, RESP_BAD, "Invalid command");
157 }
167 158
168 return command->func (argc, argv); 159 command->tag = tag;
160 return command->func (command, sp);
169 } 161 }
170 162
163 /* FIXME: What is this for? */
171 int 164 int
172 util_start (char *seq) 165 util_start (char *tag)
173 { 166 {
167 (void)tag;
174 return 0; 168 return 0;
175 } 169 }
176 170
171 /* FIXME: Incomplete send errmsg to syslog, see pop3d:pop3_abquit(). */
172 void
173 util_quit (int err)
174 {
175 exit (err);
176 }
177
178 /* FIXME: What is this for? */
177 int 179 int
178 util_getstate (void) 180 util_getstate (void)
179 { 181 {
...@@ -183,7 +185,7 @@ util_getstate (void) ...@@ -183,7 +185,7 @@ util_getstate (void)
183 struct imap4d_command * 185 struct imap4d_command *
184 util_getcommand (char *cmd) 186 util_getcommand (char *cmd)
185 { 187 {
186 int i = 0, len = strlen (cmd); 188 size_t i, len = strlen (cmd);
187 189
188 for (i = 0; imap4d_command_table[i].name != 0; i++) 190 for (i = 0; imap4d_command_table[i].name != 0; i++)
189 { 191 {
......