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