util.c 9.52 KB
Newer Older
1 2 3
/*
 * Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4
 * Copyright (C) 1999-2000 Brendan Cully <brendan@kublai.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
 * 
 *     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
18
 *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
19 20 21 22 23
 */ 

/* general IMAP utility functions */

#include "mutt.h"
24
#include "mx.h"	/* for M_IMAP */
25
#include "url.h"
26
#include "imap_private.h"
27
#include "imap_ssl.h"
28 29 30 31

#include <stdlib.h>
#include <ctype.h>

32 33 34 35 36 37
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>

#include <errno.h>

38 39 40 41 42 43 44 45 46 47
/* imap_continue: display a message and ask the user if she wants to
 *   go on. */
int imap_continue (const char* msg, const char* resp)
{
  imap_error (msg, resp);
  return mutt_yesorno (_("Continue?"), 0);
}

/* imap_error: show an error and abort */
void imap_error (const char *where, const char *msg)
48
{
49 50
  mutt_error (_("%s [%s]\n"), where, msg);
  sleep (2);
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
}

/*
 * Fix up the imap path.  This is necessary because the rest of mutt
 * assumes a hierarchy delimiter of '/', which is not necessarily true
 * in IMAP.  Additionally, the filesystem converts multiple hierarchy
 * delimiters into a single one, ie "///" is equal to "/".  IMAP servers
 * are not required to do this.
 */
char *imap_fix_path (IMAP_DATA *idata, char *mailbox, char *path, 
    size_t plen)
{
  int x = 0;

  if (!mailbox || !*mailbox)
  {
    strfcpy (path, "INBOX", plen);
    return path;
  }

  while (mailbox && *mailbox && (x < (plen - 1)))
  {
    if ((*mailbox == '/') || (*mailbox == idata->delim))
    {
      while ((*mailbox == '/') || (*mailbox == idata->delim)) mailbox++;
      path[x] = idata->delim;
    }
    else
    {
      path[x] = *mailbox;
      mailbox++;
    }
    x++;
  }
  path[x] = '\0';
  return path;
}

/* imap_get_literal_count: write number of bytes in an IMAP literal into
 *   bytes, return 0 on success, -1 on failure. */
int imap_get_literal_count(const char *buf, long *bytes)
{
  char *pc;
  char *pn;

  if (!(pc = strchr (buf, '{')))
    return (-1);
  pc++;
  pn = pc;
  while (isdigit (*pc))
    pc++;
  *pc = 0;
  *bytes = atoi(pn);
  return (0);
}

107 108 109
/* imap_get_qualifier: in a tagged response, skip tag and status for
 *   the qualifier message. Used by imap_copy_message for TRYCREATE */
char* imap_get_qualifier (char* buf)
110
{
111 112 113 114 115 116
  char *s = buf;

  /* skip tag */
  s = imap_next_word (s);
  /* skip OK/NO/BAD response */
  s = imap_next_word (s);
117

118
  return s;
119 120 121 122 123 124 125 126 127 128 129 130
}

/* imap_next_word: return index into string where next IMAP word begins */
char *imap_next_word (char *s)
{
  while (*s && !ISSPACE (*s))
    s++;
  SKIPWS (s);
  return s;
}

/* imap_parse_path: given an IMAP mailbox name, return host, port
Thomas Roessler's avatar
Thomas Roessler committed
131 132
 *   and a path IMAP servers will recognise.
 * mx.mbox is malloc'd, caller must free it */
