move signal.c into lib/sys
[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 #if HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/ascii.h>
19 #include <lib-lib/macros.h>
20
21 #include <lib-ui/enter.h>
22
23 #include "mutt.h"
24 #include "account.h"
25 #include "url.h"
26
27
28 /* mutt_account_match: compare account info (host/port/user/login) */
29 int mutt_account_match (const ACCOUNT * a1, const ACCOUNT * a2)
30 {
31     const char* user = NONULL (Username);
32     const char* login = NONULL (Username);
33
34     if (a1->type != a2->type)
35         return 0;
36     if (ascii_strcasecmp (a1->host, a2->host))
37         return 0;
38     if (a1->port != a2->port)
39         return 0;
40
41     if (a1->type == M_ACCT_TYPE_IMAP) {
42         if (ImapUser && (ImapUser[0] != '\0'))
43             user = ImapUser;
44         if (ImapLogin && (ImapLogin[0] != '\0'))
45             login = ImapLogin;
46     }
47
48     if (a1->type == M_ACCT_TYPE_POP && PopUser)
49         user = PopUser;
50
51 #ifdef USE_NNTP
52     if (a1->type == M_ACCT_TYPE_NNTP && NntpUser)
53         user = NntpUser;
54 #endif
55
56     if (a1->flags & a2->flags & M_ACCT_USER)
57         return (!m_strcmp(a1->user, a2->user));
58     if (a1->flags & M_ACCT_USER)
59         return (!m_strcmp(a1->user, user));
60     if (a2->flags & M_ACCT_USER)
61         return (!m_strcmp(a2->user, user));
62
63     return 1;
64 }
65
66 /* mutt_account_fromurl: fill account with information from url. */
67 int mutt_account_fromurl(ACCOUNT *account, ciss_url_t *url)
68 {
69     /* must be present */
70     if (!url->host)
71         return -1;
72     m_strcpy(account->host, sizeof(account->host), url->host);
73
74     if (url->user) {
75         m_strcpy(account->user, sizeof(account->user), url->user);
76         account->flags |= M_ACCT_USER;
77     }
78     if (url->pass) {
79         m_strcpy(account->pass, sizeof(account->pass), url->pass);
80         account->flags |= M_ACCT_PASS;
81     }
82     if (url->port) {
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     if (account->type == M_ACCT_TYPE_IMAP) {
102         if (account->flags & M_ACCT_SSL)
103             url->scheme = U_IMAPS;
104         else
105             url->scheme = U_IMAP;
106     }
107
108     if (account->type == M_ACCT_TYPE_POP) {
109         if (account->flags & M_ACCT_SSL)
110             url->scheme = U_POPS;
111         else
112             url->scheme = U_POP;
113     }
114
115 #ifdef USE_NNTP
116     if (account->type == M_ACCT_TYPE_NNTP) {
117         if (account->flags & M_ACCT_SSL)
118             url->scheme = U_NNTPS;
119         else
120             url->scheme = U_NNTP;
121     }
122 #endif
123
124     url->host = account->host;
125     if (account->flags & M_ACCT_PORT)
126         url->port = account->port;
127     if (account->flags & M_ACCT_USER)
128         url->user = account->user;
129     if (account->flags & M_ACCT_PASS)
130         url->pass = account->pass;
131 }
132
133 /* mutt_account_getuser: retrieve username into ACCOUNT, if necessary */
134 int mutt_account_getuser (ACCOUNT * account)
135 {
136     char prompt[SHORT_STRING];
137
138     /* already set */
139     if (account->flags & M_ACCT_USER)
140         return 0;
141     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapUser))
142         m_strcpy(account->user, sizeof(account->user), ImapUser);
143     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopUser))
144         m_strcpy(account->user, sizeof(account->user), PopUser);
145 #ifdef USE_NNTP
146     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpUser))
147         m_strcpy(account->user, sizeof(account->user), NntpUser);
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         m_strcpy(account->user, sizeof(account->user), NONULL(Username));
153         if (mutt_get_field_unbuffered(prompt, account->user,
154                                       sizeof(account->user), 0))
155             return -1;
156     }
157
158     account->flags |= M_ACCT_USER;
159
160     return 0;
161 }
162
163 int mutt_account_getlogin (ACCOUNT* account)
164 {
165     /* already set */
166     if (account->flags & M_ACCT_LOGIN)
167         return 0;
168     else if (account->type == M_ACCT_TYPE_IMAP)
169     {
170         if (!m_strisempty(ImapLogin)) {
171             m_strcpy(account->login, sizeof(account->login), ImapLogin);
172             account->flags |= M_ACCT_LOGIN;
173         }
174     }
175
176     if (!(account->flags & M_ACCT_LOGIN)) {
177         mutt_account_getuser (account);
178         m_strcpy(account->login, sizeof(account->login), account->user);
179     }
180
181     account->flags |= M_ACCT_LOGIN;
182
183     return 0;
184 }
185
186 /* mutt_account_getpass: fetch password into ACCOUNT, if neccessary */
187 int mutt_account_getpass (ACCOUNT * account)
188 {
189     char prompt[SHORT_STRING];
190
191     if (account->flags & M_ACCT_PASS)
192         return 0;
193     else if ((account->type == M_ACCT_TYPE_IMAP) && !m_strisempty(ImapPass))
194         m_strcpy(account->pass, sizeof(account->pass), ImapPass);
195     else if ((account->type == M_ACCT_TYPE_POP) && !m_strisempty(PopPass))
196         m_strcpy(account->pass, sizeof(account->pass), PopPass);
197 #ifdef USE_NNTP
198     else if ((account->type == M_ACCT_TYPE_NNTP) && !m_strisempty(NntpPass))
199         m_strcpy(account->pass, sizeof(account->pass), NntpPass);
200 #endif
201     else {
202         snprintf(prompt, sizeof(prompt), _("Password for %s@%s: "),
203                  account->flags & M_ACCT_LOGIN ? account->login : account->user,
204                  account->host);
205         account->pass[0] = '\0';
206         if (mutt_get_field_unbuffered(prompt, account->pass,
207                                       sizeof(account->pass), M_PASS))
208             return -1;
209     }
210
211     account->flags |= M_ACCT_PASS;
212
213     return 0;
214 }
215
216 void mutt_account_unsetpass (ACCOUNT * account)
217 {
218     account->flags &= !M_ACCT_PASS;
219 }