Blame view

mailbox/address.c 10.8 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 1999, 2000, 2001, 2005, 2006,
   2007 Free Software Foundation, Inc.
4

5 6 7
   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
8
   version 3 of the License, or (at your option) any later version.
9

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

15 16 17 18
   You should have received a copy of the GNU Lesser General
   Public License along with this library; if not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301 USA */
19 20 21 22 23

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

24
#include <assert.h>
25 26
#include <ctype.h>
#include <errno.h>
27 28 29 30 31 32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>

#include <sys/types.h>
33

34
#include <mailutils/errno.h>
35
#include <mailutils/mutil.h>
36
#include <mailutils/parse822.h>
37

38
#include <address0.h>
39

40
/* Get email addresses from rfc822 address.  */
41
int
42
mu_address_create (mu_address_t * a, const char *s)
43
{
44
  /* 'a' must exist, and can't already have been initialized
45 46
   */
  int status;
47

48
  if (!a)
49 50
    return MU_ERR_OUT_PTR_NULL;

51
  if (!s)
52
    return EINVAL;
53 54

  *a = NULL;
55
  status = mu_parse822_address_list (a, s);
56
  if (status == 0)
57
    {
Alain Magloire authored
58
      /* And address-list may contain 0 addresses but parse correctly.
59 60
       */
      if (!*a)
61
	return MU_ERR_EMPTY_ADDRESS;
62

63
      (*a)->addr = strdup (s);
64
      if (!(*a)->addr)
65
	{
66
	  mu_address_destroy (a);
67
	  return ENOMEM;
68
	}
69
    }
70 71 72
  return status;
}

73 74
/* Get email addresses from array of rfc822 addresses. */
int
75
mu_address_createv (mu_address_t * a, const char *sv[], size_t len)
76 77 78
{
  int status = 0;
  size_t buflen = 0;
79
  char *buf = 0;
80
  size_t i;
81

82
  if (!a)
83 84
    return MU_ERR_OUT_PTR_NULL;

85
  if (!sv)
86 87
    return EINVAL;

88
  if (len == (size_t) - 1)
89
    {
90
      const char **vp = sv;
91 92

      len = 0;
93

94 95 96
      for (len = 0; *vp; vp++, len++)
	;
    }
97

98
  if (len == 0)
99 100
    return EINVAL;

101
  for (i = 0; i < len; i++)
102 103 104 105 106
    {
      /* NULL strings are allowed */
      if (sv[i])
	buflen += strlen (sv[i]);
    }
107

108
  buflen += (len - 1) * strlen (", ");
109
  buflen += 1;			/* Termination null.  */
110

111
  buf = malloc (buflen);
112

113
  if (!buf)
114 115
    return ENOMEM;

116 117
  for (i = 0, buf[0] = '\0'; i < len; i++)
    {
118
      if (i != 0)
119
	strcat (buf, ", ");
120

121 122
      if (sv[i])
	strcat (buf, sv[i]);
123
    }
124

125
  status = mu_address_create (a, buf);
126

127
  free (buf);
128 129 130 131

  return status;
}

132
void
133
mu_address_destroy (mu_address_t * paddress)
134 135 136
{
  if (paddress && *paddress)
    {
137 138
      mu_address_t address = *paddress;
      mu_address_t current;
139 140
      for (; address; address = current)
	{
141 142
	  if (address->addr)
	    free (address->addr);
143 144 145 146 147 148
	  if (address->comments)
	    free (address->comments);
	  if (address->personal)
	    free (address->personal);
	  if (address->email)
	    free (address->email);
149 150 151 152 153 154
	  if (address->local_part)
	    free (address->local_part);
	  if (address->domain)
	    free (address->domain);
	  if (address->route)
	    free (address->route);
155 156 157 158 159 160 161
	  current = address->next;
	  free (address);
	}
      *paddress = NULL;
    }
}

162
int
163
mu_address_concatenate (mu_address_t to, mu_address_t * from)
164
{
165
  if (!to || !from || !*from)
166 167
    return EINVAL;

168
  while (to->next)
169 170
    to = to->next;

171
  assert (to && !to->next);
172 173 174 175

  to->next = *from;
  *from = NULL;

176 177 178 179 180 181 182
  /* discard the current string cache as it is now inaccurate */
  if (to->addr)
    {
      free (to->addr);
      to->addr = NULL;
    }

183 184
  to = to->next;

185
  /* only the first address must have a cache */
186 187 188 189 190
  if (to->addr)
    {
      free (to->addr);
      to->addr = NULL;
    }
191 192 193 194

  return 0;
}

195 196
mu_address_t 
_address_get_nth (mu_address_t addr, size_t no)
197 198 199 200 201 202 203 204 205
{
  int i;
  
  for (i = 1; addr; addr = addr->next, i++)
    if (i == no)
      break;
  return addr;
}

