2c97b8431a31fa1f51f966afa123e1adcf25ab72
[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 <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "internet_addressing.h"
43 #include "md5.h"
44 #include "ctdl_module.h"
45
46 #ifdef HAVE_EXPAT
47 #include <expat.h>
48 #include "serv_xmpp.h"
49
50 int queue_event_seq = 0;
51
52 void xmpp_queue_event(int event_type, char *email_addr) {
53
54         struct xmpp_event *xptr = NULL;
55         struct xmpp_event *new_event = NULL;
56         struct xmpp_event *last = NULL;
57         int purged_something = 0;
58         struct CitContext *cptr;
59
60         lprintf(CTDL_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
61
62         /* Purge events more than a minute old */
63         begin_critical_section(S_XMPP_QUEUE);
64         do {
65                 purged_something = 0;
66                 if (xmpp_queue != NULL) {
67                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
68                                 xptr = xmpp_queue->next;
69                                 free(xmpp_queue);
70                                 xmpp_queue = xptr;
71                                 purged_something = 1;
72                         }
73                 }
74         } while(purged_something);
75         end_critical_section(S_XMPP_QUEUE);
76
77         /* Create a new event */
78         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
79         new_event->next = NULL;
80         new_event->event_time = time(NULL);
81         new_event->event_seq = ++queue_event_seq;
82         new_event->event_type = event_type;
83         new_event->session_which_generated_this_event = CC->cs_pid;
84         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
85
86         /* Add it to the list */
87         begin_critical_section(S_XMPP_QUEUE);
88         if (xmpp_queue == NULL) {
89                 xmpp_queue = new_event;
90         }
91         else {
92                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
93                         if (xptr->next == NULL) {
94                                 last = xptr;
95                         }
96                 }
97                 last->next = new_event;
98         }
99         end_critical_section(S_XMPP_QUEUE);
100
101         /* Tell the sessions that something is happening */
102         begin_critical_section(S_SESSION_TABLE);
103         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
104                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
105                         cptr->async_waiting = 1;
106                 }
107         }
108         end_critical_section(S_SESSION_TABLE);
109 }
110
111
112 /* 
113  * Are we interested in anything from the queue?  (Called in async loop)
114  */
115 void xmpp_process_events(void) {
116         struct xmpp_event *xptr = NULL;
117         int highest_event = 0;
118
119         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
120                 if (xptr->event_seq > XMPP->last_event_processed) {
121
122                         switch(xptr->event_type) {
123
124                                 case XMPP_EVT_LOGIN:
125                                 case XMPP_EVT_LOGOUT:
126                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
127                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
128                                         }
129                                         break;
130                         }
131
132                         if (xptr->event_seq > highest_event) {
133                                 highest_event = xptr->event_seq;
134                         }
135                 }
136         }
137
138         XMPP->last_event_processed = highest_event;
139 }
140
141
142 #endif  /* HAVE_EXPAT */