]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/xmpp/xmpp_queue.c
Remove preprocessor tests for OpenSSL. It's a requirement.
[citadel.git] / citadel / server / modules / xmpp / xmpp_queue.c
1 // XMPP event queue
2 // Copyright (c) 2007-2024 by Art Cancro
3 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
4
5 #include "../../sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <signal.h>
11 #include <pwd.h>
12 #include <errno.h>
13 #include <sys/types.h>
14 #include <time.h>
15 #include <sys/wait.h>
16 #include <string.h>
17 #include <limits.h>
18 #include <ctype.h>
19 #include <expat.h>
20 #include <libcitadel.h>
21 #include "../../citadel_defs.h"
22 #include "../../server.h"
23 #include "../../citserver.h"
24 #include "../../support.h"
25 #include "../../config.h"
26 #include "../../internet_addressing.h"
27 #include "../../ctdl_module.h"
28 #include "serv_xmpp.h"
29
30 int queue_event_seq = 0;
31
32 void xmpp_queue_event(int event_type, char *email_addr) {
33
34         struct xmpp_event *xptr = NULL;
35         struct xmpp_event *new_event = NULL;
36         struct xmpp_event *last = NULL;
37         int purged_something = 0;
38         struct CitContext *cptr;
39
40         syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
41
42         // Purge events more than a minute old
43         begin_critical_section(S_XMPP_QUEUE);
44         do {
45                 purged_something = 0;
46                 if (xmpp_queue != NULL) {
47                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
48                                 xptr = xmpp_queue->next;
49                                 free(xmpp_queue);
50                                 xmpp_queue = xptr;
51                                 purged_something = 1;
52                         }
53                 }
54         } while(purged_something);
55         end_critical_section(S_XMPP_QUEUE);
56
57         // Create a new event
58         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
59         new_event->next = NULL;
60         new_event->event_time = time(NULL);
61         new_event->event_seq = ++queue_event_seq;
62         new_event->event_type = event_type;
63         new_event->session_which_generated_this_event = CC->cs_pid;
64         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
65
66         // Add it to the list
67         begin_critical_section(S_XMPP_QUEUE);
68         if (xmpp_queue == NULL) {
69                 xmpp_queue = new_event;
70         }
71         else {
72                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
73                         if (xptr->next == NULL) {
74                                 last = xptr;
75                         }
76                 }
77                 last->next = new_event;
78         }
79         end_critical_section(S_XMPP_QUEUE);
80
81         // Tell the sessions that something is happening
82         begin_critical_section(S_SESSION_TABLE);
83         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
84                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
85                         set_async_waiting(cptr);
86                 }
87         }
88         end_critical_section(S_SESSION_TABLE);
89 }
90
91
92 // Are we interested in anything from the queue?  (Called in async loop)
93 void xmpp_process_events(void) {
94         struct xmpp_event *xptr = NULL;
95         int highest_event = 0;
96
97         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
98                 if (xptr->event_seq > XMPP->last_event_processed) {
99
100                         switch(xptr->event_type) {
101
102                                 case XMPP_EVT_LOGIN:
103                                 case XMPP_EVT_LOGOUT:
104                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
105                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
106                                         }
107                                         break;
108                         }
109
110                         if (xptr->event_seq > highest_event) {
111                                 highest_event = xptr->event_seq;
112                         }
113                 }
114         }
115
116         XMPP->last_event_processed = highest_event;
117 }