fix parsing issues in slurp_newsrc
[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(mod_core.username);
23     const char* login = NONULL(mod_core.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->has_user && a2->has_user)
48         return !m_strcmp(a1->user, a2->user);
49     if (a1->has_user)
50         return !m_strcmp(a1->user, user);
51     if (a2->has_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->has_user = 1;
68     }
69     if (url->pass) {
70         m_strcpy(account->pass, sizeof(account->pass), url->pass);
71         account->has_pass = 1;
72     }
73     if (url->port) {
74         account->port = url->port;
75         account->has_port = 1;
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         url->scheme = account->has_ssl ? U_IMAPS : U_IMAP;
94     }
95
96     if (account->type == M_ACCT_TYPE_POP) {
97         url->scheme = account->has_ssl ? U_POPS : U_POP;
98     }
99
100 #ifdef USE_NNTP
101     if (account->type == M_ACCT_TYPE_NNTP) {
102         url->scheme = account->has_ssl ? U_NNTPS : U_NNTP;
103     }
104 #endif
105
106     url->host = account->host;
107     if (account->has_port)
108         url->port = account->port;
109     if (account->has_user)
110         url->user = account->user;
111     if (account->has_pass)
112         url->pass = account->pass;
113 }
114
115 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
116 int mutt_account_getuser (ACCOUNT * account)
117 {
118     char prompt[STRING];
119
120     /* already set */
121     if (account->has_user)
122         return 0;
123     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser))
124         m_strcpy(account->user, sizeof(account->user), ImapUser);
125     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopUser))
126         m_strcpy(account->user, sizeof(account->user), PopUser);
127 #ifdef USE_NNTP
128     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpUser))
129         m_strcpy(account->user, sizeof(account->user), NntpUser);
130 #endif
131     /* prompt (defaults to unix username), copy into account->user */
132     else {
133         snprintf(prompt, sizeof(prompt), _("Username at %s: "), account->host);
134         m_strcpy(account->user, sizeof(account->user), NONULL(mod_core.username));
135         if (mutt_get_field_unbuffered(prompt, account->user,
136                                       sizeof(account->user), 0))
137             return -1;
138     }
139
140     account->has_user = 1;
141
142     return 0;
143 }
144
145 int mutt_account_getlogin (ACCOUNT* account)
146 {
147     /* already set */
148     if (account->has_login)
149         return 0;
150     else if (account->type == M_ACCT_TYPE_IMAP)
151     {
152         if (!m_strisempty(ImapLogin)) {
153             m_strcpy(account->login, sizeof(account->login), ImapLogin);
154             account->has_login = 1;
155         }
156     }
157
158     if (!account->has_login) {
159         mutt_account_getuser (account);
160         m_strcpy(account->login, sizeof(account->login), account->user);
161     }
162
163     account->has_login = 1;
164
165     return 0;
166 }
167
168 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
169 int mutt_account_getpass (ACCOUNT * account)
170 {
171     char prompt[STRING];
172
173     if (account->has_pass)
174         return 0;
175     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass))
176         m_strcpy(account->pass, sizeof(account->pass), ImapPass);
177     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopPass))
178         m_strcpy(account->pass, sizeof(account->pass), PopPass);
179 #ifdef USE_NNTP
180     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpPass))
181         m_strcpy(account->pass, sizeof(account->pass), NntpPass);
182 #endif
183     else {
184         snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
185                  account->has_login ? account->login : account->user,
186                  account->host);
187         account->pass[0] = '\0';
188         if (mutt_get_field_unbuffered(prompt, account->pass,
189                                       sizeof(account->pass), M_PASS))
190             return -1;
191     }
192
193     account->has_pass = 1;
194
195     return 0;
196 }