eb014f43852f55452d9afab34c491feccb266abf
[citadel.git] / webcit / messages.h
1 /*
2  * Copyright (c) 1996-2013 by the citadel.org team
3  *
4  * This program is open source software.  You can redistribute it and/or
5  * modify it under the terms of the GNU General Public License, version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 extern CtxType CTX_MAILSUM;
14 extern CtxType CTX_MIME_ATACH;
15 extern HashList *MsgHeaderHandler;
16 extern HashList *MimeRenderHandler;
17 extern HashList *ReadLoopHandler;
18 typedef struct wc_mime_attachment wc_mime_attachment;
19 typedef void (*RenderMimeFunc)(StrBuf *Target, WCTemplputParams *TP, StrBuf *FoundCharset);
20 typedef struct _RenderMimeFuncStruct {
21         RenderMimeFunc f;
22 } RenderMimeFuncStruct;
23
24 struct wc_mime_attachment {
25         int level;
26         StrBuf *Name;
27         StrBuf *FileName;
28         StrBuf *PartNum;
29         StrBuf *Disposition;
30         StrBuf *ContentType;
31         StrBuf *Charset;
32         StrBuf *Data;
33         size_t length;          /* length of the mimeattachment */
34         long size_known;
35         long lvalue;            /* if we put a long... */
36         long msgnum;            /* the message number on the citadel server derived from message_summary */
37         const RenderMimeFuncStruct *Renderer;
38 };
39 void DestroyMime(void *vMime);
40
41 #define MSGFLAG_READ (1<<0)
42
43 typedef struct _message_summary {
44         long msgnum;            /* the message number on the citadel server */
45         int Flags;
46
47         time_t date;            /* its creation date */
48         int nhdr;
49         int format_type;
50         StrBuf *euid;
51         StrBuf *from;           /* the author */
52         StrBuf *to;             /* the recipient */
53         StrBuf *subj;           /* the title / subject */
54         StrBuf *reply_inreplyto;
55         long    reply_inreplyto_hash;
56         StrBuf *reply_references;
57         long    reply_references_hash;
58         StrBuf *ReplyTo;
59         StrBuf *cccc;
60         StrBuf *hnod;
61         StrBuf *AllRcpt;
62         StrBuf *Room;
63         StrBuf *Rfca;
64         StrBuf *EnvTo;
65         StrBuf *OtherNode;
66         const StrBuf *PartNum;
67
68         HashList *Attachments;  /* list of attachments */
69         HashList *Submessages;
70         HashList *AttachLinks;
71
72         HashList *AllAttach;
73
74         int hasattachments;
75
76
77         /* The mime part of the message */
78         wc_mime_attachment *MsgBody;
79 } message_summary;
80 void DestroyMessageSummary(void *vMsg);
81
82
83
84 static inline message_summary* GetMessagePtrAt(int n, HashList *Summ)
85 {
86         const char *Key;
87         long HKLen;
88         void *vMsg;
89
90         if (Summ == NULL)
91                 return NULL;
92         GetHashAt(Summ, n, &HKLen, &Key, &vMsg);
93         return (message_summary*) vMsg;
94 }
95
96 typedef void (*ExamineMsgHeaderFunc)(message_summary *Msg, StrBuf *HdrLine, StrBuf *FoundCharset);
97
98 void evaluate_mime_part(StrBuf *Target, WCTemplputParams *TP);
99
100
101 typedef enum _eCustomRoomRenderer {
102         eUseDefault = VIEW_JOURNAL + 100, 
103         eReadEUIDS
104 }eCustomRoomRenderer;
105
106 enum {
107         do_search,
108         headers,
109         readfwd,
110         readnew,
111         readold,
112         readgt,
113         readlt
114 };
115
116 /**
117  * @brief function to parse the | separated message headers list
118  * @param Line the raw line with your message data
119  * @param Msg put your parser results here...
120  * @param ConversionBuffer if you need some workbuffer, don't free me!
121  * @returns 0: failure, trash this message. 1: all right, store it
122  */
123 typedef int (*load_msg_ptrs_detailheaders) (StrBuf *Line, 
124                                             const char **pos, 
125                                             message_summary *Msg, 
126                                             StrBuf *ConversionBuffer);
127
128 typedef void (*readloop_servcmd)(char *buf, long bufsize);
129
130 typedef struct _readloopstruct {
131         ConstStr name;
132         readloop_servcmd cmd;
133 } readloop_struct;
134
135 extern readloop_struct rlid[];
136
137 void readloop(long oper, eCustomRoomRenderer ForceRenderer);
138 int read_message(StrBuf *Target, 
139                  const char *tmpl, long tmpllen, 
140                  long msgnum, 
141                  const StrBuf *section, 
142                  const StrBuf **OutMime);
143 int load_message(message_summary *Msg, 
144                  StrBuf *FoundCharset,
145                  StrBuf **Error);
146
147
148
149
150 typedef struct _SharedMessageStatus {
151         long load_seen;        /* should read information be loaded */
152         long sortit;           /* should we sort it using the standard sort API? */
153         long defaultsortorder; /* if we should sort it, which direction should be the default? */
154
155         long maxload;          /* how many headers should we accept from the server? defaults to 10k */
156         long maxmsgs;          /* how many message bodies do you want to load at most?*/
157
158         long startmsg;         /* which is the start message? */
159         long nummsgs;          /* How many messages are available to your view? */
160         long numNewmsgs;       /* if you load the seen-status, this is the count of them. */
161         long num_displayed;    /* counted up for LoadMsgFromServer */ /* TODO: unclear who should access this and why */
162
163         long lowest_found;     /* smallest Message ID found;  */
164         long highest_found;    /* highest Message ID found;  */
165
166 } SharedMessageStatus;
167
168 int load_msg_ptrs(const char *servcmd,
169                   const char *filter,
170                   SharedMessageStatus *Stat, 
171                   load_msg_ptrs_detailheaders LH);
172
173 typedef int (*GetParamsGetServerCall_func)(SharedMessageStatus *Stat, 
174                                            void **ViewSpecific, 
175                                            long oper, 
176                                            char *cmd, 
177                                            long len,
178                                            char *filter,
179                                            long flen);
180
181 typedef int (*PrintViewHeader_func)(SharedMessageStatus *Stat, void **ViewSpecific);
182
183 typedef int (*LoadMsgFromServer_func)(SharedMessageStatus *Stat, 
184                                       void **ViewSpecific, 
185                                       message_summary* Msg, 
186                                       int is_new, 
187                                       int i);
188
189 typedef int (*RenderView_or_Tail_func)(SharedMessageStatus *Stat, 
190                                        void **ViewSpecific, 
191                                        long oper);
192 typedef int (*View_Cleanup_func)(void **ViewSpecific);
193
194 void RegisterReadLoopHandlerset(
195         /**
196          * RoomType: which View definition are you going to be called for
197          */
198         int RoomType,
199
200         /**
201          * GetParamsGetServerCall should do the following:
202          *  * allocate your private context structure
203          *  * evaluate your commandline arguments, put results to your private struct.
204          *  * fill cmd with the command to load the message pointer list:
205          *    * might depend on bstr/oper depending on your needs
206          *    * might stay empty if no list should loaded and LoadMsgFromServer 
207          *      is skipped.
208          *  * influence the behaviour by presetting values on SharedMessageStatus
209          */
210         GetParamsGetServerCall_func GetParamsGetServerCall,
211
212         /**
213          * PrintpageHeader prints the surrounding information like iconbar, header etc.
214          * by default, output_headers() is called.
215          *
216          */
217         PrintViewHeader_func PrintPageHeader,
218
219         /**
220          * PrintViewHeader is here to print informations infront of your messages.
221          * The message list is already loaded & sorted (if) so you can evaluate 
222          * its result on the SharedMessageStatus struct.
223          */
224         PrintViewHeader_func PrintViewHeader,
225
226         /**
227          * LH is the function, you specify if you want to load more than just message
228          * numbers from the server during the listing fetch operation.
229          */
230         load_msg_ptrs_detailheaders LH,
231
232         /**
233          * LoadMsgFromServer is called for every message in the message list:
234          *  * which is 
235          *    * after 'startmsg'  
236          *    * up to 'maxmsgs' after your 'startmsg'
237          *  * it should load and parse messages from citserer.
238          *  * depending on your needs you might want to print your message here...
239          *  * if cmd was empty, its skipped alltogether.
240          */
241         LoadMsgFromServer_func LoadMsgFromServer,
242
243         /**
244          * RenderView_or_Tail is called last; 
245          *  * if you used PrintViewHeader to print messages, you might want to print 
246          *    trailing information here
247          *  * if you just pre-loaded your messages, put your render code here.
248          */
249         RenderView_or_Tail_func RenderView_or_Tail,
250
251         /**
252          * ViewCleanup should just clear your private data so all your mem can go back to 
253          * VALgrindHALLA.
254          * it also should release the content for delivery via end_burst() or wDumpContent(1);
255          */
256         View_Cleanup_func ViewCleanup
257         );
258 /*
259 GetParamsGetServerCall
260
261 PrintViewHeader
262
263 LoadMsgFromServer
264
265 RenderView_or_Tail
266 */
267
268
269 int ParseMessageListHeaders_Detail(StrBuf *Line, 
270                                    const char **pos, 
271                                    message_summary *Msg, 
272                                    StrBuf *ConversionBuffer);
273
274
275
276 /**
277  * @brief function to register the availability to render a specific message
278  * @param HeaderName Mimetype we know howto display
279  * @param HdrNLen length...
280  * @param InlineRenderable Should we announce to citserver that we want to receive these mimeparts immediately?
281  * @param Priority if multipart/alternative; which mimepart/Renderer should be prefered? (only applies if InlineRenderable)
282  */
283 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, 
284                           RenderMimeFunc MimeRenderer,
285                           int InlineRenderable,
286                           int Priority);
287
288
289 /**
290  * @brief fill the header parts of Msg with the headers loaded by MSG0
291  * @param Msg empty message struct, only preinitialized with the msgid
292  * @param FoundCharset buffer with the prefered charset of the headers
293  * @param buf linebuffer used to buffer citserver replies
294  */
295 int ReadOneMessageSummary(message_summary *Msg, StrBuf *FoundCharset, StrBuf *Buf);