aboutsummaryrefslogtreecommitdiffstats
path: root/camel/providers/pop3/camel-pop3-engine.c
blob: 2186136f95cfcf3cdcda6114324b4a6a08a90d3e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*-
 *
 * Author:
 *  Michael Zucchi <notzed@ximian.com>
 *
 * Copyright 1999, 2000 Ximian, Inc. (www.ximian.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>

#include <string.h>
#include <stdio.h>

#include <glib.h>

#include "camel-pop3-engine.h"
#include "camel-pop3-stream.h"
#include <camel/camel-service.h>
#include <camel/camel-sasl.h>

/* max 'outstanding' bytes in output stream, so we can't deadlock waiting
   for the server to accept our data when pipelining */
#define CAMEL_POP3_SEND_LIMIT (1024)


extern int camel_verbose_debug;
#define dd(x) (camel_verbose_debug?(x):0)

static void get_capabilities(CamelPOP3Engine *pe, int read_greeting);

static CamelObjectClass *parent_class = NULL;

/* Returns the class for a CamelStream */
#define CS_CLASS(so) CAMEL_POP3_ENGINE_CLASS(CAMEL_OBJECT_GET_CLASS(so))

static void
camel_pop3_engine_class_init (CamelPOP3EngineClass *camel_pop3_engine_class)
{
    parent_class = camel_type_get_global_classfuncs( CAMEL_OBJECT_TYPE );
}

static void
camel_pop3_engine_init(CamelPOP3Engine *pe, CamelPOP3EngineClass *peclass)
{
    e_dlist_init(&pe->active);
    e_dlist_init(&pe->queue);
    e_dlist_init(&pe->done);
    pe->state = CAMEL_POP3_ENGINE_DISCONNECT;
}

static void
camel_pop3_engine_finalise(CamelPOP3Engine *pe)
{
    /* FIXME: Also flush/free any outstanding requests, etc */

    if (pe->stream)
        camel_object_unref((CamelObject *)pe->stream);
}

CamelType
camel_pop3_engine_get_type (void)
{
    static CamelType camel_pop3_engine_type = CAMEL_INVALID_TYPE;

    if (camel_pop3_engine_type == CAMEL_INVALID_TYPE) {
        camel_pop3_engine_type = camel_type_register(camel_object_get_type(),
                                 "CamelPOP3Engine",
                                 sizeof( CamelPOP3Engine ),
                                 sizeof( CamelPOP3EngineClass ),
                                 (CamelObjectClassInitFunc) camel_pop3_engine_class_init,
                                 NULL,
                                 (CamelObjectInitFunc) camel_pop3_engine_init,
                                 (CamelObjectFinalizeFunc) camel_pop3_engine_finalise );
    }

    return camel_pop3_engine_type;
}

/**
 * camel_pop3_engine_new:
 * @source: source stream
 * @flags: engine flags
 *
 * Returns a NULL stream.  A null stream is always at eof, and
 * always returns success for all reads and writes.
 *
 * Return value: the stream
 **/
CamelPOP3Engine *
camel_pop3_engine_new(CamelStream *source, guint32 flags)
{
    CamelPOP3Engine *pe;

    pe = (CamelPOP3Engine *)camel_object_new(camel_pop3_engine_get_type ());

    pe->stream = (CamelPOP3Stream *)camel_pop3_stream_new(source);
    pe->state = CAMEL_POP3_ENGINE_AUTH;
    pe->flags = flags;
    
    get_capabilities(pe, TRUE);

    return pe;
}


/**
 * camel_pop3_engine_reget_capabilities:
 * @engine: pop3 engine
 *
 * Regets server capabilities (needed after a STLS command is issued for example).
 **/
void
camel_pop3_engine_reget_capabilities (CamelPOP3Engine *engine)
{
    g_return_if_fail (CAMEL_IS_POP3_ENGINE (engine));
    
    get_capabilities (engine, FALSE);
}


/* TODO: read implementation too?
   etc? */
struct {
    char *cap;
    guint32 flag;
} capa[] = {
    { "APOP" , CAMEL_POP3_CAP_APOP },
    { "TOP" , CAMEL_POP3_CAP_TOP },
    { "UIDL", CAMEL_POP3_CAP_UIDL },
    { "PIPELINING", CAMEL_POP3_CAP_PIPE },
    { "STLS", CAMEL_POP3_CAP_STLS },  /* STARTTLS */
};

static void
cmd_capa(CamelPOP3Engine *pe, CamelPOP3Stream *stream, void *data)
{
    unsigned char *line, *tok, *next;
    unsigned int len;
    int ret;
    int i;
    CamelServiceAuthType *auth;

    dd(printf("cmd_capa\n"));

    do {
        ret = camel_pop3_stream_line(stream, &line, &len);
        if (ret >= 0) {
            if (strncmp(line, "SASL ", 5) == 0) {
                tok = line+5;
                dd(printf("scanning tokens '%s'\n", tok));
                while (tok) {
                    next = strchr(tok, ' ');
                    if (next)
                        *next++ = 0;
                    auth = camel_sasl_authtype(tok);
                    if (auth) {
                        dd(printf("got auth type '%s'\n", tok));
                        pe->auth = g_list_prepend(pe->auth, auth);
                    } else {
                        dd(printf("unsupported auth type '%s'\n", tok));
                    }
                    tok = next;
                }
            } else {
                for (i=0;i<sizeof(capa)/sizeof(capa[0]);i++) {
                    if (strcmp(capa[i].cap, line) == 0)
                        pe->capa |= capa[i].flag;
                }
            }
        }
    } while (ret>0);
}

static void
get_capabilities(CamelPOP3Engine *pe, int read_greeting)
{
    CamelPOP3Command *pc;
    unsigned char *line, *apop, *apopend;
    unsigned int len;
    extern CamelServiceAuthType camel_pop3_password_authtype;
    extern CamelServiceAuthType camel_pop3_apop_authtype;
    
    if (read_greeting) {
        /* first, read the greeting */
        if (camel_pop3_stream_line(pe->stream, &line, &len) == -1
            || strncmp(line, "+OK", 3) != 0)
            return;
        
        if ((apop = strchr(line+3, '<'))
            && (apopend = strchr(apop, '>'))) {
            apopend[1] = 0;
            pe->apop = g_strdup(apop);
            pe->capa = CAMEL_POP3_CAP_APOP;
            pe->auth = g_list_append(pe->auth, &camel_pop3_apop_authtype);
        }
        
        pe->auth = g_list_prepend(pe->auth, &camel_pop3_password_authtype);
    }
    
    if (!(pe->flags & CAMEL_POP3_ENGINE_DISABLE_EXTENSIONS)) {
        pc = camel_pop3_engine_command_new(pe, CAMEL_POP3_COMMAND_MULTI, cmd_capa, NULL, "CAPA\r\n");
        while (camel_pop3_engine_iterate(pe, pc) > 0)
            ;
        camel_pop3_engine_command_free(pe, pc);
        
        if (pe->state == CAMEL_POP3_ENGINE_TRANSACTION && !(pe->capa & CAMEL_POP3_CAP_UIDL)) {
            /* check for UIDL support manually */
            pc = camel_pop3_engine_command_new (pe, CAMEL_POP3_COMMAND_SIMPLE, NULL, NULL, "UIDL 1\r\n");
            while (camel_pop3_engine_iterate (pe, pc) > 0)
                ;
            
            if (pc->state == CAMEL_POP3_COMMAND_OK)
                pe->capa |= CAMEL_POP3_CAP_UIDL;
            
            camel_pop3_engine_command_free (pe, pc);
        }
    }
}

/* returns true if the command was sent, false if it was just queued */
static int
engine_command_queue(CamelPOP3Engine *pe, CamelPOP3Command *pc)
{
    if (((pe->capa & CAMEL_POP3_CAP_PIPE) == 0 || (pe->sentlen + strlen(pc->data)) > CAMEL_POP3_SEND_LIMIT)
        && pe->current != NULL) {
        e_dlist_addtail(&pe->queue, (EDListNode *)pc);
        return FALSE;
    } else {
        /* ??? */
        if (camel_stream_write((CamelStream *)pe->stream, pc->data, strlen(pc->data)) == -1) {
            e_dlist_addtail(&pe->queue, (EDListNode *)pc);
            return FALSE;
        }

        pe->sentlen += strlen(pc->data);

        pc->state = CAMEL_POP3_COMMAND_DISPATCHED;

        if (pe->current == NULL)
            pe->current = pc;
        else
            e_dlist_addtail(&pe->active, (EDListNode *)pc);

        return TRUE;
    }
}

/* returns -1 on error (sets errno), 0 when no work to do, or >0 if work remaining */
int
camel_pop3_engine_iterate(CamelPOP3Engine *pe, CamelPOP3Command *pcwait)
{
    unsigned char *p;
    unsigned int len;
    CamelPOP3Command *pc, *pw, *pn;

    if (pcwait && pcwait->state >= CAMEL_POP3_COMMAND_OK)
        return 0;

    pc = pe->current;
    if (pc == NULL)
        return 0;

    /* LOCK */

    if (camel_pop3_stream_line(pe->stream, &pe->line, &pe->linelen) == -1)
        return -1;

    p = pe->line;
    switch (p[0]) {
    case '+':
        dd(printf("Got + response\n"));
        if (pc->flags & CAMEL_POP3_COMMAND_MULTI) {
            pc->state = CAMEL_POP3_COMMAND_DATA;
            camel_pop3_stream_set_mode(pe->stream, CAMEL_POP3_STREAM_DATA);

            if (pc->func)
                pc->func(pe, pe->stream, pc->func_data);

            /* Make sure we get all data before going back to command mode */
            while (camel_pop3_stream_getd(pe->stream, &p, &len) > 0)
                ;
            camel_pop3_stream_set_mode(pe->stream, CAMEL_POP3_STREAM_LINE);
        } else {
            pc->state = CAMEL_POP3_COMMAND_OK;
        }
        break;
    case '-':
        pc->state = CAMEL_POP3_COMMAND_ERR;
        break;
    default:
        /* what do we do now?  f'knows! */
        g_warning("Bad server response: %s\n", p);
        pc->state = CAMEL_POP3_COMMAND_ERR;
        break;
    }

    e_dlist_addtail(&pe->done, (EDListNode *)pc);
    pe->sentlen -= strlen(pc->data);

    /* Set next command */
    pe->current = (CamelPOP3Command *)e_dlist_remhead(&pe->active);
    
    /* check the queue for sending any we can now send also */
    pw = (CamelPOP3Command *)pe->queue.head;
    pn = pw->next;
    while (pn) {
        if (((pe->capa & CAMEL_POP3_CAP_PIPE) == 0 || (pe->sentlen + strlen(pw->data)) > CAMEL_POP3_SEND_LIMIT)
            && pe->current != NULL)
            break;

        if (camel_stream_write((CamelStream *)pe->stream, pw->data, strlen(pw->data)) == -1)
            return -1;

        e_dlist_remove((EDListNode *)pw);

        pe->sentlen += strlen(pw->data);
        pw->state = CAMEL_POP3_COMMAND_DISPATCHED;

        if (pe->current == NULL)
            pe->current = pw;
        else
            e_dlist_addtail(&pe->active, (EDListNode *)pw);

        pw = pn;
        pn = pn->next;
    }

    /* UNLOCK */

    if (pcwait && pcwait->state >= CAMEL_POP3_COMMAND_OK)
        return 0;

    return pe->current==NULL?0:1;
}

CamelPOP3Command *
camel_pop3_engine_command_new(CamelPOP3Engine *pe, guint32 flags, CamelPOP3CommandFunc func, void *data, const char *fmt, ...)
{
    CamelPOP3Command *pc;
    va_list ap;

    pc = g_malloc0(sizeof(*pc));
    pc->func = func;
    pc->func_data = data;
    pc->flags = flags;
    
    va_start(ap, fmt);
    pc->data = g_strdup_vprintf(fmt, ap);
    pc->state = CAMEL_POP3_COMMAND_IDLE;

    /* TODO: what about write errors? */
    engine_command_queue(pe, pc);

    return pc;
}

void
camel_pop3_engine_command_free(CamelPOP3Engine *pe, CamelPOP3Command *pc)
{
    if (pe->current != pc)
        e_dlist_remove((EDListNode *)pc);
    g_free(pc->data);
    g_free(pc);
}