No more cleanup hooks. The OS can reclaim memory better than we can. We want to...
[citadel.git] / citadel / modules / xmpp / xmpp_queue.c
1 /*
2  * XMPP event queue
3  *
4  * Copyright (c) 2007-2021 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  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <signal.h>
21 #include <pwd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <time.h>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <time.h>
33 # endif
34 #endif
35
36 #include <sys/wait.h>
37 #include <string.h>
38 #include <limits.h>
39 #include <ctype.h>
40 #include <expat.h>
41 #include <libcitadel.h>
42 #include "citadel.h"
43 #include "server.h"
44 #include "citserver.h"
45 #include "support.h"
46 #include "config.h"
47 #include "internet_addressing.h"
48 #include "ctdl_module.h"
49 #include "serv_xmpp.h"
50
51 int queue_event_seq = 0;
52
53 void xmpp_queue_event(int event_type, char *email_addr) {
54
55         struct xmpp_event *xptr = NULL;
56         struct xmpp_event *new_event = NULL;
57         struct xmpp_event *last = NULL;
58         int purged_something = 0;
59         struct CitContext *cptr;
60
61         syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
62
63         /* Purge events more than a minute old */
64         begin_critical_section(S_XMPP_QUEUE);
65         do {
66                 purged_something = 0;
67                 if (xmpp_queue != NULL) {
68                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
69                                 xptr = xmpp_queue->next;
70                                 free(xmpp_queue);
71                                 xmpp_queue = xptr;
72                                 purged_something = 1;
73                         }
74                 }
75         } while(purged_something);
76         end_critical_section(S_XMPP_QUEUE);
77
78         /* Create a new event */
79         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
80         new_event->next = NULL;
81         new_event->event_time = time(NULL);
82         new_event->event_seq = ++queue_event_seq;
83         new_event->event_type = event_type;
84         new_event->session_which_generated_this_event = CC->cs_pid;
85         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
86
87         /* Add it to the list */
88         begin_critical_section(S_XMPP_QUEUE);
89         if (xmpp_queue == NULL) {
90                 xmpp_queue = new_event;
91         }
92         else {
93                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
94                         if (xptr->next == NULL) {
95                                 last = xptr;
96                         }
97                 }
98                 last->next = new_event;
99         }
100         end_critical_section(S_XMPP_QUEUE);
101
102         /* Tell the sessions that something is happening */
103         begin_critical_section(S_SESSION_TABLE);
104         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
105                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
106                         set_async_waiting(cptr);
107                 }
108         }
109         end_critical_section(S_SESSION_TABLE);
110 }
111
112
113 /* 
114  * Are we interested in anything from the queue?  (Called in async loop)
115  */
116 void xmpp_process_events(void) {
117         struct xmpp_event *xptr = NULL;
118         int highest_event = 0;
119
120         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
121                 if (xptr->event_seq > XMPP->last_event_processed) {
122
123                         switch(xptr->event_type) {
124
125                                 case XMPP_EVT_LOGIN:
126                                 case XMPP_EVT_LOGOUT:
127                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
128                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
129                                         }
130                                         break;
131                         }
132
133                         if (xptr->event_seq > highest_event) {
134                                 highest_event = xptr->event_seq;
135                         }
136                 }
137         }
138
139         XMPP->last_event_processed = highest_event;
140 }