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