File: libimap.h

package info (click to toggle)
balsa 2.3.0-2sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 17,528 kB
  • ctags: 6,644
  • sloc: ansic: 71,107; sh: 10,979; xml: 4,363; makefile: 445; awk: 60
file content (273 lines) | stat: -rw-r--r-- 8,119 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#ifndef __IMAP_H__
#define __IMAP_H__ 1
/* libimap library.
 * Copyright (C) 2003-2004 Pawel Salek.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option) 
 * any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the  
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

#include <time.h>
#include <glib.h>

/* connection states, as defined in rfc-2060, 3 */
typedef enum {
  IMHS_DISCONNECTED,
  IMHS_CONNECTED, /* non authenticated */
  IMHS_AUTHENTICATED,
  IMHS_SELECTED
} ImapConnectionState;


typedef enum {
  IMAP_SUCCESS = 0,  /* action succeeded*/
  IMAP_NOMEM,          /* no memory */
  IMAP_CONNECT_FAILED, /* transport level connect failed */
  IMAP_PROTOCOL_ERROR, /* usually unexpected server response */
  IMAP_AUTH_FAILURE, /* authentication failure */
  IMAP_AUTH_UNAVAIL, /* no supported authentication method available */
  IMAP_UNSECURE,     /* secure connection was requested but could not
                      * be established. */
  IMAP_SELECT_FAILED /* SELECT command failed */
} ImapResult;

typedef enum {
  IMAP_TLS_DISABLED,
  IMAP_TLS_ENABLED,
  IMAP_TLS_REQUIRED
} ImapTlsMode;

typedef enum { 
  IMLIST_MARKED = 0,
  IMLIST_UNMARKED,
  IMLIST_NOSELECT, 
  IMLIST_NOINFERIORS,
  IMLIST_HASCHILDREN,
  IMLIST_HASNOCHILDREN,
  IMLIST_LAST
} ImapMboxFlag;

typedef unsigned ImapMboxFlags;
#define IMAP_MBOX_HAS_FLAG(flg,f) ((flg) &  (1 << (f)))
#define IMAP_MBOX_SET_FLAG(flg,f) ((flg) |= (1 << (f)))

/* see section 2.3.2 of draft-crispin-imapv-20.txt */
typedef enum {
  IMSGF_SEEN     = 1 << 0,
  IMSGF_ANSWERED = 1 << 1,
  IMSGF_FLAGGED  = 1 << 2,
  IMSGF_DELETED  = 1 << 3,
  IMSGF_DRAFT    = 1 << 4,
  IMSGF_RECENT   = 1 << 5
} ImapMsgFlag;

typedef unsigned ImapMsgFlags;

#define IMSG_FLAG_SEEN(flags)     ((flags)&IMSGF_SEEN)
#define IMSG_FLAG_ANSWERED(flags) ((flags)&IMSGF_ANSWERED)
#define IMSG_FLAG_FLAGGED(flags)  ((flags)&IMSGF_FLAGGED)
#define IMSG_FLAG_DELETED(flags)  ((flags)&IMSGF_DELETED)
#define IMSG_FLAG_DRAFT(flags)    ((flags)&IMSGF_DRAFT)
#define IMSG_FLAG_RECENT(flags)   ((flags)&IMSGF_RECENT)

#define IMSG_FLAG_SET(flags, flag) do{ (flags) |= (flag); }while(0)
#define IMSG_FLAG_UNSET(flags, flag) do{ (flags) &= ~(flag); }while(0)

#define IMAP_FLAGS_EMPTY (0)

typedef enum {
  IMSO_MSGNO,
  IMSO_ARRIVAL,
  IMSO_CC,
  IMSO_DATE,
  IMSO_FROM,
  IMSO_SIZE,
  IMSO_SUBJECT,
  IMSO_TO
} ImapSortKey;

/* ImapMessage: Structure used to pass around and cache IMAP message data.
 */
typedef unsigned ImapUID;
typedef time_t ImapDate;

/* ImapAddress is an anddress as seen by IMAP/RFC2822.
   ImapAddress conventions:
   - mailbox == NULL: begin group (See RFC2822, sec 3.4), name
     contains group's name.

   - name == NULL and mailbox == NULL: end group.
   other cases are ordinary e-mail mailboxes.
*/
typedef struct ImapAddress_ ImapAddress;
struct ImapAddress_ {
  gchar * name;      /* comment */
  gchar * addr_spec; /* the "real" mailbox */
  ImapAddress *next;
};

ImapAddress *imap_address_new(gchar *name, gchar *addr_spec);
void imap_address_free(ImapAddress* addr);

typedef struct ImapEnvelope_ {
  ImapDate date;  /* as in the message header */
  gchar *subject; /* mime encoded, 7-bit subject as fetched from server */
  ImapAddress *from;
  ImapAddress *sender;
  ImapAddress *replyto;
  ImapAddress *to;
  ImapAddress *cc;
  ImapAddress *bcc;
  gchar *in_reply_to;
  gchar *message_id;
} ImapEnvelope;

