| 1 | /* |
|---|
| 2 | * Copyright (C) 2000-7 Brendan Cully <brendan@kublai.com> |
|---|
| 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 | /* remote host account manipulation (POP/IMAP) */ |
|---|
| 20 | |
|---|
| 21 | #if HAVE_CONFIG_H |
|---|
| 22 | # include "config.h" |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include "mutt.h" |
|---|
| 26 | #include "account.h" |
|---|
| 27 | #include "url.h" |
|---|
| 28 | |
|---|
| 29 | /* mutt_account_match: compare account info (host/port/user/login) */ |
|---|
| 30 | int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* a2) |
|---|
| 31 | { |
|---|
| 32 | const char* user = NONULL (Username); |
|---|
| 33 | const char* login = NONULL (Username); |
|---|
| 34 | |
|---|
| 35 | if (a1->type != a2->type) |
|---|
| 36 | return 0; |
|---|
| 37 | if (ascii_strcasecmp (a1->host, a2->host)) |
|---|
| 38 | return 0; |
|---|
| 39 | if (a1->port != a2->port) |
|---|
| 40 | return 0; |
|---|
| 41 | |
|---|
| 42 | #ifdef USE_IMAP |
|---|
| 43 | if (a1->type == M_ACCT_TYPE_IMAP) |
|---|
| 44 | { |
|---|
| 45 | if (ImapUser) |
|---|
| 46 | user = ImapUser; |
|---|
| 47 | if (ImapLogin) |
|---|
| 48 | login = ImapLogin; |
|---|
| 49 | } |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | #ifdef USE_POP |
|---|
| 53 | if (a1->type == M_ACCT_TYPE_POP && PopUser) |
|---|
| 54 | user = PopUser; |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | if (a1->flags & a2->flags & M_ACCT_USER) |
|---|
| 58 | return (!strcmp (a1->user, a2->user)); |
|---|
| 59 | if (a1->flags & M_ACCT_USER) |
|---|
| 60 | return (!strcmp (a1->user, user)); |
|---|
| 61 | if (a2->flags & M_ACCT_USER) |
|---|
| 62 | return (!strcmp (a2->user, user)); |
|---|
| 63 | |
|---|
| 64 | return 1; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /* mutt_account_fromurl: fill account with information from url. */ |
|---|
| 68 | int mutt_account_fromurl (ACCOUNT* account, ciss_url_t* url) |
|---|
| 69 | { |
|---|
| 70 | /* must be present */ |
|---|
| 71 | if (url->host) |
|---|
| 72 | strfcpy (account->host, url->host, sizeof (account->host)); |
|---|
| 73 | else |
|---|
| 74 | return -1; |
|---|
| 75 | |
|---|
| 76 | if (url->user) |
|---|
| 77 | { |
|---|
| 78 | strfcpy (account->user, url->user, sizeof (account->user)); |
|---|
| 79 | account->flags |= M_ACCT_USER; |
|---|
| 80 | } |
|---|
| 81 | if (url->pass) |
|---|
| 82 | { |
|---|
| 83 | strfcpy (account->pass, url->pass, sizeof (account->pass)); |
|---|
| 84 | account->flags |= M_ACCT_PASS; |
|---|
| 85 | } |
|---|
| 86 | if (url->port) |
|---|
| 87 | { |
|---|
| 88 | account->port = url->port; |
|---|
| 89 | account->flags |= M_ACCT_PORT; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | return 0; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | /* mutt_account_tourl: fill URL with info from account. The URL information |
|---|
| 96 | * is a set of pointers into account - don't free or edit account until |
|---|
| 97 | * you've finished with url (make a copy of account if you need it for |
|---|
| 98 | * a while). */ |
|---|
| 99 | void mutt_account_tourl (ACCOUNT* account, ciss_url_t* url) |
|---|
| 100 | { |
|---|
| 101 | url->scheme = U_UNKNOWN; |
|---|
| 102 | url->user = NULL; |
|---|
| 103 | url->pass = NULL; |
|---|
| 104 | url->port = 0; |
|---|
| 105 | |
|---|
| 106 | #ifdef USE_IMAP |
|---|
| 107 | if (account->type == M_ACCT_TYPE_IMAP) |
|---|
| 108 | { |
|---|
| 109 | if (account->flags & M_ACCT_SSL) |
|---|
| 110 | url->scheme = U_IMAPS; |
|---|
| 111 | else |
|---|
| 112 | url->scheme = U_IMAP; |
|---|
| 113 | } |
|---|
| 114 | #endif |
|---|
| 115 | |
|---|
| 116 | #ifdef USE_POP |
|---|
| 117 | if (account->type == M_ACCT_TYPE_POP) |
|---|
| 118 | { |
|---|
| 119 | if (account->flags & M_ACCT_SSL) |
|---|
| 120 | url->scheme = U_POPS; |
|---|
| 121 | else |
|---|
| 122 | url->scheme = U_POP; |
|---|
| 123 | } |
|---|
| 124 | #endif |
|---|
| 125 | |
|---|
| 126 | #ifdef USE_SMTP |
|---|
| 127 | if (account->type == M_ACCT_TYPE_SMTP) |
|---|
| 128 | { |
|---|
| 129 | if (account->flags & M_ACCT_SSL) |
|---|
| 130 | url->scheme = U_SMTPS; |
|---|
| 131 | else |
|---|
| 132 | url->scheme = U_SMTP; |
|---|
| 133 | } |
|---|
| 134 | #endif |
|---|
| 135 | |
|---|
| 136 | url->host = account->host; |
|---|
| 137 | if (account->flags & M_ACCT_PORT) |
|---|
| 138 | url->port = account->port; |
|---|
| 139 | if (account->flags & M_ACCT_USER) |
|---|
| 140 | url->user = account->user; |
|---|
| 141 | if (account->flags & M_ACCT_PASS) |
|---|
| 142 | url->pass = account->pass; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */ |
|---|
| 146 | int mutt_account_getuser (ACCOUNT* account) |
|---|
| 147 | { |
|---|
| 148 | char prompt[SHORT_STRING]; |
|---|
| 149 | |
|---|
| 150 | /* already set */ |
|---|
| 151 | if (account->flags & M_ACCT_USER) |
|---|
| 152 | return 0; |
|---|
| 153 | #ifdef USE_IMAP |
|---|
| 154 | else if ((account->type == M_ACCT_TYPE_IMAP) && ImapUser) |
|---|
| 155 | strfcpy (account->user, ImapUser, sizeof (account->user)); |
|---|
| 156 | #endif |
|---|
| 157 | #ifdef USE_POP |
|---|
| 158 | else if ((account->type == M_ACCT_TYPE_POP) && PopUser) |
|---|
| 159 | strfcpy (account->user, PopUser, sizeof (account->user)); |
|---|
| 160 | #endif |
|---|
| 161 | /* prompt (defaults to unix username), copy into account->user */ |
|---|
| 162 | else |
|---|
| 163 | { |
|---|
| 164 | snprintf (prompt, sizeof (prompt), _("Username at %s: "), account->host); |
|---|
| 165 | strfcpy (account->user, NONULL (Username), sizeof (account->user)); |
|---|
| 166 | if (mutt_get_field_unbuffered (prompt, account->user, sizeof (account->user), 0)) |
|---|
| 167 | return -1; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | account->flags |= M_ACCT_USER; |
|---|
| 171 | |
|---|
| 172 | return 0; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | int mutt_account_getlogin (ACCOUNT* account) |
|---|
| 176 | { |
|---|
| 177 | /* already set */ |
|---|
| 178 | if (account->flags & M_ACCT_LOGIN) |
|---|
| 179 | return 0; |
|---|
| 180 | #ifdef USE_IMAP |
|---|
| 181 | else if (account->type == M_ACCT_TYPE_IMAP) |
|---|
| 182 | { |
|---|
| 183 | if (ImapLogin) |
|---|
| 184 | { |
|---|
| 185 | strfcpy (account->login, ImapLogin, sizeof (account->login)); |
|---|
| 186 | account->flags |= M_ACCT_LOGIN; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | #endif |
|---|
| 190 | |
|---|
| 191 | if (!(account->flags & M_ACCT_LOGIN)) |
|---|
| 192 | { |
|---|
| 193 | mutt_account_getuser (account); |
|---|
| 194 | strfcpy (account->login, account->user, sizeof (account->login)); |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | account->flags |= M_ACCT_LOGIN; |
|---|
| 198 | |
|---|
| 199 | return 0; |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /* mutt_account_getpass: fetch password into ACCOUNT, if necessary */ |
|---|
| 203 | int mutt_account_getpass (ACCOUNT* account) |
|---|
| 204 | { |
|---|
| 205 | char prompt[SHORT_STRING]; |
|---|
| 206 | |
|---|
| 207 | if (account->flags & M_ACCT_PASS) |
|---|
| 208 | return 0; |
|---|
| 209 | #ifdef USE_IMAP |
|---|
| 210 | else if ((account->type == M_ACCT_TYPE_IMAP) && ImapPass) |
|---|
| 211 | strfcpy (account->pass, ImapPass, sizeof (account->pass)); |
|---|
| 212 | #endif |
|---|
| 213 | #ifdef USE_POP |
|---|
| 214 | else if ((account->type == M_ACCT_TYPE_POP) && PopPass) |
|---|
| 215 | strfcpy (account->pass, PopPass, sizeof (account->pass)); |
|---|
| 216 | #endif |
|---|
| 217 | #ifdef USE_SMTP |
|---|
| 218 | else if ((account->type == M_ACCT_TYPE_SMTP) && SmtpPass) |
|---|
| 219 | strfcpy (account->pass, SmtpPass, sizeof (account->pass)); |
|---|
| 220 | #endif |
|---|
| 221 | else |
|---|
| 222 | { |
|---|
| 223 | snprintf (prompt, sizeof (prompt), _("Password for %s@%s: "), |
|---|
| 224 | account->flags & M_ACCT_LOGIN ? account->login : account->user, |
|---|
| 225 | account->host); |
|---|
| 226 | account->pass[0] = '\0'; |
|---|
| 227 | if (mutt_get_password (prompt, account->pass, sizeof (account->pass))) |
|---|
| 228 | return -1; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | account->flags |= M_ACCT_PASS; |
|---|
| 232 | |
|---|
| 233 | return 0; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | void mutt_account_unsetpass (ACCOUNT* account) |
|---|
| 237 | { |
|---|
| 238 | account->flags &= ~M_ACCT_PASS; |
|---|
| 239 | } |
|---|