Commit e6c6ca30 e6c6ca30a4e40a07eb3b21803dea175580edc7c9 by Alain Magloire

First draft of the implementation of the public low level call to POP3

1 parent 579b593e
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include "../md5.h"
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26
27 #include <mailutils/sys/pop3.h>
28
29 /*
30 * APOP name digest
31 * a string identifying a mailbox and a MD5 digest string (both required)
32 */
33 int
34 mu_pop3_apop (mu_pop3_t pop3, const char *user, const char *secret)
35 {
36 int status;
37
38 /* Sanity checks. */
39 if (pop3 == NULL || user == NULL || secret == NULL)
40 {
41 return EINVAL;
42 }
43
44 /* The server did not offer a timestamp in the greeting, bailout early. */
45 if (pop3->timestamp == NULL)
46 {
47 return ENOTSUP;
48 }
49
50 switch (pop3->state)
51 {
52 /* Generate the md5 from the secret and timestamp. */
53 case MU_POP3_NO_STATE:
54 {
55 MD5_CTX md5context;
56 unsigned char md5digest[16];
57 char digest[64]; /* Really it just has to be 32 + 1(null). */
58 char *tmp;
59 size_t n;
60
61 MD5Init (&md5context);
62 MD5Update (&md5context, (unsigned char *)pop3->timestamp, strlen (pop3->timestamp));
63 MD5Update (&md5context, (unsigned char *)secret, strlen (secret));
64 MD5Final (md5digest, &md5context);
65 for (tmp = digest, n = 0; n < 16; n++, tmp += 2)
66 {
67 sprintf (tmp, "%02x", md5digest[n]);
68 }
69 *tmp = '\0';
70
71 status = mu_pop3_writeline (pop3, "APOP %s %s\r\n", user, digest);
72 MU_POP3_CHECK_ERROR (pop3, status);
73 mu_pop3_debug_cmd (pop3);
74 pop3->state = MU_POP3_APOP;
75 }
76
77 case MU_POP3_APOP:
78 status = mu_pop3_send (pop3);
79 MU_POP3_CHECK_EAGAIN (pop3, status);
80 pop3->acknowledge = 0;
81 pop3->state = MU_POP3_APOP_ACK;
82
83 case MU_POP3_APOP_ACK:
84 status = mu_pop3_response (pop3, NULL, 0, NULL);
85 MU_POP3_CHECK_EAGAIN (pop3, status);
86 mu_pop3_debug_ack (pop3);
87 MU_POP3_CHECK_OK (pop3);
88 pop3->state = MU_POP3_NO_STATE;
89 break;
90
91 /* They must deal with the error first by reopening. */
92 case MU_POP3_ERROR:
93 status = ECANCELED;
94 break;
95
96 /* No case in the switch another operation was in progress. */
97 default:
98 status = EINPROGRESS;
99 }
100
101 return status;
102 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include <mailutils/error.h>
28 #include <mailutils/sys/pop3.h>
29
30 /*
31 CAPA command, return a list that contains the result.
32 It is the responsability of the caller to destroy the list(list_destroy).
33 */
34 int
35 mu_pop3_capa (mu_pop3_t pop3, list_t *plist)
36 {
37 int status;
38
39 if (pop3 == NULL || plist == NULL)
40 return EINVAL;
41
42 switch (pop3->state)
43 {
44 case MU_POP3_NO_STATE:
45 status = mu_pop3_writeline (pop3, "CAPA\r\n");
46 MU_POP3_CHECK_ERROR (pop3, status);
47 mu_pop3_debug_cmd (pop3);
48 pop3->state = MU_POP3_CAPA;
49
50 case MU_POP3_CAPA:
51 status = mu_pop3_send (pop3);
52 MU_POP3_CHECK_EAGAIN (pop3, status);
53 pop3->acknowledge = 0;
54 pop3->state = MU_POP3_CAPA_ACK;
55
56 case MU_POP3_CAPA_ACK:
57 status = mu_pop3_response (pop3, NULL, 0, NULL);
58 MU_POP3_CHECK_EAGAIN (pop3, status);
59 mu_pop3_debug_ack (pop3);
60 MU_POP3_CHECK_OK (pop3);
61 status = list_create (plist);
62 MU_POP3_CHECK_ERROR(pop3, status);
63 list_set_destroy_item(*plist, free);
64 pop3->state = MU_POP3_CAPA_RX;
65
66 case MU_POP3_CAPA_RX:
67 {
68 /* CAPA line are 512 octets maximum according to RFC 2449.
69 But do not use the stack and malloc. */
70 char *capability;
71 size_t n = 0;
72
73 capability = malloc (512);
74 if (capability == NULL)
75 {
76 MU_POP3_CHECK_ERROR(pop3, ENOMEM);
77 }
78 while ((status = mu_pop3_readline (pop3, capability, 512, &n)) > 0 && n > 0)
79 {
80 /* Nuke the trailing newline */
81 if (capability[n - 1] == '\n')
82 capability[n - 1] = '\0';
83 /* add to the list. */
84 list_append (*plist, strdup (capability));
85 n = 0;
86 }
87 free (capability);
88 MU_POP3_CHECK_EAGAIN (pop3, status);
89 pop3->state = MU_POP3_NO_STATE;
90 break;
91 }
92
93 /* They must deal with the error first by reopening. */
94 case MU_POP3_ERROR:
95 status = ECANCELED;
96 break;
97
98 default:
99 status = EINPROGRESS;
100 }
101
102 return status;
103 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_set_carrier (mu_pop3_t pop3, stream_t carrier)
29 {
30 /* Sanity checks. */
31 if (pop3 == NULL)
32 return EINVAL;
33
34 if (pop3->carrier)
35 {
36 /* Close any old carrier. */
37 mu_pop3_disconnect (pop3);
38 stream_destroy (&pop3->carrier, pop3);
39 }
40 pop3->carrier = carrier;
41 return 0;
42 }
43
44 int
45 mu_pop3_get_carrier (mu_pop3_t pop3, stream_t *pcarrier)
46 {
47 /* Sanity checks. */
48 if (pop3 == NULL || pcarrier == NULL)
49 return EINVAL;
50
51 *pcarrier = pop3->carrier;
52 return 0;
53 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <sys/time.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <mailutils/sys/pop3.h>
28
29 static int mu_pop3_sleep (int seconds);
30
31 /* Open the connection to the server. The server sends an affirmative greeting
32 that may contain a timestamp for APOP. */
33 int
34 mu_pop3_connect (mu_pop3_t pop3)
35 {
36 int status;
37
38 /* Sanity checks. */
39 if (pop3 == NULL)
40 return EINVAL;
41
42 /* A networking stack. */
43 if (pop3->carrier)
44 return EINVAL;
45
46 /* Enter the pop state machine, and boogy: AUTHORISATION State. */
47 switch (pop3->state)
48 {
49 default:
50 /* FALLTHROUGH */
51 /* If pop3 was in an error state going through here should clear it. */
52
53 case MU_POP3_NO_STATE:
54 /* If the stream was previoulsy open this is sudden death:
55 for many pop servers, it is important to let them time to remove any locks or move
56 the .user.pop files. This happen when we do close() and immediately open().
57 For example, the user does not want to read the entire file, and wants to start
58 to read a new message, closing the connection and immediately
59 contacting the server again, and he'll end up having
60 "-ERR Mail Lock busy" or something similar. To prevent this race
61 condition we sleep 2 seconds. You can see this behaviour in an
62 environment where QPopper(Qualcomm POP3 server) is use and the user as a big mailbox. */
63 status = mu_pop3_disconnect (pop3);
64 if (status != 0)
65 mu_pop3_sleep (2);
66 pop3->state = MU_POP3_CONNECT;
67
68 case MU_POP3_CONNECT:
69 /* Establish the connection. */
70 status = stream_open (pop3->carrier);
71 MU_POP3_CHECK_EAGAIN (pop3, status);
72 pop3->acknowledge = 0;
73 pop3->state = MU_POP3_GREETINGS;
74
75 case MU_POP3_GREETINGS:
76 /* Get the greetings. */
77 {
78 size_t len = 0;
79 char *right, *left;
80 status = mu_pop3_response (pop3, NULL, 0, &len);
81 MU_POP3_CHECK_EAGAIN (pop3, status);
82 mu_pop3_debug_ack (pop3);
83 if (strncasecmp (pop3->ack.buf, "+OK", 3) != 0)
84 {
85 stream_close (pop3->carrier);
86 pop3->state = MU_POP3_NO_STATE;
87 return EACCES;
88 }
89
90 /* Get the timestamp. */
91 right = memchr (pop3->ack.buf, '<', len);
92 if (right)
93 {
94 len = len - (right - pop3->ack.buf);
95 left = memchr (right, '>', len);
96 if (left)
97 {
98 len = left - right + 1;
99 pop3->timestamp = calloc (len + 1, 1);
100 if (pop3->timestamp == NULL)
101 {
102 stream_close (pop3->carrier);
103 MU_POP3_CHECK_ERROR (pop3, ENOMEM);
104 }
105 memcpy (pop3->timestamp, right, len);
106 }
107 }
108 pop3->state = MU_POP3_NO_STATE;
109 }
110 } /* End AUTHORISATION state. */
111
112 return status;
113 }
114
115 /* GRRRRR!! We can not use sleep in the library since this we'll
116 muck up any alarm() done by the user. */
117 static int
118 mu_pop3_sleep (int seconds)
119 {
120 struct timeval tval;
121 tval.tv_sec = seconds;
122 tval.tv_usec = 0;
123 return select (1, NULL, NULL, NULL, &tval);
124 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/sys/pop3.h>
26
27 /* Initialise a mu_pop3_t handle. */
28
29 int
30 mu_pop3_create (mu_pop3_t *ppop3)
31 {
32 mu_pop3_t pop3;
33
34 /* Sanity check. */
35 if (ppop3 == NULL)
36 return EINVAL;
37
38 pop3 = calloc (1, sizeof *pop3);
39 if (pop3 == NULL)
40 return ENOMEM;
41
42 /* Reserve space for the ack(nowledgement) response.
43 According to RFC 2449: The maximum length of the first line of a
44 command response (including the initial greeting) is unchanged at
45 512 octets (including the terminating CRLF). */
46 pop3->ack.len = 512;
47 pop3->ack.buf = calloc (pop3->ack.len, 1);
48 if (pop3->ack.buf == NULL)
49 {
50 mu_pop3_destroy (&pop3);
51 return ENOMEM;
52 }
53 pop3->ack.ptr = pop3->ack.buf;
54
55 /* Reserve space for the data response/content.
56 RFC 2449 recommands 255, but we grow it as needed. */
57 pop3->io.len = 255;
58 pop3->io.buf = calloc (pop3->io.len, 1);
59 if (pop3->io.buf == NULL)
60 {
61 mu_pop3_destroy (&pop3);
62 return ENOMEM;
63 }
64 pop3->io.ptr = pop3->io.buf;
65
66 pop3->state = MU_POP3_NO_STATE; /* Init with no state. */
67 pop3->timeout = (10 * 60) * 100; /* The default Timeout is 10 minutes. */
68 pop3->acknowledge = 0; /* No Ack received. */
69
70 *ppop3 = pop3;
71 return 0; /* Okdoke. */
72 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_set_debug (mu_pop3_t pop3, void (*print)(const char *buffer))
28 {
29 if (pop3 == NULL)
30 return EINVAL;
31
32 pop3->debug = print;
33 return 0;
34 }
35
36 int
37 mu_pop3_debug_cmd (mu_pop3_t pop3)
38 {
39 if (pop3->debug)
40 pop3->debug (pop3->io.buf);
41 return 0;
42 }
43
44 int
45 mu_pop3_debug_ack (mu_pop3_t pop3)
46 {
47 if (pop3->debug)
48 {
49 pop3->debug (pop3->ack.buf);
50 pop3->debug ("\n");
51 }
52 return 0;
53 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_dele (mu_pop3_t pop3, unsigned msgno)
28 {
29 int status;
30
31 if (pop3 == NULL || msgno == 0)
32 return MU_ERR_INVALID_PARAMETER;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "DELE %d\r\n", msgno);
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_DELE;
41
42 case MU_POP3_DELE:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_DELE_ACK;
47
48 case MU_POP3_DELE_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 /* They must deal with the error first by reopening. */
57 case MU_POP3_ERROR:
58 status = ECANCELED;
59 break;
60
61 default:
62 status = EINPROGRESS;
63 }
64 return status;
65 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <mailutils/errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 void
27 mu_pop3_destroy (mu_pop3_t *ppop3)
28 {
29 if (ppop3 && *ppop3)
30 {
31 mu_pop3_t pop3 = *ppop3;
32
33 /* Free the response buffer. */
34 if (pop3->ack.buf)
35 free (pop3->ack.buf);
36
37 /* Free the io buffer. */
38 if (pop3->io.buf)
39 free (pop3->io.buf);
40
41 /* Free the timestamp use for APOP. */
42 if (pop3->timestamp)
43 free (pop3->timestamp);
44
45 /* Release the carrier. */
46 if (pop3->carrier)
47 stream_destroy (&pop3->carrier, pop3);
48
49 free (pop3);
50
51 *ppop3 = NULL;
52 }
53 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_disconnect (mu_pop3_t pop3)
29 {
30 /* Sanity checks. */
31 if (pop3 == NULL)
32 return EINVAL;
33
34 /* We can keep some of the fields, if they decide to pop3_connect() again but
35 clear the states. */
36 pop3->state = MU_POP3_NO_STATE;
37 pop3->acknowledge = 0;
38
39 /* Clear the buffers. */
40 memset (pop3->io.buf, '\0', pop3->io.len);
41 pop3->io.ptr = pop3->io.buf;
42 memset (pop3->ack.buf, '\0', pop3->ack.len);
43 pop3->ack.ptr = pop3->ack.buf;
44
45 /* Free the timestamp, it will be different on each connection. */
46 if (pop3->timestamp)
47 {
48 free (pop3->timestamp);
49 pop3->timestamp = NULL;
50 }
51
52 /* Close the stream. */
53 return stream_close (pop3->carrier);
54 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_list (mu_pop3_t pop3, unsigned int msgno, size_t *psize)
29 {
30 int status;
31
32 if (pop3 == NULL || msgno == 0 || psize == NULL)
33 return EINVAL;
34
35 switch (pop3->state)
36 {
37 case MU_POP3_NO_STATE:
38 status = mu_pop3_writeline (pop3, "LIST %d\r\n", msgno);
39 MU_POP3_CHECK_ERROR (pop3, status);
40 mu_pop3_debug_cmd (pop3);
41 pop3->state = MU_POP3_LIST;
42
43 case MU_POP3_LIST:
44 status = mu_pop3_send (pop3);
45 MU_POP3_CHECK_EAGAIN (pop3, status);
46 pop3->acknowledge = 0;
47 pop3->state = MU_POP3_LIST_ACK;
48
49 case MU_POP3_LIST_ACK:
50 status = mu_pop3_response (pop3, NULL, 0, NULL);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_EAGAIN (pop3, status);
53 MU_POP3_CHECK_OK (pop3);
54 pop3->state = MU_POP3_NO_STATE;
55
56 /* Parse the answer. */
57 *psize = 0;
58 sscanf (pop3->ack.buf, "+OK %d %d", &msgno, psize);
59 break;
60
61 /* They must deal with the error first by reopening. */
62 case MU_POP3_ERROR:
63 status = ECANCELED;
64 break;
65
66 default:
67 status = EINPROGRESS;
68 }
69
70 return status;
71 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <mailutils/sys/pop3.h>
27
28 int
29 mu_pop3_list_all (mu_pop3_t pop3, list_t *plist)
30 {
31 int status;
32
33 if (pop3 == NULL || plist == NULL)
34 return EINVAL;
35
36 switch (pop3->state)
37 {
38 case MU_POP3_NO_STATE:
39 status = mu_pop3_writeline (pop3, "LIST\r\n");
40 MU_POP3_CHECK_ERROR (pop3, status);
41 mu_pop3_debug_cmd (pop3);
42 pop3->state = MU_POP3_LIST;
43
44 case MU_POP3_LIST:
45 status = mu_pop3_send (pop3);
46 MU_POP3_CHECK_EAGAIN (pop3, status);
47 pop3->acknowledge = 0;
48 pop3->state = MU_POP3_LIST_ACK;
49
50 case MU_POP3_LIST_ACK:
51 status = mu_pop3_response (pop3, NULL, 0, NULL);
52 MU_POP3_CHECK_EAGAIN (pop3, status);
53 mu_pop3_debug_cmd (pop3);
54 MU_POP3_CHECK_OK (pop3);
55 status = list_create (plist);
56 MU_POP3_CHECK_ERROR(pop3, status);
57 list_set_destroy_item(*plist, free);
58 pop3->state = MU_POP3_LIST_RX;
59
60 case MU_POP3_LIST_RX:
61 {
62 /* LIST line should not be over 512 octets maximum according to RFC 2449.
63 But do not use the stack and malloc. */
64 char *lista;
65 size_t n = 0;
66
67 lista = malloc (512);
68 if (lista == NULL)
69 {
70 MU_POP3_CHECK_ERROR(pop3, ENOMEM);
71 }
72 while ((status = mu_pop3_readline (pop3, lista, 512, &n)) > 0 && n > 0)
73 {
74 /* Nuke the trailing newline */
75 if (lista[n - 1] == '\n')
76 lista[n - 1] = '\0';
77 /* add to the list. */
78 list_append (*plist, strdup (lista));
79 n = 0;
80 }
81 free (lista);
82 MU_POP3_CHECK_EAGAIN (pop3, status);
83 pop3->state = MU_POP3_NO_STATE;
84 break;
85 }
86
87 /* They must deal with the error first by reopening. */
88 case MU_POP3_ERROR:
89 status = ECANCELED;
90 break;
91
92 default:
93 status = EINPROGRESS;
94 }
95
96 return status;
97 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_noop (mu_pop3_t pop3)
28 {
29 int status;
30
31 if (pop3 == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "NOOP\r\n");
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_NOOP;
41
42 case MU_POP3_NOOP:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_NOOP_ACK;
47
48 case MU_POP3_NOOP_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 /* They must deal with the error first by reopening. */
57 case MU_POP3_ERROR:
58 status = ECANCELED;
59 break;
60
61 default:
62 status = EINPROGRESS;
63 }
64
65 return status;
66 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_pass (mu_pop3_t pop3, const char *passwd)
28 {
29 int status;
30
31 if (pop3 == NULL || passwd == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "PASS %s\r\n", passwd);
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_PASS;
41
42 case MU_POP3_PASS:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_PASS_ACK;
47
48 case MU_POP3_PASS_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 /* They must deal with the error first by reopening. */
57 case MU_POP3_ERROR:
58 status = ECANCELED;
59 break;
60
61 default:
62 status = EINPROGRESS;
63 }
64
65 return status;
66 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_quit (mu_pop3_t pop3)
28 {
29 int status;
30
31 if (pop3 == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "QUIT\r\n");
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_QUIT;
41
42 case MU_POP3_QUIT:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_QUIT_ACK;
47
48 case MU_POP3_QUIT_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 default:
57 status = EINPROGRESS;
58 }
59
60 return status;
61 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <mailutils/sys/pop3.h>
28 #include <mailutils/error.h>
29
30 static int
31 mu_pop3_carrier_is_read_ready(stream_t carrier, int timeout)
32 {
33 int fd = -1;
34 int ready = 0;
35
36 stream_get_fd (carrier, &fd);
37 if (fd >= 0)
38 {
39 struct timeval tv;
40 fd_set fset;
41
42 FD_ZERO (&fset);
43 FD_SET (fds->fd, &fset);
44
45 tv.tv_sec = timeout / 100;
46 tv.tv_usec = (timeout % 1000) * 1000;
47
48 ready = select (fds->fd + 1, &fset, NULL, NULL, (timeout == -1) ? NULL: &tv);
49 ready = (ready == -1) ? 0 : 1;
50 }
51 return ready;
52 }
53
54 /* Read a complete line from the pop server. Transform CRLF to LF, remove
55 the stuff byte termination octet ".", put a null in the buffer
56 when done. And Do a select() (stream_is_readready()) for the timeout. */
57 static int
58 mu_pop3_getline (pop3_t pop3)
59 {
60 size_t n = 0;
61 size_t total = pop3->io.ptr - pop3->io.buf;
62 int status = 0;
63
64 /* Must get a full line before bailing out. */
65 do
66 {
67 /* Timeout with select(), note that we have to reset select()
68 since on linux tv is modified when error. */
69 if (pop3->timeout)
70 {
71 int ready = mu_pop3_carrier_is_read_ready (pop3->carrier, pop3->timeout);
72 if (ready == 0)
73 return ETIMEDOUT;
74 }
75
76 status = stream_readline (pop3->carrier, pop3->io.buf + total, pop3->io.len - total, pop3->io.offset, &n);
77 if (status != 0)
78 return status;
79 pop3->io.offset += n;
80
81 /* The server went away: It maybe a timeout and some pop server
82 does not send the -ERR. Consider this like an error. */
83 if (n == 0)
84 return EIO;
85
86 total += n;
87 pop3->io.nl = memchr (pop3->io.buf, '\n', total);
88 if (pop3->io.nl == NULL) /* Do we have a full line. */
89 {
90 /* Allocate a bigger buffer ? */
91 if (total >= pop3->io.len - 1)
92 {
93 pop3->io.len *= 2;
94 pop3->io.buf = realloc (pop3->io.buf, pop3->io.len + 1);
95 if (pop3->io.buf == NULL)
96 return ENOMEM;
97 }
98 }
99 pop3->io.ptr = pop3->io.buf + total;
100 }
101 while (pop3->io.nl == NULL); /* Bail only if we have a complete line. */
102
103 /* When examining a multi-line response, the client checks to see if the
104 line begins with the termination octet "."(DOT). If yes and if octets
105 other than CRLF follow, the first octet of the line (the termination
106 octet) is stripped away. */
107 if (total >= 3 && pop3->io.buf[0] == '.')
108 {
109 if (pop3->io.buf[1] != '\r' && pop3->io.buf[2] != '\n')
110 {
111 memmove (pop3->io.buf, pop3->io.buf + 1, total - 1);
112 pop3->io.ptr--;
113 pop3->io.nl--;
114 }
115 /* And if CRLF immediately follows the termination character, then
116 the response from the POP server is ended and the line containing
117 ".CRLF" is not considered part of the multi-line response. */
118 else if (pop3->io.buf[1] == '\r' && pop3->io.buf[2] == '\n')
119 {
120 pop3->io.buf[0] = '\0';
121 pop3->io.ptr = pop3->io.buf;
122 pop3->io.nl = NULL;
123 }
124 }
125 /* \r\n --> \n\0, conversion. */
126 if (pop3->io.nl > pop3->io.buf)
127 {
128 *(pop3->io.nl - 1) = '\n';
129 *(pop3->io.nl) = '\0';
130 pop3->io.ptr = pop3->io.nl;
131 }
132 return status;
133 }
134
135 /* Call pop3_getline() for the dirty work, and consume i.e. put
136 in the user buffer only buflen. If buflen == 0 or buffer == NULL
137 nothing is consume, the data is save for another call to pop3_readline()
138 with a buffer != NULL.
139 */
140 int
141 mu_pop3_readline (pop3_t pop3, char *buffer, size_t buflen, size_t *pnread)
142 {
143 size_t nread = 0;
144 size_t n = 0;
145 int status = 0;
146
147 /* Do we need to fill up? Yes if no NL or the buffer is empty. */
148 if (pop3->carrier && (pop3->io.nl == NULL || pop3->io.ptr == pop3->io.buf))
149 {
150 status = pop3_getline (pop3);
151 if (status != 0)
152 return status;
153 }
154
155 /* How much we can copy ? */
156 n = pop3->io.ptr - pop3->io.buf;
157
158 /* Consume the line? */
159 if (buffer && buflen)
160 {
161 buflen--; /* For the null. */
162 if (buflen)
163 {
164 int nleft = buflen - n;
165 /* We got more then requested. */
166 if (nleft < 0)
167 {
168 size_t sentinel;
169 nread = buflen;
170 sentinel = pop3->io.ptr - (pop3->io.buf + nread);
171 memcpy (buffer, pop3->io.buf, nread);
172 memmove (pop3->io.buf, pop3->io.buf + nread, sentinel);
173 pop3->io.ptr = pop3->io.buf + sentinel;
174 }
175 else
176 {
177 /* Drain our buffer. */;
178 nread = n;
179 memcpy (buffer, pop3->io.buf, nread);
180 pop3->io.ptr = pop3->io.buf;
181 /* Clear of all residue. */
182 memset (pop3->io.buf, '\0', pop3->io.len);
183 }
184 }
185 buffer[nread] = '\0';
186 }
187 else
188 nread = n;
189
190 if (pnread)
191 *pnread = nread;
192 return status;
193 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 /* If we did not grap the ack already, call pop3_readline() but handle
27 Nonblocking also. */
28 int
29 mu_pop3_response (mu_pop3_t pop3, char *buffer, size_t buflen, size_t *pnread)
30 {
31 size_t n = 0;
32 int status = 0;
33
34 if (pop3 == NULL)
35 return EINVAL;
36
37 if (!pop3->acknowledge)
38 {
39 size_t len = pop3->ack.len - (pop3->ack.ptr - pop3->ack.buf);
40 status = mu_pop3_readline (pop3, pop3->ack.ptr, len, &n);
41 pop3->ack.ptr += n;
42 if (status == 0)
43 {
44 len = pop3->ack.ptr - pop3->ack.buf;
45 if (len && pop3->ack.buf[len - 1] == '\n')
46 pop3->ack.buf[len - 1] = '\0';
47 pop3->acknowledge = 1; /* Flag that we have the ack. */
48 pop3->ack.ptr = pop3->ack.buf;
49 }
50 else
51 {
52 /* Provide them with an error. */
53 const char *econ = "-ERR POP3 IO ERROR";
54 n = strlen (econ);
55 strcpy (pop3->ack.buf, econ);
56 }
57 }
58 else
59 n = strlen (pop3->ack.buf);
60
61 if (buffer)
62 {
63 buflen--; /* Leave space for the NULL. */
64 n = (buflen < n) ? buflen : n;
65 memcpy (buffer, pop3->ack.buf, n);
66 buffer[n] = '\0';
67 }
68
69 if (pnread)
70 *pnread = n;
71 return status;
72 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_retr (mu_pop3_t pop3, unsigned int msgno, stream_t *pstream)
29 {
30 int status;
31
32 if (pop3 == NULL || msgno == 0 || pstream == NULL)
33 return EINVAL;
34
35 switch (pop3->state)
36 {
37 case MU_POP3_NO_STATE:
38 status = mu_pop3_writeline (pop3, "RETR %d\r\n", msgno);
39 MU_POP3_CHECK_ERROR (pop3, status);
40 mu_pop3_debug_cmd (pop3);
41 pop3->state = MU_POP3_RETR;
42
43 case MU_POP3_RETR:
44 status = mu_pop3_send (pop3);
45 MU_POP3_CHECK_EAGAIN (pop3, status);
46 pop3->acknowledge = 0;
47 pop3->state = MU_POP3_RETR_ACK;
48
49 case MU_POP3_RETR_ACK:
50 status = mu_pop3_response (pop3, NULL, 0, NULL);
51 MU_POP3_CHECK_EAGAIN (pop3, status);
52 mu_pop3_debug_ack (pop3);
53 MU_POP3_CHECK_OK (pop3);
54 pop3->state = MU_POP3_RETR_RX;
55
56 case MU_POP3_RETR_RX:
57 status = mu_pop3_stream_create (pop3, pstream);
58 MU_POP3_CHECK_ERROR (pop3, status);
59 break;
60
61 /* They must deal with the error first by reopening. */
62 case MU_POP3_ERROR:
63 status = ECANCELED;
64 break;
65
66 default:
67 status = EINPROGRESS;
68 }
69
70 return status;
71 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_rset (mu_pop3_t pop3)
28 {
29 int status;
30
31 if (pop3 == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "RSET\r\n");
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_RSET;
41
42 case MU_POP3_RSET:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_RSET_ACK;
47
48 case MU_POP3_RSET_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 /* They must deal with the error first by reopening. */
57 case MU_POP3_ERROR:
58 status = ECANCELED;
59 break;
60
61 default:
62 status = EINPROGRESS;
63 }
64
65 return status;
66 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #define _GNU_SOURCE
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include <errno.h>
32 #include <mailutils/sys/pop3.h>
33
34 static int mu_pop3_carrier_is_write_ready (stream_t carrier, int timeout);
35
36 /* A socket may write less then expected but stream.c:stream_write() will
37 always try to send the entire buffer unless an error is reported. We have
38 to cope with nonblocking, it is done by keeping track with the pop3->ptr
39 pointer if the write failed we keep track and restart where we left. */
40 int
41 mu_pop3_send (mu_pop3_t pop3)
42 {
43 int status = 0;
44 if (pop3->carrier && (pop3->io.ptr > pop3->io.buf))
45 {
46 size_t n = 0;
47 size_t len = pop3->io.ptr - pop3->io.buf;
48
49 /* Timeout with select(), note that we have to reset select()
50 since on linux tv is modified when error. */
51 if (pop3->timeout)
52 {
53 int ready = mu_pop3_carrier_is_write_ready (pop3->carrier, pop3->timeout);
54 if (ready == 0)
55 return ETIMEDOUT;
56 }
57
58 status = stream_write (pop3->carrier, pop3->io.buf, len, 0, &n);
59 if (n)
60 {
61 /* Consume what we sent. */
62 memmove (pop3->io.buf, pop3->io.buf + n, len - n);
63 pop3->io.ptr -= n;
64 }
65 }
66 else
67 pop3->io.ptr = pop3->io.buf;
68 return status;
69 }
70
71 /* According to RFC 2449: The maximum length of a command is increased from
72 47 characters (4 character command, single space, 40 character argument,
73 CRLF) to 255 octets, including the terminating CRLF. But we are flexible
74 on this and realloc() as needed. NOTE: The terminated CRLF is not
75 included. */
76 int
77 mu_pop3_writeline (mu_pop3_t pop3, const char *format, ...)
78 {
79 int len;
80 va_list ap;
81 int done = 1;
82
83 va_start(ap, format);
84 /* C99 says that a conforming implementation of snprintf () should
85 return the number of char that would have been call but many old
86 GNU/Linux && BSD implementations return -1 on error. Worse,
87 QnX/Neutrino actually does not put the terminal null char. So
88 let's try to cope. */
89 do
90 {
91 len = vsnprintf (pop3->io.buf, pop3->io.len - 1, format, ap);
92 if (len < 0 || len >= (int)pop3->io.len
93 || !memchr (pop3->io.buf, '\0', len + 1))
94 {
95 pop3->io.len *= 2;
96 pop3->io.buf = realloc (pop3->io.buf, pop3->io.len);
97 if (pop3->io.buf == NULL)
98 return ENOMEM;
99 done = 0;
100 }
101 else
102 done = 1;
103 }
104 while (!done);
105 va_end(ap);
106 pop3->io.ptr = pop3->io.buf + len;
107 return 0;
108 }
109
110 int
111 mu_pop3_sendline (mu_pop3_t pop3, const char *line)
112 {
113 if (line)
114 {
115 int status = mu_pop3_writeline (pop3, line);
116 if (status)
117 return status;
118 }
119 return mu_pop3_send (pop3);
120 }
121
122 static int
123 mu_pop3_carrier_is_write_ready (stream_t carrier, int timeout)
124 {
125 int fd = -1;
126 int ready = 0;
127
128 stream_get_fd (carrier, &fd);
129
130 if (fd >= 0)
131 {
132 struct timeval tv;
133 fd_set fset;
134
135 FD_ZERO (&fset);
136 FD_SET (fd, &fset);
137
138 tv.tv_sec = timeout / 100;
139 tv.tv_usec = (timeout % 1000) * 1000;
140
141 ready = select (fd + 1, NULL, &fset, NULL, (timeout == -1) ? NULL: &tv);
142 ready = (ready == -1) ? 0 : 1;
143 }
144 return ready;
145 }
146
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_stat (mu_pop3_t pop3, unsigned *msg_count, size_t *size)
28 {
29 int status;
30
31 if (pop3 == NULL || msg_count == NULL || size == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "STAT\r\n");
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_STAT;
41
42 case MU_POP3_STAT:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_STAT_ACK;
47
48 case MU_POP3_STAT_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54
55 /* Parse the answer. */
56 *msg_count = 0;
57 *size = 0;
58 sscanf (pop3->ack.buf, "+OK %d %d", msg_count, size);
59 break;
60
61 /* They must deal with the error first by reopening. */
62 case MU_POP3_ERROR:
63 status = ECANCELED;
64 break;
65
66 default:
67 status = EINPROGRESS;
68 }
69
70 return status;
71 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <mailutils/sys/pop3.h>
26
27 /* Implementation of the stream for TOP and RETR. */
28 struct mu_pop3_stream
29 {
30 mu_pop3_t pop3;
31 int done;
32 };
33
34 static void
35 mu_pop3_stream_destroy (stream_t stream)
36 {
37 struct mu_pop3_stream *pop3_stream = stream_get_owner (stream);
38 if (pop3_stream)
39 {
40 free (pop3_stream);
41 }
42 }
43
44 static int
45 mu_pop3_stream_read (stream_t stream, char *buf, size_t buflen, off_t offset, size_t *pn)
46 {
47 struct mu_pop3_stream *pop3_stream = stream_get_owner (stream);
48 size_t n = 0;
49 int status = 0;
50 char *p = buf;
51
52 (void)offset;
53 if (pop3_stream)
54 {
55 if (!pop3_stream->done)
56 {
57 do
58 {
59 size_t nread = 0;
60
61 /* The pop3_readline () function will always read one less to
62 be able to null terminate the buffer, this will cause
63 serious grief for stream_read() where it is legitimate to
64 have a buffer of 1 char. So we must catch it here. */
65 if (buflen == 1)
66 {
67 char buffer[2];
68 *buffer = '\0';
69 status = mu_pop3_readline (pop3_stream->pop3, buffer, 2, &nread);
70 *p = *buffer;
71 }
72 else
73 status = mu_pop3_readline (pop3_stream->pop3, p, buflen, &nread);
74
75 if (status != 0)
76 break;
77 if (nread == 0)
78 {
79 pop3_stream->pop3->state = MU_POP3_NO_STATE;
80 pop3_stream->done = 1;
81 break;
82 }
83 n += nread;
84 buflen -= nread;
85 p += nread;
86 }
87 while (buflen > 0);
88 }
89 }
90 if (pn)
91 *pn = n;
92 return status;
93 }
94
95 static int
96 mu_pop3_stream_readline (stream_t stream, char *buf, size_t buflen, off_t offset, size_t *pn)
97 {
98 struct mu_pop3_stream *pop3_stream = stream_get_owner (stream);
99 size_t n = 0;
100 int status = 0;
101
102 (void)offset;
103 if (pop3_stream)
104 {
105 if (!pop3_stream->done)
106 {
107 status = mu_pop3_readline (pop3_stream->pop3, buf, buflen, &n);
108 if (n == 0)
109 {
110 pop3_stream->pop3->state = MU_POP3_NO_STATE;
111 pop3_stream->done = 1;
112 }
113 }
114 }
115 if (pn)
116 *pn = n;
117 return status;
118 }
119
120 int
121 mu_pop3_stream_create (mu_pop3_t pop3, stream_t *pstream)
122 {
123 struct mu_pop3_stream *pop3_stream;
124 int status;
125
126 pop3_stream = malloc (sizeof *pop3_stream);
127 if (pop3_stream == NULL)
128 return ENOMEM;
129
130 pop3_stream->pop3 = pop3;
131 pop3_stream->done = 0;
132
133 status = stream_create (pstream, MU_STREAM_READ | MU_STREAM_NO_CLOSE | MU_STREAM_NO_CHECK, pop3_stream);
134 if (status != 0)
135 {
136 free (pop3_stream);
137 return status;
138 }
139
140 stream_set_read (*pstream, mu_pop3_stream_read, pop3_stream);
141 stream_set_readline (*pstream, mu_pop3_stream_readline, pop3_stream);
142 stream_set_destroy (*pstream, mu_pop3_stream_destroy, pop3_stream);
143
144 return 0;
145 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_set_timeout (mu_pop3_t pop3, int timeout)
28 {
29 /* Sanity checks. */
30 if (pop3 == NULL)
31 return EINVAL;
32
33 pop3->timeout = timeout;
34 return 0;
35 }
36
37 int
38 mu_pop3_get_timeout (mu_pop3_t pop3, int *ptimeout)
39 {
40 /* Sanity checks. */
41 if (pop3 == NULL || ptimeout == NULL)
42 return EINVAL;
43
44 *ptimeout = pop3->timeout;
45 return 0;
46 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_top (mu_pop3_t pop3, unsigned msgno, unsigned int lines, stream_t *pstream)
28 {
29 int status;
30
31 if (pop3 == NULL || msgno == 0 || pstream == NULL)
32 return EINVAL;
33
34
35 switch (pop3->state)
36 {
37 case MU_POP3_NO_STATE:
38 status = mu_pop3_writeline (pop3, "TOP %d %d\r\n", msgno, lines);
39 MU_POP3_CHECK_ERROR (pop3, status);
40 mu_pop3_debug_cmd (pop3);
41 pop3->state = MU_POP3_TOP;
42
43 case MU_POP3_TOP:
44 status = mu_pop3_send (pop3);
45 MU_POP3_CHECK_EAGAIN (pop3, status);
46 pop3->acknowledge = 0;
47 pop3->state = MU_POP3_TOP_ACK;
48
49 case MU_POP3_TOP_ACK:
50 status = mu_pop3_response (pop3, NULL, 0, NULL);
51 MU_POP3_CHECK_EAGAIN (pop3, status);
52 mu_pop3_debug_ack (pop3);
53 MU_POP3_CHECK_OK (pop3);
54 pop3->state = MU_POP3_TOP_RX;
55
56 case MU_POP3_TOP_RX:
57 status = mu_pop3_stream_create (pop3, pstream);
58 MU_POP3_CHECK_ERROR (pop3, status);
59 break;
60
61 /* They must deal with the error first by reopening. */
62 case MU_POP3_ERROR:
63 status = ECANCELED;
64 break;
65
66 default:
67 status = EINPROGRESS;
68 }
69
70 return status;
71 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_uidl (mu_pop3_t pop3, unsigned int msgno, char **uidl)
29 {
30 int status;
31
32 if (pop3 == NULL || uidl == NULL)
33 return EINVAL;
34
35 switch (pop3->state)
36 {
37 case MU_POP3_NO_STATE:
38 status = mu_pop3_writeline (pop3, "UIDL %d\r\n", msgno);
39 MU_POP3_CHECK_ERROR (pop3, status);
40 mu_pop3_debug_cmd (pop3);
41 pop3->state = MU_POP3_UIDL;
42
43 case MU_POP3_UIDL:
44 status = mu_pop3_send (pop3);
45 MU_POP3_CHECK_EAGAIN (pop3, status);
46 pop3->acknowledge = 0;
47 pop3->state = MU_POP3_UIDL_ACK;
48
49 case MU_POP3_UIDL_ACK:
50 status = mu_pop3_response (pop3, NULL, 0, NULL);
51 MU_POP3_CHECK_EAGAIN (pop3, status);
52 mu_pop3_debug_ack (pop3);
53 MU_POP3_CHECK_OK (pop3);
54 pop3->state = MU_POP3_NO_STATE;
55
56 *uidl = NULL;
57 {
58 char *space;
59 /* Format: +OK msgno uidlstring */
60
61 /* Pass the "+OK". */
62 space = strchr (pop3->ack.buf, ' ');
63 if (space)
64 {
65 /* Skip spaces. */
66 while (*space == ' ') space++;
67 /* Pass the number. */
68 space = strchr (space, ' ');
69 if (space)
70 {
71 size_t len;
72 /* Skip spaces between msgno and uidlstring */
73 while (*space == ' ') space++;
74 len = strlen (space);
75 if (space[len - 1] == '\n')
76 {
77 space[len - 1] = '\0';
78 len--;
79 }
80 *uidl = calloc (len + 1, 1);
81 if (*uidl)
82 memcpy (*uidl, space, len);
83 }
84 }
85 }
86 if (*uidl == NULL) /* What can we do? */
87 {
88 *uidl = malloc (1);
89 if (*uidl)
90 **uidl = '\0';
91 else
92 status = ENOMEM;
93 }
94 break;
95
96 /* They must deal with the error first by reopening. */
97 case MU_POP3_ERROR:
98 status = ECANCELED;
99 break;
100
101 default:
102 status = EINPROGRESS;
103 }
104
105 return status;
106 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 # include <errno.h>
24 #include <stdlib.h>
25 #include <mailutils/sys/pop3.h>
26
27 int
28 mu_pop3_uidl_all (mu_pop3_t pop3, list_t *plist)
29 {
30 int status;
31
32 if (pop3 == NULL || plist == NULL)
33 return EINVAL;
34
35 switch (pop3->state)
36 {
37 case MU_POP3_NO_STATE:
38 status = mu_pop3_writeline (pop3, "UIDL\r\n");
39 MU_POP3_CHECK_ERROR (pop3, status);
40 mu_pop3_debug_cmd (pop3);
41 pop3->state = MU_POP3_UIDL;
42
43 case MU_POP3_UIDL:
44 status = mu_pop3_send (pop3);
45 MU_POP3_CHECK_EAGAIN (pop3, status);
46 pop3->acknowledge = 0;
47 pop3->state = MU_POP3_UIDL_ACK;
48
49 case MU_POP3_UIDL_ACK:
50 status = mu_pop3_response (pop3, NULL, 0, NULL);
51 MU_POP3_CHECK_EAGAIN (pop3, status);
52 mu_pop3_debug_ack (pop3);
53 MU_POP3_CHECK_OK (pop3);
54 pop3->state = MU_POP3_UIDL_RX;
55
56 case MU_POP3_UIDL_RX:
57 {
58 /* UIDL line are 512 octets maximum according to RFC 1939.
59 But do not use the stack and malloc. */
60 char *uidla;
61 size_t n = 0;
62
63 uidla = malloc (512);
64 if (uidla == NULL)
65 {
66 MU_POP3_CHECK_ERROR(pop3, ENOMEM);
67 }
68 while ((status = mu_pop3_readline (pop3, uidla, 512, &n)) > 0 && n > 0)
69 {
70 /* Nuke the trailing newline */
71 if (uidla[n - 1] == '\n')
72 uidla[n - 1] = '\0';
73 /* add to the list. */
74 list_append (*plist, strdup (uidla));
75 n = 0;
76 }
77 free (uidla);
78 MU_POP3_CHECK_EAGAIN (pop3, status);
79 pop3->state = MU_POP3_NO_STATE;
80 break;
81 }
82
83 /* They must deal with the error first by reopening. */
84 case MU_POP3_ERROR:
85 status = ECANCELED;
86 break;
87
88 default:
89 status = EINPROGRESS;
90 }
91
92 return status;
93 }
94
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2003 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <string.h>
23 #include <errno.h>
24 #include <mailutils/sys/pop3.h>
25
26 int
27 mu_pop3_user (mu_pop3_t pop3, const char *user)
28 {
29 int status;
30
31 if (pop3 == NULL || user == NULL)
32 return EINVAL;
33
34 switch (pop3->state)
35 {
36 case MU_POP3_NO_STATE:
37 status = mu_pop3_writeline (pop3, "USER %s\r\n", user);
38 MU_POP3_CHECK_ERROR (pop3, status);
39 mu_pop3_debug_cmd (pop3);
40 pop3->state = MU_POP3_USER;
41
42 case MU_POP3_USER:
43 status = mu_pop3_send (pop3);
44 MU_POP3_CHECK_EAGAIN (pop3, status);
45 pop3->acknowledge = 0;
46 pop3->state = MU_POP3_USER_ACK;
47
48 case MU_POP3_USER_ACK:
49 status = mu_pop3_response (pop3, NULL, 0, NULL);
50 MU_POP3_CHECK_EAGAIN (pop3, status);
51 mu_pop3_debug_ack (pop3);
52 MU_POP3_CHECK_OK (pop3);
53 pop3->state = MU_POP3_NO_STATE;
54 break;
55
56 /* They must deal with the error first by reopening. */
57 case MU_POP3_ERROR:
58 status = ECANCELED;
59 break;
60
61 default:
62 status = EINPROGRESS;
63 }
64
65 return status;
66 }