4 * Copyright (c) 2007-2017 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>
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
41 #include <libcitadel.h>
44 #include "citserver.h"
47 #include "internet_addressing.h"
49 #include "ctdl_module.h"
50 #include "serv_xmpp.h"
52 int queue_event_seq = 0;
54 void xmpp_queue_event(int event_type, char *email_addr) {
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;
62 syslog(LOG_DEBUG, "xmpp: xmpp_queue_event(%d, %s)", event_type, email_addr);
64 /* Purge events more than a minute old */
65 begin_critical_section(S_XMPP_QUEUE);
68 if (xmpp_queue != NULL) {
69 if ((time(NULL) - xmpp_queue->event_time) > 60) {
70 xptr = xmpp_queue->next;
76 } while(purged_something);
77 end_critical_section(S_XMPP_QUEUE);
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);
88 /* Add it to the list */
89 begin_critical_section(S_XMPP_QUEUE);
90 if (xmpp_queue == NULL) {
91 xmpp_queue = new_event;
94 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
95 if (xptr->next == NULL) {
99 last->next = new_event;
101 end_critical_section(S_XMPP_QUEUE);
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);
110 end_critical_section(S_SESSION_TABLE);
115 * Are we interested in anything from the queue? (Called in async loop)
117 void xmpp_process_events(void) {
118 struct xmpp_event *xptr = NULL;
119 int highest_event = 0;
121 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
122 if (xptr->event_seq > XMPP->last_event_processed) {
124 switch(xptr->event_type) {
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);
134 if (xptr->event_seq > highest_event) {
135 highest_event = xptr->event_seq;
140 XMPP->last_event_processed = highest_event;
144 void xmpp_cleanup_events(void)
146 struct xmpp_event *ptr, *ptr2;
147 begin_critical_section(S_XMPP_QUEUE);
150 while (ptr != NULL) {
155 end_critical_section(S_XMPP_QUEUE);