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