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);
32 const char* login = NONULL (Username);
35 if (a1->type != a2->type)
37 if (ascii_strcasecmp (a1->host, a2->host))
39 if (a1->port != a2->port)
43 if (a1->type == M_ACCT_TYPE_IMAP) {
44 if (ImapUser && (ImapUser[0] != '\0'))
46 if (ImapLogin && (ImapLogin[0] != '\0'))
52 if (a1->type == M_ACCT_TYPE_POP && PopUser)
57 if (a1->type == M_ACCT_TYPE_NNTP && NntpUser)
61 if (a1->flags & a2->flags & M_ACCT_USER)
62 return (!m_strcmp(a1->user, a2->user));
63 if (a1->flags & M_ACCT_USER)
64 return (!m_strcmp(a1->user, user));
65 if (a2->flags & M_ACCT_USER)
66 return (!m_strcmp(a2->user, user));
71 /* mutt_account_fromurl: fill account with information from url. */
72 int mutt_account_fromurl(ACCOUNT *account, ciss_url_t *url)
77 m_strcpy(account->host, sizeof(account->host), url->host);
80 m_strcpy(account->user, sizeof(account->user), url->user);
81 account->flags |= M_ACCT_USER;
84 m_strcpy(account->pass, sizeof(account->pass), url->pass);
85 account->flags |= M_ACCT_PASS;
88 account->port = url->port;
89 account->flags |= M_ACCT_PORT;
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
99 void mutt_account_tourl (ACCOUNT * account, ciss_url_t * url)
101 url->scheme = U_UNKNOWN;
107 if (account->type == M_ACCT_TYPE_IMAP) {
108 if (account->flags & M_ACCT_SSL)
109 url->scheme = U_IMAPS;
111 url->scheme = U_IMAP;
116 if (account->type == M_ACCT_TYPE_POP) {
117 if (account->flags & M_ACCT_SSL)
118 url->scheme = U_POPS;
125 if (account->type == M_ACCT_TYPE_NNTP) {
126 if (account->flags & M_ACCT_SSL)
127 url->scheme = U_NNTPS;
129 url->scheme = U_NNTP;
133 url->host = account->host;
134 if (account->flags & M_ACCT_PORT)
135 url->port = account->port;
136 if (account->flags & M_ACCT_USER)
137 url->user = account->user;
138 if (account->flags & M_ACCT_PASS)
139 url->pass = account->pass;
142 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
143 int mutt_account_getuser (ACCOUNT * account)
145 char prompt[SHORT_STRING];
148 if (account->flags & M_ACCT_USER)
151 else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser))
152 m_strcpy(account->user, sizeof(account->user), ImapUser);
155 else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopUser))
156 m_strcpy(account->user, sizeof(account->user), PopUser);
159 else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpUser))
160 m_strcpy(account->user, sizeof(account->user), NntpUser);
162 /* prompt (defaults to unix username), copy into account->user */
164 snprintf(prompt, sizeof(prompt), _("Username at %s: "), account->host);
165 m_strcpy(account->user, sizeof(account->user), NONULL(Username));
166 if (mutt_get_field_unbuffered(prompt, account->user,
167 sizeof(account->user), 0))
171 account->flags |= M_ACCT_USER;
176 int mutt_account_getlogin (ACCOUNT* account)
179 if (account->flags & M_ACCT_LOGIN)
182 else if (account->type == M_ACCT_TYPE_IMAP)
184 if (!m_strisempty(ImapLogin)) {
185 m_strcpy(account->login, sizeof(account->login), ImapLogin);
186 account->flags |= M_ACCT_LOGIN;
191 if (!(account->flags & M_ACCT_LOGIN)) {
192 mutt_account_getuser (account);
193 m_strcpy(account->login, sizeof(account->login), account->user);
196 account->flags |= M_ACCT_LOGIN;
201 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
202 int mutt_account_getpass (ACCOUNT * account)
204 char prompt[SHORT_STRING];
206 if (account->flags & M_ACCT_PASS)
209 else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass))
210 m_strcpy(account->pass, sizeof(account->pass), ImapPass);
213 else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopPass))
214 m_strcpy(account->pass, sizeof(account->pass), PopPass);
217 else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpPass))
218 m_strcpy(account->pass, sizeof(account->pass), NntpPass);
221 snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
222 account->flags & M_ACCT_LOGIN ? account->login : account->user,
224 account->pass[0] = '\0';
225 if (mutt_get_field_unbuffered(prompt, account->pass,
226 sizeof(account->pass), M_PASS))
230 account->flags |= M_ACCT_PASS;
235 void mutt_account_unsetpass (ACCOUNT * account)
237 account->flags &= !M_ACCT_PASS;