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