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