1 # =====================================================================
2 # _wikicpi.awk: application-level CPI processor.
3 #
4 # Copyright (c) 2010,2011,2012,2021 Carlo Strozzi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 dated June, 1991.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # =====================================================================
20 # string _wikicpi(string s)
21 #
22 # Parse "s" Wiki statements and turn them into the corresponding XHTML
23 # markup.
24 # =====================================================================
25
26 function _wikicpi(s, tmp,prog,args,i,S,ext,saved) {
27
28 # ===================================================================
29 # Handle calls to local procedures (kind of "stored-procedures").
30 # Note that while the called procs may return CPIs, any of the
31 # latters that are in the form of "(:p-something ...:)" will NOT
32 # be recursively parsed, which seems sensible from a security
33 # standpoint. Furthermore, only those procedures stored by the
34 # sysadmin in a well-defined TW directory will be sought for, and
35 # they will have to apply every possible caution while parsing any
36 # received arguments. In fact, the called program will have access
37 # to the full TW environment, so any arguments should be take from
38 # there and not from the command-line. The latter capability may even
39 # be dropped altogether in the future.
40 # ===================================================================
41
42 gsub(/:\)/,"\001",s)
43
44 while (_extract(s,"\\(:p-[^\\001]+\\001")) {
45
46 # Better comment this out while debugging.
47 S = S _RLMATCH
48
49 saved = _RMATCH
50
51 sub(/^\(:p-/,"",_RMATCH)
52 sub(/\001/,"",_RMATCH)
53 prog = args = _RMATCH
54 #prog = tolower(prog)
55 sub(/ .*/,"",prog)
56 sub(/[^ ]+ +/,"",args)
57
58 # procedure name is strictly constrained.
59 if (prog !~ /^[a-z]+[-a-z0-9]*$/) {
60 # Better comment this out while debugging.
61 S = S "(:p-" _RMATCH "\001"
62 continue
63 }
64
65 # As usual, only one single _CMD can be opened at a time.
66 if (_CMD != _NULL) close(_CMD)
67
68 # Note that programs that can be called with the (:p-...:)
69 # wiki tag are sought for in a directory other than the
70 # one hosting local programs called with the $[....|:.]
71 # CSA tag, because the two sets of programs should be
72 # regarded has having potentially different security
73 # constraints. That is, there may be programs that are
74 # meant to be called only in the "$[...]" webmaster context
75 # but not in the "(:p-...:)" wiki editor context. Any
76 # programs that need to be called from within both contexts
77 # will have to be handled by symlinking them as appropriate.
78 # Furthermore, these two classes of local procedures behave
79 # differently, with the CSA-level ones which are expected to
80 # print a path to a file as a result, while the TW-level ones
81 # are expected to output the actual inclusion content.
82
83 # For security reasons, the actual passing of arguments is
84 # currently disabled, as it requires further thinking.
85 #_CMD = ENVIRON["CSA_ROOT"] "/var/proclib/" prog " " args
86 _CMD = ENVIRON["CSA_ROOT"] "/var/proclib/" prog
87
88 i=0
89 while ((_CMD | getline tmp) > 0) {
90 if (i) S = S "\n"
91 gsub(/:\)/,"\001",tmp) # in case proc returns CPIs
92 S = S tmp
93 i++
94 }
95
96 close(_CMD)
97 _CMD = _NULL
98 }
99
100 # Better comment this out while debugging.
101 S = S _RRMATCH
102
103 # Restore CPI closing tags.
104 gsub(/\001/,":)",S)
105
106 s = S
107
108 # ===================================================================
109 # removal of any page abstract section: (::ab:) ... (:ab::)
110 #
111 # Note how the TW concept of "abstract" works: an editor can select
112 # the portion of a page which will be used as the page abstract by
113 # surrounding such portion by suitable application-level wiki tags.
114 # This means that a page abstract isn't a separate piece of text
115 # but it is simply a selected part of the page body that will be
116 # rendered ***in alternative*** to the full page text in the
117 # tw-recent-pages static view and possibly others. When a page is
118 # rendered for normal display the abstract markers are removed,
119 # along with the text inside them.
120 # ===================================================================
121
122 # get rid of page abstract upon normal display.
123 sub(/\(::ab:\).*\(:ab::\)/,"",s)
124
125 # get rid of any orphan "abstract" markers
126 # (this is now probably useless).
127 gsub(/\(::?ab::?\)/,"",s)
128
129 # (:i:) -> ""
130 gsub(/\(:i:\)/,_NULL,s)
131
132 # (:at:) -> "@"
133 gsub(/\(:at:\)/,"\\@",s)
134
135 # (:amp:) -> "&"
136 gsub(/\(:amp:\)/,"\\&",s)
137
138 # Authentication-related CPIs.
139 gsub(/\(:u-nick:\)/,ENVIRON["CSA_AUTH_USER"],s)
140 gsub(/\(:u-auth:\)/,ENVIRON["TNS_AUTH_GRP"],s)
141
142 # Emoticons, with tags loosely based on
143 # http://www.mediawiki.org/wiki/Extension:Emoticons
144
145 # Set emoticon-related defaults if necessary.
146 if ((tmp=_rcget("TNS_GROUP_MISC_PROP",8)) \
147 !~ /^\/.*/ || tmp ~ /\.\./) tmp = "/icons"
148
149 if ((ext=_rcget("TNS_GROUP_MISC_PROP",9)) != "gif") ext = "png"
150
151 tmp = ENVIRON["CSA_URL"] tmp
152
153 # List of supported emoticons. Make sure their CPIs do not collide with those
154 # already used in Typew for other purposes.
155 gsub(/\(:angry:\)/,"",s)
156 gsub(/\(:bat:\)/,"",s)
157 gsub(/\(:beer:\)/,"",s)
158 gsub(/\(:biggrin:\)/,"",s)
159 gsub(/\(:blush:\)/,"",s)
160 gsub(/\(:boy:\)/,"",s)
161 gsub(/\(:brflower:\)/,"",s)
162 gsub(/\(:brheart:\)/,"",s)
163 gsub(/\(:coffee:\)/,"",s)
164 gsub(/\(:coolglasses:\)/,"",s)
165 gsub(/\(:cry:\)/,"",s)
166 gsub(/\(:cuffs:\)/,"",s)
167 gsub(/\(:devil:\)/,"",s)
168 gsub(/\(:drink:\)/,"",s)
169 gsub(/\(:flower:\)/,"",s)
170 gsub(/\(:frowning:\)/,"",s)
171 gsub(/\(:girl:\)/,"",s)
172 gsub(/\(:grin:\)/,"",s)
173 gsub(/\(:heart:\)/,"",s)
174 gsub(/\(:hugleft:\)/,"",s)
175 gsub(/\(:hugright:\)/,"",s)
176 gsub(/\(:ikes:\)/,"",s)
177 gsub(/\(:kiss:\)/,"",s)
178 gsub(/\(:lamp:\)/,"",s)
179 gsub(/\(:mail:\)/,"",s)
180 gsub(/\(:music:\)/,"",s)
181 gsub(/\(:no:\)/,"",s)
182 gsub(/\(:phone:\)/,"",s)
183 gsub(/\(:photo:\)/,"",s)
184 gsub(/\(:pussy:\)/,"",s)
185 gsub(/\(:rainbow:\)/,"",s)
186 gsub(/\(:stare:\)/,"",s)
187 gsub(/\(:star:\)/,"",s)
188 gsub(/\(:tongue:\)/,"",s)
189 gsub(/\(:unhappy:\)/,"",s)
190 gsub(/\(:wink:\)/,"",s)
191 gsub(/\(:yes:\)/,"",s)
192
193 # Add support for further markup below.
194 # ...
195
196 #debug
197 #if (ENVIRON["REMOTE_ADDR"] == "2001:470:1f0b:13a5:1::2") return _nblcpi(s)
198
199 # Handle embedded NBL queries.
200 return _nblcpi(s)
201 }
202