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