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