1 # =====================================================================
2 # cmtList.awk: RPC I/O function for rpclib/cmtList.
3 #
4 # Copyright (c) 2007-2010 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
21 # =====================================================================
22 # void _userproc(int mode)
23 # =====================================================================
24
25 function _userproc(mode, value,outfile,i,j,x,url,\
26 a,fmt1,fmt2,tmp,blkno,curr,\
27 first,last,tot,fmt3) {
28
29 if (mode == _O_REQUEST) { # request.
30
31 # target group
32 value = _request("1",1)
33
34 # group must not be null and it may not contain the
35 # unescaped ``.'' character.
36
37 if (value != _NULL && value !~ /\./) {
38 _rcset("cgi.group",unixify(value))
39 _rcset("cgi.group.literal",value)
40 }
41
42 # target page within group.
43 value = _request("2",1)
44
45 # page name must be at least 2-character long.
46 if (length(value) > 1) {
47 _rcset("cgi.page.literal",value)
48 value = unixify(value,1)
49 _rcset("cgi.page",value)
50 }
51
52 # optional author name.
53 if ((value=_request("twauthor",1)) != _NULL) {
54 sub(/[\t\r\n<>]+/," ",value)
55 _rcset("cgi.author",substr(_strip(value,_O_MIDDLE),1,32))
56 }
57
58 # Optional starting page no. for paged results.
59 if ((value=_request("4",1)/1) != 0) _rcset("cgi.paging",value)
60
61 # The following test is necessary since the address could,
62 # at least in theory, have been set to any string by the
63 # remote user, due to how it is handled to cope with stunnel(8)
64 # and the lack of transproxy support in kernel 2.4.x.
65
66 if (_isipaddr(ENVIRON["REMOTE_ADDR"]) == _TRUE)
67 value = ENVIRON["REMOTE_ADDR"]
68 else value = "0.0.0.0"
69
70 _rcset("REMOTE_ADDR",value)
71 }
72
73 else { # response.
74
75 outfile = _rcget("tpl.include.tw.page")
76 if (outfile !~ /^\/\.*[a-zA-Z0-9]/)
77 return(_sys("csaExit.fault 0041 outfile"))
78
79 # Set output format string for threaded listing.
80
81 fmt1 = readfmt("tw-page-comments")
82 gsub(/%/,"%%",fmt1) # turn plain '%' into '%%'.
83 gsub(/\\/,"\\\\&",fmt1) # turn '\' into '\\'.
84 gsub(/[\n\r]+/,"",fmt1) # just in case.
85
86 # this format string is currently not customizable.
87 for (i=1; i<=5; i++) {
88 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
89 }
90
91 # encode any extra markers.
92 gsub(//, "\\<tw:s/\\>",fmt1)
93
94 fmt1 = fmt1 "\n"
95
96 # Set output format string last comment in threaded listing.
97
98 fmt2 = readfmt("tw-page-comments-last")
99 gsub(/%/,"%%",fmt2) # turn plain '%' into '%%'.
100 gsub(/\\/,"\\\\&",fmt2) # turn '\' into '\\'.
101 gsub(/[\n\r]+/,"",fmt2) # just in case.
102
103 # this format string is currently not customizable.
104 for (i=1; i<=5; i++) {
105 if (!sub(//,"%s",fmt2)) fmt2 = fmt2 ""
106 }
107
108 # encode any extra markers.
109 gsub(//, "\\<tw:s/\\>",fmt2)
110
111 fmt2 = fmt2 "\n"
112
113 # Set output format string for flat listing.
114
115 fmt3 = readfmt("tw-page-comments-flat")
116 gsub(/%/,"%%",fmt3) # turn plain '%' into '%%'.
117 gsub(/\\/,"\\\\&",fmt3) # turn '\' into '\\'.
118 gsub(/[\n\r]+/,"",fmt3) # just in case.
119
120 # this format string is currently not customizable.
121 for (i=1; i<=6; i++) {
122 if (!sub(//,"%s",fmt3)) fmt3 = fmt3 ""
123 }
124
125 # encode any extra markers.
126 gsub(//, "\\<tw:s/\\>",fmt3)
127
128 fmt3 = fmt3 "\n"
129
130 url = ENVIRON["CSA_CGI_STEM"] ENVIRON["CSA_REQUEST_URI"] "/"
131 gsub(/.UNKNOWN/,_NULL,url) # can be /UNKNOWN?UNKNOWN
132 sub(/\?[^\/]+/,_NULL,url) # remove paging specs if any
133 gsub(/ /,"%20",url) # just in case.
134
135 if (!(x = ENVIRON["TNS_CMT_INDENT"]/1)) x = 14 # default
136
137 value = _rcget("tpl.var.tw.page.cmt.last")
138
139 # trigger paging if appropriate.
140 blkno = _rcget("cgi.paging")/1
141 if (blkno != 0) {
142 blksize = _rcget("TNS_PAGER_BLKSIZE",2)
143 if ((blksize/=1) <= 0) blksize = 10
144 # A negative page value means last page.
145 if (blkno < 0) blkno = int(_TBLS[1,0]/blksize+1)
146 first = blkno * blksize - blksize + 1
147 last = first + blksize - 1
148 }
149
150 # Make sure output file is cleared, in case we are re-using
151 # an old temporary file and we have nothing to print to it.
152 _creat(outfile,_O_TRUNC)
153
154 # read input table.
155 # cmt, ctime, subject, tree, banned, gravatar, creau, text
156
157 i=1
158 while (split(_TBLS[1,i++],a,"\t")) {
159
160 # Handle output paging.
161 if (blkno && ++tot && (++curr < first || curr > last)) continue
162
163 # Provide a machine-readable format.
164 #_wsresponse(1,i-1,url a[1])
165 #_wsresponse(2,i-1,a[3])
166
167 if (a[5] != _NULL) {
168 # This is expected to be already XML-encoded.
169 a[3] = ENVIRON["TNS_BANNED_SUBJECT"]
170 if (a[3] == _NULL)
171 a[3] = a[8] = _nlsmap(_NULL,"banned","cmtList")
172 } else a[3] = _xmlencode(a[3])
173
174 # gravatar support.
175 tmp = ENVIRON["TNS_GRAVATAR_URL"] "/" a[6] \
176 "?d=identicon&r=" ENVIRON["TNS_GRAVATAR_RATING"]
177
178 # flat listing.
179 if (_rcget("TNS_GROUP_MISC_PROP",5) ~ /^f/) {
180
181 url = ENVIRON["CSA_RPC_URI"] "/" ENVIRON["CSA_LANG"] "/" \
182 _uriencode(_rcget("tpl.var.tw.group",1),1) "/" \
183 _uriencode(_rcget("tpl.var.tw.page",1),1) "/comment/" a[1]
184
185 url = url "?5=" _rcget("cgi.paging",1)/1
186
187 printf(fmt3,a[1],_xmlencode(a[8]),tmp,_xmlencode(a[7]),\
188 _localdate(a[2],1),_xmlencode(url)) > outfile
189 }
190
191 # threaded listing.
192 else {
193 # mark current comment if applicable.
194 if (a[1] == value)
195 printf(fmt2,x*(length(a[4])-1),\
196 _xmlencode(url a[1]) "?5=" blkno,\
197 a[3],_localdate(a[2],1),_xmlencode(tmp)) > outfile
198 else
199 printf(fmt1,x*(length(a[4])-1),\
200 _xmlencode(url a[1]) "?5=" blkno,\
201 a[3],_localdate(a[2],1),_xmlencode(tmp)) > outfile
202 }
203 }
204
205 close(outfile)
206
207 # show the pager if appropriate.
208 if (blkno && tot > blksize) {
209
210 # Set pager output format string.
211
212 fmt1 = readfmt("tw-pager")
213 gsub(/%/,"%%",fmt1) # turn plain '%' into '%%'.
214 gsub(/\\/,"\\\\&",fmt1) # turn '\' into '\\'.
215 gsub(/[\n\r]+/,"",fmt1) # just in case.
216
217 # the pager format string is currently not customizable.
218 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
219 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
220
221 # encode any extra markers.
222 gsub(//,"\\<tw:s/\\>",fmt1)
223
224 fmt1 = fmt1 "\n"
225
226 url = ENVIRON["CSA_RPC_URI"] "/" \
227 ENVIRON["CSA_LANG"] "/" \
228 _rcget("tbl_group.g_uri") "/" \
229 _rcget("tbl_page.p_uri") "/comment"
230
231 # modify here to include the search parameters (if any)
232 # in the pager links..
233 #if ((tmp=_rcget("cgi.subcat")) != _NULL) url = url "/" tmp "/"
234 #else url = url "?3="
235
236 url = url "?4="
237
238 tot % blksize ? tmp = 1 : tmp = 0
239
240 _response("tpl.var.tw.pager",\
241 pager(blkno,int(tot/blksize)+tmp,url,fmt1))
242 }
243
244 # Default page title to page name and set page subcat name if any.
245
246 value = tmp = _rcget("tpl.var.tw.page",1)
247
248 if ((tmp=getcat(tmp)) != _NULL) {
249 _response("tpl.var.tw.page.subcat",tmp)
250 sub(/[^.]+\./,_NULL,value)
251 _html("show","tpl.if.tw.subcat","tpl.fi.tw.subcat")
252 } else _html("hide","tpl.if.tw.subcat","tpl.fi.tw.subcat")
253
254 value = _rcget("tbl_group.g_descr") ": " \
255 value " (" _nlsmap(_NULL,"comments") ")"
256 _response("tpl.var.html.title",value)
257
258 # generic template conditionals.
259
260 ifsections()
261 }
262 }
263
264 # EOF