attribute.c 9.62 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2009, 2010, 2011
   Free Software Foundation, Inc.

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <mailutils/errno.h>
#include <mailutils/util.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/attribute.h>

int
mu_attribute_create (mu_attribute_t *pattr, void *owner)
{
  mu_attribute_t attr;
  if (pattr == NULL)
    return MU_ERR_OUT_PTR_NULL;
  attr = calloc (1, sizeof(*attr));
  if (attr == NULL)
    return ENOMEM;
  attr->owner = owner;
  *pattr = attr;
  return 0;
}

void
mu_attribute_destroy (mu_attribute_t *pattr, void *owner)
{
  if (pattr && *pattr)
    {
      mu_attribute_t attr = *pattr;
      if (attr->owner == owner)
	free (*pattr);
      /* Loose the link */
      *pattr = NULL;
    }
}

void *
mu_attribute_get_owner (mu_attribute_t attr)
{
  return (attr) ? attr->owner : NULL;
}

int
mu_attribute_is_modified (mu_attribute_t attr)
{
  return (attr) ? attr->flags & MU_ATTRIBUTE_MODIFIED : 0;
}

int
mu_attribute_clear_modified (mu_attribute_t attr)
{
  if (attr)
    attr->flags &= ~MU_ATTRIBUTE_MODIFIED;
  return 0;
}

int
mu_attribute_set_modified (mu_attribute_t attr)
{
  if (attr)
    attr->flags |= MU_ATTRIBUTE_MODIFIED;
  return 0;
}

int
mu_attribute_get_flags (mu_attribute_t attr, int *pflags)
{
  if (attr == NULL)
    return EINVAL;
  if (pflags == NULL)
    return MU_ERR_OUT_PTR_NULL;
  if (attr->_get_flags)
    return attr->_get_flags (attr, pflags);
  *pflags = attr->flags;
  return 0;
}

int
mu_attribute_set_flags (mu_attribute_t attr, int flags)
{
  int status = 0;
  int oflags = 0;
  
  if (attr == NULL)
    return EINVAL;

  /* If the required bits are already set, do not modify anything */
  mu_attribute_get_flags (attr, &oflags);
  if ((oflags & flags) == flags)
    return 0;
  
  if (attr->_set_flags)
    status = attr->_set_flags (attr, flags);
  else
    attr->flags |= flags;
  if (status == 0)
    mu_attribute_set_modified (attr);
  return 0;
}

int
mu_attribute_unset_flags (mu_attribute_t attr, int flags)
{
  int status = 0;
  int oflags = 0;

  if (attr == NULL)
    return EINVAL;

  /* If the required bits are already cleared, do not modify anything */
  mu_attribute_get_flags (attr, &oflags);
  if ((oflags & flags) == 0)
    return 0;

  if (attr->_unset_flags)
    status = attr->_unset_flags (attr, flags);
  else
    attr->flags &= ~flags;
  if (status == 0)
    mu_attribute_set_modified (attr);
  return 0;
}

int
mu_attribute_set_get_flags (mu_attribute_t attr, int (*_get_flags)
			 (mu_attribute_t, int *), void *owner)
{
  if (attr == NULL)
    return EINVAL;
  if (attr->owner != owner)
    return EACCES;
  attr->_get_flags = _get_flags;
  return 0;
}

int
mu_attribute_set_set_flags (mu_attribute_t attr, int (*_set_flags)
			 (mu_attribute_t, int), void *owner)
{
  if (attr == NULL)
    return EINVAL;
  if (attr->owner != owner)
    return EACCES;
  attr->_set_flags = _set_flags;
  return 0;
}

int
mu_attribute_set_unset_flags (mu_attribute_t attr, int (*_unset_flags)
			 (mu_attribute_t, int), void *owner)
{
  if (attr == NULL)
    return EINVAL;
  if (attr->owner != owner)
    return EACCES;
  attr->_unset_flags = _unset_flags;
  return 0;
}

/* We add support for "USER" flag, it is a way for external objects
   Not being the owner to add custom flags.  */
int
mu_attribute_set_userflag (mu_attribute_t attr, int flag)
{
  if (attr == NULL)
    return EINVAL;
  attr->user_flags |= flag;
  return 0;
}

int
mu_attribute_set_seen (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_SEEN);
}

int
mu_attribute_set_answered (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_ANSWERED);
}

int
mu_attribute_set_flagged (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_FLAGGED);
}

int
mu_attribute_set_read (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_READ);
}

int
mu_attribute_set_deleted (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DELETED);
}

int
mu_attribute_set_draft (mu_attribute_t attr)
{
  return mu_attribute_set_flags (attr, MU_ATTRIBUTE_DRAFT);
}

int
mu_attribute_set_recent (mu_attribute_t attr)
{
  int status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
  if (status == 0)
    status = mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
  return status;
}

