| 1 | /* |
|---|
| 2 | * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org> |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program; if not, write to the Free Software |
|---|
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #if HAVE_CONFIG_H |
|---|
| 20 | # include "config.h" |
|---|
| 21 | #endif |
|---|
| 22 | |
|---|
| 23 | #include "mutt.h" |
|---|
| 24 | #include "mutt_menu.h" |
|---|
| 25 | #include "mapping.h" |
|---|
| 26 | #include "sort.h" |
|---|
| 27 | |
|---|
| 28 | #include "mutt_idna.h" |
|---|
| 29 | |
|---|
| 30 | #include <string.h> |
|---|
| 31 | #include <stdlib.h> |
|---|
| 32 | #include <ctype.h> |
|---|
| 33 | |
|---|
| 34 | #define RSORT(x) (SortAlias & SORT_REVERSE) ? -x : x |
|---|
| 35 | |
|---|
| 36 | static struct mapping_t AliasHelp[] = { |
|---|
| 37 | { N_("Exit"), OP_EXIT }, |
|---|
| 38 | { N_("Del"), OP_DELETE }, |
|---|
| 39 | { N_("Undel"), OP_UNDELETE }, |
|---|
| 40 | { N_("Select"), OP_GENERIC_SELECT_ENTRY }, |
|---|
| 41 | { N_("Help"), OP_HELP }, |
|---|
| 42 | { NULL } |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | static const char * |
|---|
| 46 | alias_format_str (char *dest, size_t destlen, size_t col, char op, const char *src, |
|---|
| 47 | const char *fmt, const char *ifstring, const char *elsestring, |
|---|
| 48 | unsigned long data, format_flag flags) |
|---|
| 49 | { |
|---|
| 50 | char tmp[SHORT_STRING], adr[SHORT_STRING]; |
|---|
| 51 | ALIAS *alias = (ALIAS *) data; |
|---|
| 52 | |
|---|
| 53 | switch (op) |
|---|
| 54 | { |
|---|
| 55 | case 'f': |
|---|
| 56 | snprintf (tmp, sizeof (tmp), "%%%ss", fmt); |
|---|
| 57 | snprintf (dest, destlen, tmp, alias->del ? "D" : " "); |
|---|
| 58 | break; |
|---|
| 59 | case 'a': |
|---|
| 60 | mutt_format_s (dest, destlen, fmt, alias->name); |
|---|
| 61 | break; |
|---|
| 62 | case 'r': |
|---|
| 63 | adr[0] = 0; |
|---|
| 64 | rfc822_write_address (adr, sizeof (adr), alias->addr, 1); |
|---|
| 65 | snprintf (tmp, sizeof (tmp), "%%%ss", fmt); |
|---|
| 66 | snprintf (dest, destlen, tmp, adr); |
|---|
| 67 | break; |
|---|
| 68 | case 'n': |
|---|
| 69 | snprintf (tmp, sizeof (tmp), "%%%sd", fmt); |
|---|
| 70 | snprintf (dest, destlen, tmp, alias->num + 1); |
|---|
| 71 | break; |
|---|
| 72 | case 't': |
|---|
| 73 | dest[0] = alias->tagged ? '*' : ' '; |
|---|
| 74 | dest[1] = 0; |
|---|
| 75 | break; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | return (src); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | static void alias_entry (char *s, size_t slen, MUTTMENU *m, int num) |
|---|
| 82 | { |
|---|
| 83 | mutt_FormatString (s, slen, 0, NONULL (AliasFmt), alias_format_str, (unsigned long) ((ALIAS **) m->data)[num], M_FORMAT_ARROWCURSOR); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | static int alias_tag (MUTTMENU *menu, int n, int m) |
|---|
| 87 | { |
|---|
| 88 | ALIAS *cur = ((ALIAS **) menu->data)[n]; |
|---|
| 89 | int ot = cur->tagged; |
|---|
| 90 | |
|---|
| 91 | cur->tagged = (m >= 0 ? m : !cur->tagged); |
|---|
| 92 | |
|---|
| 93 | return cur->tagged - ot; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | static int alias_SortAlias (const void *a, const void *b) |
|---|
| 97 | { |
|---|
| 98 | ALIAS *pa = *(ALIAS **) a; |
|---|
| 99 | ALIAS *pb = *(ALIAS **) b; |
|---|
| 100 | int r = mutt_strcasecmp (pa->name, pb->name); |
|---|
| 101 | |
|---|
| 102 | return (RSORT (r)); |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | static int alias_SortAddress (const void *a, const void *b) |
|---|
| 106 | { |
|---|
| 107 | ADDRESS *pa = (*(ALIAS **) a)->addr; |
|---|
| 108 | ADDRESS *pb = (*(ALIAS **) b)->addr; |
|---|
| 109 | int r; |
|---|
| 110 | |
|---|
| 111 | if (pa == pb) |
|---|
| 112 | r = 0; |
|---|
| 113 | else if (pa == NULL) |
|---|
| 114 | r = -1; |
|---|
| 115 | else if (pb == NULL) |
|---|
| 116 | r = 1; |
|---|
| 117 | else if (pa->personal) |
|---|
| 118 | { |
|---|
| 119 | if (pb->personal) |
|---|
| 120 | r = mutt_strcasecmp (pa->personal, pb->personal); |
|---|
| 121 | else |
|---|
| 122 | r = 1; |
|---|
| 123 | } |
|---|
| 124 | else if (pb->personal) |
|---|
| 125 | r = -1; |
|---|
| 126 | else |
|---|
| 127 | r = ascii_strcasecmp (pa->mailbox, pb->mailbox); |
|---|
| 128 | return (RSORT (r)); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | void mutt_alias_menu (char *buf, size_t buflen, ALIAS *aliases) |
|---|
| 132 | { |
|---|
| 133 | ALIAS *aliasp; |
|---|
| 134 | MUTTMENU *menu; |
|---|
| 135 | ALIAS **AliasTable = NULL; |
|---|
| 136 | int t = -1; |
|---|
| 137 | int i, done = 0; |
|---|
| 138 | int op; |
|---|
| 139 | char helpstr[LONG_STRING]; |
|---|
| 140 | |
|---|
| 141 | int omax; |
|---|
| 142 | |
|---|
| 143 | if (!aliases) |
|---|
| 144 | { |
|---|
| 145 | mutt_error _("You have no aliases!"); |
|---|
| 146 | return; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | /* tell whoever called me to redraw the screen when I return */ |
|---|
| 150 | set_option (OPTNEEDREDRAW); |
|---|
| 151 | |
|---|
| 152 | menu = mutt_new_menu (); |
|---|
| 153 | menu->make_entry = alias_entry; |
|---|
| 154 | menu->tag = alias_tag; |
|---|
| 155 | menu->menu = MENU_ALIAS; |
|---|
| 156 | menu->title = _("Aliases"); |
|---|
| 157 | menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_ALIAS, AliasHelp); |
|---|
| 158 | |
|---|
| 159 | new_aliases: |
|---|
| 160 | |
|---|
| 161 | omax = menu->max; |
|---|
| 162 | |
|---|
| 163 | /* count the number of aliases */ |
|---|
| 164 | for (aliasp = aliases; aliasp; aliasp = aliasp->next) |
|---|
| 165 | { |
|---|
| 166 | aliasp->self->del = 0; |
|---|
| 167 | aliasp->self->tagged = 0; |
|---|
| 168 | menu->max++; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | safe_realloc (&AliasTable, menu->max * sizeof (ALIAS *)); |
|---|
| 172 | menu->data = AliasTable; |
|---|
| 173 | |
|---|
| 174 | for (i = omax, aliasp = aliases; aliasp; aliasp = aliasp->next, i++) |
|---|
| 175 | { |
|---|
| 176 | AliasTable[i] = aliasp->self; |
|---|
| 177 | aliases = aliasp; |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | if ((SortAlias & SORT_MASK) != SORT_ORDER) |
|---|
| 181 | { |
|---|
| 182 | qsort (AliasTable, i, sizeof (ALIAS *), |
|---|
| 183 | (SortAlias & SORT_MASK) == SORT_ADDRESS ? alias_SortAddress : alias_SortAlias); |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | for (i=0; i<menu->max; i++) AliasTable[i]->num = i; |
|---|
| 187 | |
|---|
| 188 | while (!done) |
|---|
| 189 | { |
|---|
| 190 | if (aliases->next) |
|---|
| 191 | { |
|---|
| 192 | menu->redraw |= REDRAW_FULL; |
|---|
| 193 | aliases = aliases->next; |
|---|
| 194 | goto new_aliases; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | switch ((op = mutt_menuLoop (menu))) |
|---|
| 198 | { |
|---|
| 199 | case OP_DELETE: |
|---|
| 200 | case OP_UNDELETE: |
|---|
| 201 | if (menu->tagprefix) |
|---|
| 202 | { |
|---|
| 203 | for (i = 0; i < menu->max; i++) |
|---|
| 204 | if (AliasTable[i]->tagged) |
|---|
| 205 | AliasTable[i]->del = (op == OP_DELETE) ? 1 : 0; |
|---|
| 206 | menu->redraw |= REDRAW_INDEX; |
|---|
| 207 | } |
|---|
| 208 | else |
|---|
| 209 | { |
|---|
| 210 | AliasTable[menu->current]->self->del = (op == OP_DELETE) ? 1 : 0; |
|---|
| 211 | menu->redraw |= REDRAW_CURRENT; |
|---|
| 212 | if (option (OPTRESOLVE) && menu->current < menu->max - 1) |
|---|
| 213 | { |
|---|
| 214 | menu->current++; |
|---|
| 215 | menu->redraw |= REDRAW_INDEX; |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | break; |
|---|
| 219 | case OP_GENERIC_SELECT_ENTRY: |
|---|
| 220 | t = menu->current; |
|---|
| 221 | case OP_EXIT: |
|---|
| 222 | done = 1; |
|---|
| 223 | break; |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | for (i = 0; i < menu->max; i++) |
|---|
| 228 | { |
|---|
| 229 | if (AliasTable[i]->tagged) |
|---|
| 230 | { |
|---|
| 231 | mutt_addrlist_to_local (AliasTable[i]->addr); |
|---|
| 232 | rfc822_write_address (buf, buflen, AliasTable[i]->addr, 0); |
|---|
| 233 | t = -1; |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if(t != -1) |
|---|
| 238 | { |
|---|
| 239 | mutt_addrlist_to_local (AliasTable[t]->addr); |
|---|
| 240 | rfc822_write_address (buf, buflen, AliasTable[t]->addr, 0); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | mutt_menuDestroy (&menu); |
|---|
| 244 | FREE (&AliasTable); |
|---|
| 245 | |
|---|
| 246 | } |
|---|