Exemple #1
0
static int
msgset_cur (mu_mailbox_t mbox, size_t *pnum)
{
  size_t i, count = 0;
  static int cached_n = 0;

  if (cached_n)
    {
      *pnum = cached_n;
      return 0;
    }
  
  mu_mailbox_messages_count (mbox, &count);
  for (i = 1; i <= count; i++)
    {
      mu_message_t msg = NULL;
      size_t uid = 0;
      
      mu_mailbox_get_message (mbox, i, &msg);
      mh_message_number (msg, &uid);
      if (uid == current_message)
	{
	  *pnum = cached_n = i;
	  return 0;
	}
    }
  mu_error (_("no cur message"));
  exit (1);
}
Exemple #2
0
size_t
mh_get_message (mu_mailbox_t mbox, size_t seqno, mu_message_t *mesg)
{
  size_t num, count;
  mu_message_t msg;

  if (mu_mailbox_get_message (mbox, 1, &msg)
      || mh_message_number (msg, &num))
    return 0;
  if (seqno < num)
    return 0;
  else if (seqno == num)
    {
      if (mesg)
	*mesg = msg;
      return 1;
    }

  if (mu_mailbox_messages_count (mbox, &count)
      || mu_mailbox_get_message (mbox, count, &msg)
      || mh_message_number (msg, &num))
    return 0;
  if (seqno > num)
    return 0;
  else if (seqno == num)
    {
      if (mesg)
	*mesg = msg;
      return count;
    }

  return mh_search_message (mbox, 1, count, seqno, mesg);
}
Exemple #3
0
/* Negate the message set: on return `msgset' consists of the messages
   _not contained_ in the input message set. Any memory associated with
   the input message set is freed */
void
mh_msgset_negate (mu_mailbox_t mbox, mh_msgset_t *msgset)
{
  size_t i, total = 0, msgno;
  size_t *list;

  mu_mailbox_messages_count (mbox, &total);
  list = calloc (total, sizeof (list[0]));
  if (!list)
    mh_err_memory (1);
  for (i = 1, msgno = 0; i <= total; i++)
    {
      if (!mh_msgset_member (msgset, i))
	list[msgno++] = i;
    }

  list = realloc (list, sizeof (list[0]) * msgno);
  if (!list)
    {
      mu_error (_("not enough memory"));
      abort ();
    }
  mh_msgset_free (msgset);
  msgset->count = msgno;
  msgset->list = list;
}
Exemple #4
0
static int
mbx_finished_p (void *owner)
{
  struct mailbox_iterator *itr = owner;
  size_t count;

  if (mu_mailbox_messages_count (itr->mbx, &count))
    return 1;
  return itr->idx > count;
}
Exemple #5
0
int
mail_tmp_finish (struct mail_tmp *mtmp, mu_mailbox_t *mbox)
{
  int status;
  static char *newline = "\n";
  size_t n;
  
  if (!mtmp->had_nl)
    status = mu_stream_sequential_write (mtmp->stream, newline, 1);

  status = mu_stream_sequential_write (mtmp->stream, newline, 1);
  unlink (mtmp->tempfile);
  free (mtmp->tempfile);
  mtmp->tempfile = NULL;
  
  if (status)
    {
      errno = status;
      maidag_error (_("error writing temporary file: %s"), 
                    mu_strerror (status));
      mu_stream_destroy (&mtmp->stream, mu_stream_get_owner (mtmp->stream));
      return status;
    }

  mu_stream_flush (mtmp->stream);
  if ((status = mu_mailbox_create (mbox, "mbox:/dev/null")) 
      || (status = mu_mailbox_open (*mbox, MU_STREAM_READ))
      || (status = mu_mailbox_set_stream (*mbox, mtmp->stream)))
    {
      maidag_error (_("error opening temporary file: %s"), 
                    mu_strerror (status));
      mu_stream_destroy (&mtmp->stream, mu_stream_get_owner (mtmp->stream));
      return status;
    }

  status = mu_mailbox_messages_count (*mbox, &n);
  if (status)
    {
      errno = status;
      maidag_error (_("error creating temporary message: %s"),
		    mu_strerror (status));
      mu_stream_destroy (&mtmp->stream, mu_stream_get_owner (mtmp->stream));
      return status;
    }

  mtmp->stream = NULL;
  mtmp->line = 0;
  
  return status;
  
}
Exemple #6
0
static int
msgset_next (mu_mailbox_t mbox, size_t *pnum)
{
  size_t cur_n = 0, total = 0;
  msgset_cur (mbox, &cur_n);
  mu_mailbox_messages_count (mbox, &total);
  if (cur_n + 1 > total)
    {
      mu_error (_("no next message"));
      exit (1);
    }
  *pnum = cur_n + 1;
  return 0;
}
Exemple #7
0
static int
msgset_last (mu_mailbox_t mbox, size_t *pnum)
{
  int rc;
  size_t count = 0;

  rc = mu_mailbox_messages_count (mbox, &count);
  if (rc)
    {
      mu_error (_("cannot get last message: %s"), mu_strerror (rc));
      exit (1);
    }
  *pnum = count;
  return 0;
}
Exemple #8
0
static int
mbx_getitem (void *owner, void **pret, const void **pkey)
{
  struct mailbox_iterator *itr = owner;
  size_t count;
  int rc;
  
  rc = mu_mailbox_messages_count (itr->mbx, &count);
  if (rc)
    return rc;
  if (itr->idx > count)
    return MU_ERR_NOENT;
  rc = mu_mailbox_get_message (itr->mbx, itr->idx, (mu_message_t*)pret);
  if (rc == 0 && pkey)
    *pkey = NULL; /* FIXME: Perhaps return UIDL or other unique id? */
  return rc;
}
Exemple #9
0
int
pop3d_uidl (char *arg, struct pop3d_session *sess)
{
  size_t mesgno;
  char uidl[128];
  mu_message_t msg;
  mu_attribute_t attr;

  if (state != TRANSACTION)
    return ERR_WRONG_STATE;

  if (strchr (arg, ' ') != NULL)
    return ERR_BAD_ARGS;

  if (strlen (arg) == 0)
    {
      size_t total = 0;
      pop3d_outf ("+OK\n");
      mu_mailbox_messages_count (mbox, &total);
      for (mesgno = 1; mesgno <= total; mesgno++)
        {
          mu_mailbox_get_message (mbox, mesgno, &msg);
          mu_message_get_attribute (msg, &attr);
          if (!pop3d_is_deleted (attr))
            {
              mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL);
              pop3d_outf ("%s %s\n", mu_umaxtostr (0, mesgno), uidl);
            }
        }
      pop3d_outf (".\n");
    }
  else
    {
      mesgno = strtoul (arg, NULL, 10);
      if (mu_mailbox_get_message (mbox, mesgno, &msg) != 0)
        return ERR_NO_MESG;
      mu_message_get_attribute (msg, &attr);
      if (pop3d_is_deleted (attr))
        return ERR_MESG_DELE;
      mu_message_get_uidl (msg, uidl, sizeof (uidl), NULL);
      pop3d_outf ("+OK %s %s\n", mu_umaxtostr (0, mesgno), uidl);
    }

  return OK;
}
Exemple #10
0
/* Negate the message set: on return PNSET consists of the messages
   _not contained_ in the input message set. */
