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