Example #1
0
static char *get_query_string(struct nm_ctxdata *data)
{
    struct uri_tag *item;

    if (!data)
        return NULL;
    if (data->db_query)
        return data->db_query;

    for (item = data->query_items; item; item = item->next) {
        if (!item->value || !item->name)
            continue;

        if (strcmp(item->name, "limit") == 0) {
            if (mutt_atoi(item->value, &data->db_limit))
                mutt_error (_("failed to parse notmuch limit: %s"), item->value);

        } else if (strcmp(item->name, "type") == 0)
            data->query_type = string_to_guery_type(item->value);

        else if (strcmp(item->name, "query") == 0)
            data->db_query = safe_strdup(item->value);
    }

    if (!data->query_type)
        data->query_type = string_to_guery_type(NULL);

    dprint(2, (debugfile, "nm: query '%s'\n", data->db_query));

    return data->db_query;
}
Example #2
0
static char *get_query_string(struct nm_ctxdata *data)
{
	struct uri_tag *item;

	if (!data)
		return NULL;
	if (data->db_query)
		return data->db_query;

	for (item = data->query_items; item; item = item->next) {
		if (!item->value || !item->name)
			continue;

		if (strcmp(item->name, "limit") == 0) {
			if (mutt_atoi(item->value, &data->db_limit))
				mutt_error (_("failed to parse notmuch limit: %s"), item->value);

		} else if (strcmp(item->name, "type") == 0) {
			if (strcmp(item->value, "threads") == 0)
				data->query_type = NM_QUERY_TYPE_THREADS;
			else if (strcmp(item->value, "messages") == 0)
				data->query_type = NM_QUERY_TYPE_MESGS;
			else
				mutt_error (_("failed to parse notmuch query type: %s"), item->value);

		} else if (strcmp(item->name, "query") == 0)
			data->db_query = safe_strdup(item->value);
	}

	dprint(2, (debugfile, "nm: query '%s'\n", data->db_query));

	return data->db_query;
}
Example #3
0
static int
hcache_open_db4 (struct header_cache* h, const char* path)
{
  struct stat sb;
  int ret;
  u_int32_t createflags = DB_CREATE;
  int pagesize;

  if (mutt_atoi (HeaderCachePageSize, &pagesize) < 0 || pagesize <= 0)
    pagesize = 16384;

  snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path);

  h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (h->fd < 0)
    return -1;

  if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5))
    goto fail_close;

  ret = db_env_create (&h->env, 0);
  if (ret)
    goto fail_unlock;

  ret = (*h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE,
	0600);
  if (ret)
    goto fail_env;

  ret = db_create (&h->db, h->env, 0);
  if (ret)
    goto fail_env;

  if (stat(path, &sb) != 0 && errno == ENOENT)
  {
    createflags |= DB_EXCL;
    h->db->set_pagesize(h->db, pagesize);
  }

  ret = (*h->db->open)(h->db, NULL, path, h->folder, DB_BTREE, createflags,
                       0600);
  if (ret)
    goto fail_db;

  return 0;

  fail_db:
  h->db->close (h->db, 0);
  fail_env:
  h->env->close (h->env, 0);
  fail_unlock:
  mx_unlock_file (h->lockfile, h->fd, 0);
  fail_close:
  close (h->fd);
  unlink (h->lockfile);

  return -1;
}
Example #4
0
File: url.c Project: Wuodan/mutt-kz
/* ciss_parse_userhost: fill in components of ciss with info from src. Note
 *   these are pointers into src, which is altered with '\0's. Port of 0
 *   means no port given. */