133
int imap_parse_path (const char* path, IMAP_MBOX* mx)
134
{
135
  char tmp[128];
136 137
  url_scheme_t scheme;
  ciss_url_t url;
138
  char *c;
139
  int n;
140

141
  /* Defaults */
142 143
  mx->account.flags = 0;
  mx->account.port = IMAP_PORT;
144 145 146 147 148 149
  mx->account.type = M_ACCT_TYPE_IMAP;

#ifdef USE_SSL
  if (option (OPTIMAPFORCESSL))
    mx->account.flags |= M_ACCT_SSL;
#endif
150

151 152
  scheme = url_check_scheme (NONULL (path));
  if (scheme == U_IMAP || scheme == U_IMAPS)
153
  {
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    if (url_parse_ciss (&url, path) < 0)
      return -1;

    n = mutt_account_fromurl (&mx->account, &url);
    FREE (&url.user);
    FREE (&url.pass);
    FREE (&url.host);

    if (n < 0)
    {
      FREE (&url.path);
      return -1;
    }
      
    mx->mbox = url.path;

    if (scheme == U_IMAPS)
      mx->account.flags |= M_ACCT_SSL;
172
  }
173 174
  /* old PINE-compatibility code */
  else
175
  {
176 177 178 179 180 181 182 183 184
    if (sscanf (path, "{%128[^}]}", tmp) != 1) 
      return -1;

    c = strchr (path, '}');
    if (!c)
      return -1;
    else
      /* walk past closing '}' */
      mx->mbox = safe_strdup (c+1);
185
  
186
    if ((c = strrchr (tmp, '@')))
187
    {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
      *c = '\0';
      strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
      strfcpy (tmp, c+1, sizeof (tmp));
      mx->account.flags |= M_ACCT_USER;
    }
  
    if ((n = sscanf (tmp, "%128[^:/]%128s", mx->account.host, tmp)) < 1)
    {
      dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
      FREE (&mx->mbox);
      return -1;
    }
  
    if (n > 1) {
      if (sscanf (tmp, ":%hd%128s", &(mx->account.port), tmp) >= 1)
	mx->account.flags |= M_ACCT_PORT;
      if (sscanf (tmp, "/%s", tmp) == 1)
205
      {
206 207 208 209 210 211 212 213
	if (!strncmp (tmp, "ssl", 3))
	  mx->account.flags |= M_ACCT_SSL;
	else
	{
	  dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
	  FREE (&mx->mbox);
	  return -1;
	}
214 215
      }
    }
216
  }
217 218 219
  
  if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
    mx->account.port = IMAP_SSL_PORT;
220

221 222 223
  return 0;
}

224

225 226 227
/* imap_qualify_path: make an absolute IMAP folder target, given host, port
 *   and relative path. Use this and maybe it will be easy to convert to
 *   IMAP URLs */
228
void imap_qualify_path (char *dest, size_t len, const IMAP_MBOX *mx,
229 230
  const char* path, const char* name)
{
231 232 233
  char tmp[128];
  
  strcpy (dest, "{");
234
  if ((mx->account.flags & M_ACCT_USER) && (!ImapUser || strcmp (mx->account.user, ImapUser)))
235
  {
236
    snprintf (tmp, sizeof (tmp), "%s@", mx->account.user);
237 238
    strncat (dest, tmp, len);
  }
239 240
  strncat (dest, mx->account.host, len);
  if (mx->account.flags & M_ACCT_PORT)
241
  {
242
    snprintf (tmp, sizeof (tmp), ":%d", mx->account.port);
243 244
    strncat (dest, tmp, len);
  }
245 246 247
  if (mx->account.flags & M_ACCT_SSL)
    strncat (dest, "/ssl", len);

248 249
  snprintf (tmp, sizeof (tmp), "}%s%s", NONULL (path), NONULL (name));
  strncat (dest, tmp, len);
250 251
}

252

253 254
/* imap_quote_string: quote string according to IMAP rules:
 *   surround string with quotes, escape " and \ with \ */
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
void imap_quote_string (char *dest, size_t slen, const char *src)
{
  char quote[] = "\"\\", *pt;
  const char *s;
  size_t len = slen;

  pt = dest;
  s  = src;

  *pt++ = '"';
  /* save room for trailing quote-char */
  len -= 2;
  
  for (; *s && len; s++)
  {
    if (strchr (quote, *s))
    {
      len -= 2;
      if (!len)
	break;
      *pt++ = '\\';
      *pt++ = *s;
    }
    else
    {
      *pt++ = *s;
      len--;
    }
  }
  *pt++ = '"';
  *pt = 0;
}

