Add.
[gsasl.git] / src / imap.c
blob0951d9f043de1c5f2797413bc450986ade1f200c
1 /* imap.c --- Implement IMAP profile of SASL login.
2 * Copyright (C) 2002, 2003, 2004, 2005 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 (p[0] != '+' || p[1] != ' ')
151 fprintf (stderr, _("error: Server did not return expected SASL "
152 "data (it must begin with '+ '):\n%s\n"), p);
153 return 0;
156 memmove (&p[0], &p[2], strlen (p) - 1);
158 if (p[strlen (p) - 1] == '\n')
159 p[strlen (p) - 1] = '\0';
160 if (p[strlen (p) - 1] == '\r')
161 p[strlen (p) - 1] = '\0';
163 return 1;
167 imap_auth_finish (void)
169 char *in;
171 if (!readln (&in))
172 return 0;
174 return 1;
178 imap_logout (void)
180 char *in;
182 if (!writeln (". LOGOUT"))
183 return 0;
185 /* read "* BYE ..." */
186 if (!readln (&in))
187 return 0;
189 free (in);
191 /* read ". OK ..." */
192 if (!readln (&in))
193 return 0;
195 free (in);
197 return 1;