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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors: Jeffrey Stedfast <fejj@ximian.com>
*
* Copyright 2001-2004 Ximian, Inc. (www.ximian.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "internet-address.h"
#include "gmime-table-private.h"
#include "gmime-utils.h"
#include "gmime-iconv-utils.h"
#ifdef ENABLE_WARNINGS
#define w(x) x
#else
#define w(x)
#endif /* ENABLE_WARNINGS */
#define d(x)
/* public functions that we don't care for the user of GMime to know or care about */
void decode_lwsp (const char **in);
char *decode_word (const char **in);
char *decode_addrspec (const char **in);
/**
* internet_address_new:
*
* Creates a new #InternetAddress object
*
* Returns a new #InternetAddress object.
**/
InternetAddress *
internet_address_new ()
{
InternetAddress *ia;
ia = g_new (InternetAddress, 1);
ia->type = INTERNET_ADDRESS_NONE;
ia->refcount = 1;
ia->name = NULL;
ia->value.addr = NULL;
return ia;
}
/**
* internet_address_destroy:
* @ia: internet address
*
* Destroy the #InternetAddress object pointed to by @ia.
**/
static void
internet_address_destroy (InternetAddress *ia)
{
if (ia) {
g_free (ia->name);
if (ia->type == INTERNET_ADDRESS_GROUP) {
internet_address_list_destroy (ia->value.members);
} else {
g_free (ia->value.addr);
}
g_free (ia);
}
}
/**
* internet_address_ref:
* @ia: internet address
*
* Ref's the internet address.
**/
void
internet_address_ref (InternetAddress *ia)
{
ia->refcount++;
}
/**
* internet_address_unref:
* @ia: internet address
*
* Unref's the internet address.
**/
void
internet_address_unref (InternetAddress *ia)
{
if (ia->refcount <= 1) {
internet_address_destroy (ia);
} else {
ia->refcount--;
}
}
/**
* internet_address_new_name:
* @name: person's name
* @addr: person's address
*
* Creates a new #InternetAddress object with name @name and address
* @addr.
*
* Returns a new #InternetAddress object.
**/
InternetAddress *
internet_address_new_name (const char *name, const char *addr)
{
InternetAddress *ia;
g_return_val_if_fail (addr != NULL, NULL);
ia = internet_address_new ();
ia->type = INTERNET_ADDRESS_NAME;
if (name) {
ia->name = g_mime_utils_8bit_header_decode (name);
g_mime_utils_unquote_string (ia->name);
}
ia->value.addr = g_strdup (addr);
return ia;
}
/**
* internet_address_new_group:
* @name: group name
*
* Creates a new #InternetAddress object with group name @name.
*
* Returns a new #InternetAddress object.
**/
InternetAddress *
internet_address_new_group (const char *name)
{
InternetAddress *ia;
ia = internet_address_new ();
ia->type = INTERNET_ADDRESS_GROUP;
if (name) {
ia->name = g_mime_utils_8bit_header_decode (name);
g_mime_utils_unquote_string (ia->name);
}
return ia;
}
/**
* internet_address_set_name:
* @ia: internet address
* @name: group or contact's name
*
* Set the name of the internet address.
**/
void
internet_address_set_name (InternetAddress *ia, const char *name)
{
g_return_if_fail (ia != NULL);
g_free (ia->name);
if (name) {
ia->name = g_mime_utils_8bit_header_decode (name);
g_mime_utils_unquote_string (ia->name);
} else
ia->name = NULL;
}
/**
* internet_address_set_addr:
* @ia: internet address
* @addr: contact's email address
*
* Set the internet address's address.
**/
void
internet_address_set_addr (InternetAddress *ia, const char *addr)
{
g_return_if_fail (ia != NULL);
g_return_if_fail (ia->type != INTERNET_ADDRESS_GROUP);
ia->type = INTERNET_ADDRESS_NAME;
g_free (ia->value.addr);
ia->value.addr = g_strdup (addr);
}
/**
* internet_address_set_group:
* @ia: internet address
* @group: a list of internet addresses
*
* Set the members of the internet address group.
**/
void
internet_address_set_group (InternetAddress *ia, InternetAddressList *group)
{
/*InternetAddressList *members, *next; */
g_return_if_fail (ia != NULL);
g_return_if_fail (ia->type != INTERNET_ADDRESS_NAME);
ia->type = INTERNET_ADDRESS_GROUP;
internet_address_list_destroy (ia->value.members);
ia->value.members = group;
}
/**
* internet_address_add_member:
* @ia: internet address
* @member: group member's internet address
*
* Add a contact to the internet address group.
**/
void
internet_address_add_member (InternetAddress *ia, InternetAddress *member)
{
g_return_if_fail (ia != NULL);
g_return_if_fail (ia->type != INTERNET_ADDRESS_NAME);
g_return_if_fail (member != NULL);
ia->type = INTERNET_ADDRESS_GROUP;
ia->value.members = internet_address_list_append (ia->value.members, member);
}
/**
* internet_address_list_prepend:
* @list: a list of internet addresses
* @ia: internet address to prepend
*
* Prepends the internet address to the list of internet addresses
* pointed to by @list.
*
* Returns the resultant list.
**/
InternetAddressList *
internet_address_list_prepend (InternetAddressList *list, InternetAddress *ia)
{
InternetAddressList *node;
g_return_val_if_fail (ia != NULL, NULL);
internet_address_ref (ia);
node = g_new (InternetAddressList, 1);
node->next = list;
node->address = ia;
return node;
}
/**
* internet_address_list_append:
* @list: a list of internet addresses
* @ia: internet address to append
*
* Appends the internet address to the list of internet addresses
* pointed to by @list.
*
* Returns the resultant list.
**/
InternetAddressList *
internet_address_list_append (InternetAddressList *list, InternetAddress *ia)
{
InternetAddressList *node, *n;
g_return_val_if_fail (ia != NULL, NULL);
internet_address_ref (ia);
node = g_new (InternetAddressList, 1);
node->next = NULL;
node->address = ia;
if (list == NULL)
return node;
n = list;
while (n->next)
n = n->next;
n->next = node;
return list;
}
/**
* internet_address_list_concat:
* @a: first list
* @b: second list
*
* Concatenates a copy of list @b onto the end of list @a.
*
* Returns the resulting list.
**/
InternetAddressList *
internet_address_list_concat (InternetAddressList *a, InternetAddressList *b)
{
InternetAddressList *node, *tail, *n;
if (b == NULL)
return a;
/* find the end of list a */
if (a != NULL) {
tail = a;
while (tail->next)
tail = tail->next;
} else {
tail = (InternetAddressList *) &a;
}
/* concat a copy of list b to list a */
node = b;
while (node) {
internet_address_ref (node->address);
n = g_new (InternetAddressList, 1);
n->next = NULL;
n->address = node->address;
tail->next = n;
tail = n;
node = node->next;
}
return a;
}
/**
* internet_address_list_length:
* @list: list of internet addresses
*
* Calculates the length of the list of addresses.
*
* Returns the number of internet addresses in @list.
**/
int
internet_address_list_length (InternetAddressList *list)
{
InternetAddressList *node;
int len = 0;
node = list;
while (node) {
node = node->next;
len++;
}
return len;
}
/**
* internet_address_list_destroy:
* @list: address list
*
* Destroys the list of internet addresses.
**/
void
internet_address_list_destroy (InternetAddressList *list)
{
InternetAddressList *node, *next;
node = list;
while (node) {
next = node->next;
internet_address_unref (node->address);
g_free (node);
node = next;
}
}
static char *
encoded_name (const char *raw, gboolean rfc2047_encode)
{
char *name;
g_return_val_if_fail (raw != NULL, NULL);
if (rfc2047_encode && g_mime_utils_text_is_8bit (raw, strlen (raw))) {
name = g_mime_utils_8bit_header_encode_phrase (raw);
} else {
name = g_mime_utils_quote_string (raw);
}
return name;
}
static void
internet_address_list_to_string_internal (InternetAddressList *list, gboolean encode, GString *string)
{
while (list) {
char *addr;
addr = internet_address_to_string (list->address, encode);
if (addr) {
g_string_append (string, addr);
g_free (addr);
if (list->next)
g_string_append (string, ", ");
}
list = list->next;
}
}
/**
* internet_address_to_string:
* @ia: Internet Address object
* @encode: TRUE if the address should be rfc2047 encoded
*
* Allocates a string containing the contents of the #InternetAddress
* object.
*
* Returns the #InternetAddress object as an allocated string in
* rfc822 format.
**/
char *
internet_address_to_string (InternetAddress *ia, gboolean encode)
{
char *str = NULL;
if (ia->type == INTERNET_ADDRESS_NAME) {
if (ia->name && *ia->name) {
char *name;
name = encoded_name (ia->name, encode);
str = g_strdup_printf ("%s <%s>", name, ia->value.addr);
g_free (name);
} else {
str = g_strdup (ia->value.addr);
}
} else if (ia->type == INTERNET_ADDRESS_GROUP) {
InternetAddressList *members;
GString *string;
string = g_string_new (ia->name);
g_string_append (string, ": ");
members = ia->value.members;
internet_address_list_to_string_internal (members, encode, string);
g_string_append (string, ";");
str = string->str;
g_string_free (string, FALSE);
}
return str;
}
/**
* internet_address_list_to_string:
* @list: list of internet addresses
* @encode: %TRUE if the address should be rfc2047 encoded
*
* Allocates a string buffer containing the rfc822 formatted addresses
* in @list.
*
* Returns a string containing the list of addresses in rfc822 format.
**/
char *
internet_address_list_to_string (InternetAddressList *list, gboolean encode)
{
GString *string;
char *str;
string = g_string_new ("");
internet_address_list_to_string_internal (list, encode, string);
str = string->str;
g_string_free (string, FALSE);
return str;
}
void
decode_lwsp (const char **in)
{
const char *inptr = *in;
while (*inptr && (*inptr == '(' || is_lwsp (*inptr))) {
while (*inptr && is_lwsp (*inptr))
inptr++;
/* skip over any comments */
if (*inptr == '(') {
int depth = 1;
inptr++;
while (*inptr && depth) {
if (*inptr == '\\' && *(inptr + 1))
inptr++;
else if (*inptr == '(')
depth++;
else if (*inptr == ')')
depth--;
inptr++;
}
}
}
*in = inptr;
}
static char *
decode_quoted_string (const char **in)
{
const char *inptr = *in;
char *out = NULL;
decode_lwsp (&inptr);
if (*inptr == '"') {
out = (char *) inptr;
inptr++;
while (*inptr && *inptr != '"') {
if (*inptr == '\\')
inptr++;
if (*inptr)
inptr++;
}
if (*inptr == '"')
inptr++;
out = g_strndup (out, inptr - out);
}
*in = inptr;
return out;
}
static char *
decode_atom (const char **in)
{
const char *inptr = *in, *start;
decode_lwsp (&inptr);
start = inptr;
while (is_atom (*inptr))
inptr++;
*in = inptr;
if (inptr > start)
return g_strndup (start, inptr - start);
else
return NULL;
}
char *
decode_word (const char **in)
{
const char *inptr = *in;
decode_lwsp (&inptr);
if (*inptr == '"') {
*in = inptr;
return decode_quoted_string (in);
} else {
*in = inptr;
return decode_atom (in);
}
}
static gboolean
decode_subliteral (const char **in, GString *domain)
{
const char *inptr = *in;
gboolean got = FALSE;
while (*inptr && *inptr != '.' && *inptr != ']') {
if (is_dtext (*inptr)) {
g_string_append_c (domain, *inptr);
inptr++;
got = TRUE;
} else if (is_lwsp (*inptr))
decode_lwsp (&inptr);
else
break;
}
*in = inptr;
return got;
}
static void
decode_domain_literal (const char **in, GString *domain)
{
const char *inptr = *in;
decode_lwsp (&inptr);
while (*inptr && *inptr != ']') {
if (decode_subliteral (&inptr, domain) && *inptr == '.') {
g_string_append_c (domain, *inptr);
inptr++;
} else if (*inptr != ']') {
w(g_warning ("Malformed domain-literal, unexpected char '%c': %s",
*inptr, *in));
/* try and skip to the next char ?? */
inptr++;
}
}
*in = inptr;
}
static char *
decode_domain (const char **in)
{
const char *inptr, *save;
GString *domain;
char *dom, *atom;
domain = g_string_new ("");
inptr = *in;
while (inptr && *inptr) {
decode_lwsp (&inptr);
if (*inptr == '[') {
/* domain literal */
g_string_append_c (domain, '[');
inptr++;
decode_domain_literal (&inptr, domain);
if (*inptr == ']') {
g_string_append_c (domain, ']');
inptr++;
} else
w(g_warning ("Missing ']' in domain-literal: %s", *in));
} else {
if (!(atom = decode_atom (&inptr))) {
w(g_warning ("Unexpected char '%c' in domain: %s", *inptr, *in));
/* remove the last '.' */
if (domain->len && domain->str[domain->len - 1] == '.')
g_string_truncate (domain, domain->len - 1);
break;
}
g_string_append (domain, atom);
g_free (atom);
}
save = inptr;
decode_lwsp (&inptr);
if (*inptr != '.') {
inptr = save;
break;
}
g_string_append_c (domain, '.');
inptr++;
}
if (domain->len)
dom = domain->str;
else
dom = NULL;
g_string_free (domain, dom ? FALSE : TRUE);
*in = inptr;
return dom;
}
char *
decode_addrspec (const char **in)
{
char *domain, *word, *str = NULL;
const char *inptr;
GString *addrspec;
decode_lwsp (in);
inptr = *in;
if (!(word = decode_word (&inptr))) {
w(g_warning ("No local-part in addr-spec: %s", *in));
return NULL;
}
addrspec = g_string_new (word);
g_free (word);
/* get the rest of the local-part */
decode_lwsp (&inptr);
while (*inptr == '.') {
g_string_append_c (addrspec, *inptr++);
word = decode_word (&inptr);
if (word) {
g_string_append (addrspec, word);
decode_lwsp (&inptr);
g_free (word);
} else {
w(g_warning ("Invalid local-part in addr-spec: %s", *in));
goto exception;
}
}
/* we should be at the '@' now... */
if (*inptr++ != '@') {
w(g_warning ("Invalid addr-spec; missing '@': %s", *in));
goto exception;
}
if (!(domain = decode_domain (&inptr))) {
w(g_warning ("No domain in addr-spec: %s", *in));
goto exception;
}
g_string_append_c (addrspec, '@');
g_string_append (addrspec, domain);
g_free (domain);
str = addrspec->str;
g_string_free (addrspec, FALSE);
*in = inptr;
return str;
exception:
g_string_free (addrspec, TRUE);
return NULL;
}
static InternetAddress *
decode_mailbox (const char **in)
{
InternetAddress *mailbox = NULL;
const char *inptr;
gboolean bracket = FALSE;
GString *name = NULL;
GString *addr;
char *pre;
addr = g_string_new ("");
decode_lwsp (in);
inptr = *in;
pre = decode_word (&inptr);
decode_lwsp (&inptr);
if (*inptr && !strchr (",.@", *inptr)) {
gboolean retried = FALSE;
/* this mailbox has a name part, so get the name */
name = g_string_new ("");
while (pre) {
retried = FALSE;
g_string_append (name, pre);
g_free (pre);
retry:
pre = decode_word (&inptr);
if (pre)
g_string_append_c (name, ' ');
}
decode_lwsp (&inptr);
if (*inptr == '<') {
inptr++;
bracket = TRUE;
pre = decode_word (&inptr);
} else if (!retried && *inptr) {
w(g_warning ("Unexpected char '%c' in address: %s: attempting recovery.",
*inptr, *in));
/* chew up this bad char and then attempt 1 more pass at parsing */
g_string_append_c (name, *inptr++);
retried = TRUE;
goto retry;
} else {
g_string_free (name, TRUE);
g_string_free (addr, TRUE);
*in = inptr;
return NULL;
}
}
if (pre) {
g_string_append (addr, pre);
} else {
w(g_warning ("No local part for email address: %s", *in));
if (name)
g_string_free (name, TRUE);
g_string_free (addr, TRUE);
*in = inptr + 1;
return NULL;
}
/* get the rest of the local-part */
decode_lwsp (&inptr);
while (*inptr == '.' && pre) {
inptr++;
g_free (pre);
pre = decode_word (&inptr);
if (pre) {
g_string_append_c (addr, '.');
g_string_append (addr, pre);
}
decode_lwsp (&inptr);
}
g_free (pre);
/* we should be at the '@' now... */
if (*inptr == '@') {
char *domain;
inptr++;
domain = decode_domain (&inptr);
if (domain) {
g_string_append_c (addr, '@');
g_string_append (addr, domain);
g_free (domain);
}
} else {
w(g_warning ("No domain in email address: %s", *in));
}
if (bracket) {
decode_lwsp (&inptr);
if (*inptr == '>')
inptr++;
else
w(g_warning ("Missing closing '>' bracket for email address: %s", *in));
}
if (!name || !name->len) {
/* look for a trailing comment to use as the name? */
char *comment;
if (name) {
g_string_free (name, TRUE);
name = NULL;
}
comment = (char *) inptr;
decode_lwsp (&inptr);
if (inptr > comment) {
comment = memchr (comment, '(', inptr - comment);
if (comment) {
const char *cend;
/* find the end of the comment */
cend = inptr - 1;
while (cend > comment && is_lwsp (*cend))
cend--;
if (*cend == ')')
cend--;
comment = g_strndup (comment + 1, cend - comment);
g_strstrip (comment);
name = g_string_new (comment);
g_free (comment);
}
}
}
*in = inptr;
if (addr->len) {
if (name && !g_utf8_validate (name->str, -1, NULL)) {
/* A (broken) mailer has sent us an unencoded
* 8bit value (and it doesn't seem to be valid
* UTF-8 either). Attempt to save it by
* assuming it's in the user's locale and
* converting to UTF-8 */
char *buf;
buf = g_mime_iconv_locale_to_utf8 (name->str);
if (buf) {
g_string_truncate (name, 0);
g_string_append (name, buf);
g_free (buf);
} else {
d(g_warning ("Failed to convert \"%s\" to UTF-8: %s",
name->str, g_strerror (errno)));
}
}
mailbox = internet_address_new_name (name ? name->str : NULL, addr->str);
}
g_string_free (addr, TRUE);
if (name)
g_string_free (name, TRUE);
return mailbox;
}
static InternetAddress *
decode_address (const char **in)
{
InternetAddress *addr = NULL;
const char *inptr, *start;
GString *name;
char *pre;
decode_lwsp (in);
start = inptr = *in;
/* pre-scan */
name = g_string_new ("");
pre = decode_word (&inptr);
while (pre) {
g_string_append (name, pre);
g_free (pre);
pre = decode_word (&inptr);
if (pre)
g_string_append_c (name, ' ');
}
decode_lwsp (&inptr);
if (*inptr == ':') {
/* this is a group */
InternetAddressList *group = NULL, *tail;
tail = (InternetAddressList *) &group;
inptr++;
addr = internet_address_new_group (name->str);
decode_lwsp (&inptr);
while (*inptr && *inptr != ';') {
InternetAddress *member;
member = decode_mailbox (&inptr);
if (member) {
tail->next = g_new (InternetAddressList, 1);
tail = tail->next;
tail->next = NULL;
tail->address = member;
}
decode_lwsp (&inptr);
while (*inptr == ',') {
inptr++;
decode_lwsp (&inptr);
member = decode_mailbox (&inptr);
if (member) {
tail->next = g_new (InternetAddressList, 1);
tail = tail->next;
tail->next = NULL;
tail->address = member;
}
decode_lwsp (&inptr);
}
}
if (*inptr == ';')
inptr++;
else
w(g_warning ("Invalid group spec, missing closing ';': %.*s",
inptr - start, start));
internet_address_set_group (addr, group);
*in = inptr;
} else {
/* this is a mailbox */
addr = decode_mailbox (in);
}
g_string_free (name, TRUE);
return addr;
}
/**
* internet_address_parse_string:
* @string: a string containing internet addresses
*
* Construct a list of internet addresses from the given string.
*
* Returns a linked list of internet addresses. *Must* be free'd by
* the caller.
**/
InternetAddressList *
internet_address_parse_string (const char *string)
{
InternetAddressList *node, *tail, *addrlist = NULL;
const char *inptr;
inptr = string;
tail = (InternetAddressList *) &addrlist;
while (inptr && *inptr) {
InternetAddress *addr;
const char *start;
start = inptr;
addr = decode_address (&inptr);
if (addr) {
node = g_new (InternetAddressList, 1);
node->next = NULL;
node->address = addr;
tail->next = node;
tail = node;
} else {
w(g_warning ("Invalid or incomplete address: %.*s",
inptr - start, start));
}
decode_lwsp (&inptr);
if (*inptr == ',')
inptr++;
else if (*inptr) {
w(g_warning ("Parse error at '%s': expected ','", inptr));
/* try skipping to the next address */
inptr = strchr (inptr, ',');
if (inptr)
inptr++;
}
}
return addrlist;
}
|