File: imap.c

package info (click to toggle)
asmail 2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,024 kB
  • ctags: 301
  • sloc: ansic: 3,045; sh: 183; makefile: 88
file content (307 lines) | stat: -rw-r--r-- 6,574 bytes parent folder | download | duplicates (6)
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
/*
 * asmail is the AfterStep mailbox monitor
 * Copyright (c) 2002 Albert Dorofeev <albert@tigr.net>
 * For the updates see http://www.tigr.net/
 *
 * This software is distributed under GPL. For details see LICENSE file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#include "globals.h"
#include "imap.h"
#include "socklib.h"


#define CREATE_OPEN_SOCK { \
	if ( !( *sock = Sopen() ) ) { \
		return STAT_FAIL; \
	} \
	s = *sock; \
\
   if ( Sclient(s, mb->server, mb->port) == -1) { \
		BYE(STAT_CONN); \
	} \
}


#define CHECK_GREETING { \
   WAITOK; \
   if ( strncmp(input, "* OK", 4) ) { \
      BYE(STAT_CONN); \
   } \
}


#define BYE(RET_STATUS) { \
	Sclose(s); \
   *sock = NULL; \
	return(RET_STATUS); \
}


#ifdef HAVE_OPENSSL_SSL_H

#define WRITE_OUTPUT { \
   ret = (mb->flags & FLAG_SSL) && (s->ssl) ? \
      Sslwrite(s, output) : Swrite(s->sd, output); \
   if ( ret == -1 ) { \
      BYE(STAT_CONN); \
   } \
}

#define WAITOK { \
   ret = (mb->flags & FLAG_SSL) && (s->ssl) ? \
      Sslread(s, input, MAX_INPUT_LENGTH, mb->timeout) : \
      Sread(s->sd, input, MAX_INPUT_LENGTH, mb->timeout); \
\
   switch (ret) {	\
      case -1:	/* Error */			\
         BYE(STAT_CONN);				\
         break; \
      case 0:		/* Timeout */			\
         BYE(STAT_TIMEOUT);				\
         break; \
   } \
}

#else

#define WRITE_OUTPUT { \
   ret = Swrite(s->sd, output); \
   if ( ret == -1 ) { \
      BYE(STAT_CONN); \
   } \
}


#define WAITOK { \
   ret = Sread(s->sd, input, MAX_INPUT_LENGTH, mb->timeout); \
   switch (ret) {	\
      case -1:	/* Error */	\
         BYE(STAT_CONN); \
         break; \
      case 0:		/* Timeout */ \
         BYE(STAT_TIMEOUT); \
         break; \
   } \
}

#endif




int imap_login( struct mbox_struct * mb, SOCKET ** sock ) {
   int ret;
	char input[MAX_INPUT_LENGTH+1];
	char output[MAX_INPUT_LENGTH+1];
	SOCKET * s;


   CREATE_OPEN_SOCK;

#ifdef HAVE_OPENSSL_SSL_H
   if ( (mb->flags & FLAG_SSL) && (Sslclient(s, mb->trustedCaDir) == -1) ) {

      /* no imaps server:
       *    close the connection, try to re-connect and negotiate tls */

      Sclose(s);
      s = NULL;

      CREATE_OPEN_SOCK;
      CHECK_GREETING;

      sprintf(output, "A000 STARTTLS\r\n");
      WRITE_OUTPUT;
      WAITOK;

      if ( strncmp(input, "A000 OK", 7) ) {
         BYE(STAT_CONN);
      }

      if ( Sslclient(s, mb->trustedCaDir) == -1) {
         BYE(STAT_CONN);
      }
   }
   else {
      CHECK_GREETING;
   }
#else
   CHECK_GREETING;
#endif

	/* connection is open, let's log in */

	sprintf(output, "A000 LOGIN %s %s\r\n", mb->user, mb->pass);
   WRITE_OUTPUT;
	WAITOK;
	if ( strncmp(input, "A000 OK", 7) ) {
		BYE(STAT_LOGIN);
	}
	return(STAT_IDLE);
}


