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