206
int
207
mu_address_get_nth (mu_address_t addr, size_t no, mu_address_t *pret)
208
{
209
  mu_address_t subaddr = _address_get_nth (addr, no);
210
  if (!subaddr)
211
    return MU_ERR_NOENT;
212
  *pret = mu_address_dup (subaddr);
213 214 215
  return 0;
}

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

/* General accessors: */
#define AC4(a,b,c,d) a ## b ## c ## d
#define ACCESSOR(action,field) AC4(mu_address_,action,_,field)

#define DECL_SET(field)							\
int									\
ACCESSOR(set, field) (mu_address_t addr, size_t no, const char *buf)	\
{									\
  char *s;								\
  mu_address_t subaddr;							\
  									\
  if (addr == NULL)							\
    return EINVAL;							\
									\
  subaddr = _address_get_nth (addr, no);				\
  if (!subaddr)								\
    return MU_ERR_NOENT;						\
									\
  s = strdup (buf);							\
  if (!s)								\
    return errno;							\
  									\
  free (subaddr->field);						\
  subaddr->field = s;							\
									\
  return 0;								\
243 244
}

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#define DECL_SGET(field)						\
int									\
ACCESSOR(sget,field) (mu_address_t addr, size_t no, char const **sptr)	\
{									\
  mu_address_t subaddr;							\
  									\
  if (addr == NULL)							\
    return EINVAL;							\
									\
  subaddr = _address_get_nth (addr, no);				\
  if (!subaddr)								\
    return MU_ERR_NOENT;						\
  									\
  *sptr = subaddr->field;						\
  return 0;								\
260 261
}

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
#define DECL_GET(field)							  \
int									  \
ACCESSOR(get,field) (mu_address_t addr, size_t no, char *buf, size_t len, \
		     size_t * n)					  \
{									  \
  size_t i;								  \
  const char *str;							  \
  int status = ACCESSOR(sget, field) (addr, no, &str);			  \
  									  \
  if (status)								  \
    return status;							  \
									  \
  i = mu_cpystr (buf, str, len);					  \
  if (n)								  \
    *n = i;								  \
  return 0;								  \
278 279
}

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
#define DECL_AGET(field)						\
int									\
ACCESSOR(aget, field) (mu_address_t addr, size_t no, char **buf)	\
{									\
  const char *str;							\
  int status = ACCESSOR(sget, field) (addr, no, &str);			\
									\
  if (status)								\
    return status;							\
									\
  if (str)								\
    {									\
      *buf = strdup (str);						\
      if (!*buf)							\
	status = ENOMEM;						\
    }									\
  else									\
    *buf = NULL;							\
  return status;							\
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
#define DECL_ACCESSORS(field)			\
DECL_SET(field)					\
DECL_SGET(field)				\
DECL_GET(field)					\
DECL_AGET(field)          



/* Personal part */
DECL_ACCESSORS(personal)
/* Comments */
DECL_ACCESSORS(comments)
/* Email */
DECL_ACCESSORS(email)     
/* Local part */
DECL_ACCESSORS(local_part)
/* Domain */
DECL_ACCESSORS(domain)
/* Route */
DECL_ACCESSORS(route)



324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
#define format_char(c) do {\
 if (buflen) \
   {\
      *buf++ = c;\
      buflen--;\
   }\
 else\
   rc++;\
} while(0) 

#define format_string(str) do {\
 if (buflen) \
   {\
      int n = snprintf (buf, buflen, "%s", str);\
      buf += n;\
      buflen -= n;\
   }\
 else\
   rc += strlen (str);\
} while (0)
     
size_t
346
mu_address_format_string (mu_address_t addr, char *buf, size_t buflen)
347 348 349 350 351
{
  int rc = 0;
  int comma = 0;
  
  for (;addr; addr = addr->next)
352
    {
353
      if (addr->email)
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
	  int space = 0;

	  if (comma)
	    format_char (',');
	  
	  if (addr->personal)
	    {
	      format_char ('"');
	      format_string (addr->personal);
	      format_char ('"');
	      space++;
	    }
	  
	  if (addr->comments)
	    {
	      if (space)
		format_char (' ');
	      format_char ('(');
	      format_string (addr->comments);
	      format_char (')');
	      space++;
	    }
	  
	  if (space)
	    format_char (' ');
	  format_char ('<');
	  format_string (addr->email);
	  format_char ('>');
	  comma++;
384 385
	}
    }
386 387 388 389
  format_char (0);
  return rc;
}

390
static int
391
_address_is_group (mu_address_t addr)
392 393 394 395 396 397
{
  if (addr->personal && !addr->local_part && !addr->domain)
    return 1;
  return 0;
}