int imap_checkmbox ( struct mbox_struct * mb, SOCKET ** sock ) {
   int ret;
	int ctotal = 0;
	int cnew = 0;
	char input[MAX_INPUT_LENGTH+1];
	char output[MAX_INPUT_LENGTH+1];
	char * tmp_str;
	SOCKET * s;
	s = *sock;


   if (mb->status != (STAT_IDLE | STAT_RUN))
      return(mb->status);

	/* seems we are logged in, get statistics */
	sprintf(output, "A001 STATUS %s (MESSAGES)\r\n", mb->mbox);
   WRITE_OUTPUT;
	WAITOK;
	if ( strncmp(input, "* STATUS", 8) ) {
		/* Notes 6 sends a different (non-standard?) response */
		/* According to Jason Day <jasonday@worldnet.att.net> :
		 * * OK Domino IMAP4 Server Release 6.0.1 ready
		 * A000 LOGIN xxx xxx
		 * A000 OK LOGIN completed
		 * A001 STATUS INBOX (MESSAGES)
		 * * 10 EXISTS
		 * * 0 RECENT
		 * * STATUS INBOX (MESSAGES 10)
		 * A001 OK STATUS completed
		 */
		if (strncmp (input, "* ", 2)) {
		BYE(STAT_CONN);
		}
		WAITOK;

		if (strncmp (input, "* ", 2)) {
			BYE(STAT_CONN);
		}
		WAITOK;

		if ( strncmp(input, "* STATUS", 8) ) {
			BYE(STAT_CONN);
		}
	}
	if ( (tmp_str = strstr(input, "MESSAGES")) ) {
		sscanf(tmp_str, "MESSAGES %d", &ctotal);
	} else {
		printf("asmail: imap_mailcheck: did not get MESSAGES in response to the STATUS.\n");
		BYE(STAT_FAIL);
	}
	WAITOK;
	if ( strncmp(input, "A001 OK", 7) ) {
		BYE(STAT_CONN);
	}
	sprintf(output, "A002 STATUS %s (UNSEEN)\r\n", mb->mbox);
   WRITE_OUTPUT;
	WAITOK;
	if ( strncmp(input, "* STATUS", 8) ) {
		BYE(STAT_CONN);
	}
	if ( (tmp_str = strstr(input, "UNSEEN")) ) {
		sscanf(tmp_str, "UNSEEN %d", &cnew);
	} else {
		printf("asmail: imap_mailcheck: did not get UNSEEN in response to the STATUS.\n");
		BYE(STAT_FAIL);
	}
	WAITOK;
	if ( strncmp(input, "A002 OK", 7) ) {
		BYE(STAT_CONN);
	}
	if ( (mb->cnew != cnew) && (cnew != 0) ) {
		pthread_mutex_lock(&mb->mutex);
		mb->flags |= FLAG_ARRIVED;
		pthread_mutex_unlock(&mb->mutex);
	}
	mb->cnew = cnew;
	mb->ctotal = ctotal;
	return(STAT_IDLE);
}

int imap_goodbye ( struct mbox_struct * mb, SOCKET ** sock ) {
   int ret;
	char input[MAX_INPUT_LENGTH+1];
	char output[MAX_INPUT_LENGTH+1];
	SOCKET * s;
	s = *sock;

   if (!s)
      return(mb->status);

	/* and good-bye */
	sprintf(output, "A004 LOGOUT\r\n");
   WRITE_OUTPUT;
   WAITOK;
	if ( strncmp(input, "* BYE", 5) ) {
		BYE(STAT_CONN);
	}
	Sclose(*sock);
   *sock = NULL;
	return(STAT_IDLE);
}

void imap_handle( struct mbox_struct * mb ) {
	SOCKET * s;
	/* Sanity check */
	if ( mb->type != MBOX_IMAP ) {
		printf("asmail: imap_handle: Cowardly refusing to work with a non-IMAP server.\n");
		mb->status = STAT_FAIL;
		pthread_exit(NULL);
	}
	if ( ! strlen(mb->server) ) {
		printf("asmail: imap_handle: no server name specified!\n");
		mb->status = STAT_FAIL;
		pthread_exit(NULL);
	}
	if ( ! strlen(mb->user) ) {
		printf("asmail: imap_handle: no user name specified!\n");
		mb->status = STAT_FAIL;
		pthread_exit(NULL);
	}
	if ( mb->port == 0 ) {
      mb->port = (mb->flags & FLAG_SSL) ? PORT_IMAPS : PORT_IMAP;
   }
	if ( ! strlen(mb->mbox) )
		strcpy(mb->mbox, IMAP_MAILBOX_DEFAULT);
	if ( mb->flags & FLAG_PERSISTENT_CONN )
		mb->status = imap_login(mb, &s);
	while (1) {
		mb->status |= STAT_RUN;
		signal_update();

		if ( mb->flags & FLAG_PERSISTENT_CONN ) {
			if ( (mb->status = imap_checkmbox(mb, &s)) ) {
				/* something failed - retry once */
				mb->status = imap_goodbye(mb, &s);
				mb->status = imap_login(mb, &s) | 
					imap_checkmbox(mb, &s);
			}
		} else {
			mb->status |= imap_login(mb, &s);
			mb->status |= imap_checkmbox(mb, &s);
			mb->status |= imap_goodbye(mb, &s);
		}
		mb->status &= ~STAT_RUN;
		if ( ! mb->status ) {
			mb->status = STAT_IDLE;
			if ( mb->cnew > 0)
				mb->mail = MAIL_NEW;
			else if ( mb->ctotal > 0 )
				mb->mail = MAIL_OLD;
			else
				mb->mail = MAIL_NONE;
		}
		signal_update();
		sleep_check(mb->update);
	}
}