d81bdea7ba47a13a2916fe33b4ec8ff255897995
[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                  WCTemplputParams *TP);
144 int load_message(message_summary *Msg, 
145                  StrBuf *FoundCharset,
146                  StrBuf **Error);
147
148
149
150
151 typedef struct _SharedMessageStatus {
152         long load_seen;        /* should read information be loaded */
153         long sortit;           /* should we sort it using the standard sort API? */
154         long defaultsortorder; /* if we should sort it, which direction should be the default? */
155
156         long maxload;          /* how many headers should we accept from the server? defaults to 10k */
157         long maxmsgs;          /* how many message bodies do you want to load at most?*/
158
159         long startmsg;         /* which is the start message? */
160         long nummsgs;          /* How many messages are available to your view? */
161         long numNewmsgs;       /* if you load the seen-status, this is the count of them. */
162         long num_displayed;    /* counted up for LoadMsgFromServer */ /* TODO: unclear who should access this and why */
163
164         long lowest_found;     /* smallest Message ID found;  */
165         long highest_found;    /* highest Message ID found;  */
166
167 } SharedMessageStatus;
168
169 int load_msg_ptrs(const char *servcmd,
170                   const char *filter,
171                   SharedMessageStatus *Stat, 
172                   load_msg_ptrs_detailheaders LH);
173
174 typedef int (*GetParamsGetServerCall_func)(SharedMessageStatus *Stat, 
175                                            void **ViewSpecific, 
176                                            long oper, 
177                                            char *cmd, 
178                                            long len,
179                                            char *filter,
180                                            long flen);
181
182 typedef int (*PrintViewHeader_func)(SharedMessageStatus *Stat, void **ViewSpecific);
183
184 typedef int (*LoadMsgFromServer_func)(SharedMessageStatus *Stat, 
185                                       void **ViewSpecific, 
186                                       message_summary* Msg, 
187                                       int is_new, 
188                                       int i);
189
190 typedef int (*RenderView_or_Tail_func)(SharedMessageStatus *Stat, 
191                                        void **ViewSpecific, 
192                                        long oper);
193 typedef int (*View_Cleanup_func)(void **ViewSpecific);
194
195 void RegisterReadLoopHandlerset(
196         /**
197          * RoomType: which View definition are you going to be called for
198          */
199         int RoomType,
200
201         /**
202          * GetParamsGetServerCall should do the following:
203          *  * allocate your private context structure
204          *  * evaluate your commandline arguments, put results to your private struct.
205          *  * fill cmd with the command to load the message pointer list:
206          *    * might depend on bstr/oper depending on your needs
207          *    * might stay empty if no list should loaded and LoadMsgFromServer 
208          *      is skipped.
209          *  * influence the behaviour by presetting values on SharedMessageStatus
210          */
211         GetParamsGetServerCall_func GetParamsGetServerCall,
212
213         /**
214          * PrintpageHeader prints the surrounding information like iconbar, header etc.
215          * by default, output_headers() is called.
216          *
217          */
218         PrintViewHeader_func PrintPageHeader,
219
220         /**
221          * PrintViewHeader is here to print informations infront of your messages.
222          * The message list is already loaded & sorted (if) so you can evaluate 
223          * its result on the SharedMessageStatus struct.
224          */
225         PrintViewHeader_func PrintViewHeader,
226
227         /**
228          * LH is the function, you specify if you want to load more than just message
229          * numbers from the server during the listing fetch operation.
230          */
231         load_msg_ptrs_detailheaders LH,
232
233         /**
234          * LoadMsgFromServer is called for every message in the message list:
235          *  * which is 
236          *    * after 'startmsg'  
237          *    * up to 'maxmsgs' after your 'startmsg'
238          *  * it should load and parse messages from citserer.
239          *  * depending on your needs you might want to print your message here...
240          *  * if cmd was empty, its skipped alltogether.
241          */
242         LoadMsgFromServer_func LoadMsgFromServer,
243
244         /**
245          * RenderView_or_Tail is called last; 
246          *  * if you used PrintViewHeader to print messages, you might want to print 
247          *    trailing information here
248          *  * if you just pre-loaded your messages, put your render code here.
249          */
250         RenderView_or_Tail_func RenderView_or_Tail,
251
252         /**
253          * ViewCleanup should just clear your private data so all your mem can go back to 
254          * VALgrindHALLA.
255          * it also should release the content for delivery via end_burst() or wDumpContent(1);
256          */
257         View_Cleanup_func ViewCleanup
258         );
259 /*
260 GetParamsGetServerCall
261
262 PrintViewHeader
263
264 LoadMsgFromServer
265
266 RenderView_or_Tail
267 */
268
269
270 int ParseMessageListHeaders_Detail(StrBuf *Line, 
271                                    const char **pos, 
272                                    message_summary *Msg, 
273                                    StrBuf *ConversionBuffer);
274
275
276
277 /**
278  * @brief function to register the availability to render a specific message
279  * @param HeaderName Mimetype we know howto display
280  * @param HdrNLen length...
281  * @param InlineRenderable Should we announce to citserver that we want to receive these mimeparts immediately?
282  * @param Priority if multipart/alternative; which mimepart/Renderer should be prefered? (only applies if InlineRenderable)
283  */
284 void RegisterMimeRenderer(const char *HeaderName, long HdrNLen, 
285                           RenderMimeFunc MimeRenderer,
286                           int InlineRenderable,
287                           int Priority);
288
289
290 /**
291  * @brief fill the header parts of Msg with the headers loaded by MSG0
292  * @param Msg empty message struct, only preinitialized with the msgid
293  * @param FoundCharset buffer with the prefered charset of the headers
294  * @param buf linebuffer used to buffer citserver replies
295  */
296 int ReadOneMessageSummary(message_summary *Msg, StrBuf *FoundCharset, StrBuf *Buf);