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