NeoMutt  2024-10-02-7-gd3e66a
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
accountcmd.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdio.h>
32#include <sys/types.h>
33#include "mutt/lib.h"
34#include "config/lib.h"
35#include "core/lib.h"
36#include "accountcmd.h"
37#include "connaccount.h"
38#include "globals.h"
39#include "mutt_account.h"
40
47static void make_cmd(struct Buffer *buf, const struct ConnAccount *cac, const char *cmd)
48{
49 ASSERT(buf && cac);
50
51 buf_addstr(buf, cmd);
52 buf_add_printf(buf, " --hostname %s", cac->host);
53 if (cac->flags & MUTT_ACCT_USER)
54 {
55 buf_add_printf(buf, " --username %s", cac->user);
56 }
57
58 static const char *types[] = { "", "imap", "pop", "smtp", "nntp" };
59 ASSERT(sizeof(types) / sizeof(*types) == MUTT_ACCT_TYPE_MAX);
60 if (cac->type != MUTT_ACCT_TYPE_NONE)
61 {
62 buf_add_printf(buf, " --type %s%c", types[cac->type],
63 (cac->flags & MUTT_ACCT_SSL) ? 's' : '\0');
64 }
65}
66
74static MuttAccountFlags parse_one(struct ConnAccount *cac, char *line)
75{
76 ASSERT(cac && line);
77
78 const regmatch_t *match = mutt_prex_capture(PREX_ACCOUNT_CMD, line);
79 if (!match)
80 {
81 mutt_perror(_("Line is malformed: expected <key: val>, got <%s>"), line);
82 return MUTT_ACCT_NO_FLAGS;
83 }
84
85 const regmatch_t *keymatch = &match[PREX_ACCOUNT_CMD_MATCH_KEY];
86 const regmatch_t *valmatch = &match[PREX_ACCOUNT_CMD_MATCH_VALUE];
87 line[mutt_regmatch_start(keymatch) + mutt_regmatch_len(keymatch)] = '\0';
88 const char *key = line + mutt_regmatch_start(keymatch);
89 const char *val = line + mutt_regmatch_start(valmatch);
90
91 switch (key[0])
92 {
93 case 'l':
94 if (mutt_str_equal(key + 1, "ogin"))
95 {
96 mutt_str_copy(cac->login, val, sizeof(cac->login));
97 return MUTT_ACCT_LOGIN;
98 }
99 break;
100
101 case 'p':
102 if (mutt_str_equal(key + 1, "assword"))
103 {
104 mutt_str_copy(cac->pass, val, sizeof(cac->pass));
105 return MUTT_ACCT_PASS;
106 }
107 break;
108
109 case 'u':
110 if (mutt_str_equal(key + 1, "sername"))
111 {
112 mutt_str_copy(cac->user, val, sizeof(cac->user));
113 return MUTT_ACCT_USER;
114 }
115 break;
116
117 default:
118 break;
119 }
120
121 mutt_warning(_("Unhandled key in line <%s: %s>"), key, val);
122 return MUTT_ACCT_NO_FLAGS;
123}
124
132static MuttAccountFlags call_cmd(struct ConnAccount *cac, const struct Buffer *cmd)
133{
134 ASSERT(cac && cmd);
135
137
138 FILE *fp = NULL;
139 pid_t pid = filter_create(buf_string(cmd), NULL, &fp, NULL, EnvList);
140 if (pid < 0)
141 {
142 mutt_perror(_("Unable to run account command"));
143 return rc;
144 }
145
146 size_t len = 0;
147 char *line = NULL;
148 while ((line = mutt_file_read_line(NULL, &len, fp, NULL, MUTT_RL_NO_FLAGS)))
149 {
150 rc |= parse_one(cac, line);
151 FREE(&line);
152 }
153
154 mutt_file_fclose(&fp);
155 filter_wait(pid);
156 return rc;
157}
158
166{
167 if (!cac || (cac->host[0] == '\0') || (cac->type == MUTT_ACCT_TYPE_NONE))
168 {
169 return MUTT_ACCT_NO_FLAGS;
170 }
171
172 const char *c_account_command = cs_subset_string(NeoMutt->sub, "account_command");
173 if (!c_account_command)
174 {
175 return MUTT_ACCT_NO_FLAGS;
176 }
177
179 struct Buffer *cmd_buf = buf_pool_get();
180
181 make_cmd(cmd_buf, cac, c_account_command);
182 rc = call_cmd(cac, cmd_buf);
183 cac->flags |= rc;
184
185 buf_pool_release(&cmd_buf);
186 return rc;
187}
static MuttAccountFlags call_cmd(struct ConnAccount *cac, const struct Buffer *cmd)
Call the account command.
Definition: accountcmd.c:132
static MuttAccountFlags parse_one(struct ConnAccount *cac, char *line)
Parse a single line of the response.
Definition: accountcmd.c:74
static void make_cmd(struct Buffer *buf, const struct ConnAccount *cac, const char *cmd)
Build the command line for the external account command.
Definition: accountcmd.c:47
MuttAccountFlags mutt_account_call_external_cmd(struct ConnAccount *cac)
Retrieve account credentials via an external command.
Definition: accountcmd.c:165
Connection Credentials External Command.
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:204
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:291
Convenience wrapper for the config headers.
Connection Credentials.
uint8_t MuttAccountFlags
Flags, Which ConnAccount fields are initialised, e.g. MUTT_ACCT_PORT.
Definition: connaccount.h:41
#define MUTT_ACCT_NO_FLAGS
No flags are set.
Definition: connaccount.h:42
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
Definition: connaccount.h:47
#define MUTT_ACCT_PASS
Password field has been set.
Definition: connaccount.h:46
#define MUTT_ACCT_USER
User field has been set.
Definition: connaccount.h:44
#define MUTT_ACCT_LOGIN
Login field has been set.
Definition: connaccount.h:45
Convenience wrapper for the core headers.
char * mutt_file_read_line(char *line, size_t *size, FILE *fp, int *line_num, ReadLineFlags flags)
Read a line from a file.
Definition: file.c:808
#define mutt_file_fclose(FP)
Definition: file.h:138
#define MUTT_RL_NO_FLAGS
No flags are set.
Definition: file.h:40
char ** EnvList
Private copy of the environment variables.
Definition: globals.c:76
#define mutt_warning(...)
Definition: logging2.h:90
#define mutt_perror(...)
Definition: logging2.h:93
#define FREE(x)
Definition: memory.h:45
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:220
pid_t filter_create(const char *cmd, FILE **fp_in, FILE **fp_out, FILE **fp_err, char **envlist)
Set up filter program.
Definition: filter.c:209
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:581
ConnAccount object used by POP and IMAP.
@ MUTT_ACCT_TYPE_NONE
Account type is unknown.
Definition: mutt_account.h:37
@ MUTT_ACCT_TYPE_MAX
Definition: mutt_account.h:42
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
regmatch_t * mutt_prex_capture(enum Prex which, const char *str)
Match a precompiled regex against a string.
Definition: prex.c:298
@ PREX_ACCOUNT_CMD_MATCH_KEY
[key]: value
Definition: prex.h:225
@ PREX_ACCOUNT_CMD_MATCH_VALUE
key: [value]
Definition: prex.h:226
@ PREX_ACCOUNT_CMD
key: value
Definition: prex.h:42
static size_t mutt_regmatch_len(const regmatch_t *match)
Return the length of a match.
Definition: regex3.h:76
static regoff_t mutt_regmatch_start(const regmatch_t *match)
Return the start of a match.
Definition: regex3.h:56
#define ASSERT(COND)
Definition: signal2.h:58
String manipulation buffer.
Definition: buffer.h:36
Login details for a remote server.
Definition: connaccount.h:53
char login[128]
Login name.
Definition: connaccount.h:55
char user[128]
Username.
Definition: connaccount.h:56
char pass[256]
Password.
Definition: connaccount.h:57
char host[128]
Server to login to.
Definition: connaccount.h:54
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition: connaccount.h:59
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:60
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46