int
mu_attribute_is_userflag (mu_attribute_t attr, int flag)
{
  if (attr == NULL)
    return 0;
  return attr->user_flags & flag;
}

int
mu_attribute_is_seen (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_SEEN;
  return 0;
}

int
mu_attribute_is_answered (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_ANSWERED;
  return 0;
}

int
mu_attribute_is_flagged (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_FLAGGED;
  return 0;
}

int
mu_attribute_is_read (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_READ;
  return 0;
}

int
mu_attribute_is_deleted (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_DELETED;
  return 0;
}

int
mu_attribute_is_draft (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return flags & MU_ATTRIBUTE_DRAFT;
  return 0;
}

int
mu_attribute_is_recent (mu_attribute_t attr)
{
  int flags = 0;
  if (mu_attribute_get_flags (attr, &flags) == 0)
    return MU_ATTRIBUTE_IS_UNSEEN(flags);
  return 0;
}

int
mu_attribute_unset_userflag (mu_attribute_t attr, int flag)
{
  if (attr == NULL)
    return 0;
  attr->user_flags &= ~flag;
  return 0;
}

int
mu_attribute_unset_seen (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
}

int
mu_attribute_unset_answered (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_ANSWERED);
}

int
mu_attribute_unset_flagged (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_FLAGGED);
}

int
mu_attribute_unset_read (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_READ);
}

int
mu_attribute_unset_deleted (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DELETED);
}

int
mu_attribute_unset_draft (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_DRAFT);
}

int
mu_attribute_unset_recent (mu_attribute_t attr)
{
  return mu_attribute_unset_flags (attr, MU_ATTRIBUTE_SEEN);
}

int
mu_attribute_is_equal (mu_attribute_t attr, mu_attribute_t attr2)
{
  int flags2 = 0, flags = 0;
  mu_attribute_get_flags (attr, &flags);
  mu_attribute_get_flags (attr2, &flags2);
  return flags == flags;
}

/*   Miscellaneous.  */
int
mu_attribute_copy (mu_attribute_t dest, mu_attribute_t src)
{
  if (dest == NULL || src == NULL)
    return EINVAL;
  /* Can not be a deep copy.  */
  /* memcpy (dest, src, sizeof (*dest)); */
  dest->flags = src->flags;
  return 0;
}

struct flagtrans
{
  int flag;
  char letter;
};

/* The two macros below are taken from gnulib module verify.h */
#define mu_verify_true(R) \
  (!!sizeof \
   (struct { unsigned int verify_error_if_negative_size__: (R) ? 1 : -1; }))
#define mu_verify(R) extern int (* verify_function__ (void)) [mu_verify_true (R)]

static struct flagtrans flagtrans[] = {
  { MU_ATTRIBUTE_SEEN, 'O' },
  { MU_ATTRIBUTE_ANSWERED, 'A' },
  { MU_ATTRIBUTE_FLAGGED, 'F' },
  { MU_ATTRIBUTE_READ, 'R' },
  { MU_ATTRIBUTE_DELETED, 'd' },
  { 0 }
};

/* If cc reports an error in this statement, fix the MU_STATUS_BUF_SIZE
   declaration in include/mailutils/attribute.h */
mu_verify (MU_ARRAY_SIZE (flagtrans) == MU_STATUS_BUF_SIZE);

int
mu_string_to_flags (const char *buffer, int *pflags)
{
  const char *sep;

  if (pflags == NULL)
    return EINVAL;

  /* Set the attribute */
  if (mu_c_strncasecmp (buffer, "Status:", 7) == 0)
    {
      sep = strchr(buffer, ':'); /* pass the ':' */
      sep++;
    }
  else
    sep = buffer;

  for (; *sep; sep++)
    {
      struct flagtrans *ft;

      for (ft = flagtrans; ft->flag; ft++)
	if (ft->letter == *sep)
	  {
	    *pflags |= ft->flag;
	    break;
	  }
    }
  return 0;
}

/* NOTE: When adding/removing flags, make sure to update the
   MU_STATUS_BUF_SIZE define in include/mailutils/attribute.h */
int
mu_attribute_to_string (mu_attribute_t attr, char *buffer, size_t len,
			size_t *pn)
{
  int flags = 0;
  char buf[MU_STATUS_BUF_SIZE];
  int i;
  int rc;
  struct flagtrans *ft;
  
  rc = mu_attribute_get_flags (attr, &flags);
  if (rc)
    return rc;

  i = 0;
  for (ft = flagtrans; ft->flag; ft++)
    if (ft->flag & flags)
      buf[i++] = ft->letter;
  buf[i++] = 0;

  i = mu_cpystr (buffer, buf, i);
  if (pn)
    *pn = i;
  return 0;
}