Merge branch 'master' into nohook
[apps/madmutt.git] / account.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-5 Brendan Cully <brendan@kublai.com>
4  *
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.
8  */
9
10 /* remote host account manipulation (POP/IMAP) */
11
12 #include <lib-lib/lib-lib.h>
13 #include <lib-ui/lib-ui.h>
14
15 #include "mutt.h"
16 #include "account.h"
17
18 /* mutt_account_match: compare account info (host/port/user/login) */
19 int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2)
20 {
21     const char* user  = NONULL(mod_core.username);
22
23     if (a1->type != a2->type)
24         return 0;
25     if (ascii_strcasecmp (a1->host, a2->host))
26         return 0;
27     if (a1->port != a2->port)
28         return 0;
29
30     if (a1->type == M_ACCT_TYPE_IMAP && !m_strisempty(ImapUser))
31         user = ImapUser;
32
33     if (a1->type == M_ACCT_TYPE_POP && !m_strisempty(PopUser))
34         user = PopUser;
35
36     if (a1->has_user && a2->has_user)
37         return !m_strcmp(a1->user, a2->user);
38     if (a1->has_user)
39         return !m_strcmp(a1->user, user);
40     if (a2->has_user)
41         return !m_strcmp(a2->user, user);
42
43     return 1;
44 }
45
46 /* mutt_account_fromurl: fill account with information from url. */
47 int mutt_account_fromurl(ACCOUNT *account, ciss_url_t *url)
48 {
49     /* must be present */
50     if (!url->host)
51         return -1;
52     m_strcpy(account->host, sizeof(account->host), url->host);
53
54     if (url->user) {
55         m_strcpy(account->user, sizeof(account->user), url->user);
56         account->has_user = 1;
57     }
58     if (url->pass) {
59         m_strcpy(account->pass, sizeof(account->pass), url->pass);
60         account->has_pass = 1;
61     }
62     if (url->port) {
63         account->port = url->port;
64         account->has_port = 1;
65     }
66
67     return 0;
68 }
69
70 /* mutt_account_tourl: fill URL with info from account. The URL information
71  *   is a set of pointers into account - don't free or edit account until
72  *   you've finished with url (make a copy of account if you need it for
73  *   a while). */
74 void mutt_account_tourl(ACCOUNT *account, ciss_url_t *url)
75 {
76     url->scheme = U_UNKNOWN;
77     url->user = NULL;
78     url->pass = NULL;
79     url->port = 0;
80
81     if (account->type == M_ACCT_TYPE_IMAP) {
82         url->scheme = account->has_ssl ? U_IMAPS : U_IMAP;
83     }
84
85     if (account->type == M_ACCT_TYPE_POP) {
86         url->scheme = account->has_ssl ? U_POPS : U_POP;
87     }
88
89     url->host = account->host;
90     if (account->has_port)
91         url->port = account->port;
92     if (account->has_user)
93         url->user = account->user;
94     if (account->has_pass)
95         url->pass = account->pass;
96 }
97
98 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
99 int mutt_account_getuser (ACCOUNT * account)
100 {
101     char prompt[STRING];
102
103     /* already set */
104     if (account->has_user)
105         return 0;
106     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser))
107         m_strcpy(account->user, sizeof(account->user), ImapUser);
108     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopUser))
109         m_strcpy(account->user, sizeof(account->user), PopUser);
110     /* prompt (defaults to unix username), copy into account->user */
111     else {
112         snprintf(prompt, sizeof(prompt), _("Username at %s: "), account->host);
113         m_strcpy(account->user, sizeof(account->user), NONULL(mod_core.username));
114         if (mutt_get_field_unbuffered(prompt, account->user,
115                                       sizeof(account->user), 0))
116             return -1;
117     }
118
119     account->has_user = 1;
120
121     return 0;
122 }
123
124 int mutt_account_getlogin (ACCOUNT* account)
125 {
126     /* already set */
127     if (account->has_login)
128         return 0;
129
130     if (account->type == M_ACCT_TYPE_IMAP && !m_strisempty(ImapLogin)) {
131         m_strcpy(account->login, sizeof(account->login), ImapLogin);
132         account->has_login = 1;
133     } else {
134         mutt_account_getuser (account);
135         m_strcpy(account->login, sizeof(account->login), account->user);
136     }
137
138     account->has_login = 1;
139
140     return 0;
141 }
142
143 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
144 int mutt_account_getpass (ACCOUNT * account)
145 {
146     char prompt[STRING];
147
148     if (account->has_pass)
149         return 0;
150     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass))
151         m_strcpy(account->pass, sizeof(account->pass), ImapPass);
152     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopPass))
153         m_strcpy(account->pass, sizeof(account->pass), PopPass);
154     else {
155         snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
156                  account->has_login ? account->login : account->user,
157                  account->host);
158         account->pass[0] = '\0';
159         if (mutt_get_field_unbuffered(prompt, account->pass,
160                                       sizeof(account->pass), M_PASS))
161             return -1;
162     }
163
164     account->has_pass = 1;
165
166     return 0;
167 }