Blame view

mail/file.c 3.07 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2001, 2002, 2005, 2007, 2010 Free Software
   Foundation, Inc.
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19

#include "mail.h"

20 21
static char *prev_name;

22 23 24 25 26
/* Expand mail special characters:
 * #	    the previous file
 * &	    the current mbox
 * +file    the file named in the folder directory (set folder=foo)
 * Note 1) The followig notations are left intact, since they are
27
 * handled by mu_mailbox_create_default:
28
 * %	    system mailbox
Sergey Poznyakoff authored
29
 * %user    system mailbox of the user 
30 31 32 33 34 35 36 37 38
 * Note 2) Allocates memory
 */
char *
mail_expand_name (const char *name)
{
  switch (name[0])
    {
    case '#':
      if (!prev_name)
39
	{
40
	  mu_error (_("No previous file"));
41 42
	  return NULL;
	}
43 44 45 46 47 48 49
      else
	name = xstrdup (prev_name);
      break;
	  
    case '&':
      name = getenv ("MBOX");
      if (!name)
50
	mu_error (_("MBOX environment variable not set"));
51 52 53 54 55
      else
	name = xstrdup (name);
      break;
	  
    case '+':
56
      name = util_folder_path (name);
57 58 59 60 61 62 63 64 65
      break;

    default:
      name = xstrdup (name);
      break;
    }
  return (char*) name;
}

66 67 68 69 70 71 72 73
/*
 * fi[le] [file]
 * fold[er] [file]
 */

int
mail_file (int argc, char **argv)
{
Jakob Kaivo authored
74 75
  if (argc == 1)
    {
76
      mail_summary (0, NULL);
Jakob Kaivo authored
77 78 79
    }
  else if (argc == 2)
    {
80
      /* switch folders */
81
      char *pname;
82 83
      mu_url_t url;
      mu_mailbox_t newbox = NULL;
84
      char *name = mail_expand_name (argv[1]);
Sergey Poznyakoff authored
85
      int status;
86 87 88

      if (!name)
	return 1;
89
      
90 91
      if ((status = mu_mailbox_create_default (&newbox, name)) != 0 
	  || (status = mu_mailbox_open (newbox, MU_STREAM_RDWR)) != 0)
92
	{
93
	  mu_mailbox_destroy (&newbox);
94
	  mu_error(_("Cannot open mailbox %s: %s"), name, mu_strerror (status));
95
	  free (name);
96 97
	  return 1;
	}
98

99
      free (name); /* won't need it any more */
100 101
      page_invalidate (1); /* Invalidate current page map */
      
102
      mu_mailbox_get_url (mbox, &url);
103
      pname = strdup (mu_url_to_string (url));
104 105
      if (mail_mbox_close ())
	{
106 107
	  if (pname)
	    free (pname);
108 109
	  mu_mailbox_close (newbox);
	  mu_mailbox_destroy (&newbox);
110 111 112
	  return 1;
	}
      
113 114 115 116
      if (prev_name)
	free (prev_name);
      prev_name = pname;
      
Jakob Kaivo authored
117
      mbox = newbox;
118
      mu_mailbox_messages_count (mbox, &total);
119
      set_cursor (1);
120
      if (mailvar_get (NULL, "header", mailvar_type_boolean, 0) == 0)
121 122
	{
	  util_do_command ("summary");
Sergey Poznyakoff authored
123
	  util_do_command ("headers");
124
	}
Jakob Kaivo authored
125 126
      return 0;
    }
127 128
  else
    {
129
      mu_error (_("%s takes only one argument"), argv[0]);
130
    }
131 132
  return 1;
}
133