1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010-2021 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 3 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
15 Public License along with this library. If not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <mailutils/errno.h>
23 #include <mailutils/stream.h>
24 #include <mailutils/sys/imap.h>
25
26 int
mu_imap_logout(mu_imap_t imap)27 mu_imap_logout (mu_imap_t imap)
28 {
29 int status;
30
31 if (imap == NULL)
32 return EINVAL;
33 if (!imap->io)
34 return MU_ERR_NO_TRANSPORT;
35 if (imap->session_state == MU_IMAP_SESSION_INIT)
36 return MU_ERR_SEQ;
37
38 switch (imap->client_state)
39 {
40 case MU_IMAP_CLIENT_READY:
41 status = _mu_imap_tag_next (imap);
42 MU_IMAP_CHECK_EAGAIN (imap, status);
43 status = mu_imapio_printf (imap->io, "%s LOGOUT\r\n",
44 imap->tag_str);
45 MU_IMAP_CHECK_EAGAIN (imap, status);
46 MU_IMAP_FCLR (imap, MU_IMAP_RESP);
47 imap->client_state = MU_IMAP_CLIENT_LOGOUT_RX;
48
49 case MU_IMAP_CLIENT_LOGOUT_RX:
50 status = _mu_imap_response (imap, NULL, NULL);
51 MU_IMAP_CHECK_EAGAIN (imap, status);
52 imap->client_state = MU_IMAP_CLIENT_READY;
53 imap->session_state = MU_IMAP_SESSION_INIT;
54 break;
55
56 default:
57 status = EINPROGRESS;
58 }
59 return status;
60 }
61
62