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
|
/*
* WMMail - Window Maker Mail
*
* Copyright (c) 1996, 1997, 1998 Per Liden
* Copyright (c) 1997, 1998 Bryan Chan
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* imap.c: functions to handle IMAP4 mailboxes
*
* $Id: imap.c,v 1.5 1999/03/29 13:05:46 bryan.chan Exp $
*
*/
#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <proplist.h>
#include "wmmail.h"
#include "wmutil.h"
#include "net.h"
#ifdef IMAP_SUPPORT
/*
* Supported key(s) in the Options dictionary:
*
* Hostname
* Port (optional)
* Username
* Password
* Folder (optional)
*
* See mbox.c for information on states and transitions.
*
* For a local mailbox, errors encountered during an update usually
* cause the program to give up monitoring the mailbox ("ignored").
*
* For remote mailboxes, network errors are ignored. Query will be
* re-tried at the next update. Other types of errors (such as invalid
* options) still cause the program to give up.
*
*/
/* internal data structure */
typedef struct {
char *folder_name;
int socket;
int cmd_seq;
} Connection;
/* internal functions */
static Connection *init_connection(Mailbox *);
static int count_mail(Connection *, int *, int *);
static int imap_ok(char *, int);
static void close_connection(Connection *);
#define MAX_STRING_SIZE 1024
#define GET_ENTRY(x,y,z) { proplist_t sub_key = PLMakeString(z); \
y = PLGetDictionaryEntry(x, sub_key); \
PLRelease(sub_key); }
int IMAP_check(Mailbox *mailbox, int *beep, int *redraw, int *run)
{
Connection *connection;
int prev_status,
prev_new_mail_count;
connection = init_connection(mailbox);
mailbox->last_update = time(NULL);
if (connection == NULL)
return False;
else if (connection == (Connection *) -1)
return True;
else
{
prev_status = mailbox->status;
prev_new_mail_count = mailbox->new_mail_count;
if (count_mail(connection,
&mailbox->total_mail_count,
&mailbox->new_mail_count))
{
close_connection(connection);
if (mailbox->total_mail_count == 0)
{
/* there is no mail in the mailbox */
mailbox->status = NO_MAIL;
}
else if (mailbox->new_mail_count == 0)
{
/* there are no new mails */
mailbox->status = OLD_MAIL;
}
else if (mailbox->new_mail_count > prev_new_mail_count)
{
/* new mails have arrived! */
if (mailbox->status == NEW_MAIL && always_new_mail_exec)
*run |= True;
else if (mailbox->status == NEW_MAIL)
*run |= False;
else
*run |= True;
*beep = True;
mailbox->status = NEW_MAIL;
}
/* else no change */
}
else
{
croak("cannot count messages in mailbox \"%s\"; ignored", mailbox->name);
close_connection(connection);
return False;
}
*redraw |= (prev_status != mailbox->status);
return True;
}
}
/* creates a connection to the IMAP server and logs in
*
* returns: -1 if recoverable error encountered
* NULL if unrecoverable error encountered
* pointer to Connection if successfully initiated
*/
static Connection *init_connection(Mailbox *mailbox)
{
Connection *connection;
struct sockaddr_in remote_addr;
struct hostent *host;
char buf[MAX_STRING_SIZE];
int sockfd,
port_no = -1;
proplist_t hostname,
port,
username,
password,
folder;
GET_ENTRY(mailbox->options, hostname, "Hostname");
GET_ENTRY(mailbox->options, port, "Port");
GET_ENTRY(mailbox->options, username, "Username");
GET_ENTRY(mailbox->options, password, "Password");
GET_ENTRY(mailbox->options, folder, "Folder");
if (!username || !password || !hostname)
{
croak("mailbox \"%s\" missing one of the options \"Username\", "
"\"Password\", and \"Hostname\"; ignored", mailbox->name);
return NULL;
}
else if ( !PLIsString(hostname) ||
!PLIsString(username) ||
!PLIsString(password) ||
(folder && !PLIsString(folder)) ||
(port && (!PLIsString(port) ||
sscanf(PLGetString(port), "%i", &port_no) != 1)) )
{
croak("mailbox \"%s\" has invalid options; ignored", mailbox->name);
return (Connection *) NULL;
}
if (( host = gethostbyname(PLGetString(hostname)) ) == NULL)
{
croak("gethostbyname() failed for mailbox \"%s\"; continuing",
mailbox->name);
return (Connection *) -1;
}
if (( sockfd = socket(AF_INET, SOCK_STREAM, 0) ) == -1)
{
croak("socket() failed for mailbox\"%s\"; continuing", mailbox->name);
return (Connection *) -1;
}
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(port_no == -1 ? 143 : port_no);
remote_addr.sin_addr = *((struct in_addr *) host->h_addr);
bzero(&remote_addr.sin_zero, 8);
if (connect(sockfd, (struct sockaddr *) &remote_addr,
sizeof(struct sockaddr)) == -1)
{
croak("connect() failed for mailbox\"%s\"; continuing", mailbox->name);
close(sockfd);
return (Connection *) -1;
}
/* are we welcome? */
socket_read(sockfd, buf, MAX_STRING_SIZE);
if (!imap_ok(buf, -1))
{
/* give up if not; we don't want to force open a door that won't */
croak("no service from remote server for mailbox \"%s\"; ignored",
mailbox->name);
close(sockfd);
return NULL;
}
sprintf(buf, "A000 LOGIN %s %s\r\n", PLGetString(username),
PLGetString(password));
socket_write(sockfd, buf);
socket_read(sockfd, buf, MAX_STRING_SIZE);
if (imap_ok(buf, 0))
{
connection = wmalloc(sizeof(Connection));
connection->folder_name = folder ? PLGetString(folder) : "INBOX";
connection->socket = sockfd;
connection->cmd_seq = 1; /* A000 is already issued, above */
return connection;
}
else
{
/* FIXME: remove this debug message before release! */
croak("%s", buf);
/* if we can't login, then don't try any more */
croak("login failed for mailbox \"%s\"; ignored", mailbox->name);
close(sockfd);
return NULL;
}
}
/* FIXME: socket errors are transparent! */
static int count_mail(connection, total_mail_count, new_mail_count)
Connection *connection;
int *total_mail_count,
*new_mail_count;
{
char buf[MAX_STRING_SIZE];
char cmd[MAX_STRING_SIZE];
sprintf(cmd, "A%03i SELECT %s\r\n",
connection->cmd_seq++,
connection->folder_name);
if (socket_write(connection->socket, cmd))
do
{
if (!socket_read(connection->socket, buf, MAX_STRING_SIZE))
break;
if (strstr(buf, "EXISTS"))
*total_mail_count = atoi(strstr(buf, "*") + 1);
}
while (strncmp(buf, cmd, 4));
sprintf(cmd, "A%03i CHECK\r\n", connection->cmd_seq++);
if (socket_write(connection->socket, cmd))
do
if (!socket_read(connection->socket, buf, MAX_STRING_SIZE))
break;
while (strncmp(buf, cmd, 4));
*new_mail_count = 0;
sprintf(cmd, "A%03i SEARCH UNSEEN\r\n", connection->cmd_seq++);
if (socket_write(connection->socket, cmd))
{
socket_read(connection->socket, buf, MAX_STRING_SIZE);
while (strncmp(buf, cmd, 4))
{
char *c;
/* From RFC1730:
*
* " SEARCH Response
*
* Data: zero or more numbers
*
* .... The number(s) refer to those messages that match the
* search criteria. For SEARCH, these are message sequence
* numbers;.... Each number is delimited by a space.
*
* Example: S: * SEARCH 2 3 6 10 "
*
*/
for (c = &buf[7]; *c != '\0'; c++)
if (*c == ' ')
(*new_mail_count)++;
/* server closed connection? */
if (!socket_read(connection->socket, buf, MAX_STRING_SIZE))
break;
}
}
return True;
}
/* verify that the return string from the server contains "OK";
* we should probably make use of this function more frequently
*/
static int imap_ok(char *buf, int cmd_seq)
{
/* make a slightly larger buffer just in case;
* maybe we should just assert(cmd_seq <= 999)
*/
char s[10];
if (!buf)
return False;
/* assume untagged response if cmd_seq is negative */
if (cmd_seq < 0)
sprintf(s, "* OK");
else
sprintf(s, "A%03i OK", cmd_seq);
if (strncasecmp(buf, s, strlen(s)))
return False;
else
return True;
}
static void close_connection(Connection *connection)
{
char buf[MAX_STRING_SIZE];
sprintf(buf, "A%03i LOGOUT\r\n", connection->cmd_seq);
socket_write(connection->socket, buf);
socket_read(connection->socket, buf, MAX_STRING_SIZE);
close(connection->socket);
wfree(connection);
}
#endif
|