Bump versions.
[gsasl.git] / src / imap.c
blobd3408c408f5803653fa8f15fb64008b60c97b401
1 /* imap.c --- Implement IMAP profile of SASL login.
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
4 * This file is part of GNU SASL.
6 * This program 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 3 of the License, or
9 * (at your option) any later version.
11 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "imap.h"
23 #define MAX_LINE_LENGTH BUFSIZ
25 int
26 imap_greeting (void)
28 char *in;
30 if (!readln (&in))
31 return 0;
33 return 1;
36 int
37 imap_has_starttls (void)
39 char *in, *capa;
40 int has_tls = 0;
42 if (!writeln (". CAPABILITY"))
43 return 0;
45 if (!readln (&in))
46 return 0;
48 has_tls = strstr (in, "STARTTLS") != NULL;
50 if (!readln (&in))
51 return 0;
53 return has_tls;
56 int
57 imap_starttls (void)
59 char *in;
61 if (!writeln (". STARTTLS"))
62 return 0;
64 if (!readln (&in))
65 return 0;
67 return 1;
70 int
71 imap_select_mechanism (char **mechlist)
73 char *in;
75 if (args_info.server_flag)
77 if (!args_info.quiet_given)
78 fprintf (stderr, _("Chose SASL mechanisms:\n"));
79 if (!readln (&in))
80 return 0;
81 *mechlist = in;
83 else
85 if (!writeln (". CAPABILITY"))
86 return 0;
88 if (!readln (&in))
89 return 0;
91 /* XXX parse IMAP capability line */
93 *mechlist = in;
95 if (!readln (&in))
96 return 0;
99 return 1;
103 imap_authenticate (const char *mech)
105 if (args_info.server_flag)
107 if (!args_info.quiet_given)
108 fprintf (stderr, _("Using mechanism:\n"));
109 puts (mech);
111 else
113 char input[MAX_LINE_LENGTH];
115 sprintf (input, ". AUTHENTICATE %s", mech);
116 if (!writeln (input))
117 return 0;
120 return 1;
124 imap_step_send (const char *data)
126 char input[MAX_LINE_LENGTH];
128 if (args_info.server_flag)
129 sprintf (input, "+ %s", data);
130 else
131 sprintf (input, "%s", data);
132 if (!writeln (input))
133 return 0;
135 return 1;
139 imap_step_recv (char **data)
141 char *p;
143 if (!readln (data))
144 return 0;
146 p = *data;
148 if (!args_info.server_flag)
150 if (p[0] != '+' || p[1] != ' ')
152 fprintf (stderr, _("error: Server did not return expected SASL "
153 "data (it must begin with '+ '):\n%s\n"), p);
154 return 0;
157 memmove (&p[0], &p[2], strlen (p) - 1);
160 if (p[strlen (p) - 1] == '\n')
161 p[strlen (p) - 1] = '\0';
162 if (p[strlen (p) - 1] == '\r')
163 p[strlen (p) - 1] = '\0';
165 return 1;
169 imap_auth_finish (void)
171 char *in;
173 if (!readln (&in))
174 return 0;
176 return 1;
180 imap_logout (void)
182 char *in;
184 if (!writeln (". LOGOUT"))
185 return 0;
187 /* read "* BYE ..." */
188 if (!readln (&in))
189 return 0;
191 free (in);
193 /* read ". OK ..." */
194 if (!readln (&in))
195 return 0;
197 free (in);
199 return 1;