Blame view

mh/mh_global.c 5.27 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 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 20 21

/* Global MH state. */

#include <mh.h>

22
static const char *current_folder = NULL;
23
int rcpt_mask = RCPT_DEFAULT;
24
int mh_auto_install = 1;
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
mu_property_t
mh_read_property_file (char *name, int ro)
{
  mu_property_t prop;
  struct mu_mh_prop *mhprop;
  int rc;
  
  mhprop = xzalloc (sizeof (mhprop[0]));
  mhprop->filename = name;
  mhprop->ro = ro;
  rc = mu_property_create_init (&prop, mu_mh_property_init, mhprop);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_property_create_init", name, rc);
      exit (1);
    }
  return prop;
}
44

45 46
static int
prop_merger (const char *field, const char *value, void *data)
47
{
48
  mu_property_t dst = data;
49
  return mu_property_set_value (dst, field, value, 1);
50 51
}

52 53
void
mh_property_merge (mu_property_t dst, mu_property_t src)
54
{
55 56 57 58 59 60 61 62 63 64
  int rc;
  
  if (!src)
    return;
  rc = mu_mhprop_iterate (src, prop_merger, dst);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_mhprop_iterate", NULL, rc);
      exit (1);
    }
65
}
66 67
  
/* Global profile */
68

69 70
void
_mh_init_global_context ()
71
{
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  char *p, *ctx_name;
  
  if (mu_mh_context)
    return;
  p = getenv ("CONTEXT");
  if (!p)
    p = MH_CONTEXT_FILE;
  ctx_name = mh_expand_name (NULL, p, 0);

  mu_mh_context = mh_read_property_file (ctx_name, 0);
  
  if (!current_folder)
    current_folder = mh_global_context_get ("Current-Folder",
					    mh_global_profile_get ("Inbox",
								   "inbox"));
87 88
}

89 90 91 92
void
mh_read_profile ()
{
  char *p;
93
  const char *fallback;
94 95 96 97 98 99 100 101 102
  
  p = getenv ("MH");
  if (p)
    p = mu_tilde_expansion (p, "/", NULL);
  else
    {
      char *home = mu_get_homedir ();
      if (!home)
	abort (); /* shouldn't happen */
103
      p = mh_safe_make_file_name (home, MH_USER_PROFILE);
104 105
      free (home);
    }
106 107 108

  if (mh_auto_install && access (p, R_OK))
    mh_install (p, 1);
109

110
  mu_mh_profile = mh_read_property_file (p, 0);
111

Sergey Poznyakoff authored
112 113
  mu_set_folder_directory (mh_get_dir ());

114
  mh_set_reply_regex (mh_global_profile_get ("Reply-Regex", NULL));
115 116
  fallback = mh_global_profile_get ("Decode-Fallback", NULL);
  if (fallback && mu_set_default_fallback (fallback))
117
    mu_error (_("Incorrect value for decode-fallback"));
118 119 120 121

  _mh_init_global_context ();
}

122
/* Global context */
123

124
const char *
125 126 127 128 129 130
mh_current_folder ()
{
  return mh_global_context_get ("Current-Folder",
				mh_global_profile_get ("Inbox", "inbox"));
}

131 132 133
const char *
mh_set_current_folder (const char *val)
{
134 135 136 137 138 139 140
  int rc = mh_global_context_set ("Current-Folder", val);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mh_global_context_set",
		       "Current-Folder", rc);
      exit (1);
    }
141 142 143
  current_folder = mh_current_folder ();
  return current_folder;
}
144

145
/* Global sequences */
146 147
mu_property_t
mh_mailbox_get_property (mu_mailbox_t mbox)
148
{
149 150 151 152 153 154 155 156
  mu_property_t prop;
  int rc = mu_mailbox_get_property (mbox, &prop);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_mailbox_get_property", NULL, rc);
      exit (1);
    }
  return prop;
157
}
158
  
159
void
160
mh_global_sequences_drop (mu_mailbox_t mbox)
161
{
162 163 164 165 166 167 168
  mu_property_t prop = mh_mailbox_get_property (mbox);
  int rc = mu_property_clear (prop);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_property_clear", NULL, rc);
      exit (1);
    }
169 170
}

171
const char *
172 173 174 175 176 177 178 179 180 181 182 183 184 185
mh_global_sequences_get (mu_mailbox_t mbox, const char *name,
			 const char *defval)
{
  mu_property_t prop = mh_mailbox_get_property (mbox);
  const char *s;
  int rc = mu_property_sget_value (prop, name, &s);
  if (rc == MU_ERR_NOENT)
    s = defval;
  else if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_property_sget_value", name, rc);
      exit (1);
    }
  return s;
186 187
}

188 189 190
void
mh_global_sequences_set (mu_mailbox_t mbox, const char *name,
			 const char *value)
191
{
192 193 194 195 196 197 198
  mu_property_t prop = mh_mailbox_get_property (mbox);
  int rc = mu_property_set_value (prop, name, value, 1);
  if (rc && !(!value && rc == MU_ERR_NOENT))
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_property_set_value", name, rc);
      exit (1);
    }
199 200
}

201 202
void
mh_global_sequences_iterate (mu_mailbox_t mbox,
203
                             mu_mhprop_iterator_t fp, void *data)
204
{
205 206 207 208 209 210 211 212 213 214
  int rc;
  mu_iterator_t itr;
  mu_property_t prop = mh_mailbox_get_property (mbox);

  rc = mu_property_get_iterator (prop, &itr);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_property_get_iterator", NULL, rc);
      exit (1);
    }
215
  mu_mhprop_iterate (prop, fp, data);
216 217
}

218 219 220 221 222
/* Global state */

void
mh_global_save_state ()
{
223 224 225 226 227 228 229 230 231
  int rc;
  
  mh_global_context_set ("Current-Folder", current_folder);
  rc = mu_property_save (mu_mh_context);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_profile_save", "context", rc);
      exit (1);
    }
232
}
233 234