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
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
// This is a roxen module. Copyright © 2000 - 2009, Roxen IS. 
// 
 
constant cvs_version="$Id: wiretap.pike,v 1.36 2009/05/07 14:15:54 mast Exp $"; 
 
#include <module.h> 
inherit "module"; 
 
 
//---------------------- Module Registration -------------------------------- 
 
constant module_type   = MODULE_TAG; 
constant module_name   = "Tags: HTML color wiretap"; 
constant module_doc    =  
#"<p>Parses HTML tags for the text and background colors all over the 
page. This information can be used to let graphical modules like 
\"Graphic text\" and \"GButton\" generate images that automatically 
blend into the page.</p> 
 
<p>Note that this module can degrade performance, since it causes many 
common HTML tags to be parsed by the RXML parser.</p>"; 
constant thread_safe   = 1; 
 
void create() 
{ 
  defvar("colorparsing", ({ "td", "layer", "ilayer", "table" }), 
         "Tags to parse for color", 
         TYPE_STRING_LIST|VAR_NOT_CFIF, 
         "Which tags should be parsed for document colors? " 
         "This will affect documents without gtext as well as documents " 
         "with it, the parsing time is relative to the number of parsed " 
         "tags in a document."); 
 
  defvar("colormode", 0, "Normalize colors in parsed tags", 
         TYPE_FLAG|VAR_NOT_CFIF, #"\ 
If set, replace 'roxen' colors (@c,m,y,k etc) with 'netscape' 
colors (#rrggbb). This means that if this is enabled, Roxen will 
rewrite the parsed tags, which can potentially cause problems with 
quotes etc if they aren't correcly written. Setting this to off will 
also lessen the performance impact of the 'Tags to parse for color' 
option quite dramatically. You can try this out with the &lt;gauge&gt; 
tag."); 
} 
 
 
TAGDOCUMENTATION 
#ifdef manual 
constant tagdoc = ([ "body" : 
#"<desc type='cont'><p> 
<short>The color wiretap functionality is active in the content.</short></p> 
</desc> 
 
<attr name='wiretap' value='yes|no'> 
<p>The color wiretap can be disabled for an individual web page by 
adding <tt>wiretap='no'</tt> to the document's <tag>body</tag> tag.</p> 
</attr>" ]); 
#endif 
 
 
 
// -------------------- The actual wiretap code  -------------------------- 
 
 
//  When < 2.2 compatibility is requested we tap all tags all the time 
//  unlike the newer design where we require a <body> tag to enable 
//  the wiretap. In that case this is the tag set for the runtime 
//  tags. 
RXML.TagSet runtime_wiretap_tags = RXML.TagSet (this_module(), "runtime"); 
 
// The currently implemented settings. 
int compat_mode, colormode; 
array(string) colorparsing; 
 
array(RXML.Tag) get_tag_variants(string tag_name, mixed tag) 
{ 
  return map(Array.uniq( ({ tag_name, 
                            upper_case(tag_name), 
                            lower_case(tag_name), 
                            String.capitalize(lower_case(tag_name)) }) ), 
             tag); 
} 
 
void init_tag_set (int new_compat_mode, 
                   array(string) new_colorparsing, 
                   int new_colormode) 
{ 
  //  Register start and end tags individually so we can handle the 
  //  <body> container properly. If compatibility with 2.1 or earlier 
  //  is needed we register all tags at once. 
 
  array(RXML.Tag) body_tags = 
    get_tag_variants ("body", TagBody) + 
    get_tag_variants ("body", TagEndBody); 
  array(RXML.Tag) non_body_tags = ({}); 
 
  foreach(new_colorparsing, string tag) 
    if (lower_case(tag) != "body") { 
      non_body_tags += 
        get_tag_variants(tag, TagPushColor) + 
        get_tag_variants(tag, TagPopColor); 
    } 
 
  module_tag_set->clear(); 
  runtime_wiretap_tags->clear(); 
 
  if (new_compat_mode) 
    module_tag_set->add_tags (body_tags + non_body_tags); 
  else { 
    module_tag_set->add_tags (body_tags); 
    runtime_wiretap_tags->add_tags (non_body_tags); 
  } 
 
  compat_mode = new_compat_mode; 
  colorparsing = new_colorparsing; 
  colormode = new_colormode; 
} 
 
 
class TagBody 
{ 
  inherit RXML.Tag; 
 
  string name; 
  constant flags = (RXML.FLAG_EMPTY_ELEMENT | 
                    RXML.FLAG_COMPAT_PARSE | 
                    RXML.FLAG_NO_PREFIX); 
 
  // Cached settings. 
  int body_compat_mode = compat_mode, body_colormode = colormode; 
  RXML.TagSet body_runtime_wiretap_tags =runtime_wiretap_tags; 
   
  void create(string _name) 
  { 
    name = _name; 
  } 
 
  class Frame 
  { 
    inherit RXML.Frame; 
    string raw_tag_text; 
 
    array do_return(RequestID id) 
    { 
      //  Check if <body> attribute disables wiretap 
      if (args["wiretap"] != "no") { 
        //  Register temporary tags unless we're running in compatibility 
        //  mode. 
        if (!body_compat_mode) { 
          RXML.Context ctx = RXML_CONTEXT; 
          foreach(body_runtime_wiretap_tags->get_local_tags(), RXML.Tag tag) 
            ctx->add_runtime_tag(tag); 
        } 
         
        args = 
          mkmapping(map(indices(args), lower_case), values(args)) - 
          ({ "wiretap" }); 
        if (Roxen.init_wiretap_stack(args, id, body_colormode)) 
          return ({ propagate_tag(args) }); 
      } 
      if (args["wiretap"]) 
        return ({ propagate_tag(args - ({ "wiretap" }) ) }); 
      else 
        return ({ propagate_tag() }); 
    } 
  } 
} 
 
 
class TagEndBody 
{ 
  inherit RXML.Tag; 
 
  string name, tagname; 
  constant flags = (RXML.FLAG_EMPTY_ELEMENT | 
                    RXML.FLAG_COMPAT_PARSE | 
                    RXML.FLAG_NO_PREFIX); 
   
  // Cached settings. 
  int body_compat_mode = compat_mode; 
  RXML.TagSet body_runtime_wiretap_tags =runtime_wiretap_tags; 
   
  void create(string _name) 
  { 
    tagname = _name; 
    name = "/" + _name; 
  } 
   
  class Frame 
  { 
    inherit RXML.Frame; 
    string raw_tag_text; 
     
    array do_return(RequestID id) 
    { 
      //  Unregister our temporary tags unless we're in compatibility mode 
      if (!body_compat_mode) { 
        RXML.Context ctx = RXML_CONTEXT; 
        foreach(body_runtime_wiretap_tags->get_local_tags(), RXML.Tag tag) 
          ctx->remove_runtime_tag(tag); 
      } 
      return ({ propagate_tag() }); 
    } 
  } 
} 
 
 
class TagPushColor 
{ 
  inherit RXML.Tag; 
   
  string name; 
  constant flags = (RXML.FLAG_EMPTY_ELEMENT | 
                    RXML.FLAG_COMPAT_PARSE | 
                    RXML.FLAG_NO_PREFIX); 
   
  // Cached settings. 
  int body_colormode = colormode; 
   
  void create(string _name) 
  { 
    name = _name; 
  } 
 
  class Frame 
  { 
    inherit RXML.Frame; 
    string raw_tag_text; 
     
    array do_return(RequestID id) 
    { 
      args = mkmapping(map(indices(args), lower_case), values(args)); 
      if (Roxen.push_color(name, args, id, body_colormode)) 
        return ({ propagate_tag(args) }); 
      return ({ propagate_tag() }); 
    } 
  } 
} 
 
 
class TagPopColor 
{ 
  inherit RXML.Tag; 
   
  string name, tagname; 
  constant flags = (RXML.FLAG_EMPTY_ELEMENT | 
                    RXML.FLAG_COMPAT_PARSE | 
                    RXML.FLAG_NO_PREFIX); 
   
  void create(string _name) 
  { 
    tagname = _name; 
    name = "/" + _name; 
  } 
 
  class Frame 
  { 
    inherit RXML.Frame; 
    string raw_tag_text; 
 
    array do_return(RequestID id) 
    { 
      Roxen.pop_color(tagname, id); 
      return ({ propagate_tag() }); 
    } 
  } 
} 
 
 
// --------------- Tag and container registration ---------------------- 
 
void start() 
{ 
  if (module_tag_set) { 
    int new_compat_mode = my_configuration()->query("compat_level") < "2.2"; 
    array(string) new_colorparsing = query ("colorparsing"); 
    int new_colormode = query ("colormode"); 
    if (compat_mode != new_compat_mode || 
        !equal (colorparsing, new_colorparsing) || 
        colormode != new_colormode) 
      init_tag_set (new_compat_mode, new_colorparsing, new_colormode); 
  } 
} 
 
RXML.TagSet query_tag_set() 
{ 
  if (!module_tag_set) { 
    module_tag_set = RXML.TagSet (this_module(), ""); 
    module_tag_set->add_tag_set_dependency (runtime_wiretap_tags); 
  } 
  init_tag_set (my_configuration()->query("compat_level") < "2.2", 
                query ("colorparsing"), 
                query ("colormode")); 
  return module_tag_set; 
}