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