2 * Copyright notice from original mutt:
3 * Copyright (C) 2000-5 Brendan Cully <brendan@kublai.com>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* remote host account manipulation (POP/IMAP) */
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/ascii.h>
19 #include <lib-lib/macros.h>
27 /* mutt_account_match: compare account info (host/port/user/login) */
28 int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2)
30 const char* user = NONULL (Username);
31 const char* login = NONULL (Username);
33 if (a1->type != a2->type)
35 if (ascii_strcasecmp (a1->host, a2->host))
37 if (a1->port != a2->port)
41 if (a1->type == M_ACCT_TYPE_IMAP) {
42 if (ImapUser && (ImapUser[0] != '\0'))
44 if (ImapLogin && (ImapLogin[0] != '\0'))
50 if (a1->type == M_ACCT_TYPE_POP && PopUser)
55 if (a1->type == M_ACCT_TYPE_NNTP && NntpUser)
59 if (a1->flags & a2->flags & M_ACCT_USER)
60 return (!str_cmp (a1->user, a2->user));
61 if (a1->flags & M_ACCT_USER)
62 return (!str_cmp (a1->user, user));
63 if (a2->flags & M_ACCT_USER)
64 return (!str_cmp (a2->user, user));
69 /* mutt_account_fromurl: fill account with information from url. */
70 int mutt_account_fromurl (ACCOUNT * account, ciss_url_t * url)
74 strfcpy (account->host, url->host, sizeof (account->host));
79 strfcpy (account->user, url->user, sizeof (account->user));
80 account->flags |= M_ACCT_USER;
83 strfcpy (account->pass, url->pass, sizeof (account->pass));
84 account->flags |= M_ACCT_PASS;
87 account->port = url->port;
88 account->flags |= M_ACCT_PORT;
94 /* mutt_account_tourl: fill URL with info from account. The URL information
95 * is a set of pointers into account - don't free or edit account until
96 * you've finished with url (make a copy of account if you need it for
98 void mutt_account_tourl (ACCOUNT * account, ciss_url_t * url)
100 url->scheme = U_UNKNOWN;
106 if (account->type == M_ACCT_TYPE_IMAP) {
107 if (account->flags & M_ACCT_SSL)
108 url->scheme = U_IMAPS;
110 url->scheme = U_IMAP;
115 if (account->type == M_ACCT_TYPE_POP) {
116 if (account->flags & M_ACCT_SSL)
117 url->scheme = U_POPS;
124 if (account->type == M_ACCT_TYPE_NNTP) {
125 if (account->flags & M_ACCT_SSL)
126 url->scheme = U_NNTPS;
128 url->scheme = U_NNTP;
132 url->host = account->host;
133 if (account->flags & M_ACCT_PORT)
134 url->port = account->port;
135 if (account->flags & M_ACCT_USER)
136 url->user = account->user;
137 if (account->flags & M_ACCT_PASS)
138 url->pass = account->pass;
141 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
142 int mutt_account_getuser (ACCOUNT * account)
144 char prompt[SHORT_STRING];
147 if (account->flags & M_ACCT_USER)
150 else if ((account->type == M_ACCT_TYPE_IMAP) && ImapUser && (ImapUser[0] != '\0'))
151 strfcpy (account->user, ImapUser, sizeof (account->user));
154 else if ((account->type == M_ACCT_TYPE_POP) && PopUser)
155 strfcpy (account->user, PopUser, sizeof (account->user));
158 else if ((account->type == M_ACCT_TYPE_NNTP) && NntpUser)
159 strfcpy (account->user, NntpUser, sizeof (account->user));
161 /* prompt (defaults to unix username), copy into account->user */
163 snprintf (prompt, sizeof (prompt), _("Username at %s: "), account->host);
164 strfcpy (account->user, NONULL (Username), sizeof (account->user));
165 if (mutt_get_field_unbuffered (prompt, account->user,
166 sizeof (account->user), 0))
170 account->flags |= M_ACCT_USER;
175 int mutt_account_getlogin (ACCOUNT* account)
178 if (account->flags & M_ACCT_LOGIN)
181 else if (account->type == M_ACCT_TYPE_IMAP)
183 if (ImapLogin && (ImapLogin[0] != '\0')) {
184 strfcpy (account->login, ImapLogin, sizeof (account->login));
185 account->flags |= M_ACCT_LOGIN;
190 if (!(account->flags & M_ACCT_LOGIN)) {
191 mutt_account_getuser (account);
192 strfcpy (account->login, account->user, sizeof (account->login));
195 account->flags |= M_ACCT_LOGIN;
200 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
201 int mutt_account_getpass (ACCOUNT * account)
203 char prompt[SHORT_STRING];
205 if (account->flags & M_ACCT_PASS)
208 else if ((account->type == M_ACCT_TYPE_IMAP) && ImapPass)
209 strfcpy (account->pass, ImapPass, sizeof (account->pass));
212 else if ((account->type == M_ACCT_TYPE_POP) && PopPass)
213 strfcpy (account->pass, PopPass, sizeof (account->pass));
216 else if ((account->type == M_ACCT_TYPE_NNTP) && NntpPass)
217 strfcpy (account->pass, NntpPass, sizeof (account->pass));
220 snprintf (prompt, sizeof (prompt), _("Password for %s@%s: "),
221 account->flags & M_ACCT_LOGIN ? account->login : account->user,
223 account->pass[0] = '\0';
224 if (mutt_get_field_unbuffered (prompt, account->pass,
225 sizeof (account->pass), M_PASS))
229 account->flags |= M_ACCT_PASS;
234 void mutt_account_unsetpass (ACCOUNT * account)
236 account->flags &= !M_ACCT_PASS;