Andreas Krennmair:
[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 #include "mutt.h"
22 #include "account.h"
23 #include "url.h"
24
25 /* mutt_account_match: compare account info (host/port/user) */
26 int mutt_account_match (const ACCOUNT* a1, const ACCOUNT* a2)
27 {
28   const char* user = NONULL (Username);
29
30   if (a1->type != a2->type)
31     return 0;
32   if (ascii_strcasecmp (a1->host, a2->host))
33     return 0;
34   if (a1->port != a2->port)
35     return 0;
36
37 #ifdef USE_IMAP
38   if (a1->type == M_ACCT_TYPE_IMAP && ImapUser)
39     user = ImapUser;
40 #endif
41
42 #ifdef USE_POP
43   if (a1->type == M_ACCT_TYPE_POP && PopUser)
44     user = PopUser;
45 #endif
46   
47 #ifdef USE_NNTP
48   if (a1->type == M_ACCT_TYPE_NNTP && NntpUser)
49     user = NntpUser;
50 #endif
51
52   if (a1->flags & a2->flags & M_ACCT_USER)
53     return (!strcmp (a1->user, a2->user));
54   if (a1->flags & M_ACCT_USER)
55     return (!strcmp (a1->user, user));
56   if (a2->flags & M_ACCT_USER)
57     return (!strcmp (a2->user, user));
58
59   return 1;
60 }
61
62 /* mutt_account_fromurl: fill account with information from url. */
63 int mutt_account_fromurl (ACCOUNT* account, ciss_url_t* url)
64 {
65   /* must be present */
66   if (url->host)
67     strfcpy (account->host, url->host, sizeof (account->host));
68   else
69     return -1;
70
71   if (url->user)
72   {
73     strfcpy (account->user, url->user, sizeof (account->user));
74     account->flags |= M_ACCT_USER;
75   }
76   if (url->pass)
77   {
78     strfcpy (account->pass, url->pass, sizeof (account->pass));
79     account->flags |= M_ACCT_PASS;
80   }
81   if (url->port)
82   {
83     account->port = url->port;
84     account->flags |= M_ACCT_PORT;
85   }
86
87   return 0;
88 }
89
90 /* mutt_account_tourl: fill URL with info from account. The URL information
91  *   is a set of pointers into account - don't free or edit account until
92  *   you've finished with url (make a copy of account if you need it for
93  *   a while). */
94 void mutt_account_tourl (ACCOUNT* account, ciss_url_t* url)
95 {
96   url->scheme = U_UNKNOWN;
97   url->user = NULL;
98   url->pass = NULL;
99   url->port = 0;
100
101 #ifdef USE_IMAP
102   if (account->type == M_ACCT_TYPE_IMAP)
103   {
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   {
114     if (account->flags & M_ACCT_SSL)
115       url->scheme = U_POPS;
116     else
117       url->scheme = U_POP;
118   }
119 #endif
120
121 #ifdef USE_NNTP
122   if (account->type == M_ACCT_TYPE_NNTP)
123   {
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 neccessary */
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   {
163     snprintf (prompt, sizeof (prompt), _("Username at %s: "), account->host);
164     strfcpy (account->user, NONULL (Username), sizeof (account->user));
165     if (mutt_get_field (prompt, account->user, sizeof (account->user), 0))
166       return -1;
167   }
168
169   account->flags |= M_ACCT_USER;
170
171   return 0;
172 }
173
174 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
175 int mutt_account_getpass (ACCOUNT* account)
176 {
177   char prompt[SHORT_STRING];
178
179   if (account->flags & M_ACCT_PASS)
180     return 0;
181 #ifdef USE_IMAP
182   else if ((account->type == M_ACCT_TYPE_IMAP) && ImapPass)
183     strfcpy (account->pass, ImapPass, sizeof (account->pass));
184 #endif
185 #ifdef USE_POP
186   else if ((account->type == M_ACCT_TYPE_POP) && PopPass)
187     strfcpy (account->pass, PopPass, sizeof (account->pass));
188 #endif
189 #ifdef USE_NNTP
190   else if ((account->type == M_ACCT_TYPE_NNTP) && NntpPass)
191     strfcpy (account->pass, NntpPass, sizeof (account->pass));
192 #endif
193   else
194   {
195     snprintf (prompt, sizeof (prompt), _("Password for %s@%s: "),
196       account->user, account->host);
197     account->pass[0] = '\0';
198     if (mutt_get_password (prompt, account->pass, sizeof (account->pass)))
199       return -1;
200   }
201
202   account->flags |= M_ACCT_PASS;
203
204   return 0;
205 }
206
207 void mutt_account_unsetpass (ACCOUNT* account)
208 {
209   account->flags &= !M_ACCT_PASS;
210 }