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.
5 #include "../../sysdep.h"
13 #include <sys/types.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"
30 int queue_event_seq = 0;
32 void xmpp_queue_event(int event_type, char *email_addr) {
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;
40 syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
42 // Purge events more than a minute old
43 begin_critical_section(S_XMPP_QUEUE);
46 if (xmpp_queue != NULL) {
47 if ((time(NULL) - xmpp_queue->event_time) > 60) {
48 xptr = xmpp_queue->next;
54 } while(purged_something);
55 end_critical_section(S_XMPP_QUEUE);
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);
67 begin_critical_section(S_XMPP_QUEUE);
68 if (xmpp_queue == NULL) {
69 xmpp_queue = new_event;
72 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
73 if (xptr->next == NULL) {
77 last->next = new_event;
79 end_critical_section(S_XMPP_QUEUE);
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);
88 end_critical_section(S_SESSION_TABLE);
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;
97 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
98 if (xptr->event_seq > XMPP->last_event_processed) {
100 switch(xptr->event_type) {
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);
110 if (xptr->event_seq > highest_event) {
111 highest_event = xptr->event_seq;
116 XMPP->last_event_processed = highest_event;