static int ciss_parse_userhost (ciss_url_t *ciss, char *src)
{
  char *t, *p;

  ciss->user = NULL;
  ciss->pass = NULL;
  ciss->host = NULL;
  ciss->port = 0;

  if (strncmp (src, "//", 2) != 0)
  {
    ciss->path = src;
    return url_pct_decode (ciss->path);
  }

  src += 2;

  if ((ciss->path = strchr (src, '/')))
    *ciss->path++ = '\0';

  if ((t = strrchr (src, '@')))
  {
    *t = '\0';
    if ((p = strchr (src, ':')))
    {
      *p = '\0';
      ciss->pass = p + 1;
      if (url_pct_decode (ciss->pass) < 0)
	return -1;
    }
    ciss->user = src;
    if (url_pct_decode (ciss->user) < 0)
      return -1;
    t++;
  }
  else
    t = src;

  if ((p = strchr (t, ':')))
  {
    int t;
    *p++ = '\0';
    if (mutt_atoi (p, &t) < 0 || t < 0 || t > 0xffff)
      return -1;
    ciss->port = (unsigned short)t;
  }
  else
    ciss->port = 0;

  ciss->host = t;
  return url_pct_decode (ciss->host) >= 0 &&
    (!ciss->path || url_pct_decode (ciss->path) >= 0) ? 0 : -1;
}
Example #5
0
static int smtp_code (char *buf, size_t len, int *n)
{
        char code[4];

        if (len < 4)
                return -1;
        code[0] = buf[0];
        code[1] = buf[1];
        code[2] = buf[2];
        code[3] = 0;
        if (mutt_atoi (code, n) < 0)
                return -1;
        return 0;
}
Example #6
0
static int
hcache_open_gdbm (struct header_cache* h, const char* path)
{
  int pagesize;

  if (mutt_atoi (HeaderCachePageSize, &pagesize) < 0 || pagesize <= 0)
    pagesize = 16384;

  h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL);
  if (h->db)
    return 0;

  /* if rw failed try ro */
  h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL);
  if (h->db)
    return 0;

  return -1;
}
Example #7
0
int mutt_parse_score (BUFFER *buf, BUFFER *s, unsigned long data, BUFFER *err)
{
    SCORE *ptr, *last;
    char *pattern, *pc;
    struct pattern_t *pat;

    mutt_extract_token (buf, s, 0);
    if (!MoreArgs (s))
    {
        strfcpy (err->data, _("score: too few arguments"), err->dsize);
        return (-1);
    }
    pattern = buf->data;
    memset (buf, 0, sizeof (BUFFER));
    mutt_extract_token (buf, s, 0);
    if (MoreArgs (s))
    {
        FREE (&pattern);
        strfcpy (err->data, _("score: too many arguments"), err->dsize);
        return (-1);
    }

    /* look for an existing entry and update the value, else add it to the end
       of the list */
    for (ptr = Score, last = NULL; ptr; last = ptr, ptr = ptr->next)
        if (mutt_strcmp (pattern, ptr->str) == 0)
            break;
    if (!ptr)
    {
        if ((pat = mutt_pattern_comp (pattern, 0, err)) == NULL)
        {
            FREE (&pattern);
            return (-1);
        }
        ptr = safe_calloc (1, sizeof (SCORE));
        if (last)
            last->next = ptr;
        else
            Score = ptr;
        ptr->pat = pat;
        ptr->str = pattern;
    } else
        /* 'buf' arg was cleared and 'pattern' holds the only reference;
         * as here 'ptr' != NULL -> update the value only in which case
         * ptr->str already has the string, so pattern should be freed.
         */
        FREE (&pattern);
    pc = buf->data;
    if (*pc == '=')
    {
        ptr->exact = 1;
        pc++;
    }
    if (mutt_atoi (pc, &ptr->val) < 0)
    {
        FREE (&pattern);
        strfcpy (err->data, _("Error: score: invalid number"), err->dsize);
        return (-1);
    }
    set_option (OPTNEEDRESCORE);
    return 0;
}
Example #8
0
static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
{
  pgp_uid_t *uid = NULL;
  int field = 0, is_uid = 0;
  int is_pub = 0;
  char *pend, *p;
  int trust = 0;
  int flags = 0;
  struct pgp_keyinfo tmp;

  *is_subkey = 0;
  if (!*buf)
    return NULL;

  /* if we're given a key, merge our parsing results, else
   * start with a fresh one to work with so that we don't
   * mess up the real key in case we find parsing errors. */
  if (k)
    memcpy (&tmp, k, sizeof (tmp));
  else
    memset (&tmp, 0, sizeof (tmp));

  dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));

  for (p = buf; p; p = pend)
  {
    if ((pend = strchr (p, ':')))
      *pend++ = 0;
    field++;
    if (!*p && (field != 1) && (field != 10))
      continue;

    switch (field)
    {
      case 1:			/* record type */
      {
	dprint (2, (debugfile, "record type: %s\n", p));

	if (!mutt_strcmp (p, "pub"))
	  is_pub = 1;
	else if (!mutt_strcmp (p, "sub"))
	  *is_subkey = 1;
	else if (!mutt_strcmp (p, "sec"))
	  ;
	else if (!mutt_strcmp (p, "ssb"))
	  *is_subkey = 1;
	else if (!mutt_strcmp (p, "uid"))
	  is_uid = 1;
	else
	  return NULL;

	if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
	  memset (&tmp, 0, sizeof (tmp));

	break;
      }
      case 2:			/* trust info */
      {
	dprint (2, (debugfile, "trust info: %s\n", p));

	switch (*p)
	{				/* look only at the first letter */
	  case 'e':
	    flags |= KEYFLAG_EXPIRED;
	    break;
	  case 'r':
	    flags |= KEYFLAG_REVOKED;
	    break;
	  case 'd':
	    flags |= KEYFLAG_DISABLED;
	    break;
	  case 'n':
	    trust = 1;
	    break;
	  case 'm':
	    trust = 2;
	    break;
	  case 'f':
	    trust = 3;
	    break;
	  case 'u':
	    trust = 3;
	    break;
	}

        if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
	  tmp.flags |= flags;

	break;
      }
      case 3:			/* key length  */
      {
	dprint (2, (debugfile, "key len: %s\n", p));

	if (!(*is_subkey && option (OPTPGPIGNORESUB)) &&
	    mutt_atos (p, &tmp.keylen) < 0)
	  goto bail;
	break;
      }
      case 4:			/* pubkey algo */
      {
	dprint (2, (debugfile, "pubkey algorithm: %s\n", p));

	if (!(*is_subkey && option (OPTPGPIGNORESUB)))
	{
	  int x = 0;
	  if (mutt_atoi (p, &x) < 0)
	    goto bail;
	  tmp.numalg = x;
	  tmp.algorithm = pgp_pkalgbytype (x);
	}
	break;
      }
      case 5:			/* 16 hex digits with the long keyid. */
      {
	dprint (2, (debugfile, "key id: %s\n", p));

	if (!(*is_subkey && option (OPTPGPIGNORESUB)))
	  mutt_str_replace (&tmp.keyid, p);
	break;

      }
      case 6:			/* timestamp (1998-02-28) */
      {
	char tstr[11];
	struct tm time;

	dprint (2, (debugfile, "time stamp: %s\n", p));

	if (!p)
	  break;
	time.tm_sec = 0;
	time.tm_min = 0;
	time.tm_hour = 12;
	strncpy (tstr, p, 11);
	tstr[4] = '\0';
	tstr[7] = '\0';
	if (mutt_atoi (tstr, &time.tm_year) < 0)
	{
	  p = tstr;
	  goto bail;
	}
	time.tm_year -= 1900;
	if (mutt_atoi (tstr+5, &time.tm_mon) < 0)
	{
	  p = tstr+5;
	  goto bail;
	}
	time.tm_mon -= 1;
	if (mutt_atoi (tstr+8, &time.tm_mday) < 0)
	{
	  p = tstr+8;
	  goto bail;
	}
	tmp.gen_time = mutt_mktime (&time, 0);
        break;
      }
      case 7:			/* valid for n days */
        break;
      case 8:			/* Local id         */
        break;
      case 9:			/* ownertrust       */
        break;
      case 10:			/* name             */
      {
        /* Empty field or no trailing colon.
         * We allow an empty field for a pub record type because it is
         * possible for a primary uid record to have an empty User-ID
         * field.  Without any address records, it is not possible to
         * use the key in mutt.
         */
        if (!(pend && (*p || is_pub)))
	  break;

	/* ignore user IDs on subkeys */
	if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
	  break;

	dprint (2, (debugfile, "user ID: %s\n", NONULL (p)));

	uid = safe_calloc (sizeof (pgp_uid_t), 1);
	fix_uid (p);
	uid->addr = safe_strdup (p);
	uid->trust = trust;
	uid->flags |= flags;
	uid->next = tmp.address;
	tmp.address = uid;

	if (strstr (p, "ENCR"))
	  tmp.flags |= KEYFLAG_PREFER_ENCRYPTION;
	if (strstr (p, "SIGN"))
	  tmp.flags |= KEYFLAG_PREFER_SIGNING;

	break;
      }
      case 11:			/* signature class  */
        break;
      case 12:			/* key capabilities */
	dprint (2, (debugfile, "capabilities info: %s\n", p));
	
	while(*p)
	  {
	    switch(*p++)
	      {
	      case 'D':
		flags |= KEYFLAG_DISABLED;
		break;

	      case 'e':
		flags |= KEYFLAG_CANENCRYPT;
		break;

	      case 's':
		flags |= KEYFLAG_CANSIGN;
		break;
	      }
	  }

        if (!is_uid && 
	    (!*is_subkey || !option (OPTPGPIGNORESUB)
	     || !((flags & KEYFLAG_DISABLED)
		  || (flags & KEYFLAG_REVOKED)
		  || (flags & KEYFLAG_EXPIRED))))
	  tmp.flags |= flags;

	break;
      
      default:
        break;
    }
  }

  /* merge temp key back into real key */
  if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
    k = safe_malloc (sizeof (*k));
  memcpy (k, &tmp, sizeof (*k));
  /* fixup parentship of uids after mering the temp key into
   * the real key */
  if (tmp.address)
  {
    for (uid = k->address; uid; uid = uid->next)
      uid->parent = k;
  }

  return k;

bail:
  dprint(5,(debugfile,"parse_pub_line: invalid number: '%s'\n", p));
  return NULL;
}