int
mu_msgset_negate (mu_msgset_t msgset, mu_msgset_t *pnset)
{
  int rc;
  struct invert_closure clos;
  size_t total;
  
  if (!msgset)
    return EINVAL;
  if (!msgset->mbox)
    return MU_ERR_NOT_OPEN;

  rc = mu_msgset_aggregate (msgset);
  if (rc)
    return rc;
  rc = mu_mailbox_messages_count (msgset->mbox, &total);
  if (rc)
    return rc;
  if (msgset->flags == MU_MSGSET_UID)
    {
      rc = mu_mailbox_translate (msgset->mbox,
				 MU_MAILBOX_MSGNO_TO_UID,
				 total, &total);
      if (rc)
	return rc;
    }
  rc = mu_msgset_create (&clos.nset, msgset->mbox, msgset->flags);
  if (rc)
    return rc;
  clos.next_num = 1;
  rc = mu_list_foreach (msgset->list, _invert_range, &clos);
  if (rc == 0)
    {
      if (clos.next_num < total)
	rc = mu_msgset_add_range (clos.nset, clos.next_num, total,
				  clos.nset->flags);
    }
  
  if (rc)
    mu_msgset_free (clos.nset);
  else
    *pnset = clos.nset;

  return rc;
}
Exemple #11
0
int
pop3d_stat (char *arg)
{
  size_t mesgno;
  size_t size = 0;
  size_t lines = 0;
  size_t total = 0;
  size_t num = 0;
  size_t tsize = 0;
  mu_message_t msg = NULL;
  mu_attribute_t attr = NULL;

  if (strlen (arg) != 0)
    return ERR_BAD_ARGS;

  if (state != TRANSACTION)
    return ERR_WRONG_STATE;

  /* rfc1939: if the POP3 server host internally represents end-of-line as a
     single character, then the POP3 server simply counts each occurrence of
     this character in a message as two octets. */
  mu_mailbox_messages_count (mbox, &total);
  for (mesgno = 1; mesgno <= total; mesgno++)
    {
      mu_mailbox_get_message (mbox, mesgno, &msg);
      mu_message_get_attribute (msg, &attr);
      /* rfc1939: Note that messages marked as deleted are not counted in
	 either total.  */
      if (!pop3d_is_deleted (attr))
	{
	  mu_message_size (msg, &size);
	  mu_message_lines (msg, &lines);
	  tsize += size + lines;
	  num++;
	}
    }
  pop3d_outf ("+OK %s %s\r\n", mu_umaxtostr (0, num), mu_umaxtostr (1, tsize));

  return OK;
}
Exemple #12
0
/* The code is shared between select and noop */
int
imap4d_select_status ()
{
  const char *mflags = "\\Answered \\Flagged \\Deleted \\Seen \\Draft";
  unsigned long uidvalidity = 0;
  size_t count = 0, recent = 0, unseen = 0, uidnext = 0;
  int status = 0;

  if (state != STATE_SEL)
    return 0; /* FIXME: this should be something! */

  if ((status = util_uidvalidity (mbox, &uidvalidity))
      || (status = mu_mailbox_uidnext (mbox, &uidnext))
      || (status = mu_mailbox_messages_count (mbox, &count))
      || (status = mu_mailbox_messages_recent (mbox, &recent))
      || (status = mu_mailbox_message_unseen (mbox, &unseen)))
    return status;

  /* This outputs EXISTS and RECENT responses */
  imap4d_sync();
  io_untagged_response (RESP_OK, "[UIDVALIDITY %lu] UID valididy status", 
                           uidvalidity);
  io_untagged_response (RESP_OK, "[UIDNEXT %lu] Predicted next uid",
	                   (unsigned long) uidnext);
  if (unseen)
    io_untagged_response (RESP_OK, "[UNSEEN %lu] first unseen message",
	                     (unsigned long) unseen);
  io_untagged_response (RESP_NONE, "FLAGS (%s)", mflags);
  /* FIXME:
     - '\*' can be supported if we use the attribute_set userflag()
     - Answered is still not set in the mailbox code.  */
  if (!(select_flags & MU_STREAM_WRITE))
    io_untagged_response (RESP_OK, "[PERMANENTFLAGS ()] No permanent flags");
  else
    io_untagged_response (RESP_OK, "[PERMANENTFLAGS (%s)] Permanent flags",
                          mflags);

  return 0;
}
static int
mu_scm_mailbox_print (SCM mailbox_smob, SCM port, scm_print_state * pstate)
{
  struct mu_mailbox *mum = (struct mu_mailbox *) SCM_CDR (mailbox_smob);
  size_t count = 0;
  mu_url_t url = NULL;

  mu_mailbox_messages_count (mum->mbox, &count);
  mu_mailbox_get_url (mum->mbox, &url);

  scm_puts ("#<mailbox ", port);

  if (mailbox_smob == SCM_BOOL_F)
    {
      /* mu_mailbox.* functions may return #f */
      scm_puts ("#f", port);
    }
  else
    {
      const char *p = mu_url_to_string (url);
      if (p)
	{
	  char buf[64];
	  
	  scm_puts (p, port);
	  
	  snprintf (buf, sizeof (buf), " (%d)", count);
	  scm_puts (buf, port);
	}
      else
	scm_puts ("uninitialized", port);
    }
  scm_puts (">", port);

  return 1;
}
Exemple #14
0
int
mail_file (int argc, char **argv)
{
  if (argc == 1)
    {
      mail_summary (0, NULL);
    }
  else if (argc == 2)
    {
      /* switch folders */
      char *pname;
      mu_url_t url;
      mu_mailbox_t newbox = NULL;
      char *name = mail_expand_name (argv[1]);
      int status;

      if (!name)
	return 1;
      
      if ((status = mu_mailbox_create_default (&newbox, name)) != 0 
	  || (status = mu_mailbox_open (newbox, MU_STREAM_RDWR)) != 0)
	{
	  mu_mailbox_destroy (&newbox);
	  util_error(_("Cannot open mailbox %s: %s"), name, mu_strerror (status));
	  free (name);
	  return 1;
	}

      free (name); /* won't need it any more */
      page_invalidate (1); /* Invalidate current page map */
      
      mu_mailbox_get_url (mbox, &url);
      pname = strdup (mu_url_to_string (url));
      if (mail_mbox_close ())
	{
	  if (pname)
	    free (pname);
	  mu_mailbox_close (newbox);
	  mu_mailbox_destroy (&newbox);
	  return 1;
	}
      
      if (prev_name)
	free (prev_name);
      prev_name = pname;
      
      mbox = newbox;
      mu_mailbox_messages_count (mbox, &total);
      set_cursor (1);
      if (mailvar_get (NULL, "header", mailvar_type_boolean, 0) == 0)
	{
	  util_do_command ("summary");
	  util_do_command ("headers");
	}
      return 0;
    }
  else
    {
      util_error(_("%s takes only one argument"), argv[0]);
    }
  return 1;
}
Exemple #15
0
int
main (int argc, char **argv)
{
  mu_mailbox_t mbox = NULL;
  size_t i;
  size_t count = 0;
  char *mailbox_name;
  int debug = 0;

  for (i = 1; i < argc; i++)
    {
      if (strcmp (argv[i], "-d") == 0)
        debug = 1;
      else if (strcmp (argv[i], "-p") == 0)
        print_attachments = 1;
      else if (strcmp (argv[i], "-i") == 0)
	{
	  if (++i == argc)
	    {
	      mu_error ("-i requires argument");
	      exit (1);
	    }
	  indent_level = strtoul (argv[i], NULL, 0);
	}
      else if (strcmp (argv[i], "-c") == 0)
	{
	  if (++i == argc)
	    {
	      mu_error ("-c requires argument");
	      exit (1);
	    }
	  charset = argv[i];
	}
      else
        break;
    }

  mailbox_name = argv[i];

  /* Registration.  */
  mu_registrar_record (mu_imap_record);
  mu_registrar_record (mu_pop_record);
  mu_registrar_record (mu_mbox_record);
  mu_registrar_set_default_record (mu_mbox_record);

  MU_ASSERT (mu_mailbox_create_default (&mbox, mailbox_name));
  
  /* Debugging trace. */
  if (debug)
    {
      mu_debug_set_category_level (MU_DEBCAT_MAILBOX, 
                                   MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT));
    }

  /* Open the mailbox for reading only.  */
  MU_ASSERT (mu_mailbox_open (mbox, MU_STREAM_READ));

  /* Iterate through the entire message set.  */
  MU_ASSERT (mu_mailbox_messages_count (mbox, &count));

  for (i = 1; i <= count; ++i)
    {
      mu_message_t msg;
      mu_header_t hdr;
      size_t nparts;
      size_t msize, nlines;

      MU_ASSERT (mu_mailbox_get_message (mbox, i, &msg));
      MU_ASSERT (mu_message_size (msg, &msize));
      MU_ASSERT (mu_message_lines (msg, &nlines));
      MU_ASSERT (mu_message_get_header (msg, &hdr));
      if (mu_header_sget_value (hdr, MU_HEADER_FROM, &from))
	from = "";
      if (mu_header_sget_value (hdr, MU_HEADER_SUBJECT, &subject))
	subject = "";
      printf ("Message: %lu\n", (unsigned long) i);
      printf ("From: %s\n", from);
      printf ("Subject: %s\n", subject);

      MU_ASSERT (mu_message_get_num_parts (msg, &nparts));
      printf ("Number of parts in message - %lu\n",
	      (unsigned long) nparts);
      printf ("Total message size - %lu/%lu\n",
	      (unsigned long) msize, (unsigned long) nlines);
      message_display_parts (msg, 0);
    }
  mu_mailbox_close (mbox);
  mu_mailbox_destroy (&mbox);
  return 0;
}
Exemple #16
0
int
main (int argc, const char **argv)
{
  char *from;
  char *subject;
  mu_mailbox_t mbox;
  size_t msgno, total = 0;
  int status;

  /* Register the formats. */
  mu_register_all_mbox_formats ();

  status = mu_mailbox_create_default (&mbox, argv[1]);
  if (status != 0)
    {
      mu_error ("mu_mailbox_create: %s", mu_strerror (status));
      exit (EXIT_FAILURE);
    }

  status = mu_mailbox_open (mbox, MU_STREAM_READ);
  if (status != 0)
    {
      mu_error ("mu_mailbox_open: %s", mu_strerror (status));
      exit (EXIT_FAILURE);
    }

  mu_mailbox_messages_count (mbox, &total);

  for (msgno = 1; msgno <= total; msgno++)
    {
      mu_message_t msg;
      mu_header_t hdr;

      if ((status = mu_mailbox_get_message (mbox, msgno, &msg)) != 0
          || (status = mu_message_get_header (msg, &hdr)) != 0)
        {
          mu_error ("Error message: %s", mu_strerror (status));
          exit (EXIT_FAILURE);
        }

      if (mu_header_aget_value (hdr, MU_HEADER_FROM, &from))
        from = strdup ("(NO FROM)");

      if (mu_header_aget_value (hdr, MU_HEADER_SUBJECT, &subject))
        subject = strdup ("(NO SUBJECT)");

      printf ("%s\t%s\n", from, subject);
      free (from);
      free (subject);
    }

  status = mu_mailbox_close (mbox);
  if (status != 0)
    {
      mu_error ("mu_mailbox_close: %s", mu_strerror (status));
      exit (EXIT_FAILURE);
    }

  mu_mailbox_destroy (&mbox);
  return 0;
}