8cdf1a4d8a28fd5a523beec22dd5acf5cffbf4ba
[citadel.git] / citadel / modules / xmpp / xmpp_queue.c
1 /*
2  * XMPP event queue
3  *
4  * Copyright (c) 2007-2017 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 "md5.h"
49 #include "ctdl_module.h"
50 #include "serv_xmpp.h"
51
52 int queue_event_seq = 0;
53
54 void xmpp_queue_event(int event_type, char *email_addr) {
55
56         struct xmpp_event *xptr = NULL;
57         struct xmpp_event *new_event = NULL;
58         struct xmpp_event *last = NULL;
59         int purged_something = 0;
60         struct CitContext *cptr;
61
62         syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
63
64         /* Purge events more than a minute old */
65         begin_critical_section(S_XMPP_QUEUE);
66         do {
67                 purged_something = 0;
68                 if (xmpp_queue != NULL) {
69                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
70                                 xptr = xmpp_queue->next;
71                                 free(xmpp_queue);
72                                 xmpp_queue = xptr;
73                                 purged_something = 1;
74                         }
75                 }
76         } while(purged_something);
77         end_critical_section(S_XMPP_QUEUE);
78
79         /* Create a new event */
80         new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
81         new_event->next = NULL;
82         new_event->event_time = time(NULL);
83         new_event->event_seq = ++queue_event_seq;
84         new_event->event_type = event_type;
85         new_event->session_which_generated_this_event = CC->cs_pid;
86         safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
87
88         /* Add it to the list */
89         begin_critical_section(S_XMPP_QUEUE);
90         if (xmpp_queue == NULL) {
91                 xmpp_queue = new_event;
92         }
93         else {
94                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
95                         if (xptr->next == NULL) {
96                                 last = xptr;
97                         }
98                 }
99                 last->next = new_event;
100         }
101         end_critical_section(S_XMPP_QUEUE);
102
103         /* Tell the sessions that something is happening */
104         begin_critical_section(S_SESSION_TABLE);
105         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
106                 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
107                         set_async_waiting(cptr);
108                 }
109         }
110         end_critical_section(S_SESSION_TABLE);
111 }
112
113
114 /* 
115  * Are we interested in anything from the queue?  (Called in async loop)
116  */
117 void xmpp_process_events(void) {
118         struct xmpp_event *xptr = NULL;
119         int highest_event = 0;
120
121         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
122                 if (xptr->event_seq > XMPP->last_event_processed) {
123
124                         switch(xptr->event_type) {
125
126                                 case XMPP_EVT_LOGIN:
127                                 case XMPP_EVT_LOGOUT:
128                                         if (xptr->session_which_generated_this_event != CC->cs_pid) {
129                                                 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
130                                         }
131                                         break;
132                         }
133
134                         if (xptr->event_seq > highest_event) {
135                                 highest_event = xptr->event_seq;
136                         }
137                 }
138         }
139
140         XMPP->last_event_processed = highest_event;
141 }
142
143
144 void xmpp_cleanup_events(void)
145 {
146         struct xmpp_event *ptr, *ptr2;
147         begin_critical_section(S_XMPP_QUEUE);
148         ptr = xmpp_queue;
149         xmpp_queue = NULL;
150         while (ptr != NULL) {
151                 ptr2 = ptr->next;
152                 free(ptr);
153                 ptr = ptr2;
154         }
155         end_critical_section(S_XMPP_QUEUE);
156
157 }