Update.
[gsasl.git] / src / imap.c
blob9c26f4196ed3bd2fb49e6e8ba26dabf0099daa26
1 /* imap.c --- Implement IMAP profile of SASL login.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006 Simon Josefsson
4 * This file is part of GNU SASL.
6 * GNU SASL is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * GNU SASL is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU SASL; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "imap.h"
24 #define MAX_LINE_LENGTH BUFSIZ
26 int
27 imap_greeting (void)
29 char *in;
31 if (!readln (&in))
32 return 0;
34 return 1;
37 int
38 imap_has_starttls (void)
40 char *in, *capa;
41 int has_tls = 0;
43 if (!writeln (". CAPABILITY"))
44 return 0;
46 if (!readln (&in))
47 return 0;
49 has_tls = strstr (in, "STARTTLS") != NULL;
51 if (!readln (&in))
52 return 0;
54 return has_tls;
57 int
58 imap_starttls (void)
60 char *in;
62 if (!writeln (". STARTTLS"))
63 return 0;
65 if (!readln (&in))
66 return 0;
68 return 1;
71 int
72 imap_select_mechanism (char **mechlist)
74 char *in;
76 if (args_info.server_flag)
78 if (!args_info.quiet_given)
79 fprintf (stderr, _("Chose SASL mechanisms:\n"));
80 if (!readln (&in))
81 return 0;
82 *mechlist = in;
84 else
86 if (!writeln (". CAPABILITY"))
87 return 0;
89 if (!readln (&in))
90 return 0;
92 /* XXX parse IMAP capability line */
94 *mechlist = in;
96 if (!readln (&in))
97 return 0;
100 return 1;
104 imap_authenticate (const char *mech)
106 if (args_info.server_flag)
108 if (!args_info.quiet_given)
109 fprintf (stderr, _("Using mechanism:\n"));
110 puts (mech);
112 else
114 char input[MAX_LINE_LENGTH];
116 sprintf (input, ". AUTHENTICATE %s", mech);
117 if (!writeln (input))
118 return 0;
121 return 1;
125 imap_step_send (const char *data)
127 char input[MAX_LINE_LENGTH];
129 if (args_info.server_flag)
130 sprintf (input, "+ %s", data);
131 else
132 sprintf (input, "%s", data);
133 if (!writeln (input))
134 return 0;
136 return 1;
140 imap_step_recv (char **data)
142 char *p;
144 if (!readln (data))
145 return 0;
147 p = *data;
149 if (!args_info.server_flag)
151 if (p[0] != '+' || p[1] != ' ')
153 fprintf (stderr, _("error: Server did not return expected SASL "
154 "data (it must begin with '+ '):\n%s\n"), p);
155 return 0;
158 memmove (&p[0], &p[2], strlen (p) - 1);
161 if (p[strlen (p) - 1] == '\n')
162 p[strlen (p) - 1] = '\0';
163 if (p[strlen (p) - 1] == '\r')
164 p[strlen (p) - 1] = '\0';
166 return 1;
170 imap_auth_finish (void)
172 char *in;
174 if (!readln (&in))
175 return 0;
177 return 1;
181 imap_logout (void)
183 char *in;
185 if (!writeln (". LOGOUT"))
186 return 0;
188 /* read "* BYE ..." */
189 if (!readln (&in))
190 return 0;
192 free (in);
194 /* read ". OK ..." */
195 if (!readln (&in))
196 return 0;
198 free (in);
200 return 1;