1 /*
2  * libEtPan! -- a mail stuff library
3  *
4  * Copyright (C) 2001, 2005 - DINH Viet Hoa
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the libEtPan! project nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * $Id: maildriver_types.h,v 1.49 2008/04/19 09:25:40 hoa Exp $
34  */
35 
36 #ifndef MAILDRIVER_TYPES_H
37 
38 #define MAILDRIVER_TYPES_H
39 
40 #ifndef _MSC_VER
41 #	ifdef HAVE_INTTYPES_H
42 #		include <inttypes.h>
43 #	endif
44 #	include <sys/types.h>
45 #endif
46 
47 #include <libetpan/mailstream.h>
48 #include <libetpan/mailimf.h>
49 #include <libetpan/mailmime.h>
50 #include <libetpan/carray.h>
51 
52 #include <libetpan/mailthread_types.h>
53 #include <libetpan/maildriver_errors.h>
54 
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 
59 typedef struct mailsession_driver mailsession_driver;
60 
61 typedef struct mailsession mailsession;
62 
63 typedef struct mailmessage_driver mailmessage_driver;
64 
65 typedef struct mailmessage mailmessage;
66 
67 
68 /*
69   mailmessage_list is a list of mailmessage
70 
71   - tab is an array of mailmessage structures
72 */
73 
74 struct mailmessage_list {
75   carray * msg_tab; /* elements are (mailmessage *) */
76 };
77 
78 LIBETPAN_EXPORT
79 struct mailmessage_list * mailmessage_list_new(carray * msg_tab);
80 
81 LIBETPAN_EXPORT
82 void mailmessage_list_free(struct mailmessage_list * env_list);
83 
84 /*
85   mail_list is a list of mailbox names
86 
87   - list is a list of mailbox names
88 */
89 
90 struct mail_list {
91   clist * mb_list; /* elements are (char *) */
92 };
93 
94 LIBETPAN_EXPORT
95 struct mail_list * mail_list_new(clist * mb_list);
96 
97 LIBETPAN_EXPORT
98 void mail_list_free(struct mail_list * resp);
99 
100 /*
101   This is a flag value.
102   Flags can be combined with OR operation
103 */
104 
105 enum {
106   MAIL_FLAG_NEW       = 1 << 0,
107   MAIL_FLAG_SEEN      = 1 << 1,
108   MAIL_FLAG_FLAGGED   = 1 << 2,
109   MAIL_FLAG_DELETED   = 1 << 3,
110   MAIL_FLAG_ANSWERED  = 1 << 4,
111   MAIL_FLAG_FORWARDED = 1 << 5,
112   MAIL_FLAG_CANCELLED = 1 << 6
113 };
114 
115 /*
116   mail_flags is the value of a flag related to a message.
117 
118   - flags is the standard flags value
119 
120   - extension is a list of unknown flags for libEtPan!
121 */
122 
123 struct mail_flags {
124   uint32_t fl_flags;
125   clist * fl_extension; /* elements are (char *) */
126 };
127 
128 LIBETPAN_EXPORT
129 struct mail_flags * mail_flags_new(uint32_t fl_flags, clist * fl_ext);
130 
131 LIBETPAN_EXPORT
132 void mail_flags_free(struct mail_flags * flags);
133 
134 /*
135   This function creates a flag for a new message
136 */
137 
138 LIBETPAN_EXPORT
139 struct mail_flags * mail_flags_new_empty(void);
140 
141 
142 /*
143   mailimf_date_time_comp compares two dates
144 
145 
146 */
147 
148 LIBETPAN_EXPORT
149 int32_t mailimf_date_time_comp(struct mailimf_date_time * date1,
150     struct mailimf_date_time * date2);
151 
152 /*
153   this is type type of the search criteria
154 */
155 
156 enum {
157   MAIL_SEARCH_KEY_ALL,        /* all messages correspond */
158   MAIL_SEARCH_KEY_ANSWERED,   /* messages with flag \Answered */
159   MAIL_SEARCH_KEY_BCC,        /* messages which Bcc field contains
160                                  a given string */
161   MAIL_SEARCH_KEY_BEFORE,     /* messages which internal date is earlier
162                                  than the specified date */
163   MAIL_SEARCH_KEY_BODY,       /* message that contains the given string
164                                  (in header and text parts) */
165   MAIL_SEARCH_KEY_CC,         /* messages whose Cc field contains the
166                                  given string */
167   MAIL_SEARCH_KEY_DELETED,    /* messages with the flag \Deleted */
168   MAIL_SEARCH_KEY_FLAGGED,    /* messages with the flag \Flagged */
169   MAIL_SEARCH_KEY_FROM,       /* messages whose From field contains the
170                                  given string */
171   MAIL_SEARCH_KEY_NEW,        /* messages with the flag \Recent and not
172                                  the \Seen flag */
173   MAIL_SEARCH_KEY_OLD,        /* messages that do not have the
174                                  \Recent flag set */
175   MAIL_SEARCH_KEY_ON,         /* messages whose internal date is the
176                                  specified date */
177   MAIL_SEARCH_KEY_RECENT,     /* messages with the flag \Recent */
178   MAIL_SEARCH_KEY_SEEN,       /* messages with the flag \Seen */
179   MAIL_SEARCH_KEY_SINCE,      /* messages whose internal date is later
180                                  than specified date */
181   MAIL_SEARCH_KEY_SUBJECT,    /* messages whose Subject field contains the
182                                  given string */
183   MAIL_SEARCH_KEY_TEXT,       /* messages whose text part contains the
184                                  given string */
185   MAIL_SEARCH_KEY_TO,         /* messages whose To field contains the
186                                  given string */
187   MAIL_SEARCH_KEY_UNANSWERED, /* messages with no flag \Answered */
188   MAIL_SEARCH_KEY_UNDELETED,  /* messages with no flag \Deleted */
189   MAIL_SEARCH_KEY_UNFLAGGED,  /* messages with no flag \Flagged */
190   MAIL_SEARCH_KEY_UNSEEN,     /* messages with no flag \Seen */
191   MAIL_SEARCH_KEY_HEADER,     /* messages whose given field
192                                  contains the given string */
193   MAIL_SEARCH_KEY_LARGER,     /* messages whose size is larger then
194                                  the given size */
195   MAIL_SEARCH_KEY_NOT,        /* not operation of the condition */
196   MAIL_SEARCH_KEY_OR,         /* or operation between two conditions */
197   MAIL_SEARCH_KEY_SMALLER,    /* messages whose size is smaller than
198                                  the given size */
199   MAIL_SEARCH_KEY_MULTIPLE    /* the boolean operator between the
200                                  conditions is AND */
201 };
202 
203 /*
204   mail_search_key is the condition on the messages to return
205 
206   - type is the type of the condition
207 
208   - bcc is the text to search in the Bcc field when type is
209     MAIL_SEARCH_KEY_BCC, should be allocated with malloc()
210 
211   - before is a date when type is MAIL_SEARCH_KEY_BEFORE
212 
213   - body is the text to search in the message when type is
214     MAIL_SEARCH_KEY_BODY, should be allocated with malloc()
215 
216   - cc is the text to search in the Cc field when type is
217     MAIL_SEARCH_KEY_CC, should be allocated with malloc()
218 
219   - from is the text to search in the From field when type is
220     MAIL_SEARCH_KEY_FROM, should be allocated with malloc()
221 
222   - on is a date when type is MAIL_SEARCH_KEY_ON
223 
224   - since is a date when type is MAIL_SEARCH_KEY_SINCE
225 
226   - subject is the text to search in the Subject field when type is
227     MAILIMAP_SEARCH_KEY_SUBJECT, should be allocated with malloc()
228 
229   - text is the text to search in the text part of the message when
230     type is MAILIMAP_SEARCH_KEY_TEXT, should be allocated with malloc()
231 
232   - to is the text to search in the To field when type is
233     MAILIMAP_SEARCH_KEY_TO, should be allocated with malloc()
234 
235   - header_name is the header name when type is MAILIMAP_SEARCH_KEY_HEADER,
236     should be allocated with malloc()
237 
238   - header_value is the text to search in the given header when type is
239     MAILIMAP_SEARCH_KEY_HEADER, should be allocated with malloc()
240 
241   - larger is a size when type is MAILIMAP_SEARCH_KEY_LARGER
242 
243   - not is a condition when type is MAILIMAP_SEARCH_KEY_NOT
244 
245   - or1 is a condition when type is MAILIMAP_SEARCH_KEY_OR
246 
247   - or2 is a condition when type is MAILIMAP_SEARCH_KEY_OR
248 
249   - sentbefore is a date when type is MAILIMAP_SEARCH_KEY_SENTBEFORE
250 
251   - senton is a date when type is MAILIMAP_SEARCH_KEY_SENTON
252 
253   - sentsince is a date when type is MAILIMAP_SEARCH_KEY_SENTSINCE
254 
255   - smaller is a size when type is MAILIMAP_SEARCH_KEY_SMALLER
256 
257   - multiple is a set of message when type is MAILIMAP_SEARCH_KEY_MULTIPLE
258 */
259 
260 #if 0
261 struct mail_search_key {
262   int sk_type;
263   union {
264     char * sk_bcc;
265     struct mailimf_date_time * sk_before;
266     char * sk_body;
267     char * sk_cc;
268     char * sk_from;
269     struct mailimf_date_time * sk_on;
270     struct mailimf_date_time * sk_since;
271     char * sk_subject;
272     char * sk_text;
273     char * sk_to;
274     char * sk_header_name;
275     char * sk_header_value;
276     size_t sk_larger;
277     struct mail_search_key * sk_not;
278     struct mail_search_key * sk_or1;
279     struct mail_search_key * sk_or2;
280     size_t sk_smaller;
281     clist * sk_multiple; /* list of (struct mailimap_search_key *) */
282   } sk_data;
283 };
284 
285 
286 struct mail_search_key *
287 mail_search_key_new(int sk_type,
288     char * sk_bcc, struct mailimf_date_time * sk_before,
289     char * sk_body, char * sk_cc, char * sk_from,
290     struct mailimf_date_time * sk_on, struct mailimf_date_time * sk_since,
291     char * sk_subject, char * sk_text, char * sk_to,
292     char * sk_header_name, char * sk_header_value, size_t sk_larger,
293     struct mail_search_key * sk_not, struct mail_search_key * sk_or1,
294     struct mail_search_key * sk_or2, size_t sk_smaller,
295     clist * sk_multiple);
296 
297 void mail_search_key_free(struct mail_search_key * key);
298 #endif
299 
300 /*
301   mail_search_result is a list of message numbers that is returned
302   by the mailsession_search_messages function()
303 */
304 
305 #if 0
306 struct mail_search_result {
307   clist * sr_list; /* list of (uint32_t *) */
308 };
309 
310 struct mail_search_result * mail_search_result_new(clist * sr_list);
311 
312 void mail_search_result_free(struct mail_search_result * search_result);
313 #endif
314 
315 
316 /*
317   There is three kinds of identities :
318   - storage
319   - folders
320   - session
321 
322   A storage (struct mailstorage) represents whether a server or
323   a main path,
324 
325   A storage can be an IMAP server, the root path of a MH or a mbox file.
326 
327   Folders (struct mailfolder) are the mailboxes we can
328   choose in the server or as sub-folder of the main path.
329 
330   Folders for IMAP are the IMAP mailboxes, for MH this is one of the
331   folder of the MH storage, for mbox, there is only one folder, the
332   mbox file content;
333 
334   A mail session (struct mailsession) is whether a connection to a server
335   or a path that is open. It is the abstraction lower folders and storage.
336   It allow us to send commands.
337 
338   We have a session driver for mail session for each kind of storage.
339 
340   From a session, we can get a message (struct mailmessage) to read.
341   We have a message driver for each kind of storage.
342 */
343 
344 /*
345   maildriver is the driver structure for mail sessions
346 
347   - name is the name of the driver
348 
349   - initialize() is the function that will initializes a data structure
350       specific to the driver, it returns a value that will be stored
351       in the field data of the session.
352       The field data of the session is the state of the session,
353       the internal data structure used by the driver.
354       It is called when creating the mailsession structure with
355       mailsession_new().
356 
357   - uninitialize() frees the structure created with initialize()
358 
359   - parameters() implements functions specific to the given mail access
360 
361   - connect_stream() connects a stream to the session
362 
363   - connect_path() notify a main path to the session
364 
365   - starttls() changes the current stream to a TLS stream
366 
367   - login() notifies the user and the password to authenticate to the
368       session
369 
370   - logout() exits the session and closes the stream
371 
372   - noop() does no operation on the session, but it can be
373       used to poll for the status of the connection.
374 
375   - build_folder_name() will return an allocated string with
376       that contains the complete path of the folder to create
377 
378   - create_folder() creates the folder that corresponds to the
379       given name
380 
381   - delete_folder() deletes the folder that corresponds to the
382       given name
383 
384   - rename_folder() change the name of the folder
385 
386   - check_folder() makes a checkpoint of the session
387 
388   - examine_folder() selects a mailbox as readonly
389 
390   - select_folder() selects a mailbox
391 
392   - expunge_folder() deletes all messages marked \Deleted
393 
394   - status_folder() queries the status of the folder
395       (number of messages, number of recent messages, number of
396       unseen messages)
397 
398   - messages_number() queries the number of messages in the folder
399 
400   - recent_number() queries the number of recent messages in the folder
401 
402   - unseen_number() queries the number of unseen messages in the folder
403 
404   - list_folders() returns the list of all sub-mailboxes
405       of the given mailbox
406 
407   - lsub_folders() returns the list of subscribed
408       sub-mailboxes of the given mailbox
409 
410   - subscribe_folder() subscribes to the given mailbox
411 
412   - unsubscribe_folder() unsubscribes to the given mailbox
413 
414   - append_message() adds a RFC 2822 message to the current
415       given mailbox
416 
417   - copy_message() copies a message whose number is given to
418        a given mailbox. The mailbox must be accessible from
419        the same session.
420 
421   - move_message() copies a message whose number is given to
422        a given mailbox. The mailbox must be accessible from the
423        same session.
424 
425   - get_messages_list() returns the list of message numbers
426       of the current mailbox.
427 
428   - get_envelopes_list() fills the parsed fields in the
429       mailmessage structures of the mailmessage_list.
430 
431   - remove_message() removes the given message from the mailbox.
432       The message is permanently deleted.
433 
434   - search_message() returns a list of message numbers that
435       corresponds to the given criteria.
436 
437   - get_message returns a mailmessage structure that corresponds
438       to the given message number.
439 
440   - get_message_by_uid returns a mailmessage structure that corresponds
441       to the given message unique identifier.
442 
443   * mandatory functions are the following :
444 
445   - connect_stream() of connect_path()
446   - logout()
447   - get_messages_list()
448   - get_envelopes_list()
449 
450   * we advise you to implement these functions :
451 
452   - select_folder() (in case a session can access several folders)
453   - noop() (to check if the server is responding)
454   - check_folder() (to make a checkpoint of the session)
455   - status_folder(), messages_number(), recent_number(), unseen_number()
456       (to get stat of the folder)
457   - append_message() (but can't be done in the case of POP3 at least)
458   - login() in a case of an authenticated driver.
459   - starttls() in a case of a stream driver, if the procotol supports
460       STARTTLS.
461   - get_message_by_uid() so that the application can remember the message
462       by UID and build its own list of messages.
463   - login_sasl() notifies the SASL information to authenticate to the
464       session.
465 
466   * drivers' specific :
467 
468   Everything that is specific to the driver will be implemented in this
469   function :
470 
471   - parameters()
472 */
473 
474 struct mailsession_driver {
475   char * sess_name;
476 
477   int (* sess_initialize)(mailsession * session);
478   void (* sess_uninitialize)(mailsession * session);
479 
480   int (* sess_parameters)(mailsession * session,
481       int id, void * value);
482 
483   int (* sess_connect_stream)(mailsession * session, mailstream * s);
484   int (* sess_connect_path)(mailsession * session, const char * path);
485 
486   int (* sess_starttls)(mailsession * session);
487 
488   int (* sess_login)(mailsession * session, const char * userid, const char * password);
489   int (* sess_logout)(mailsession * session);
490   int (* sess_noop)(mailsession * session);
491 
492   /* folders operations */
493 
494   int (* sess_build_folder_name)(mailsession * session, const char * mb,
495       const char * name, char ** result);
496 
497   int (* sess_create_folder)(mailsession * session, const char * mb);
498   int (* sess_delete_folder)(mailsession * session, const char * mb);
499   int (* sess_rename_folder)(mailsession * session, const char * mb,
500       const char * new_name);
501   int (* sess_check_folder)(mailsession * session);
502   int (* sess_examine_folder)(mailsession * session, const char * mb);
503   int (* sess_select_folder)(mailsession * session, const char * mb);
504   int (* sess_expunge_folder)(mailsession * session);
505   int (* sess_status_folder)(mailsession * session, const char * mb,
506       uint32_t * result_num, uint32_t * result_recent,
507       uint32_t * result_unseen);
508   int (* sess_messages_number)(mailsession * session, const char * mb,
509       uint32_t * result);
510   int (* sess_recent_number)(mailsession * session, const char * mb,
511       uint32_t * result);
512   int (* sess_unseen_number)(mailsession * session, const char * mb,
513       uint32_t * result);
514 
515   int (* sess_list_folders)(mailsession * session, const char * mb,
516       struct mail_list ** result);
517   int (* sess_lsub_folders)(mailsession * session, const char * mb,
518       struct mail_list ** result);
519 
520   int (* sess_subscribe_folder)(mailsession * session, const char * mb);
521   int (* sess_unsubscribe_folder)(mailsession * session, const char * mb);
522 
523   /* messages operations */
524 
525   int (* sess_append_message)(mailsession * session,
526       const char * message, size_t size);
527   int (* sess_append_message_flags)(mailsession * session,
528       const char * message, size_t size, struct mail_flags * flags);
529   int (* sess_copy_message)(mailsession * session,
530       uint32_t num, const char * mb);
531   int (* sess_move_message)(mailsession * session,
532       uint32_t num, const char * mb);
533 
534   int (* sess_get_message)(mailsession * session,
535       uint32_t num, mailmessage ** result);
536 
537   int (* sess_get_message_by_uid)(mailsession * session,
538       const char * uid, mailmessage ** result);
539 
540   int (* sess_get_messages_list)(mailsession * session,
541       struct mailmessage_list ** result);
542   int (* sess_get_envelopes_list)(mailsession * session,
543       struct mailmessage_list * env_list);
544   int (* sess_remove_message)(mailsession * session, uint32_t num);
545 
546   int (* sess_login_sasl)(mailsession * session, const char * auth_type,
547       const char * server_fqdn,
548       const char * local_ip_port,
549       const char * remote_ip_port,
550       const char * login, const char * auth_name,
551       const char * password, const char * realm);
552 };
553 
554 
555 /*
556   session is the data structure for a mail session.
557 
558   - data is the internal data structure used by the driver
559     It is called when initializing the mailsession structure.
560 
561   - driver is the driver used for the session
562 */
563 
564 struct mailsession {
565   void * sess_data;
566   mailsession_driver * sess_driver;
567 };
568 
569 
570 
571 
572 /*
573   mailmessage_driver is the driver structure to get information from messages.
574 
575   - name is the name of the driver
576 
577   - initialize() is the function that will initializes a data structure
578       specific to the driver, it returns a value that will be stored
579       in the field data of the mailsession.
580       The field data of the session is the state of the session,
581       the internal data structure used by the driver.
582       It is called when initializing the mailmessage structure with
583       mailmessage_init().
584 
585   - uninitialize() frees the structure created with initialize().
586       It will be called by mailmessage_free().
587 
588   - flush() will free from memory all temporary structures of the message
589       (for example, the MIME structure of the message).
590 
591   - fetch_result_free() will free all strings resulted by fetch() or
592       any fetch_xxx() functions that returns a string.
593 
594   - fetch() returns the content of the message (headers and text).
595 
596   - fetch_header() returns the content of the headers.
597 
598   - fetch_body() returns the message text (message content without headers)
599 
600   - fetch_size() returns the size of the message content.
601 
602   - get_bodystructure() returns the MIME structure of the message.
603 
604   - fetch_section() returns the content of a given MIME part
605 
606   - fetch_section_header() returns the header of the message
607       contained by the given MIME part.
608 
609   - fetch_section_mime() returns the MIME headers of the
610       given MIME part.
611 
612   - fetch_section_body() returns the text (if this is a message, this is the
613       message content without headers) of the given MIME part.
614 
615   - fetch_envelope() returns a mailimf_fields structure, with a list of
616       fields chosen by the driver.
617 
618   - get_flags() returns a the flags related to the message.
619       When you want to get flags of a message, you have to make sure to
620       call get_flags() at least once before using directly message->flags.
621 */
622 
623 #define LIBETPAN_MAIL_MESSAGE_CHECK
624 
625 struct mailmessage_driver {
626   char * msg_name;
627 
628   int (* msg_initialize)(mailmessage * msg_info);
629 
630   void (* msg_uninitialize)(mailmessage * msg_info);
631 
632   void (* msg_flush)(mailmessage * msg_info);
633 
634   void (* msg_check)(mailmessage * msg_info);
635 
636   void (* msg_fetch_result_free)(mailmessage * msg_info,
637 			     char * msg);
638 
639   int (* msg_fetch)(mailmessage * msg_info,
640 		char ** result,
641 		size_t * result_len);
642 
643   int (* msg_fetch_header)(mailmessage * msg_info,
644 		       char ** result,
645 		       size_t * result_len);
646 
647   int (* msg_fetch_body)(mailmessage * msg_info,
648 		     char ** result, size_t * result_len);
649 
650   int (* msg_fetch_size)(mailmessage * msg_info,
651 		     size_t * result);
652 
653   int (* msg_get_bodystructure)(mailmessage * msg_info,
654 			    struct mailmime ** result);
655 
656   int (* msg_fetch_section)(mailmessage * msg_info,
657 			struct mailmime * mime,
658 			char ** result, size_t * result_len);
659 
660   int (* msg_fetch_section_header)(mailmessage * msg_info,
661 			       struct mailmime * mime,
662 			       char ** result,
663 			       size_t * result_len);
664 
665   int (* msg_fetch_section_mime)(mailmessage * msg_info,
666 			     struct mailmime * mime,
667 			     char ** result,
668 			     size_t * result_len);
669 
670   int (* msg_fetch_section_body)(mailmessage * msg_info,
671 			     struct mailmime * mime,
672 			     char ** result,
673 			     size_t * result_len);
674 
675   int (* msg_fetch_envelope)(mailmessage * msg_info,
676 			 struct mailimf_fields ** result);
677 
678   int (* msg_get_flags)(mailmessage * msg_info,
679 		    struct mail_flags ** result);
680 };
681 
682 
683 /*
684   mailmessage is a data structure to get information from messages
685 
686   - session is the session linked to the given message, it can be NULL
687 
688   - driver is the message driver
689 
690   - index is the message number
691 
692   - uid, when it is not NULL, it means that the folder
693       the folder has persistant message numbers, the string is
694       the unique message number in the folder.
695       uid should be implemented if possible.
696       for drivers where we cannot generate real uid,
697       a suggestion is "AAAA-IIII" where AAAA is some
698       random session number and IIII the content of index field.
699 
700   - size, when it is not 0, is the size of the message content.
701 
702   - fields, when it is not NULL, are the header fields of the message.
703 
704   - flags, when it is not NULL, are the flags related to the message.
705 
706   - single_fields, when resolved != 0, is filled with the data of fields.
707 
708   - mime, when it is not NULL
709 
710   - cached is != 0 when the header fields were read from the cache.
711 
712   - data is data specific to the driver, this is internal data structure,
713       some state of the message.
714 */
715 
716 struct mailmessage {
717   mailsession * msg_session;
718   mailmessage_driver * msg_driver;
719   uint32_t msg_index;
720   char * msg_uid;
721 
722   size_t msg_size;
723   struct mailimf_fields * msg_fields;
724   struct mail_flags * msg_flags;
725 
726   int msg_resolved;
727   struct mailimf_single_fields msg_single_fields;
728   struct mailmime * msg_mime;
729 
730   /* internal data */
731 
732   int msg_cached;
733   void * msg_data;
734 
735  /*
736    msg_folder field :
737    used to reference the mailfolder, this is a workaround due
738    to the problem with initial conception, where folder notion
739    did not exist.
740  */
741   void * msg_folder;
742   /* user data */
743   void * msg_user_data;
744 };
745 
746 
747 /*
748   mailmessage_tree is a node in the messages tree (thread)
749 
750   - node_parent is the parent of the message, it is NULL if the message
751       is the root of the message tree.
752 
753   - node_msgid is the message ID of this node.
754 
755   - node_date is the date of the message in number of second elapsed
756       since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC).
757 
758   - node_msg is the message structure that is stored referenced by the node.
759       is msg is NULL, this is a dummy node.
760 
761   - node_children is an array that contains all the children of the node.
762       children are mailmessage_tree structures.
763 
764   - node_is_reply is != 0 when the message is a reply or a forward
765 
766   - node_base_subject is the extracted subject of the message.
767 */
768 
769 struct mailmessage_tree {
770   struct mailmessage_tree * node_parent;
771   char * node_msgid;
772   time_t node_date;
773   mailmessage * node_msg;
774   carray * node_children; /* array of (struct mailmessage_tree *) */
775 
776   /* private, used for threading */
777   int node_is_reply;
778   char * node_base_subject;
779 };
780 
781 LIBETPAN_EXPORT
782 struct mailmessage_tree *
783 mailmessage_tree_new(char * node_msgid, time_t node_date,
784     mailmessage * node_msg);
785 
786 LIBETPAN_EXPORT
787 void mailmessage_tree_free(struct mailmessage_tree * tree);
788 
789 /*
790   mailmessage_tree_free_recursive
791 
792   if you want to release memory of the given tree and all the sub-trees,
793   you can use this function.
794 */
795 LIBETPAN_EXPORT
796 void mailmessage_tree_free_recursive(struct mailmessage_tree * tree);
797 
798 
799 struct generic_message_t {
800   int (* msg_prefetch)(mailmessage * msg_info);
801   void (* msg_prefetch_free)(struct generic_message_t * msg);
802   int msg_fetched;
803   char * msg_message;
804   size_t msg_length;
805   void * msg_data;
806 };
807 
808 LIBETPAN_EXPORT
809 const char * maildriver_strerror(int err);
810 
811 /* basic malloc / free functions to be compliant with the library allocations */
812 LIBETPAN_EXPORT
813 void *libetpan_malloc(size_t length);
814 
815 LIBETPAN_EXPORT
816 void libetpan_free(void* data);
817 
818 #ifdef __cplusplus
819 }
820 #endif
821 
822 #endif
823