1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    GNU Mailutils is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3, or (at your option)
7    any later version.
8 
9    GNU Mailutils is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
16 
17 #include "mail.h"
18 
19 static mu_url_t prev_url;
20 
21 /* Expand mail special characters:
22  * #	    the previous file
23  * &	    the current mbox
24  * +file    the file named in the folder directory (set folder=foo)
25  * %	    system mailbox
26  * %user    system mailbox of the user
27  * @        file given by the -f option
28  */
29 int
mail_expand_name(const char * name,mu_url_t * purl)30 mail_expand_name (const char *name, mu_url_t *purl)
31 {
32   int rc;
33   char *exp = NULL;
34 
35   if (strcmp (name, "#") == 0)
36     {
37       if (!prev_url)
38 	{
39 	  mu_error (_("No previous file"));
40 	  return -1;
41 	}
42       else
43 	{
44 	  rc = mu_url_dup (prev_url, purl);
45 	  if (rc)
46 	    mu_diag_funcall (MU_DIAG_ERROR, "mu_url_dup", exp, rc);
47 	  return rc;
48 	}
49     }
50 
51   if (secondary_url && strcmp (name, "@") == 0)
52     {
53       rc = mu_url_dup (secondary_url, purl);
54       if (rc)
55 	mu_diag_funcall (MU_DIAG_ERROR, "mu_url_dup",
56 			 mu_url_to_string (secondary_url), rc);
57       return rc;
58     }
59 
60   if (strcmp (name, "&") == 0)
61     {
62       name = getenv ("MBOX");
63       if (!name)
64 	{
65 	  mu_error (_("MBOX environment variable not set"));
66 	  return MU_ERR_FAILURE;
67 	}
68       /* else fall through */
69     }
70 
71   rc = mu_mailbox_expand_name (name, &exp);
72 
73   if (rc)
74     mu_error (_("Failed to expand %s: %s"), name, mu_strerror (rc));
75 
76   rc = mu_url_create (purl, exp);
77   if (rc)
78     mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", exp, rc);
79   free (exp);
80 
81   return rc;
82 }
83 
84 /*
85  * fi[le] [file]
86  * fold[er] [file]
87  */
88 
89 int
mail_file(int argc,char ** argv)90 mail_file (int argc, char **argv)
91 {
92   if (argc == 1)
93     {
94       mail_summary (0, NULL);
95     }
96   else if (argc == 2)
97     {
98       /* switch folders */
99       mu_url_t url, tmp_url;
100       mu_mailbox_t newbox = NULL;
101       int status;
102 
103       if (mail_expand_name (argv[1], &url))
104 	return 1;
105 
106       status = mu_mailbox_create_from_url (&newbox, url);
107       if (status)
108 	{
109 	  mu_error(_("Cannot create mailbox %s: %s"),
110 		   mu_url_to_string (url),
111 		   mu_strerror (status));
112 	  mu_url_destroy (&url);
113 	  return 1;
114 	}
115       mu_mailbox_attach_ticket (newbox);
116 
117       if ((status = mu_mailbox_open (newbox, MU_STREAM_RDWR)) != 0)
118 	{
119 	  mu_error(_("Cannot open mailbox %s: %s"),
120 		   mu_url_to_string (url), mu_strerror (status));
121 	  mu_mailbox_destroy (&newbox);
122 	  return 1;
123 	}
124 
125       page_invalidate (1); /* Invalidate current page map */
126 
127       mu_mailbox_get_url (mbox, &url);
128       mu_url_dup (url, &tmp_url);
129 
130       if (mail_mbox_close ())
131 	{
132 	  mu_url_destroy (&tmp_url);
133 	  mu_mailbox_close (newbox);
134 	  mu_mailbox_destroy (&newbox);
135 	  return 1;
136 	}
137 
138       mu_url_destroy (&prev_url);
139       prev_url = tmp_url;
140 
141       mbox = newbox;
142       mu_mailbox_messages_count (mbox, &total);
143       set_cursor (1);
144       if (mailvar_is_true (mailvar_name_header))
145 	{
146 	  util_do_command ("summary");
147 	  util_do_command ("headers");
148 	}
149       return 0;
150     }
151   else
152     {
153       mu_error (_("%s takes only one argument"), argv[0]);
154     }
155   return 1;
156 }
157