OSDN Git Service

* version up.
[modchxj/mod_chxj.git] / include / mod_chxj.h
1 /*
2  * Copyright (C) 2005-2008 Atsushi Konno All rights reserved.
3  * Copyright (C) 2005 QSDN,Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef __MOD_CHXJ_H__
18 #define __MOD_CHXJ_H__
19
20 #define CHXJ_TRUE    (1)
21 #define CHXJ_FALSE   (0)
22
23 /* #define DEBUG_FD(X,args...) {FILE*fp=fopen("/tmp/error.log", "ab");fprintf(fp, X, ##args);fflush(fp);fclose(fp);} */
24
25 #ifdef UNUSED
26 #elif defined(__GNUC__)
27 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
28 #elif defined(__LCLINT__)
29 # define UNUSED(x) /*@unused@*/ x
30 #else
31 # define UNUSED(x) x
32 #endif
33
34
35 #include <string.h>
36
37 #include "chxj_apache.h"
38
39 #include "apr_strings.h"
40 #include "apr_buckets.h"
41 #include "apr_lib.h"
42 #include "apr_tables.h"
43 #include "apr_dso.h"
44 #include "apr_general.h"
45 #include "apr_pools.h"
46
47 #if defined(AP_NEED_SET_MUTEX_PERMS)
48 #  include "unixd.h"
49 #endif
50 #if !defined(AP_NEED_SET_MUTEX_PERMS)
51 #  if !defined(OS2) && !defined(WIN32) && !defined(BEOS) && !defined(NETWARE)
52 #    define AP_NEED_SET_MUTEX_PERMS
53 #  endif
54 #endif
55
56 #define STRCASEEQ(a,b,c,d) \
57   ((((a) == *(d))|| ((b) == *(d))) && strcasecmp((c),(d)) == 0)
58 #define STRNCASEEQ(a,b,c,d,e) \
59   ((((a) == *(d))|| ((b) == *(d))) && strncasecmp((c),(d),(e)) == 0)
60
61 #include "qs_ignore_sp.h"
62 #include "qs_log.h"
63 #include "qs_malloc.h"
64 #include "qs_parse_attr.h"
65 #include "qs_parse_file.h"
66 #include "qs_parse_string.h"
67 #include "qs_parse_tag.h"
68
69 #if defined(HAVE_AP_REGEX_H) && HAVE_AP_REGEX_H == 1
70 #  include "ap_regex.h"
71 #else
72 #  include "pcreposix.h"
73 #  include "pcre.h"
74 #  if !defined(AP_REG_ASSERT)
75 #    define AP_REG_ASSERT   REG_ASSERT
76 #  endif
77 #  if !defined(AP_REG_BADBR)
78 #    define AP_REG_BADBR    REG_BADBR
79 #  endif
80 #  if !defined(AP_REG_BADPAT)
81 #    define AP_REG_BADPAT   REG_BADPAT
82 #  endif
83 #  if !defined(AP_REG_BADRPT)
84 #    define AP_REG_BADRPT   REG_BADRPT
85 #  endif
86 #  if !defined(AP_REG_EBRACE)
87 #    define AP_REG_EBRACE   REG_EBRACE
88 #  endif
89 #  if !defined(AP_REG_EBRACK)
90 #    define AP_REG_EBRACK   REG_EBRACK
91 #  endif
92 #  if !defined(AP_REG_ECOLLATE)
93 #    define AP_REG_ECOLLATE REG_ECOLLATE
94 #  endif
95 #  if !defined(AP_REG_ECTYPE)
96 #    define AP_REG_ECTYPE   REG_ECTYPE
97 #  endif
98 #  if !defined(AP_REG_EESCAPE)
99 #    define AP_REG_EESCAPE  REG_EESCAPE
100 #  endif
101 #  if !defined(AP_REG_EMPTY)
102 #    define AP_REG_EMPTY    REG_EMPTY
103 #  endif
104 #  if !defined(AP_REG_EPAREN)
105 #    define AP_REG_EPAREN   REG_EPAREN
106 #  endif
107 #  if !defined(AP_REG_ERANGE)
108 #    define AP_REG_ERANGE   REG_ERANGE
109 #  endif
110 #  if !defined(AP_REG_ESIZE)
111 #    define AP_REG_ESIZE    REG_ESIZE
112 #  endif
113 #  if !defined(AP_REG_ESPACE)
114 #    define AP_REG_ESPACE   REG_ESPACE
115 #  endif
116 #  if !defined(AP_REG_ESUBREG)
117 #    define AP_REG_ESUBREG  REG_ESUBREG
118 #  endif
119 #  if !defined(AP_REG_INVARG)
120 #    define AP_REG_INVARG   REG_INVARG
121 #  endif
122 #  if !defined(AP_REG_NOMATCH)
123 #    define AP_REG_NOMATCH  REG_NOMATCH
124 #  endif
125
126 #  if !defined(AP_REG_ICASE)
127 #    define AP_REG_ICASE    REG_ICASE
128 #  endif
129 #  if !defined(AP_REG_NEWLINE)
130 #    define AP_REG_NEWLINE  REG_NEWLINE
131 #  endif
132 #  if !defined(AP_REG_NOTBOL)
133 #    define AP_REG_NOTBOL   REG_NOTBOL
134 #  endif
135 #  if !defined(AP_REG_NOTEOL)
136 #    define AP_REG_NOTEOL   REG_NOTEOL
137 #  endif
138
139 #  if !defined(AP_REG_EXTENDED)
140 #    define AP_REG_EXTENDED REG_EXTENDED
141 #  endif
142 #  if !defined(AP_REG_NOSUB)
143 #    define AP_REG_NOSUB    REG_NOSUB
144 #  endif
145
146 #  define ap_regex_t      regex_t
147 #  define ap_regmatch_t   regmatch_t
148 #endif
149
150
151
152
153 #ifdef HAVE_APR_GLOBAL_MUTEX_H
154 #  include "apr_global_mutex.h"
155 #endif
156
157 #ifdef HAVE_APR_SHM_H
158 #  include "apr_shm.h"
159 #endif
160
161
162 typedef struct imode_emoji_t imode_emoji_t;
163
164 struct imode_emoji_t {
165   char  hex1byte;
166   char  hex2byte;
167   char* string;
168   char *description;
169 };
170
171 typedef struct ezweb_emoji_t ezweb_emoji_t;
172
173 struct ezweb_emoji_t {
174   char* typeA;
175   char* typeB;
176   char* typeC;
177   char* typeD;
178 };
179
180 typedef struct jphone_emoji_t jphone_emoji_t;
181
182 struct jphone_emoji_t {
183   char* string;
184 };
185
186 typedef struct emoji_t emoji_t;
187
188 struct emoji_t {
189   struct emoji_t*  next;
190   int              no;
191   imode_emoji_t*   imode;
192   ezweb_emoji_t*   ezweb;
193   jphone_emoji_t*  jphone;
194 };
195
196 typedef struct chxjconvrule_entry chxjconvrule_entry;
197
198 struct chxjconvrule_entry {
199   char         *pattern;
200   ap_regex_t   *regexp;
201   int          flags;
202   int          action;
203   char         *encoding;
204   int          pc_flag;
205   char         *user_agent;
206 };
207
208 typedef struct tag_handler tag_handler;
209
210 struct tag_handler {
211   char* (*start_tag_handler)(void* doc, Node* node); 
212   char* (*end_tag_handler)(void* doc, Node* node); 
213 };
214
215 #include "chxj_specified_device.h"
216
217 typedef struct {
218   spec_type    type; 
219   tag_handler* handler;
220 } tag_handlers;
221
222 typedef enum {
223   tagHTML = 0,
224   tagMETA,  
225   tagTEXTAREA,
226   tagP,
227   tagPRE,
228   tagUL,
229   tagLI,
230   tagOL,
231   tagH1,
232   tagH2,
233   tagH3,
234   tagH4,
235   tagH5,
236   tagH6,
237   tagHEAD,
238   tagTITLE,
239   tagBASE,
240   tagBODY,
241   tagA,
242   tagBR,
243   tagTABLE,
244   tagTR,
245   tagTD,
246   tagTBODY,
247   tagFONT,
248   tagFORM,
249   tagINPUT,
250   tagCENTER,
251   tagHR,
252   tagIMG,
253   tagSELECT,
254   tagOPTION,
255   tagDIV,
256   tagCHXJIF,  
257   tagNOBR,  
258   tagSMALL,  
259   tagSTYLE,  
260   tagSPAN,  
261   tagTEXT,  
262   tagTH,  
263   tagB,
264   tagFIELDSET,
265   tagDT,
266   tagLEGEND,
267   tagLABEL,
268   tagBLOCKQUOTE,
269   tagDIR,
270   tagDL,
271   tagDD,
272   tagMENU,
273   tagPLAINTEXT,
274   tagBLINK,
275   tagMARQUEE,
276   tagNLMARK,      /* New Line Code */
277 } tag_type;
278
279 typedef struct mod_chxj_config mod_chxj_config;
280
281 #if defined(USE_MYSQL_COOKIE)
282 #  include "chxj_mysql.h"
283 #endif
284 #if defined(USE_MEMCACHE_COOKIE)
285 #  include "chxj_memcache.h"
286 #endif
287
288 /* cookie store type */
289 #define CHXJ_COOKIE_STORE_TYPE_DBM      "dbm"
290 #define CHXJ_COOKIE_STORE_TYPE_MYSQL    "mysql"
291 #define CHXJ_COOKIE_STORE_TYPE_MEMCACHE "memcache"
292 typedef enum {
293   COOKIE_STORE_TYPE_NONE     = 0,
294   COOKIE_STORE_TYPE_DBM      = 1,
295   COOKIE_STORE_TYPE_MYSQL    = 2,
296   COOKIE_STORE_TYPE_MEMCACHE = 3, 
297 } cookie_store_type_t;
298
299 /* new line type */
300 #define CHXJ_NEW_LINE_TYPE_CRLF         "crlf"
301 #define CHXJ_NEW_LINE_TYPE_LF           "lf"
302 #define CHXJ_NEW_LINE_TYPE_CR           "cr"
303 #define CHXJ_NEW_LINE_TYPE_NONE         "none"
304 typedef enum {
305   NLTYPE_NIL  = 0,
306   NLTYPE_CRLF = 1,
307   NLTYPE_LF   = 2,
308   NLTYPE_CR   = 3,
309   NLTYPE_NONE = 4,
310 } chxj_new_line_type_t;
311 #define IS_NLTYPE_CRLF(X)   ((X)->new_line_type == NLTYPE_CRLF)
312 #define IS_NLTYPE_LF(X)     ((X)->new_line_type == NLTYPE_LF)
313 #define IS_NLTYPE_CR(X)     ((X)->new_line_type == NLTYPE_CR)
314 #define IS_NLTYPE_NONE(X)   ((X)->new_line_type == NLTYPE_NONE)
315 #define TO_NLCODE(X)        (IS_NLTYPE_CRLF(X) ? "\r\n" : \
316                              IS_NLTYPE_LF(X)   ? "\n"   : \
317                              IS_NLTYPE_CR(X)   ? "\r"   : \
318                              IS_NLTYPE_NONE(X) ? ""     : "\r\n")
319
320 struct mod_chxj_config {
321   int                   image;
322
323   char                  *device_data_file;
324   char                  *emoji_data_file;
325
326   char                  *image_cache_dir;
327   char                  *image_copyright;
328   unsigned long         image_cache_limit;
329   device_table_list     *devices;
330   emoji_t               *emoji;
331   emoji_t               *emoji_tail;
332   char                  *server_side_encoding;
333
334   char                  *dir; /* for LOG */
335
336   apr_array_header_t    *convrules;
337
338   char                  *cookie_db_dir;
339   long                  cookie_timeout;
340   cookie_store_type_t   cookie_store_type;
341   int                   cookie_lazy_mode;
342
343 #if defined(USE_MYSQL_COOKIE)
344   mysql_t               mysql;
345 #endif
346 #if defined(USE_MEMCACHE_COOKIE)
347   memcache_t            memcache;
348 #endif
349   char                  *forward_url_base;  /* use input filter */
350   char                  *forward_server_ip; /* use input filter */
351
352   chxj_new_line_type_t  new_line_type;
353 };
354
355 #define IS_COOKIE_STORE_DBM(X)      ((X) == COOKIE_STORE_TYPE_DBM)
356 #define IS_COOKIE_STORE_MYSQL(X)    ((X) == COOKIE_STORE_TYPE_MYSQL)
357 #define IS_COOKIE_STORE_MEMCACHE(X) ((X) == COOKIE_STORE_TYPE_MEMCACHE)
358 #define IS_COOKIE_STORE_NONE(X)     ((X) == COOKIE_STORE_TYPE_NONE)
359
360 #define COOKIE_LAZY_ON                (2)
361 #define COOKIE_LAZY_OFF               (1)
362 #define IS_COOKIE_LAZY(X)             ((X)->cookie_lazy_mode == COOKIE_LAZY_ON)
363
364
365 #define CONVRULE_ENGINE_ON_BIT        (0x00000001)
366 #define CONVRULE_ENGINE_OFF_BIT       (0x00000002)
367 #define CONVRULE_COOKIE_ON_BIT        (0x00000004)
368
369 #define CONVRULE_ENGINE_ON_CMD        "EngineOn"
370 #define CONVRULE_ENGINE_OFF_CMD       "EngineOff"
371 #define CONVRULE_COOKIE_ON_CMD        "CookieOn"
372 #define CONVRULE_COOKIE_OFF_CMD       "CookieOff"
373
374
375 #define CONVRULE_FLAG_NOTMATCH        (0x00000001)
376
377 #define CONVRULE_PC_FLAG_ON_CMD       "PC"
378
379 #define CONVRULE_PC_FLAG_ON_BIT       (0x00000001)
380 #define CONVRULE_PC_FLAG_OFF_BIT      (0x00000002)
381
382
383 typedef struct {
384   apr_global_mutex_t* cookie_db_lock;
385 } mod_chxj_global_config;
386
387 typedef struct {
388   apr_size_t len;
389   chxjconvrule_entry  *entryp;
390   device_table        *spec;
391
392   apr_bucket_brigade *bb;
393   apr_pool_t *pool;
394
395   char* buffer;
396 } mod_chxj_ctx;
397
398 #include "chxj_tag_util.h"
399
400 #define CHXJ_MOD_CONFIG_KEY   "chxj_module_key"
401
402 #define HTTP_USER_AGENT       "User-Agent"
403 #define HTTP_HOST             "Host"
404 #define CHXJ_HTTP_USER_AGENT  "CHXJ_HTTP_USER_AGENT"
405 #define CHXJ_HEADER_ORIG_CLIENT_IP "X-Chxj-Orig-Client-Ip"
406
407 module AP_MODULE_DECLARE_DATA chxj_module;
408
409 #define CHXJ_IMG_ON     (2)
410 #define CHXJ_IMG_OFF    (1)
411 #define CHXJ_IMG_NONE   (0)
412
413
414 #define DBG(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_DEBUG,0,(request_rec*)(X),##args)
415 #define SDBG(X,Y)       chxj_log_error(APLOG_MARK,APLOG_DEBUG,0,(X),(Y))
416 #define PDBG(X,args...) chxj_log_perror(APLOG_MARK,APLOG_DEBUG,0,(apr_pool_t *)(X),##args)
417 #define ERR(X,args...)  chxj_log_rerror(APLOG_MARK,APLOG_ERR,0,(X), ##args)
418 #define SERR(X,args...) chxj_log_error(APLOG_MARK,APLOG_ERR,0,(X),##args)
419 #define PERR(X,args...) chxj_log_perror(APLOG_MARK,APLOG_ERR,0,(apr_pool_t *)(X),##args)
420 #define WRN(rec,format,args...)  chxj_log_rerror(APLOG_MARK,APLOG_WARNING,0,(rec),(format), ##args)
421
422 extern tag_handlers chxj_tag_handlers[];
423 extern tag_handler  chtml10_handler[];
424 extern tag_handler  chtml20_handler[];
425 extern tag_handler  chtml30_handler[];
426 extern tag_handler  chtml40_handler[];
427 extern tag_handler  chtml50_handler[];
428 extern tag_handler  xhtml_handler[];
429 extern tag_handler  hdml_handler[];
430 extern tag_handler  jhtml_handler[];
431 extern tag_handler  jxhtml_handler[];
432
433 extern char* chxj_node_convert( 
434   device_table *spec,
435   request_rec  *r,
436   void         *pdoc,
437   Doc          *doc,
438   Node         *node,
439   int          indent
440 );
441
442 #define IMAGE_CACHE_LIMIT_FMT_LEN  (20)
443
444
445 #if HAVE_MALLOC == 0
446 extern void *rpl_malloc(size_t n);
447 #endif
448
449 #endif
450 /*
451  * vim:ts=2 et
452  */