024336df871e7de743e3bebd19bbcbba902b45f9
[citadel.git] / citadel / modules / xmpp / xmpp_queue.c
1 /*
2  * XMPP event queue
3  *
4  * Copyright (c) 2007-2009 by Art Cancro
5  *
6  *  This program is open source software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License version 3.
8  *  
9  *  
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  
17  *  
18  *  
19  *
20  */
21
22 #include "sysdep.h"
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <pwd.h>
29 #include <errno.h>
30 #include <sys/types.h>
31
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
34 # include <time.h>
35 #else
36 # if HAVE_SYS_TIME_H
37 #  include <sys/time.h>
38 # else
39 #  include <time.h>
40 # endif
41 #endif
42
43 #include <sys/wait.h>
44 #include <string.h>
45 #include <limits.h>
46 #include <ctype.h>
47 #include <expat.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "internet_addressing.h"
55 #include "md5.h"
56 #include "ctdl_module.h"
57 #include "serv_xmpp.h"
58
59 int queue_event_seq = 0;
60 struct xmpp_event *xmpp_queue = NULL;
61
62 void xmpp_queue_event(int event_type, char *email_addr) {
63
64         struct xmpp_event *xptr = NULL;
65         struct xmpp_event *new_event = NULL;
66         struct xmpp_event *last = NULL;
67         int purged_something = 0;
68         struct CitContext *cptr;
69
70         XMPP_syslog(LOG_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
71
72         /* Purge events more than a minute old */
73         begin_critical_section(S_XMPP_QUEUE);
74         do {
75                 purged_something = 0;
76                 if (xmpp_queue != NULL) {
77                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
78                                 xptr = xmpp_queue->next;
79                                 free(xmpp_queue);
80                                 xmpp_queue = xptr;
81                                 purged_something = 1;
82                         }
83                 }
84         } while(purged_something);
85         end_critical_section(S_XMPP_QUEUE);
86
87         /* Create a new event */
88         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
89         new_event->next = NULL;
90         new_event->event_time = time(NULL);
91         new_event->event_seq = ++queue_event_seq;
92         new_event->event_type = event_type;
93         new_event->session_which_generated_this_event = CC->cs_pid;
94         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
95
96         /* Add it to the list */
97         begin_critical_section(S_XMPP_QUEUE);
98         if (xmpp_queue == NULL) {
99                 xmpp_queue = new_event;
100         }
101         else {
102                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
103                         if (xptr->next == NULL) {
104                                 last = xptr;
105                         }
106                 }
107                 last->next = new_event;
108         }
109         end_critical_section(S_XMPP_QUEUE);
110
111         /* Tell the sessions that something is happening */
112         begin_critical_section(S_SESSION_TABLE);
113         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
114                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
115                         set_async_waiting(cptr);
116                 }
117         }
118         end_critical_section(S_SESSION_TABLE);
119 }
120
121
122 /* 
123  * Are we interested in anything from the queue?  (Called in async loop)
124  */
125 void xmpp_process_events(void) {
126         struct xmpp_event *xptr = NULL;
127         int highest_event = 0;
128
129         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
130                 if (xptr->event_seq > XMPP->last_event_processed) {
131
132                         switch(xptr->event_type) {
133
134                                 case XMPP_EVT_LOGIN:
135                                 case XMPP_EVT_LOGOUT:
136                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
137                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
138                                         }
139                                         break;
140                         }
141
142                         if (xptr->event_seq > highest_event) {
143                                 highest_event = xptr->event_seq;
144                         }
145                 }
146         }
147
148         XMPP->last_event_processed = highest_event;
149 }
150
151
152 void xmpp_cleanup_events(void)
153 {
154         struct xmpp_event *ptr, *ptr2;
155         begin_critical_section(S_XMPP_QUEUE);
156         ptr = xmpp_queue;
157         xmpp_queue = NULL;
158         while (ptr != NULL) {
159                 ptr2 = ptr->next;
160                 free(ptr);
161                 ptr = ptr2;
162         }
163         end_critical_section(S_XMPP_QUEUE);
164
165 }
166
167 CTDL_MODULE_INIT(xmpp_queue)
168 {
169         if (!threading) {
170
171                 CtdlRegisterCleanupHook(xmpp_cleanup_events);
172         }
173
174         /* return our module name for the log */
175         return "xmpp_queue";
176 }