aboutsummaryrefslogtreecommitdiffstats
path: root/imap.c
blob: ec529cf143dd5180ab30962109b44e1461a7df95 (plain)
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
/*
 * imap.c -- IMAP2bis protocol methods
 *
 * Copyright 1996 by Eric S. Raymond
 * All rights reserved.
 * For license terms, see the file COPYING in this directory.
 */

#include  <config.h>
#include  <stdio.h>
#include  <string.h>
#include  <ctype.h>
#if defined(STDC_HEADERS)
#include  <stdlib.h>
#endif
#include  "socket.h"
#include  "fetchmail.h"

static int count, seen, recent, unseen;

int imap_ok (FILE *sockfp,  char *argbuf)
/* parse command response */
{
    char buf [POPBUFSIZE+1];

    seen = 0;
    do {
	if (SockGets(buf, sizeof(buf), sockfp) < 0)
	    return(PS_SOCKET);

	if (outlevel == O_VERBOSE)
	    fprintf(stderr,"%s\n",buf);

	/* interpret untagged status responses */
	if (strstr(buf, "EXISTS"))
	    count = atoi(buf+2);
	if (strstr(buf, "RECENT"))
	    recent = atoi(buf+2);
	if (strstr(buf, "UNSEEN"))
	    unseen = atoi(buf+2);
	if (strstr(buf, "FLAGS"))
	    seen = (strstr(buf, "Seen") != (char *)NULL);
    } while
	(tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));

    if (tag[0] == '\0')
    {
	strcpy(argbuf, buf);
	return(0); 
    }
    else
    {
	char	*cp;

	/* skip the tag */
	for (cp = buf; !isspace(*cp); cp++)
	    continue;
	while (isspace(*cp))
	    cp++;

	if (strncmp(cp, "OK", 2) == 0)
	{
	    strcpy(argbuf, cp);
	    return(0);
	}
	else if (strncmp(cp, "BAD", 2) == 0)
	    return(PS_ERROR);
	else
	    return(PS_PROTOCOL);
    }
}

int imap_getauth(FILE *sockfp, struct query *ctl, char *buf)
/* apply for connection authorization */
{
    /* try to get authorized */
    return(gen_transact(sockfp,
		  "LOGIN %s \"%s\"",
		  ctl->remotename, ctl->password));
}

static int imap_getrange(FILE *sockfp, struct query *ctl, int*countp, int*newp)
/* get range of messages to be fetched */
{
    int ok;

    /* find out how many messages are waiting */
    recent = unseen = 0;
    ok = gen_transact(sockfp,
		  "SELECT %s",
		  ctl->mailbox[0] ? ctl->mailbox : "INBOX");
    if (ok != 0)
	return(ok);

    *countp = count;

    if (unseen)		/* optional response, but better if we see it */
	*newp = unseen;
    else if (recent)	/* mandatory */
	*newp = recent;
    else
	*newp = -1;	/* should never happen, RECENT is mandatory */ 

    return(0);
}

static int imap_getsizes(FILE *sockfp, int count, int *sizes)
/* capture the sizes of all messages */
{
    char buf [POPBUFSIZE+1];

    gen_send(sockfp, "FETCH 1:%d RFC822.SIZE", count);
    while (SockGets(buf, sizeof(buf), sockfp) >= 0)
    {
	int num, size;

	if (outlevel == O_VERBOSE)
	    fprintf(stderr,"%s\n",buf);
	if (strstr(buf, "OK"))
	    break;
	else if (sscanf(buf, "* %d FETCH (RFC822.SIZE %d)", &num, &size) == 2)
	    sizes[num - 1] = size;
	else
	    sizes[num - 1] = -1;
    }

    return(0);
}

static int imap_is_old(FILE *sockfp, struct query *ctl, int num)
/* is the given message old? */
{
    int ok;

    if ((ok = gen_transact(sockfp, "FETCH %d FLAGS", num)) != 0)
	return(PS_ERROR);

    return(seen);
}

static int imap_fetch(FILE *sockfp, int number, int *lenp)
/* request nth message */
{
    char buf [POPBUFSIZE+1];
    int	num;

    gen_send(sockfp, "FETCH %d RFC822", number);

    /* looking for FETCH response */
    do {
	if (SockGets(buf, sizeof(buf), sockfp) < 0)
	    return(PS_SOCKET);
    } while
	    (sscanf(buf+2, "%d FETCH (RFC822 {%d}", &num, lenp) != 2);

    if (num != number)
	return(PS_ERROR);
    else
	return(0);
}

static int imap_trail(FILE *sockfp, struct query *ctl, int number)
/* discard tail of FETCH response after reading message text */
{
    char buf [POPBUFSIZE+1];

    if (SockGets(buf, sizeof(buf), sockfp) < 0)
	return(PS_SOCKET);
    else
	return(0);
}

static int imap_delete(FILE *sockfp, struct query *ctl, int number)
/* set delete flag for given message */
{
    return(gen_transact(sockfp, "STORE %d +FLAGS (\\Deleted)", number));
}

const static struct method imap =
{
    "IMAP",		/* Internet Message Access Protocol */
    143,		/* standard IMAP2bis/IMAP4 port */
    1,			/* this is a tagged protocol */
    0,			/* no message delimiter */
    imap_ok,		/* parse command response */
    imap_getauth,	/* get authorization */
    imap_getrange,	/* query range of messages */
    imap_getsizes,	/* grab message sizes */
    imap_is_old,	/* no UID check */
    imap_fetch,		/* request given message */
    imap_trail,		/* eat message trailer */
    imap_delete,	/* set IMAP delete flag */
    "EXPUNGE",		/* the IMAP expunge command */
    "LOGOUT",		/* the IMAP exit command */
};

int doIMAP(struct query *ctl)
/* retrieve messages using IMAP Version 2bis or Version 4 */
{
    return(do_protocol(ctl, &imap));
}

/* imap.c ends here */