1 #ifndef IMAPFILTER_H 2 #define IMAPFILTER_H 3 4 5 #include <stdio.h> 6 #include <sys/types.h> 7 #include <limits.h> 8 9 #include <lua.h> 10 #include <lualib.h> 11 12 #include <openssl/ssl.h> 13 14 #include "session.h" 15 16 17 /* Fatal error exit codes. */ 18 #define ERROR_SIGNAL 1 19 #define ERROR_CONFIG 2 20 #define ERROR_MEMALLOC 3 21 #define ERROR_PATHNAME 4 22 #define ERROR_CERTIFICATE 5 23 24 /* IMAP protocol supported by the server. */ 25 #define PROTOCOL_NONE 0 26 #define PROTOCOL_IMAP4REV1 1 27 #define PROTOCOL_IMAP4 2 28 29 /* Capabilities of mail server. */ 30 #define CAPABILITY_NONE 0x00 31 #define CAPABILITY_NAMESPACE 0x01 32 #define CAPABILITY_CRAMMD5 0x02 33 #define CAPABILITY_STARTTLS 0x04 34 #define CAPABILITY_CHILDREN 0x08 35 #define CAPABILITY_IDLE 0x10 36 #define CAPABILITY_XOAUTH2 0x20 37 38 /* Status responses and response codes. */ 39 #define STATUS_NONE 0 40 #define STATUS_OK 1 41 #define STATUS_NO 2 42 #define STATUS_BAD 3 43 #define STATUS_UNTAGGED 4 44 #define STATUS_CONTINUE 5 45 #define STATUS_BYE 6 46 #define STATUS_PREAUTH 7 47 #define STATUS_READONLY 8 48 #define STATUS_TRYCREATE 9 49 #define STATUS_TIMEOUT 10 50 #define STATUS_INTERRUPT 11 51 52 #define STATUS_DRYRUN STATUS_OK 53 54 /* Initial size for buffers. */ 55 #define INPUT_BUF 4096 56 #define OUTPUT_BUF 1024 57 #define NAMESPACE_BUF 512 58 #define CONVERSION_BUF 512 59 60 /* Maximum length, in bytes, of a utility's input line. */ 61 #ifndef LINE_MAX 62 #define LINE_MAX 2048 63 #endif 64 65 /* Wait retry timeout, in seconds, on network problems. */ 66 #define WAIT_RETRY_TIMEOUT 60 67 68 69 /* Program's options. */ 70 typedef struct options { 71 int verbose; /* Verbose mode. */ 72 int interactive; /* Act as an interpreter. */ 73 int dryrun; /* Don't send commands that do changes. */ 74 char *log; /* Log file for error messages. */ 75 char *config; /* Configuration file. */ 76 char *oneline; /* One line of program/configuration. */ 77 char *debug; /* Debug file. */ 78 char *truststore; /* CA TrustStore. */ 79 } options; 80 81 /* Environment variables. */ 82 typedef struct environment { 83 char *home; /* Program's home directory. */ 84 long pathmax; /* Maximum pathname. */ 85 } environment; 86 87 88 /* auth.c */ 89 unsigned char *auth_cram_md5(const char *user, const char *pass, 90 unsigned char *chal); 91 92 /* cert.c */ 93 int get_cert(session *ssn); 94 95 /* core.c */ 96 LUALIB_API int luaopen_ifcore(lua_State *lua); 97 98 /* file.c */ 99 void create_homedir(void); 100 int exists_file(char *fname); 101 int exists_dir(char *fname); 102 int create_file(char *fname, mode_t mode); 103 int get_pathmax(void); 104 char *get_filepath(char *fname); 105 106 /* log.c */ 107 void verbose(const char *info,...); 108 void debug(const char *debug,...); 109 void debugc(char c); 110 void error(const char *errmsg,...); 111 void fatal(unsigned int errnum, const char *fatal,...); 112 113 int open_debug(void); 114 int close_debug(void); 115 116 int open_log(void); 117 int close_log(void); 118 119 /* lua.c */ 120 void start_lua(void); 121 void stop_lua(void); 122 123 int get_option_boolean(const char *opt); 124 lua_Number get_option_number(const char *opt); 125 const char *get_option_string(const char *opt); 126 127 int set_table_boolean(const char *key, int value); 128 int set_table_number(const char *key, lua_Number value); 129 int set_table_string(const char *key, const char *value); 130 131 /* memory.c */ 132 void *xmalloc(size_t size); 133 void *xrealloc(void *ptr, size_t size); 134 void xfree(void *ptr); 135 char *xstrdup(const char *str); 136 char *xstrndup(const char *str, size_t len); 137 138 /* misc.c */ 139 const char *xstrcasestr(const char *haystack, const char *needle); 140 char *xstrncpy(char *dest, const char *src, size_t size); 141 142 /* namespace.c */ 143 const char *apply_namespace(const char *mbox, char *prefix, char delim); 144 const char *reverse_namespace(const char *mbox, char *prefix, char delim); 145 146 /* pcre.c */ 147 LUALIB_API int luaopen_ifre(lua_State *lua); 148 149 /* request.c */ 150 int request_noop(session *ssn); 151 int request_login(session **ssn, const char *server, const char *port, const 152 char *protocol, const char *user, const char *pass, const char *oauth2); 153 int request_logout(session *ssn); 154 int request_status(session *ssn, const char *mbox, unsigned int *exist, 155 unsigned int *recent, unsigned int *unseen, unsigned int *uidnext); 156 int request_select(session *ssn, const char *mbox); 157 int request_close(session *ssn); 158 int request_expunge(session *ssn); 159 int request_list(session *ssn, const char *refer, const char *name, char 160 **mboxs, char **folders); 161 int request_lsub(session *ssn, const char *refer, const char *name, char 162 **mboxs, char **folders); 163 int request_search(session *ssn, const char *criteria, const char *charset, 164 char **mesgs); 165 int request_fetchfast(session *ssn, const char *mesg, char **flags, char 166 **date, char **size); 167 int request_fetchflags(session *ssn, const char *mesg, char **flags); 168 int request_fetchdate(session *ssn, const char *mesg, char **date); 169 int request_fetchsize(session *ssn, const char *mesg, char **size); 170 int request_fetchstructure(session *ssn, const char *mesg, char **structure); 171 int request_fetchheader(session *ssn, const char *mesg, char **header, size_t 172 *len); 173 int request_fetchtext(session *ssn, const char *mesg, char **text, size_t 174 *len); 175 int request_fetchfields(session *ssn, const char *mesg, const char 176 *headerfields, char **fields, size_t *len); 177 int request_fetchpart(session *ssn, const char *mesg, const char *bodypart, 178 char **part, size_t *len); 179 int request_store(session *ssn, const char *mesg, const char *mode, const char 180 *flags); 181 int request_copy(session *ssn, const char *mesg, const char *mbox); 182 int request_append(session *ssn, const char *mbox, const char *mesg, size_t 183 mesglen, const char *flags, const char *date); 184 int request_create(session *ssn, const char *mbox); 185 int request_delete(session *ssn, const char *mbox); 186 int request_rename(session *ssn, const char *oldmbox, const char *newmbox); 187 int request_subscribe(session *ssn, const char *mbox); 188 int request_unsubscribe(session *ssn, const char *mbox); 189 int request_idle(session *ssn, char **event); 190 191 /* response.c */ 192 int response_generic(session *ssn, int tag); 193 int response_continuation(session *ssn, int tag); 194 int response_greeting(session *ssn); 195 int response_capability(session *ssn, int tag); 196 int response_authenticate(session *ssn, int tag, unsigned char **cont); 197 int response_namespace(session *ssn, int tag); 198 int response_status(session *ssn, int tag, unsigned int *exist, 199 unsigned int *recent, unsigned int *unseen, unsigned int *uidnext); 200 int response_examine(session *ssn, int tag, unsigned int *exist, 201 unsigned int *recent); 202 int response_select(session *ssn, int tag); 203 int response_list(session *ssn, int tag, char **mboxs, char **folders); 204 int response_search(session *ssn, int tag, char **mesgs); 205 int response_fetchfast(session *ssn, int tag, char **flags, char **date, 206 char **size); 207 int response_fetchflags(session *ssn, int tag, char **flags); 208 int response_fetchdate(session *ssn, int tag, char **date); 209 int response_fetchsize(session *ssn, int tag, char **size); 210 int response_fetchstructure(session *ssn, int tag, char **structure); 211 int response_fetchbody(session *ssn, int tag, char **body, size_t *len); 212 int response_idle(session *ssn, int tag, char **event); 213 214 /* signal.c */ 215 void catch_signals(void); 216 void release_signals(void); 217 void ignore_user_signals(void); 218 void catch_user_signals(void); 219 220 /* socket.c */ 221 int open_connection(session *ssn); 222 int close_connection(session *ssn); 223 ssize_t socket_read(session *ssn, char *buf, size_t len, long timeout, 224 int timeoutfail, int *interrupt); 225 ssize_t socket_write(session *ssn, const char *buf, size_t len); 226 int open_secure_connection(session *ssn); 227 int close_secure_connection(session *ssn); 228 ssize_t socket_secure_read(session *ssn, char *buf, size_t len); 229 ssize_t socket_secure_write(session *ssn, const char *buf, size_t len); 230 231 /* system.c */ 232 LUALIB_API int luaopen_ifsys(lua_State *lua); 233 234 235 #endif /* IMAPFILTER_H */ 236