1 /*
2  *
3  Copyright (c) 2004-2012 NFG Net Facilities Group BV support@nfg.nl
4  *
5  */
6 
7 #ifndef DM_COMMANDCHANNEL_H
8 #define DM_COMMANDCHANNEL_H
9 
10 #include "dbmail.h"
11 
12 // command state during idle command
13 #define IDLE -1
14 
15 /* ImapSession definition */
16 typedef struct {
17 	Mempool_T pool;
18 	pthread_mutex_t lock;
19 	ClientBase_T *ci;
20 	Capa_T preauth_capa;   // CAPABILITY
21 	Capa_T capa;           // CAPABILITY
22 	char tag[16];
23 	char command[16];
24 	int command_type;
25 	int command_state;
26 
27 	gboolean use_uid;
28 	uint64_t msg_idnr;
29 
30 	String_T buff;         // output buffer
31 
32 	int parser_state;
33 	String_T *args;
34 	uint64_t args_idx;
35 
36 	int loop;              // IDLE loop counter
37 
38 	fetch_items *fi;       // FETCH
39 	qresync_args qresync; // SELECT ... (QRESYNC ...)
40 	search_order order;    // SORT/SEARCH
41 
42 	DbmailMailbox *mailbox; // currently selected mailbox
43 	uint64_t lo;            // lower boundary for message ids
44 	uint64_t hi;            // upper boundary for message ids
45 	uint64_t ceiling;       // upper boundary during prefetching
46 
47 	DbmailMessage *message;
48 
49 	uint64_t userid;		/* userID of client in dbase */
50 
51 	GTree *ids;
52 	GList *new_ids; // store new uids after a COPY command
53 	GTree *physids;		// cache physmessage_ids for uids
54 	GTree *envelopes;
55 	GTree *mbxinfo; 	// cache MailboxState_T
56 	GList *ids_list;
57 
58 	struct cmd_t *cmd; // command structure (wip)
59 	gboolean error; // command result
60 	int error_count;
61 	ClientState_T state; // session status
62 	ImapEnabled_T enabled; // qresync/condstore enabled
63 	Connection_T c; // database-connection;
64 } ImapSession;
65 
66 
67 typedef int (*IMAP_COMMAND_HANDLER) (ImapSession *);
68 
69 /* thread data */
70 typedef struct {
71 #define DM_THREAD_DATA_MAGIC 0x5af8d
72 	unsigned int magic;
73 	Mempool_T pool;
74 	void (* cb_enter)(gpointer);	/* callback on thread entry		*/
75 	void (* cb_leave)(gpointer);	/* callback on thread exit		*/
76 	ImapSession *session;
77 	gpointer data;                  /* payload */
78 	volatile int status;		/* command result 			*/
79 } dm_thread_data;
80 
81 /* public methods */
82 
83 ImapSession * dbmail_imap_session_new(Mempool_T);
84 ImapSession * dbmail_imap_session_set_command(ImapSession * self, const char * command);
85 
86 void dbmail_imap_session_reset(ImapSession *session);
87 
88 void dbmail_imap_session_args_free(ImapSession *self, gboolean all);
89 void dbmail_imap_session_fetch_free(ImapSession *self, gboolean all);
90 void dbmail_imap_session_delete(ImapSession ** self);
91 
92 void dbmail_imap_session_buff_clear(ImapSession *self);
93 void dbmail_imap_session_buff_flush(ImapSession *self);
94 int dbmail_imap_session_buff_printf(ImapSession * self, char * message, ...);
95 
96 int dbmail_imap_session_set_state(ImapSession *self, ClientState_T state);
97 int dbmail_imap_session_handle_auth(ImapSession * self, const char * username, const char * password);
98 
99 MailboxState_T dbmail_imap_session_mbxinfo_lookup(ImapSession *self, uint64_t mailbox_idnr);
100 
101 int dbmail_imap_session_mailbox_status(ImapSession * self, gboolean update);
102 int dbmail_imap_session_mailbox_expunge(ImapSession *self, const char *set, uint64_t *modseq);
103 
104 int dbmail_imap_session_fetch_get_items(ImapSession *self);
105 int dbmail_imap_session_fetch_parse_args(ImapSession * self);
106 
107 void dbmail_imap_session_bodyfetch_free(ImapSession *self);
108 
109 int imap4_tokenizer_main(ImapSession *self, const char *buffer);
110 
111 
112 void _ic_cb_leave(gpointer data);
113 
114 #endif
115 
116