Nico Golde:
[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   {
77     strfcpy (account->user, url->user, sizeof (account->user));
78     account->flags |= M_ACCT_USER;
79   }
80   if (url->pass)
81   {
82     strfcpy (account->pass, url->pass, sizeof (account->pass));
83     account->flags |= M_ACCT_PASS;
84   }
85   if (url->port)
86   {
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   {
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   {
118     if (account->flags & M_ACCT_SSL)
119       url->scheme = U_POPS;
120     else
121       url->scheme = U_POP;
122   }
123 #endif
124
125 #ifdef USE_NNTP
126   if (account->type == M_ACCT_TYPE_NNTP)
127   {
128     if (account->flags & M_ACCT_SSL)
129       url->scheme = U_NNTPS;
130     else
131       url->scheme = U_NNTP;
132   }
133 #endif
134
135   url->host = account->host;
136   if (account->flags & M_ACCT_PORT)
137     url->port = account->port;
138   if (account->flags & M_ACCT_USER)
139     url->user = account->user;
140   if (account->flags & M_ACCT_PASS)
141     url->pass = account->pass;
142 }
143
144 /* mutt_account_getuser: retrieve username into ACCOUNT, if neccessary */
145 int mutt_account_getuser (ACCOUNT* account)
146 {
147   char prompt[SHORT_STRING];
148
149   /* already set */
150   if (account->flags & M_ACCT_USER)
151     return 0;
152 #ifdef USE_IMAP
153   else if ((account->type == M_ACCT_TYPE_IMAP) && ImapUser)
154     strfcpy (account->user, ImapUser, sizeof (account->user));
155 #endif
156 #ifdef USE_POP
157   else if ((account->type == M_ACCT_TYPE_POP) && PopUser)
158     strfcpy (account->user, PopUser, sizeof (account->user));
159 #endif
160 #ifdef USE_NNTP
161   else if ((account->type == M_ACCT_TYPE_NNTP) && NntpUser)
162     strfcpy (account->user, NntpUser, sizeof (account->user));
163 #endif
164   /* prompt (defaults to unix username), copy into account->user */
165   else
166   {
167     snprintf (prompt, sizeof (prompt), _("Username at %s: "), account->host);
168     strfcpy (account->user, NONULL (Username), sizeof (account->user));
169     if (mutt_get_field (prompt, account->user, sizeof (account->user), 0))
170       return -1;
171   }
172
173   account->flags |= M_ACCT_USER;
174
175   return 0;
176 }
177
178 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
179 int mutt_account_getpass (ACCOUNT* account)
180 {
181   char prompt[SHORT_STRING];
182
183   if (account->flags & M_ACCT_PASS)
184     return 0;
185 #ifdef USE_IMAP
186   else if ((account->type == M_ACCT_TYPE_IMAP) && ImapPass)
187     strfcpy (account->pass, ImapPass, sizeof (account->pass));
188 #endif
189 #ifdef USE_POP
190   else if ((account->type == M_ACCT_TYPE_POP) && PopPass)
191     strfcpy (account->pass, PopPass, sizeof (account->pass));
192 #endif
193 #ifdef USE_NNTP
194   else if ((account->type == M_ACCT_TYPE_NNTP) && NntpPass)
195     strfcpy (account->pass, NntpPass, sizeof (account->pass));
196 #endif
197   else
198   {
199     snprintf (prompt, sizeof (prompt), _("Password for %s@%s: "),
200       account->user, account->host);
201     account->pass[0] = '\0';
202     if (mutt_get_password (prompt, account->pass, sizeof (account->pass)))
203       return -1;
204   }
205
206   account->flags |= M_ACCT_PASS;
207
208   return 0;
209 }
210
211 void mutt_account_unsetpass (ACCOUNT* account)
212 {
213   account->flags &= !M_ACCT_PASS;
214 }