aboutsummaryrefslogtreecommitdiff
path: root/src/builtin/curhdr.bi
blob: d884f45cd8b1d3706aa69f99ff26595356d1c7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* This file is part of Mailfromd.             -*- c -*-
   Copyright (C) 2008-2019 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

MF_BUILTIN_MODULE
#include "msg.h"

/* number current_header_count([string name]) */
MF_STATE(eoh)
MF_STATE(body)
MF_STATE(eom)
MF_CAPTURE
MF_DEFUN(current_header_count, NUMBER, OPTIONAL, STRING name)
{
	mu_header_t hdr;
	size_t total;
	size_t count;
	int rc;

	rc = env_get_header(env, &hdr);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "env_get_header: %s",
		  mu_strerror(rc));
	
	rc = mu_header_get_field_count(hdr, &total);
	
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "mu_header_get_field_count: %s",
		  mu_strerror(rc));
	if (MF_DEFINED(name)) {
		size_t i;
		count = 0;
		for (i = 1; i <= total; i++) {
			const char *sptr;
			if (mu_header_sget_field_name(hdr, i, &sptr) == 0
			    && strcasecmp(sptr, name) == 0)
				count++;
		}
	} else
		count = total;
	MF_RETURN(count);
}
END

/* string current_header_nth_name(number index) */
MF_STATE(eoh)
MF_STATE(body)
MF_STATE(eom)
MF_CAPTURE
MF_DEFUN(current_header_nth_name, STRING, NUMBER index)
{
	mu_header_t hdr;
	const char *sptr;
	int rc;

	rc = env_get_header(env, &hdr);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "env_get_header: %s",
		  mu_strerror(rc));
	
	rc = mu_header_sget_field_name(hdr, index, &sptr);

	if (rc == MU_ERR_NOENT)
		MF_THROW(mfe_not_found,
			 _("requested item (#%ld) not found"),
			 index);
	else if (rc != 0)
		MF_THROW(mfe_failure,
			 "mu_header_sget_field_name: %s",
			 mu_strerror(rc));
	MF_RETURN(sptr);
}
END

/* string current_header_nth_value(number index) */
MF_STATE(eoh)
MF_STATE(body)
MF_STATE(eom)
MF_CAPTURE
MF_DEFUN(current_header_nth_value, STRING, NUMBER index)
{
	mu_header_t hdr;
	const char *sptr;
	int rc;

	rc = env_get_header(env, &hdr);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "env_get_header: %s",
		  mu_strerror(rc));
	
	rc = mu_header_sget_field_value(hdr, index, &sptr);

	if (rc == MU_ERR_NOENT)
		MF_THROW(mfe_not_found,
			 _("requested item (#%ld) not found"),
			 index);
	else if (rc != 0)
		MF_THROW(mfe_failure,
			 "mu_header_sget_field_name: %s",
			 mu_strerror(rc));
	MF_RETURN(sptr);
}
END

/* string current_header(string name[, number index]) */
MF_STATE(eoh)
MF_STATE(body)	
MF_STATE(eom)
MF_CAPTURE
MF_DEFUN(current_header, STRING, STRING name, OPTIONAL, NUMBER index)
{
	mu_header_t hdr;
	const char *sptr;
	int rc;

	rc = env_get_header(env, &hdr);
	MF_ASSERT(rc == 0,
		  mfe_failure,
		  "env_get_header: %s",
		  mu_strerror(rc));
	
	rc = mu_header_sget_value_n(hdr, name, MF_OPTVAL(index, 1), &sptr);

	if (rc == MU_ERR_NOENT)
		MF_THROW(mfe_not_found,
			 _("requested item (%s,%ld) not found"),
			 name, MF_OPTVAL(index, 1));
	else if (rc != 0)
		MF_THROW(mfe_failure,
			 "mu_header_sget_field_name: %s",
			 mu_strerror(rc));
	MF_RETURN(sptr);
}
END

Return to:

Send suggestions and report system problems to the System administrator.