From: Richard Russon Date: Tue, 28 Nov 2017 15:01:16 +0000 (+0000) Subject: move functions to mutt_address.c X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=fb76bba59e9bab89f51fb19683fa5481444a9d79;p=neomutt move functions to mutt_address.c --- diff --git a/Makefile.am b/Makefile.am index 0a4376b8f..3f313aa7c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -49,7 +49,7 @@ neomutt_SOURCES = mutt_account.c addrbook.c address.h alias.c alias.h attach.c \ envelope.c envelope.h filter.c flags.c format_flags.h from.c group.c \ handler.c hdrline.c header.h header.c help.c history.c hook.c \ init.c keymap.c main.c mbox.c mbyte.c mbtable.h \ - menu.c mh.c muttlib.c mutt_idna.c mutt_socket.c \ + menu.c mh.c muttlib.c mutt_address.c mutt_idna.c mutt_socket.c \ mx.c newsrc.c nntp.c options.h pager.c parameter.c parameter.h \ parse.c pattern.c pattern.h pop.c pop_auth.c pop_lib.c postpone.c \ query.c recvattach.c recvcmd.c rfc1524.c rfc2047.c rfc2231.c rfc3676.c \ diff --git a/Makefile.autosetup b/Makefile.autosetup index f54dbce13..bcb94c827 100644 --- a/Makefile.autosetup +++ b/Makefile.autosetup @@ -59,7 +59,7 @@ NEOMUTTOBJS= mutt_account.o addrbook.o alias.o attach.o bcache.o body.o \ curs_main.o edit.o editmsg.o enter.o envelope.o filter.o \ flags.o from.o group.o handler.o hdrline.o \ header.o help.o history.o hook.o init.o keymap.o main.o \ - mbox.o mbyte.o menu.o mh.o muttlib.o mutt_idna.o \ + mbox.o mbyte.o menu.o mh.o muttlib.o mutt_address.o mutt_idna.o \ mutt_socket.o mutt_tags.o mx.o \ newsrc.o nntp.o pager.o parameter.o parse.o pattern.o pop.o \ pop_auth.o pop_lib.o postpone.o query.o recvattach.o recvcmd.o \ diff --git a/mutt_address.c b/mutt_address.c new file mode 100644 index 000000000..8dcdecb6a --- /dev/null +++ b/mutt_address.c @@ -0,0 +1,204 @@ +/** + * @file + * Representation of an email address + * + * @authors + * Copyright (C) 1996-2000,2011-2013 Michael R. Elkins + * Copyright (C) 2017 Richard Russon + * + * @copyright + * 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, see . + */ + +#include "config.h" +#include +#include +#include "mutt/mutt.h" +#include "mutt_idna.h" +#include "rfc822.h" + +void rfc822_write_address_single(char *buf, size_t buflen, struct Address *addr, int display) +{ + size_t len; + char *pbuf = buf; + char *pc = NULL; + + if (!addr) + return; + + buflen--; /* save room for the terminal nul */ + + if (addr->personal) + { + if (strpbrk(addr->personal, RFC822Specials)) + { + if (!buflen) + goto done; + *pbuf++ = '"'; + buflen--; + for (pc = addr->personal; *pc && buflen > 0; pc++) + { + if (*pc == '"' || *pc == '\\') + { + *pbuf++ = '\\'; + buflen--; + } + if (!buflen) + goto done; + *pbuf++ = *pc; + buflen--; + } + if (!buflen) + goto done; + *pbuf++ = '"'; + buflen--; + } + else + { + if (!buflen) + goto done; + mutt_str_strfcpy(pbuf, addr->personal, buflen); + len = mutt_str_strlen(pbuf); + pbuf += len; + buflen -= len; + } + + if (!buflen) + goto done; + *pbuf++ = ' '; + buflen--; + } + + if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) + { + if (!buflen) + goto done; + *pbuf++ = '<'; + buflen--; + } + + if (addr->mailbox) + { + if (!buflen) + goto done; + if ((mutt_str_strcmp(addr->mailbox, "@") != 0) && !display) + { + mutt_str_strfcpy(pbuf, addr->mailbox, buflen); + len = mutt_str_strlen(pbuf); + } + else if ((mutt_str_strcmp(addr->mailbox, "@") != 0) && display) + { + mutt_str_strfcpy(pbuf, mutt_addr_for_display(addr), buflen); + len = mutt_str_strlen(pbuf); + } + else + { + *pbuf = '\0'; + len = 0; + } + pbuf += len; + buflen -= len; + + if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) + { + if (!buflen) + goto done; + *pbuf++ = '>'; + buflen--; + } + + if (addr->group) + { + if (!buflen) + goto done; + *pbuf++ = ':'; + buflen--; + if (!buflen) + goto done; + *pbuf++ = ' '; + buflen--; + } + } + else + { + if (!buflen) + goto done; + *pbuf++ = ';'; + buflen--; + } +done: + /* no need to check for length here since we already save space at the + beginning of this routine */ + *pbuf = 0; +} + +/** + * rfc822_write_address - Write an address to a buffer + * + * Note: it is assumed that `buf' is nul terminated! + */ +int rfc822_write_address(char *buf, size_t buflen, struct Address *addr, int display) +{ + char *pbuf = buf; + size_t len = mutt_str_strlen(buf); + + buflen--; /* save room for the terminal nul */ + + if (len > 0) + { + if (len > buflen) + return pbuf - buf; /* safety check for bogus arguments */ + + pbuf += len; + buflen -= len; + if (!buflen) + goto done; + *pbuf++ = ','; + buflen--; + if (!buflen) + goto done; + *pbuf++ = ' '; + buflen--; + } + + for (; addr && buflen > 0; addr = addr->next) + { + /* use buflen+1 here because we already saved space for the trailing + nul char, and the subroutine can make use of it */ + rfc822_write_address_single(pbuf, buflen + 1, addr, display); + + /* this should be safe since we always have at least 1 char passed into + the above call, which means `pbuf' should always be nul terminated */ + len = mutt_str_strlen(pbuf); + pbuf += len; + buflen -= len; + + /* if there is another address, and it's not a group mailbox name or + group terminator, add a comma to separate the addresses */ + if (addr->next && addr->next->mailbox && !addr->group) + { + if (!buflen) + goto done; + *pbuf++ = ','; + buflen--; + if (!buflen) + goto done; + *pbuf++ = ' '; + buflen--; + } + } +done: + *pbuf = 0; + return pbuf - buf; +} diff --git a/po/POTFILES.in b/po/POTFILES.in index 3d2a7d12d..248167b3d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -82,6 +82,7 @@ mutt/signal.c mutt/string.c muttlib.c mutt_account.c +mutt_address.c mutt_idna.c mutt_lua.c mutt_notmuch.c diff --git a/rfc822.c b/rfc822.c index 68797a812..60378c947 100644 --- a/rfc822.c +++ b/rfc822.c @@ -689,181 +689,6 @@ void rfc822_cat(char *buf, size_t buflen, const char *value, const char *special mutt_str_strfcpy(buf, value, buflen); } -void rfc822_write_address_single(char *buf, size_t buflen, struct Address *addr, int display) -{ - size_t len; - char *pbuf = buf; - char *pc = NULL; - - if (!addr) - return; - - buflen--; /* save room for the terminal nul */ - - if (addr->personal) - { - if (strpbrk(addr->personal, RFC822Specials)) - { - if (!buflen) - goto done; - *pbuf++ = '"'; - buflen--; - for (pc = addr->personal; *pc && buflen > 0; pc++) - { - if (*pc == '"' || *pc == '\\') - { - *pbuf++ = '\\'; - buflen--; - } - if (!buflen) - goto done; - *pbuf++ = *pc; - buflen--; - } - if (!buflen) - goto done; - *pbuf++ = '"'; - buflen--; - } - else - { - if (!buflen) - goto done; - mutt_str_strfcpy(pbuf, addr->personal, buflen); - len = mutt_str_strlen(pbuf); - pbuf += len; - buflen -= len; - } - - if (!buflen) - goto done; - *pbuf++ = ' '; - buflen--; - } - - if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) - { - if (!buflen) - goto done; - *pbuf++ = '<'; - buflen--; - } - - if (addr->mailbox) - { - if (!buflen) - goto done; - if ((mutt_str_strcmp(addr->mailbox, "@") != 0) && !display) - { - mutt_str_strfcpy(pbuf, addr->mailbox, buflen); - len = mutt_str_strlen(pbuf); - } - else if ((mutt_str_strcmp(addr->mailbox, "@") != 0) && display) - { - mutt_str_strfcpy(pbuf, mutt_addr_for_display(addr), buflen); - len = mutt_str_strlen(pbuf); - } - else - { - *pbuf = '\0'; - len = 0; - } - pbuf += len; - buflen -= len; - - if (addr->personal || (addr->mailbox && *addr->mailbox == '@')) - { - if (!buflen) - goto done; - *pbuf++ = '>'; - buflen--; - } - - if (addr->group) - { - if (!buflen) - goto done; - *pbuf++ = ':'; - buflen--; - if (!buflen) - goto done; - *pbuf++ = ' '; - buflen--; - } - } - else - { - if (!buflen) - goto done; - *pbuf++ = ';'; - buflen--; - } -done: - /* no need to check for length here since we already save space at the - beginning of this routine */ - *pbuf = 0; -} - -/** - * rfc822_write_address - Write an address to a buffer - * - * Note: it is assumed that `buf' is nul terminated! - */ -int rfc822_write_address(char *buf, size_t buflen, struct Address *addr, int display) -{ - char *pbuf = buf; - size_t len = mutt_str_strlen(buf); - - buflen--; /* save room for the terminal nul */ - - if (len > 0) - { - if (len > buflen) - return pbuf - buf; /* safety check for bogus arguments */ - - pbuf += len; - buflen -= len; - if (!buflen) - goto done; - *pbuf++ = ','; - buflen--; - if (!buflen) - goto done; - *pbuf++ = ' '; - buflen--; - } - - for (; addr && buflen > 0; addr = addr->next) - { - /* use buflen+1 here because we already saved space for the trailing - nul char, and the subroutine can make use of it */ - rfc822_write_address_single(pbuf, buflen + 1, addr, display); - - /* this should be safe since we always have at least 1 char passed into - the above call, which means `pbuf' should always be nul terminated */ - len = mutt_str_strlen(pbuf); - pbuf += len; - buflen -= len; - - /* if there is another address, and it's not a group mailbox name or - group terminator, add a comma to separate the addresses */ - if (addr->next && addr->next->mailbox && !addr->group) - { - if (!buflen) - goto done; - *pbuf++ = ','; - buflen--; - if (!buflen) - goto done; - *pbuf++ = ' '; - buflen--; - } - } -done: - *pbuf = 0; - return pbuf - buf; -} - /** * rfc822_cpy_adr_real - Copy the real address * @param addr Address to copy diff --git a/rfc822.h b/rfc822.h index d73bc04ba..d983ab800 100644 --- a/rfc822.h +++ b/rfc822.h @@ -56,6 +56,7 @@ int rfc822_remove_from_adrlist(struct Address **a, const char *mailbox); extern int RFC822Error; extern const char *const RFC822Errors[]; +extern const char RFC822Specials[]; #define rfc822_error(x) RFC822Errors[x] diff --git a/sendlib.c b/sendlib.c index 073473dca..72e10db38 100644 --- a/sendlib.c +++ b/sendlib.c @@ -84,8 +84,6 @@ #include #endif -extern char RFC822Specials[]; - const char MimeSpecials[] = "@.,;:<>[]\\\"()?/= \t"; static void encode_quoted(FGETCONV *fc, FILE *fout, int istext)