1 /* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
2 
3 #ifndef PUSH_NOTIFICATION_DRIVERS_H
4 #define PUSH_NOTIFICATION_DRIVERS_H
5 
6 #include "mail-user.h"
7 #include "push-notification-triggers.h"
8 
9 struct mail_user;
10 struct push_notification_driver_config;
11 struct push_notification_driver_txn;
12 struct push_notification_driver_user;
13 struct push_notification_txn_mbox;
14 struct push_notification_txn_msg;
15 
16 HASH_TABLE_DEFINE_TYPE(push_notification_config, const char *, const char *);
17 HASH_TABLE_DEFINE_TYPE(push_notification_msgs, void *,
18 					   struct push_notification_txn_msg *);
19 
20 struct push_notification_driver_vfuncs {
21 	/* Init driver. Config (from plugin configuration) is parsed once (no
22 	   user variable substitutions). Return 0 on success, or -1 if this
23 	   driver should be disabled (or on error). */
24 	int (*init)(struct push_notification_driver_config *config,
25 		    struct mail_user *user, pool_t pool, void **context,
26 		    const char **error_r);
27 	/* Called at the beginning of a notification transaction. Return TRUE on
28 	   success, or FALSE if this driver should be ignored for this
29 	   transaction. */
30 	bool (*begin_txn)(struct push_notification_driver_txn *dtxn);
31 	/* Called once for every mailbox processed. */
32 	void (*process_mbox)(struct push_notification_driver_txn *dtxn,
33 			     struct push_notification_txn_mbox *mbox);
34 	/* Called once for every message processed. */
35 	void (*process_msg)(struct push_notification_driver_txn *dtxn,
36 			    struct push_notification_txn_msg *msg);
37 	/* Called at the end of a successful notification transaction. */
38 	void (*end_txn)(struct push_notification_driver_txn *dtxn,
39 			bool success);
40 	/* Called when plugin is deinitialized. */
41 	void (*deinit)(struct push_notification_driver_user *duser);
42 	/* Called to cleanup any global resources used in plugin. */
43 	void (*cleanup)(void);
44 };
45 
46 struct push_notification_driver {
47 	const char *name;
48 	struct push_notification_driver_vfuncs v;
49 };
50 
51 struct push_notification_driver_config {
52 	HASH_TABLE_TYPE(push_notification_config) config;
53 	const char *raw_config;
54 };
55 
56 struct push_notification_driver_user {
57 	const struct push_notification_driver *driver;
58 	void *context;
59 };
60 
61 struct push_notification_driver_txn {
62 	const struct push_notification_driver_user *duser;
63 	struct push_notification_txn *ptxn;
64 
65 	/* Transaction context. */
66 	void *context;
67 };
68 
69 struct push_notification_driver_list {
70 	ARRAY(struct push_notification_driver_user *) drivers;
71 };
72 
73 struct push_notification_user {
74 	union mail_user_module_context module_ctx;
75 	struct push_notification_driver_list *driverlist;
76 };
77 
78 struct push_notification_trigger_ctx {
79 	const char *name;
80 	void *context;
81 };
82 
83 struct push_notification_txn {
84 	pool_t pool;
85 
86 	struct mailbox *mbox;
87 	struct mail_user *muser;
88 	struct push_notification_user *puser;
89 	bool initialized;
90 
91 	enum push_notification_event_trigger trigger;
92 	struct push_notification_trigger_ctx *trigger_ctx;
93 	ARRAY(struct push_notification_driver_txn *) drivers;
94 	ARRAY(struct push_notification_event_config *) events;
95 
96 	struct event *event;
97 
98 	/* Used with mailbox events. */
99 	struct push_notification_txn_mbox *mbox_txn;
100 
101 	/* Used with mailbox events. */
102 	HASH_TABLE_TYPE(push_notification_msgs) messages;
103 
104 	/* Private (used with message events). */
105 	struct mailbox_transaction_context *t;
106 };
107 
108 
109 int push_notification_driver_init(
110 	struct mail_user *user, const char *config_in, pool_t pool,
111 	struct push_notification_driver_user **duser_r);
112 void push_notification_driver_cleanup_all(void);
113 
114 void ATTR_FORMAT(3, 4)
115 push_notification_driver_debug(const char *label, struct mail_user *user,
116 			       const char *fmt, ...);
117 
118 void push_notification_driver_register(
119 	const struct push_notification_driver *driver);
120 void push_notification_driver_unregister(
121 	const struct push_notification_driver *driver);
122 
123 #endif
124