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