398
static int
399
_address_is_email (mu_address_t addr)
400 401 402 403 404 405 406
{
  if (addr->email)
    return 1;
  return 0;
}

static int
407
_address_is_unix_mailbox (mu_address_t addr)
408 409 410 411 412 413
{
  if (addr->local_part && !addr->email)
    return 1;
  return 0;
}

Alain Magloire authored
414
int
415
mu_address_is_group (mu_address_t addr, size_t no, int *yes)
Alain Magloire authored
416
{
417
  mu_address_t subaddr;
418 419
  
  if (addr == NULL)
Alain Magloire authored
420
    return EINVAL;
421 422 423

  subaddr = _address_get_nth (addr, no);
  if (!subaddr)
424
    return MU_ERR_NOENT;
425 426 427 428
  
  if (yes)
    *yes = _address_is_group (subaddr);
  return 0;
Alain Magloire authored
429
}
430 431

int
432
mu_address_to_string (mu_address_t addr, char *buf, size_t len, size_t * n)
433 434 435 436
{
  size_t i;
  if (addr == NULL)
    return EINVAL;
Alain Magloire authored
437 438
  if (buf)
    *buf = '\0';
439 440 441

  if (!addr->addr)
    {
442
      i = mu_address_format_string (addr, NULL, 0);
443 444 445
      addr->addr = malloc (i + 1);
      if (!addr->addr)
	return ENOMEM;
446
      mu_address_format_string (addr, addr->addr, i+1);
447 448
    }
      
449
  i = mu_cpystr (buf, addr->addr, len);
450 451 452 453 454 455
  if (n)
    *n = i;
  return 0;
}

int
456
mu_address_get_count (mu_address_t addr, size_t * pcount)
457
{
458 459 460 461 462
  size_t j;
  for (j = 0; addr; addr = addr->next, j++)
    ;
  if (pcount)
    *pcount = j;
463 464
  return 0;
}
465

466
int
467
mu_address_get_group_count (mu_address_t addr, size_t * pcount)
468 469 470
{
  size_t j;
  for (j = 0; addr; addr = addr->next)
471 472 473 474
    {
      if (_address_is_group (addr))
	j++;
    }
475 476 477 478
  if (pcount)
    *pcount = j;
  return 0;
}
479 480

int
481
mu_address_get_email_count (mu_address_t addr, size_t * pcount)
482 483 484
{
  size_t j;
  for (j = 0; addr; addr = addr->next)
485 486 487 488
    {
      if (_address_is_email (addr))
	j++;
    }
489 490 491 492 493 494
  if (pcount)
    *pcount = j;
  return 0;
}

int
495
mu_address_get_unix_mailbox_count (mu_address_t addr, size_t * pcount)
496 497 498
{
  size_t j;
  for (j = 0; addr; addr = addr->next)
499 500 501 502
    {
      if (_address_is_unix_mailbox (addr))
	j++;
    }
503 504 505 506 507
  if (pcount)
    *pcount = j;
  return 0;
}

508
int
509
mu_address_contains_email (mu_address_t addr, const char *email)
510 511 512 513 514 515 516
{
  for (; addr; addr = addr->next)
    if (strcasecmp (addr->email, email) == 0)
      return 1;
  return 0;
}

517 518
mu_address_t
mu_address_dup (mu_address_t src)
519
{
520
  mu_address_t dst = calloc (1, sizeof (*dst));
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541

  if (!dst)
    return NULL;

  if (src->comments)
    dst->comments = strdup (src->comments);
  if (src->personal)
    dst->personal = strdup (src->personal);
  if (src->email)
    dst->email = strdup (src->email);
  if (src->local_part)
    dst->local_part = strdup (src->local_part);
  if (src->domain)
    dst->domain = strdup (src->domain);
  if (src->route)
    dst->route = strdup (src->route);

  return dst;
}
  
int
542
mu_address_union (mu_address_t *a, mu_address_t b)
543
{
544
  mu_address_t last = NULL;
545 546 547 548 549 550
    
  if (!a || !b)
    return EINVAL;

  if (!*a)
    {
551
      *a = mu_address_dup (b);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
      if (!*a)
	return ENOMEM;
      last = *a;
      b = b->next;
    }
  else
    {
      if ((*a)->addr)
	{
	  free ((*a)->addr);
	  (*a)->addr = NULL;
	}
      for (last = *a; last->next; last = last->next)
	;
    }

  for (; b; b = b->next)
569
    if (!mu_address_contains_email (*a, b->email))
570
      {
571
	mu_address_t next = mu_address_dup (b);
572 573 574 575 576 577 578 579
	if (!next)
	  return ENOMEM;
	last->next = next;
	last = next;
      }
  return 0;
}