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