/* imap_unquote_string: equally stupid unquoting routine */
void imap_unquote_string (char *s)
{
  char *d = s;

  if (*s == '\"')
    s++;
  else
    return;

  while (*s)
  {
    if (*s == '\"')
    {
      *d = '\0';
      return;
    }
    if (*s == '\\')
    {
      s++;
    }
    if (*s)
    {
      *d = *s;
      d++;
      s++;
    }
  }
  *d = '\0';
}
318

319 320 321 322 323 324 325 326 327
/*
 * Quoting and UTF-7 conversion
 */

void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
{
  char *buf;

  buf = safe_strdup (src);
328
  imap_utf7_encode (&buf);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

  imap_quote_string (dest, dlen, buf);

  free (buf);
}

void imap_unmunge_mbox_name (char *s)
{
  char *buf;

  imap_unquote_string(s);

  buf = safe_strdup (s);
  if (buf)
  {
344
    imap_utf7_decode (&buf);
345 346 347 348 349 350
    strncpy (s, buf, strlen (s));
  }
  
  free (buf);
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/* imap_wordcasecmp: find word a in word list b */
int imap_wordcasecmp(const char *a, const char *b)
{
  char tmp[SHORT_STRING];
  char *s = (char *)b;
  int i;

  tmp[SHORT_STRING-1] = 0;
  for(i=0;i < SHORT_STRING-2;i++,s++)
  {
    if (!*s || ISSPACE(*s))
    {
      tmp[i] = 0;
      break;
    }
    tmp[i] = *s;
  }
  tmp[i+1] = 0;

  return mutt_strcasecmp(a, tmp);
}
372

373 374 375 376
/* 
 * Imap keepalive: poll the current folder to keep the
 * connection alive.
 * 
377 378 379 380 381 382 383
 */

static RETSIGTYPE alrm_handler (int sig)
{
  /* empty */
}

384 385 386 387
void imap_keepalive (void)
{
  CONTEXT *ctx = Context;
  
388
  if (ctx == NULL || ctx->magic != M_IMAP || CTX_DATA->ctx != ctx)
389 390 391 392 393
    return;

  imap_check_mailbox (ctx, NULL);
}

394 395 396 397
int imap_wait_keepalive (pid_t pid)
{
  struct sigaction oldalrm;
  struct sigaction act;
398
  sigset_t oldmask;
399 400 401 402 403 404 405
  int rc;

  short imap_passive = option (OPTIMAPPASSIVE);
  
  set_option (OPTIMAPPASSIVE);
  set_option (OPTKEEPQUIET);

406
  sigprocmask (SIG_SETMASK, NULL, &oldmask);
407

408 409 410 411 412 413 414 415 416 417
  sigemptyset (&act.sa_mask);
  act.sa_handler = alrm_handler;
#ifdef SA_INTERRUPT
  act.sa_flags = SA_INTERRUPT;
#else
  act.sa_flags = 0;
#endif

  sigaction (SIGALRM, &act, &oldalrm);

Thomas Roessler's avatar
Thomas Roessler committed
418 419 420
  /* RFC 2060 specifies a minimum of 30 minutes before disconnect when in
   * the AUTHENTICATED state. We'll poll at half that. */
  alarm (900);
421 422
  while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
  {
423
    alarm (0); /* cancel a possibly pending alarm */
424
    imap_keepalive ();
Thomas Roessler's avatar
Thomas Roessler committed
425
    alarm (900);
426 427
  }

428
  alarm (0);	/* cancel a possibly pending alarm */
429
  
430
  sigaction (SIGALRM, &oldalrm, NULL);
431
  sigprocmask (SIG_SETMASK, &oldmask, NULL);
432 433 434 435 436 437 438 439

  unset_option (OPTKEEPQUIET);
  if (!imap_passive)
    unset_option (OPTIMAPPASSIVE);

  return rc;
}

440 441 442 443
/* Allow/disallow re-opening a folder upon expunge. */

void imap_allow_reopen (CONTEXT *ctx)
{
444
  if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
445 446 447 448 449
    CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
}

void imap_disallow_reopen (CONTEXT *ctx)
{
450
  if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
451 452
    CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;
}