drop some useless crap.
[apps/madmutt.git] / account.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 2000-5 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 #include <lib-lib/lib-lib.h>
13 #include <lib-ui/enter.h>
14 #include <lib-ui/curses.h>
15
16 #include "mutt.h"
17 #include "account.h"
18
19 /* mutt_account_match: compare account info (host/port/user/login) */
20 int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2)
21 {
22     const char* user = NONULL (Username);
23     const char* login = 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     if (a1->type == M_ACCT_TYPE_IMAP) {
33         if (ImapUser && (ImapUser[0] != '\0'))
34             user = ImapUser;
35         if (ImapLogin && (ImapLogin[0] != '\0'))
36             login = ImapLogin;
37     }
38
39     if (a1->type == M_ACCT_TYPE_POP && PopUser)
40         user = PopUser;
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 (!m_strcmp(a1->user, a2->user));
49     if (a1->flags & M_ACCT_USER)
50         return (!m_strcmp(a1->user, user));
51     if (a2->flags & M_ACCT_USER)
52         return (!m_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         return -1;
63     m_strcpy(account->host, sizeof(account->host), url->host);
64
65     if (url->user) {
66         m_strcpy(account->user, sizeof(account->user), url->user);
67         account->flags |= M_ACCT_USER;
68     }
69     if (url->pass) {
70         m_strcpy(account->pass, sizeof(account->pass), url->pass);
71         account->flags |= M_ACCT_PASS;
72     }
73     if (url->port) {
74         account->port = url->port;
75         account->flags |= M_ACCT_PORT;
76     }
77
78     return 0;
79 }
80
81 /* mutt_account_tourl: fill URL with info from account. The URL information
82  *   is a set of pointers into account - don't free or edit account until
83  *   you've finished with url (make a copy of account if you need it for
84  *   a while). */
85 void mutt_account_tourl (ACCOUNT * account, ciss_url_t * url)
86 {
87     url->scheme = U_UNKNOWN;
88     url->user = NULL;
89     url->pass = NULL;
90     url->port = 0;
91
92     if (account->type == M_ACCT_TYPE_IMAP) {
93         if (account->flags & M_ACCT_SSL)
94             url->scheme = U_IMAPS;
95         else
96             url->scheme = U_IMAP;
97     }
98
99     if (account->type == M_ACCT_TYPE_POP) {
100         if (account->flags & M_ACCT_SSL)
101             url->scheme = U_POPS;
102         else
103             url->scheme = U_POP;
104     }
105
106 #ifdef USE_NNTP
107     if (account->type == M_ACCT_TYPE_NNTP) {
108         if (account->flags & M_ACCT_SSL)
109             url->scheme = U_NNTPS;
110         else
111             url->scheme = U_NNTP;
112     }
113 #endif
114
115     url->host = account->host;
116     if (account->flags & M_ACCT_PORT)
117         url->port = account->port;
118     if (account->flags & M_ACCT_USER)
119         url->user = account->user;
120     if (account->flags & M_ACCT_PASS)
121         url->pass = account->pass;
122 }
123
124 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
125 int mutt_account_getuser (ACCOUNT * account)
126 {
127     char prompt[STRING];
128
129     /* already set */
130     if (account->flags & M_ACCT_USER)
131         return 0;
132     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser))
133         m_strcpy(account->user, sizeof(account->user), ImapUser);
134     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopUser))
135         m_strcpy(account->user, sizeof(account->user), PopUser);
136 #ifdef USE_NNTP
137     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpUser))
138         m_strcpy(account->user, sizeof(account->user), NntpUser);
139 #endif
140     /* prompt (defaults to unix username), copy into account->user */
141     else {
142         snprintf(prompt, sizeof(prompt), _("Username at %s: "), account->host);
143         m_strcpy(account->user, sizeof(account->user), NONULL(Username));
144         if (mutt_get_field_unbuffered(prompt, account->user,
145                                       sizeof(account->user), 0))
146             return -1;
147     }
148
149     account->flags |= M_ACCT_USER;
150
151     return 0;
152 }
153
154 int mutt_account_getlogin (ACCOUNT* account)
155 {
156     /* already set */
157     if (account->flags & M_ACCT_LOGIN)
158         return 0;
159     else if (account->type == M_ACCT_TYPE_IMAP)
160     {
161         if (!m_strisempty(ImapLogin)) {
162             m_strcpy(account->login, sizeof(account->login), ImapLogin);
163             account->flags |= M_ACCT_LOGIN;
164         }
165     }
166
167     if (!(account->flags & M_ACCT_LOGIN)) {
168         mutt_account_getuser (account);
169         m_strcpy(account->login, sizeof(account->login), account->user);
170     }
171
172     account->flags |= M_ACCT_LOGIN;
173
174     return 0;
175 }
176
177 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
178 int mutt_account_getpass (ACCOUNT * account)
179 {
180     char prompt[STRING];
181
182     if (account->flags & M_ACCT_PASS)
183         return 0;
184     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass))
185         m_strcpy(account->pass, sizeof(account->pass), ImapPass);
186     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopPass))
187         m_strcpy(account->pass, sizeof(account->pass), PopPass);
188 #ifdef USE_NNTP
189     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpPass))
190         m_strcpy(account->pass, sizeof(account->pass), NntpPass);
191 #endif
192     else {
193         snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
194                  account->flags & M_ACCT_LOGIN ? account->login : account->user,
195                  account->host);
196         account->pass[0] = '\0';
197         if (mutt_get_field_unbuffered(prompt, account->pass,
198                                       sizeof(account->pass), M_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 }