summaryrefslogtreecommitdiff
path: root/src/sh/sys.cc
diff options
context:
space:
mode:
authorHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
committerHenrik Rydberg <rydberg@euromail.se>2011-10-08 20:30:28 +0200
commit5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch)
tree1a81af141708b826e9c61e8a04019994fcca8298 /src/sh/sys.cc
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/sh/sys.cc')
-rw-r--r--src/sh/sys.cc152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/sh/sys.cc b/src/sh/sys.cc
new file mode 100644
index 0000000..b70991b
--- /dev/null
+++ b/src/sh/sys.cc
@@ -0,0 +1,152 @@
1/*************************************************************************
2 *
3 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <sh/htc.h>
21#include <mime/mime.h>
22#include <mt/lookup.h>
23#include <mt/issue.h>
24#include <mt/dates.h>
25#include <unistd.h>
26
27////////////////////////////////////////////
28
29TOK_IMPL(sleep) { sleep(atoi(env[0])); }
30TOK_IMPL(throw) { THROW(env[0]); }
31
32////////////////////////////////////////////
33
34TOK_IMPL(sync) { dbmap::Sync(); }
35
36////////////////////////////////////////////
37
38TOK_IMPL(gethost) {
39 IPnode node; if(Lookup(node,env[0])) out<<node.name; else out<<"UNKNOWN";
40}
41
42////////////////////////////////////////////
43
44TOK_IMPL(mail) {
45 mswrite ms;
46 if(env[0].size()<2) return;
47 ms<<"From: "; encodeHeader(ms,mime_default,env[3]); ms<<" <"<<env[0]<<">\n";
48
49 //if(env[1].nempty()) ms<<"Reply-To: "<<env[1]<<"\n";
50 //!! neat simple hack - never used except as should be
51 if(env[1].nempty()) ms<<"To: "<<env[1]<<"\n";
52
53 ms<<"Date: "<<httpTime(atoi(env[4]))<<"\n";
54 ms<<"Subject: "; encodeHeader(ms,mime_default,env[5]); ms<<"\n";
55 ms<<"MIME-Version: 1.0\n";
56 ms<<"Content-Transfer-Encoding: Quoted-Printable\n";
57 ms<<"Content-Type: "<<env[6]<<"\n";
58 mswrite body; encodeQP(body,env[7]);
59 ms<<"Content-Length: "<<body.str().size()<<"\n";
60 ms<<"\n";
61 ms<<body.str();
62 HTC* htc=Req(env)->htc();
63 if(htc->mailer.size()) {
64 issue_t cmd(htc->mailer+sref(" ")+env[2]);
65 mstream m;
66 cmd.run(m,ms.str());
67 htc->syslog<<"["<<time(0)<<"][mail]["<<env[2]<<"]\n";
68 }
69 else {
70 htc->syslog<<"["<<time(0)<<"][NOT SET]["<<env[2]<<"]\n";
71 htc->syslog<<"-----------------------------------------\n";
72 htc->syslog<<ms.str()<<"\n";
73 htc->syslog<<"=========================================\n";
74 }
75 htc->syslog.flush();
76}
77
78////////////////////////////////////////////
79
80TOK_IMPL(issue) {
81 HTC* htc=Req(env)->htc();
82 htc->syslog<<"["<<time(0)<<"][issue]["<<env[0]<<"]\n";
83 htc->syslog.flush();
84 issue_t cmd(DocPath(env,env[0]));
85 cmd.run(out,env[1]);
86 htc->syslog<<"["<<time(0)<<"][finish]["<<env[0]<<"]\n";
87 htc->syslog.flush();
88}
89
90//////////////////////////////////////////////////////////
91
92void envAddSys(tokmap& T)
93{
94 TOK_ADD(sleep,"x",tok_t::SYSONLY,
95 "%Sleep for seconds\n"
96 "%Syntax: \\sleep{t}\n"
97 "%\n"
98 "%This one works just like the UNIX command\n"
99 );
100 TOK_ADD(throw,"x",tok_t::SYSONLY,
101 "%Throw an exception\n"
102 "%Syntax: \\throw{text}\n"
103 "%\n"
104 "%Throw a, for the HTC system, standard error\n"
105 "%expection, which also many of the built-in\n"
106 "%do. Enables well-behaved error handling.\n"
107 );
108
109 /////////////////////////////////////////////////////////////
110
111 TOK_ADD(sync,"",tok_t::SYSONLY,
112 "%Syncronize all open writable databases\n"
113 "%Syntax: \\sync\n"
114 "%\n"
115 "%Use in crontab only.\n"
116 );
117
118 /////////////////////////////////////////////////////////////
119
120 TOK_ADD(gethost,"x",tok_t::SYSONLY,
121 "%Lookup hostname on nameserver\n"
122 "%Syntax: \\gethost{IP address}\n"
123 "%\n"
124 "%Normally performed automatically by the server.\n"
125 );
126
127 /////////////////////////////////////////////////////////////
128
129 TOK_ADD(mail,"xxxxxxxx",tok_t::SYSONLY,
130 "%System mail\n"
131 "%Syntax: \\mail{from}{replyto}{to}{fromname}{date}{subject}{mime}{body}\n"
132 "%\n"
133 "%This mail command can do almost anything, so be careful!\n"
134 "%Usually there are several htc commands simplifying the use\n"
135 "%of this one.\n"
136 );
137
138 /////////////////////////////////////////////////////////////
139
140 TOK_ADD(issue,"xx",tok_t::SYSONLY,
141 "%Issue piped command\n"
142 "%Syntax: \\issue{uri}{stdin}\n"
143 "%\n"
144 "%Pipes stdin to cmd and output stdout.\n"
145 "%An extremely powerful command that may be used\n"
146 "%to run external programs via the web.\n"
147 "%\n"
148 "%Note that the normal uri handling is performed.\n"
149 );
150
151 /////////////////////////////////////////////////////////////
152}