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