a85782921c3b6ebd4c62966c5ca17c36bd3e644a
[apps/madmutt.git] / account.c
1 /*
2  * Copyright (C) 2000-3 Brendan Cully <brendan@kublai.com>
3  * 
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  * 
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  * 
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /* remote host account manipulation (POP/IMAP) */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "mutt.h"
26 #include "account.h"
27 #include "url.h"
28
29 /* mutt_account_match: compare account info (host/port/user) */
30 int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2)
31 {
32   const char *user = 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 && ImapUser)
43     user = ImapUser;
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 (!strcmp (a1->user, a2->user));
58   if (a1->flags & M_ACCT_USER)
59     return (!strcmp (a1->user, user));
60   if (a2->flags & M_ACCT_USER)
61     return (!strcmp (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 neccessary */
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 (prompt, account->user, sizeof (account->user), 0))
163       return -1;
164   }
165
166   account->flags |= M_ACCT_USER;
167
168   return 0;
169 }
170
171 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
172 int mutt_account_getpass (ACCOUNT * account)
173 {
174   char prompt[SHORT_STRING];
175
176   if (account->flags & M_ACCT_PASS)
177     return 0;
178 #ifdef USE_IMAP
179   else if ((account->type == M_ACCT_TYPE_IMAP) && ImapPass)
180     strfcpy (account->pass, ImapPass, sizeof (account->pass));
181 #endif
182 #ifdef USE_POP
183   else if ((account->type == M_ACCT_TYPE_POP) && PopPass)
184     strfcpy (account->pass, PopPass, sizeof (account->pass));
185 #endif
186 #ifdef USE_NNTP
187   else if ((account->type == M_ACCT_TYPE_NNTP) && NntpPass)
188     strfcpy (account->pass, NntpPass, sizeof (account->pass));
189 #endif
190   else {
191     snprintf (prompt, sizeof (prompt), _("Password for %s@%s: "),
192               account->user, account->host);
193     account->pass[0] = '\0';
194     if (mutt_get_password (prompt, account->pass, sizeof (account->pass)))
195       return -1;
196   }
197
198   account->flags |= M_ACCT_PASS;
199
200   return 0;
201 }
202
203 void mutt_account_unsetpass (ACCOUNT * account)
204 {
205   account->flags &= !M_ACCT_PASS;
206 }