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 #include <mailutils/folder.h>
19 
20 static int
list_response_printer(void * item,void * data)21 list_response_printer (void *item, void *data)
22 {
23   struct mu_list_response *resp = item;
24 
25   mu_printf ("%s", resp->name);
26   if (resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY)
27     mu_printf ("%c", resp->separator);
28   mu_printf ("\n");
29   return 0;
30 }
31 
32 static int
show_folders(mu_folder_t folder)33 show_folders (mu_folder_t folder)
34 {
35   mu_list_t list;
36 
37   if (mu_folder_is_local (folder))
38     {
39       char *lister = getenv ("LISTER");
40       if (lister && *lister)
41 	{
42 	  char const *path;
43 	  mu_url_t url;
44 	  int rc;
45 
46 	  mu_folder_get_url (folder, &url);
47 	  if ((rc = mu_url_sget_path (url, &path)) == 0)
48 	    {
49 	      util_do_command("! %s '%s'", getenv ("LISTER"), path);
50 	      return 0;
51 	    }
52 	  else
53 	    {
54 	      mu_diag_funcall (MU_DIAG_ERROR, "mu_url_sget_path", NULL, rc);
55 	      /* Retry using standard folder lister */
56 	    }
57 	}
58     }
59 
60   if (mu_folder_list (folder, "", "%", 1, &list) == 0)
61     {
62       mu_list_foreach (list, list_response_printer, NULL);
63       mu_list_destroy (&list);
64     }
65 
66   return 0;
67 }
68 
69 int
mail_folders(int argc MU_ARG_UNUSED,char ** argv MU_ARG_UNUSED)70 mail_folders (int argc MU_ARG_UNUSED, char **argv MU_ARG_UNUSED)
71 {
72   int rc;
73   mu_folder_t folder;
74   mu_url_t url;
75   char *folder_path, *temp_folder_path = NULL;
76 
77   if (mailvar_get (&folder_path, mailvar_name_folder, mailvar_type_string, 1))
78     return 1;
79 
80   if (!mu_is_proto (folder_path) && folder_path[0] != '/' &&
81       folder_path[0] != '~')
82     {
83       char *tmp = mu_alloc (strlen (folder_path) + 3);
84       tmp[0] = '~';
85       tmp[1] = '/';
86       strcpy (tmp + 2, folder_path);
87       temp_folder_path = util_fullpath (tmp);
88       folder_path = temp_folder_path;
89       free (tmp);
90     }
91 
92   rc = mu_url_create (&url, folder_path);
93   if (rc == 0)
94     {
95       rc = util_get_folder (&folder, url, any_folder);
96       if (rc == 0)
97 	{
98 	  url = NULL; /* Prevent double free: folder steals url */
99 	  rc = show_folders (folder);
100 	  mu_folder_destroy (&folder);
101 	}
102       else
103 	{
104 	  mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_folder",
105 			   folder_path, rc);
106 	}
107     }
108   else
109     {
110       mu_diag_funcall (MU_DIAG_ERROR, "mu_url_create", folder_path, rc);
111     }
112 
113   free (temp_folder_path);
114   mu_url_destroy (&url);
115   return rc;
116 }
117 
118