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
152
|
/*************************************************************************
*
* HTCd - Copyright (C) 1998-2006 Henrik Rydberg
*
* 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 2 of the License, 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <sh/htc.h>
#include <mime/mime.h>
#include <mt/lookup.h>
#include <mt/issue.h>
#include <mt/dates.h>
#include <unistd.h>
////////////////////////////////////////////
TOK_IMPL(sleep) { sleep(atoi(env[0])); }
TOK_IMPL(throw) { THROW(env[0]); }
////////////////////////////////////////////
TOK_IMPL(sync) { dbmap::Sync(); }
////////////////////////////////////////////
TOK_IMPL(gethost) {
IPnode node; if(Lookup(node,env[0])) out<<node.name; else out<<"UNKNOWN";
}
////////////////////////////////////////////
TOK_IMPL(mail) {
mswrite ms;
if(env[0].size()<2) return;
ms<<"From: "; encodeHeader(ms,mime_default,env[3]); ms<<" <"<<env[0]<<">\n";
//if(env[1].nempty()) ms<<"Reply-To: "<<env[1]<<"\n";
//!! neat simple hack - never used except as should be
if(env[1].nempty()) ms<<"To: "<<env[1]<<"\n";
ms<<"Date: "<<httpTime(atoi(env[4]))<<"\n";
ms<<"Subject: "; encodeHeader(ms,mime_default,env[5]); ms<<"\n";
ms<<"MIME-Version: 1.0\n";
ms<<"Content-Transfer-Encoding: Quoted-Printable\n";
ms<<"Content-Type: "<<env[6]<<"\n";
mswrite body; encodeQP(body,env[7]);
ms<<"Content-Length: "<<body.str().size()<<"\n";
ms<<"\n";
ms<<body.str();
HTC* htc=Req(env)->htc();
if(htc->mailer.size()) {
issue_t cmd(htc->mailer+sref(" ")+env[2]);
mstream m;
cmd.run(m,ms.str());
htc->syslog<<"["<<time(0)<<"][mail]["<<env[2]<<"]\n";
}
else {
htc->syslog<<"["<<time(0)<<"][NOT SET]["<<env[2]<<"]\n";
htc->syslog<<"-----------------------------------------\n";
htc->syslog<<ms.str()<<"\n";
htc->syslog<<"=========================================\n";
}
htc->syslog.flush();
}
////////////////////////////////////////////
TOK_IMPL(issue) {
HTC* htc=Req(env)->htc();
htc->syslog<<"["<<time(0)<<"][issue]["<<env[0]<<"]\n";
htc->syslog.flush();
issue_t cmd(DocPath(env,env[0]));
cmd.run(out,env[1]);
htc->syslog<<"["<<time(0)<<"][finish]["<<env[0]<<"]\n";
htc->syslog.flush();
}
//////////////////////////////////////////////////////////
void envAddSys(tokmap& T)
{
TOK_ADD(sleep,"x",tok_t::SYSONLY,
"%Sleep for seconds\n"
"%Syntax: \\sleep{t}\n"
"%\n"
"%This one works just like the UNIX command\n"
);
TOK_ADD(throw,"x",tok_t::SYSONLY,
"%Throw an exception\n"
"%Syntax: \\throw{text}\n"
"%\n"
"%Throw a, for the HTC system, standard error\n"
"%expection, which also many of the built-in\n"
"%do. Enables well-behaved error handling.\n"
);
/////////////////////////////////////////////////////////////
TOK_ADD(sync,"",tok_t::SYSONLY,
"%Syncronize all open writable databases\n"
"%Syntax: \\sync\n"
"%\n"
"%Use in crontab only.\n"
);
/////////////////////////////////////////////////////////////
TOK_ADD(gethost,"x",tok_t::SYSONLY,
"%Lookup hostname on nameserver\n"
"%Syntax: \\gethost{IP address}\n"
"%\n"
"%Normally performed automatically by the server.\n"
);
/////////////////////////////////////////////////////////////
TOK_ADD(mail,"xxxxxxxx",tok_t::SYSONLY,
"%System mail\n"
"%Syntax: \\mail{from}{replyto}{to}{fromname}{date}{subject}{mime}{body}\n"
"%\n"
"%This mail command can do almost anything, so be careful!\n"
"%Usually there are several htc commands simplifying the use\n"
"%of this one.\n"
);
/////////////////////////////////////////////////////////////
TOK_ADD(issue,"xx",tok_t::SYSONLY,
"%Issue piped command\n"
"%Syntax: \\issue{uri}{stdin}\n"
"%\n"
"%Pipes stdin to cmd and output stdout.\n"
"%An extremely powerful command that may be used\n"
"%to run external programs via the web.\n"
"%\n"
"%Note that the normal uri handling is performed.\n"
);
/////////////////////////////////////////////////////////////
}
|