libical, expat, and libsieve are now *required*.
[citadel.git] / citadel / modules / jabber / xmpp_queue.c
1 /*
2  * $Id$ 
3  *
4  * XMPP event queue
5  *
6  * Copyright (c) 2007 by Art Cancro
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <ctype.h>
36 #include <expat.h>
37 #include <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "internet_addressing.h"
44 #include "md5.h"
45 #include "ctdl_module.h"
46 #include "serv_xmpp.h"
47
48 int queue_event_seq = 0;
49
50 void xmpp_queue_event(int event_type, char *email_addr) {
51
52         struct xmpp_event *xptr = NULL;
53         struct xmpp_event *new_event = NULL;
54         struct xmpp_event *last = NULL;
55         int purged_something = 0;
56         struct CitContext *cptr;
57
58         lprintf(CTDL_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
59
60         /* Purge events more than a minute old */
61         begin_critical_section(S_XMPP_QUEUE);
62         do {
63                 purged_something = 0;
64                 if (xmpp_queue != NULL) {
65                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
66                                 xptr = xmpp_queue->next;
67                                 free(xmpp_queue);
68                                 xmpp_queue = xptr;
69                                 purged_something = 1;
70                         }
71                 }
72         } while(purged_something);
73         end_critical_section(S_XMPP_QUEUE);
74
75         /* Create a new event */
76         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
77         new_event->next = NULL;
78         new_event->event_time = time(NULL);
79         new_event->event_seq = ++queue_event_seq;
80         new_event->event_type = event_type;
81         new_event->session_which_generated_this_event = CC->cs_pid;
82         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
83
84         /* Add it to the list */
85         begin_critical_section(S_XMPP_QUEUE);
86         if (xmpp_queue == NULL) {
87                 xmpp_queue = new_event;
88         }
89         else {
90                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
91                         if (xptr->next == NULL) {
92                                 last = xptr;
93                         }
94                 }
95                 last->next = new_event;
96         }
97         end_critical_section(S_XMPP_QUEUE);
98
99         /* Tell the sessions that something is happening */
100         begin_critical_section(S_SESSION_TABLE);
101         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
102                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
103                         cptr->async_waiting = 1;
104                 }
105         }
106         end_critical_section(S_SESSION_TABLE);
107 }
108
109
110 /* 
111  * Are we interested in anything from the queue?  (Called in async loop)
112  */
113 void xmpp_process_events(void) {
114         struct xmpp_event *xptr = NULL;
115         int highest_event = 0;
116
117         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
118                 if (xptr->event_seq > XMPP->last_event_processed) {
119
120                         switch(xptr->event_type) {
121
122                                 case XMPP_EVT_LOGIN:
123                                 case XMPP_EVT_LOGOUT:
124                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
125                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
126                                         }
127                                         break;
128                         }
129
130                         if (xptr->event_seq > highest_event) {
131                                 highest_event = xptr->event_seq;
132                         }
133                 }
134         }
135
136         XMPP->last_event_processed = highest_event;
137 }