/* header fetching types */
typedef enum {
  IMFETCH_BODYSTRUCT = 1<<0,
  IMFETCH_ENV        = 1<<1,
  IMFETCH_FLAGS      = 1<<2,
  IMFETCH_RFC822SIZE = 1<<3,
  IMFETCH_UID        = 1<<4,
  IMFETCH_CONTENT_TYPE = 1<<5,
  IMFETCH_REFERENCES   = 1<<6,
  IMFETCH_LIST_POST    = 1<<7,
  IMFETCH_RFC822HEADERS = 1<<8,
  IMFETCH_RFC822HEADERS_SELECTED = 1<<9, /* non-overlapping with ENV. */
  IMFETCH_HEADER_MASK  = (IMFETCH_CONTENT_TYPE | IMFETCH_REFERENCES |
                          IMFETCH_LIST_POST)
} ImapFetchType;

/* body fetching option */
typedef enum {
  IMFB_NONE,
  IMFB_HEADER, /* preceed with BODY[section.HEADER] */
  IMFB_MIME    /* preceed with BODY[section.MIME]   */
} ImapFetchBodyOptions;

typedef struct ImapBody_ ImapBody;

typedef enum { IMBMEDIA_MULTIPART,
               IMBMEDIA_APPLICATION,
               IMBMEDIA_AUDIO,
               IMBMEDIA_IMAGE, 
               IMBMEDIA_MESSAGE_RFC822,
               IMBMEDIA_MESSAGE_OTHER,
               IMBMEDIA_TEXT, 
               IMBMEDIA_OTHER } ImapMediaBasic;

typedef enum { IMBENC_UNUSED,
  IMBENC_7BIT, IMBENC_8BIT, IMBENC_BINARY, IMBENC_BASE64, 
  IMBENC_QUOTED, IMBENC_OTHER } ImapBodyEncoding;

struct ImapBodyExt1Part_ {
    char *md5;
};
typedef struct ImapBodyExt1Part_ ImapBodyExt1Part;

typedef enum {
    IMBDISP_INLINE,
    IMBDISP_ATTACHMENT,
    IMBDISP_OTHER
} ImapBodyDisposition;
struct ImapBodyExtMPart_ {
  /* GHashTable         *params; not used: we put everything in ImapBody */
  GSList	     *lang;
};
typedef struct ImapBodyExtMPart_ ImapBodyExtMPart;

struct ImapBody_ {
  ImapBodyEncoding encoding;
  ImapMediaBasic media_basic;
  gchar *media_basic_name;
  gchar *media_subtype;
  GHashTable *params;
  unsigned octets;
  unsigned lines;
  char *content_id;
  char *desc;
  ImapEnvelope *envelope;/* used only if media/basic == MESSAGE */
  ImapBodyDisposition content_dsp;
  GHashTable *dsp_params;
  char *content_dsp_other;
  char *content_uri;

  union {
    ImapBodyExt1Part onepart;
    ImapBodyExtMPart mpart;
  } ext;

  ImapBody *child; /* not null for eg. message/rfc822 */
  ImapBody *next;
};

typedef struct ImapMessage_ {
  ImapUID      uid;
  ImapMsgFlags flags;
  ImapEnvelope *envelope;
  ImapBody     *body;
  ImapDate     internal_date; /* delivery date */
  int rfc822size;
  /* rfc822, rfc822.header, rfc822.text, body, body structure, body
     section have yet to be implemented. Currently, they will be ignored.
     Almost.
  */
  gchar *fetched_header_fields;
  ImapFetchType available_headers; /* Used internally. Informs, what
                                    * fields are supposed to be
                                    * already in
                                    * fetched_header_fields. Checking
                                    * the content of the string is not
                                    * sufficient: we would keep
                                    * sending requests if the header
                                    * in question is not present. */
} ImapMessage;

typedef struct _ImapMboxHandle      ImapMboxHandle;

ImapEnvelope *imap_envelope_new(void);
void imap_envelope_free(ImapEnvelope *);

ImapBody* imap_body_new(void);
void imap_body_free(ImapBody* body);

void imap_body_set_desc(ImapBody* body, char* str);
void imap_body_add_param(ImapBody *body, char *key, char *val);
void imap_body_append_part(ImapBody* body, ImapBody* sibling);
void imap_body_append_child(ImapBody* body, ImapBody* child);
void imap_body_set_id(ImapBody *body, char *id);

/* user events below usually require application's or user's
 *  intervention */
typedef enum {
  IME_GET_USER_PASS,
  IME_GET_USER,
  IME_TLS_VERIFY_ERROR,
  IME_TLS_NO_PEER_CERT,
  IME_TLS_WEAK_CIPHER
} ImapUserEventType;

typedef void (*ImapUserCb)(ImapUserEventType ue, void *arg, ...);

ImapMessage *imap_message_new(void);
void imap_message_free(ImapMessage *);
void imap_mbox_handle_msg_deserialize(ImapMboxHandle *h, unsigned msgno,
                                      void *data);
void*        imap_message_serialize(ImapMessage *);
ImapMessage* imap_message_deserialize(void *data);

const char *lbi_strerror(ImapResult rc);

#endif /* __IMAP_H__ */