4a488404e6cbaa9d914bc91be37c2e2f3154b35b
[citadel.git] / webcit / subst.h
1
2 extern HashList *Conditionals;
3 extern HashList *GlobalNS;
4 extern HashList *Iterators;
5 extern HashList *WirelessTemplateCache;
6 extern HashList *WirelessLocalTemplateCache;
7 extern HashList *TemplateCache;
8 extern HashList *LocalTemplateCache;
9
10
11 #define TYPE_STR   1
12 #define TYPE_LONG  2
13 #define TYPE_PREFSTR 3
14 #define TYPE_PREFINT 4
15 #define TYPE_GETTEXT 5
16 #define TYPE_BSTR 6
17 #define TYPE_SUBTEMPLATE 7
18 #define MAXPARAM  20
19
20
21 /*
22  * \brief Values for wcs_type
23  */
24 enum {
25         WCS_STRING,       /* its a string */
26         WCS_FUNCTION,     /* its a function callback */
27         WCS_SERVCMD,      /* its a command to send to the citadel server */
28         WCS_STRBUF,       /* its a strbuf we own */
29         WCS_STRBUF_REF,   /* its a strbuf we mustn't free */
30         WCS_LONG          /* its an integer */
31 };
32
33 typedef struct WCTemplateToken WCTemplateToken;
34 typedef struct WCTemplputParams WCTemplputParams;
35 typedef void (*WCHandlerFunc)(StrBuf *Target, WCTemplputParams *TP);
36
37 typedef struct _TemplateParam {
38         const char *Start;
39         int Type;
40         long len;
41         long lvalue;
42 } TemplateParam;
43
44 /* make a template token a lookup key: */
45 #define TKEY(a) TP->Tokens->Params[a]->Start, TP->Tokens->Params[a]->len
46 struct WCTemplateToken {
47         const StrBuf *FileName; /* Reference to print error messages; not to be freed */
48         StrBuf *FlatToken;
49         long Line;
50         const char *pTokenStart;
51         size_t TokenStart;
52         size_t TokenEnd;
53         const char *pTokenEnd;
54         int Flags;
55         void *PreEval;
56
57         const char *pName;
58         size_t NameEnd;
59
60         int HaveParameters;
61         int nParameters;
62         TemplateParam *Params[MAXPARAM];
63 };
64
65
66 /*
67  * \brief Dynamic content for variable substitution in templates
68  */
69 typedef struct _wcsubst {
70         int wcs_type;                       /* which type of Substitution are we */
71         char wcs_key[32];                   /* copy of our hashkey for debugging */
72         StrBuf *wcs_value;                  /* if we're a string, keep it here */
73         long lvalue;                        /* type long? keep data here */
74         int ContextRequired;                /* do we require a context type? */
75         WCHandlerFunc wcs_function; /* funcion hook ???*/
76 } wcsubst;
77
78 struct WCTemplputParams {
79         int nArgs;
80         WCTemplateToken *Tokens;
81         void *Context;
82         int ContextType;
83 };
84
85
86 extern WCTemplputParams NoCtx;
87
88 #define CTX TP->Context
89
90
91
92 #define CTX_NONE 0
93 #define CTX_SITECFG 1
94 #define CTX_SESSION 2
95 #define CTX_INETCFG 3
96 #define CTX_VNOTE 4
97 #define CTX_WHO 5
98 #define CTX_PREF 6
99 #define CTX_NODECONF 7
100 #define CTX_USERLIST 8
101 #define CTX_MAILSUM 9
102 #define CTX_MIME_ATACH 10
103 #define CTX_FILELIST 11
104 #define CTX_STRBUF 12
105 #define CTX_LONGVECTOR 13
106 #define CTX_ROOMS 14
107 #define CTX_FLOORS 15
108
109 void RegisterNS(const char *NSName, long len, 
110                 int nMinArgs, 
111                 int nMaxArgs, 
112                 WCHandlerFunc HandlerFunc,
113                 int ContextRequired);
114 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
115
116 typedef int (*WCConditionalFunc)(StrBuf *Target, WCTemplputParams *TP);
117 typedef struct _ConditionalStruct {
118         const char *PlainName;
119         int nParams;
120         int ContextRequired;
121         WCConditionalFunc CondF;
122 } ConditionalStruct;
123 void RegisterConditional(const char *Name, long len, 
124                          int nParams,
125                          WCConditionalFunc CondF, 
126                          int ContextRequired);
127
128
129
130 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, WCTemplputParams *TP);
131 typedef HashList *(*RetrieveHashlistFunc)(StrBuf *Target, WCTemplputParams *TP);
132 typedef void (*HashDestructorFunc) (HashList **KillMe);
133 void RegisterITERATOR(const char *Name, long len, /* Our identifier */
134                       int AdditionalParams,       /* doe we use more parameters? */
135                       HashList *StaticList,       /* pointer to webcit lifetime hashlists */
136                       RetrieveHashlistFunc GetHash, /* else retrieve the hashlist by calling this function */
137                       SubTemplFunc DoSubTempl,       /* call this function on each iteration for svput & friends */
138                       HashDestructorFunc Destructor, /* use this function to shut down the hash; NULL if its a reference */
139                       int ContextType,               /* which context do we provide to the subtemplate? */
140                       int XPectContextType,          /* which context do we expct to be called in? */
141                       int Flags);
142
143 #define IT_NOFLAG 0
144 #define IT_FLAG_DETECT_GROUPCHANGE (1<<0)
145
146 #define RegisterIterator(a, b, c, d, e, f, g, h, i) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g, h, i)
147
148 void GetTemplateTokenString(WCTemplputParams *TP,
149                             int N,
150                             const char **Value, 
151                             long *len);
152
153
154 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
155 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
156 void SVPutLong(char *keyname, size_t keylen, long Data);
157 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
158 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
159 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
160 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
161 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
162 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
163
164 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
165 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)
166
167 void DoTemplate(const char *templatename, long len, StrBuf *Target, WCTemplputParams *TP);
168 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, &NoCtx);
169 void url_do_template(void);
170
171 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
172 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
173
174 void StrBufAppendTemplate(StrBuf *Target, 
175                           WCTemplputParams *TP,
176                           const StrBuf *Source, 
177                           int FormatTypeIndex);
178 CompareFunc RetrieveSort(WCTemplputParams *TP, 
179                          const char *OtherPrefix, long OtherPrefixLen,  
180                          const char *Default, long ldefault, 
181                          long DefaultDirection);
182 void RegisterSortFunc(const char *name, long len, 
183                       const char *prepend, long preplen,
184                       CompareFunc Forward, 
185                       CompareFunc Reverse, 
186                       CompareFunc GroupChange, 
187                       long ContextType);
188
189 void dbg_print_longvector(long *LongVector);
190
191
192 #define ERR_NAME 0
193 #define ERR_PARM1 1
194 #define ERR_PARM2 2
195 void LogTemplateError (StrBuf *Target, 
196                        const char *Type, 
197                        int ErrorPos, 
198                        WCTemplputParams *TP, 
199                        const char *Format, ...);