1 #ifndef PERDIITON_MANAGESIEVE_WRITE_H
2 #define PERDIITON_MANAGESIEVE_WRITE_H
3 
4 #include "io.h"
5 #include "managesieve_response_code.h"
6 #include "perdition_types.h"
7 #include "token.h"
8 #include "unused.h"
9 
10 #define MANAGESIEVE_CMD_AUTHENTICATE	"AUTHENTICATE"
11 #define MANAGESIEVE_CMD_CAPABILITY	"CAPABILITY"
12 #define MANAGESIEVE_CMD_LOGOUT		"LOGOUT"
13 #define MANAGESIEVE_CMD_NOOP		"NOOP"
14 #define MANAGESIEVE_CMD_STARTTLS	"STARTTLS"
15 
16 #define MANAGESIEVE_GREETING	"perdition ready on"
17 #define MANAGESIEVE_QUIT	MANAGESIEVE_CMD_LOGOUT
18 #define MANAGESIEVE_OK	"OK"
19 #define MANAGESIEVE_NO	"NO"
20 #define MANAGESIEVE_BYE	"BYE"
21 #define MANAGESIEVE_CAPA_DELIMITER "  "
22 
23 /* Reflect capabilities of
24  * dovecot-1.2.10 +
25  * dovecot-1.2-sieve-0.1.15 +
26  * dovecot-1.2-managesieve-0.11.11 */
27 #define MANAGESIEVE_DEFAULT_CAPA \
28 	"\"IMPLEMENTATION\" \"perdition\""		\
29 	MANAGESIEVE_CAPA_DELIMITER			\
30 	"\"SIEVE\" \"comparator-i;octet "		\
31 		    "comparator-i;ascii-casemap "	\
32 		    "fileinto "				\
33 		    "reject "				\
34 		    "envelope "				\
35 		    "encoded-character "		\
36 		    "vacation "				\
37 		    "subaddress "			\
38 		    "comparator-i;ascii-numeric "	\
39 		    "relational "			\
40 		    "regex "				\
41 		    "imap4flags "			\
42 		    "copy "				\
43 		    "include "				\
44 		    "variables "			\
45 		    "body "				\
46 		    "enotify "				\
47 		    "environment "			\
48 		    "mailbox "				\
49 		    "date\""				\
50 	MANAGESIEVE_CAPA_DELIMITER			\
51 	"\"SASL\" \"PLAIN\""				\
52 	MANAGESIEVE_CAPA_DELIMITER			\
53 	"\"NOTIFY\" \"mailto\""				\
54 	MANAGESIEVE_CAPA_DELIMITER			\
55 	"\"VERSION\" \"1.0\""
56 
57 #define MANAGESIEVE_CAPA_STARTTLS "\"STARTTLS\""
58 
59 #define MANAGESIEVE_DEFAULT_PORT_NAME "sieve"
60 #define MANAGESIEVE_DEFAULT_PORT_NUMBER "4190"
61 
62 /**********************************************************************
63   * managesieve_write_raw
64   *       message: must not be NULL
65   *       message: must not be NULL
66   * Display an message without any alteration
67   **********************************************************************/
68 
69 int managesieve_write_raw(io_t *io, const char *message);
70 
71 /**********************************************************************
72   * managesieve_write
73   * pre: io: io_t to write to
74   *       flag: PERDITION_CLIENT or PERDITION_SERVER
75   *       command: must not be NULL
76   *       rc: response code
77   *       message: must not be NULL
78   * Display an message of the form <command> [<rc>] <message>
79   **********************************************************************/
80 
81 int managesieve_write(io_t *io, flag_t flag, const char *command,
82 		      const struct managesieve_response_code *rc,
83 		      const char *message);
84 
85 /**********************************************************************
86  * managesieve_write_str
87  * Display an message of the form <command> [<string>]
88  * Pre: io: io_t to write to
89  *      flag: flag to pass to str_write as per str.h
90  *      tag: ignored
91  *      command: command in message sent
92  *           if NULL then only string is written
93  *      string: string, omitted if NULL
94  *           At least one of command and string must be non-NULL
95  * Return 0 on success
96  *        -1 otherwise
97  **********************************************************************/
98 
99 static inline int
managesieve_write_str(io_t * io,const flag_t flag,const token_t * UNUSED (tag),const char * command,const char * str)100 managesieve_write_str(io_t *io, const flag_t flag, const token_t *UNUSED(tag),
101 		      const char *command, const char *str)
102 {
103 	if (!command && !str)
104 		return -1;
105 	if (!command)
106 		/* This is a special case used to handle opt.server_resp_line,
107 		 * and jsut requires str to be written verbatim. */
108 		 return str_write(io, WRITE_STR_NO_CLLF, 1, "%s", str);
109 	return managesieve_write(io, flag, command, NULL, str);
110 }
111 
112 static inline int
managesieve_ok(io_t * io,const struct managesieve_response_code * rc,const char * message)113 managesieve_ok(io_t *io, const struct managesieve_response_code *rc,
114 	       const char *message)
115 {
116 	return managesieve_write(io, PERDITION_SERVER,
117 				 MANAGESIEVE_OK, rc, message);
118 }
119 static inline int
managesieve_no(io_t * io,const struct managesieve_response_code * rc,const char * message)120 managesieve_no(io_t *io, const struct managesieve_response_code *rc,
121 	       const char *message)
122 {
123 	sleep(PERDITION_AUTH_FAIL_SLEEP);
124 	return managesieve_write(io, PERDITION_SERVER,
125 				 MANAGESIEVE_NO, rc, message);
126 }
127 
128 static inline int
managesieve_bye(io_t * io,const struct managesieve_response_code * rc,const char * message)129 managesieve_bye(io_t *io, const struct managesieve_response_code *rc,
130 		const char *message)
131 {
132 	sleep(PERDITION_AUTH_FAIL_SLEEP);
133 	return managesieve_write(io, PERDITION_SERVER,
134 				 MANAGESIEVE_BYE, rc, message);
135 }
136 
137 #endif /* PERDIITON_MANAGESIEVE_WRITE_H */
138