4 * Copyright (c) 2007-2021 by Art Cancro
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.
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.
23 #include <sys/types.h>
30 #include <libcitadel.h>
33 #include "citserver.h"
36 #include "internet_addressing.h"
37 #include "ctdl_module.h"
38 #include "serv_xmpp.h"
40 int queue_event_seq = 0;
42 void xmpp_queue_event(int event_type, char *email_addr) {
44 struct xmpp_event *xptr = NULL;
45 struct xmpp_event *new_event = NULL;
46 struct xmpp_event *last = NULL;
47 int purged_something = 0;
48 struct CitContext *cptr;
50 syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
52 /* Purge events more than a minute old */
53 begin_critical_section(S_XMPP_QUEUE);
56 if (xmpp_queue != NULL) {
57 if ((time(NULL) - xmpp_queue->event_time) > 60) {
58 xptr = xmpp_queue->next;
64 } while(purged_something);
65 end_critical_section(S_XMPP_QUEUE);
67 /* Create a new event */
68 new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
69 new_event->next = NULL;
70 new_event->event_time = time(NULL);
71 new_event->event_seq = ++queue_event_seq;
72 new_event->event_type = event_type;
73 new_event->session_which_generated_this_event = CC->cs_pid;
74 safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
76 /* Add it to the list */
77 begin_critical_section(S_XMPP_QUEUE);
78 if (xmpp_queue == NULL) {
79 xmpp_queue = new_event;
82 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
83 if (xptr->next == NULL) {
87 last->next = new_event;
89 end_critical_section(S_XMPP_QUEUE);
91 /* Tell the sessions that something is happening */
92 begin_critical_section(S_SESSION_TABLE);
93 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
94 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
95 set_async_waiting(cptr);
98 end_critical_section(S_SESSION_TABLE);
103 * Are we interested in anything from the queue? (Called in async loop)
105 void xmpp_process_events(void) {
106 struct xmpp_event *xptr = NULL;
107 int highest_event = 0;
109 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
110 if (xptr->event_seq > XMPP->last_event_processed) {
112 switch(xptr->event_type) {
115 case XMPP_EVT_LOGOUT:
116 if (xptr->session_which_generated_this_event != CC->cs_pid) {
117 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
122 if (xptr->event_seq > highest_event) {
123 highest_event = xptr->event_seq;
128 XMPP->last_event_processed = highest_event;