6 * Copyright (c) 2007-2009 by Art Cancro
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
56 #include "internet_addressing.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
61 int queue_event_seq = 0;
63 void xmpp_queue_event(int event_type, char *email_addr) {
65 struct xmpp_event *xptr = NULL;
66 struct xmpp_event *new_event = NULL;
67 struct xmpp_event *last = NULL;
68 int purged_something = 0;
69 struct CitContext *cptr;
71 CtdlLogPrintf(CTDL_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
73 /* Purge events more than a minute old */
74 begin_critical_section(S_XMPP_QUEUE);
77 if (xmpp_queue != NULL) {
78 if ((time(NULL) - xmpp_queue->event_time) > 60) {
79 xptr = xmpp_queue->next;
85 } while(purged_something);
86 end_critical_section(S_XMPP_QUEUE);
88 /* Create a new event */
89 new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
90 new_event->next = NULL;
91 new_event->event_time = time(NULL);
92 new_event->event_seq = ++queue_event_seq;
93 new_event->event_type = event_type;
94 new_event->session_which_generated_this_event = CC->cs_pid;
95 safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
97 /* Add it to the list */
98 begin_critical_section(S_XMPP_QUEUE);
99 if (xmpp_queue == NULL) {
100 xmpp_queue = new_event;
103 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
104 if (xptr->next == NULL) {
108 last->next = new_event;
110 end_critical_section(S_XMPP_QUEUE);
112 /* Tell the sessions that something is happening */
113 begin_critical_section(S_SESSION_TABLE);
114 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
115 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
116 set_async_waiting(cptr);
119 end_critical_section(S_SESSION_TABLE);
124 * Are we interested in anything from the queue? (Called in async loop)
126 void xmpp_process_events(void) {
127 struct xmpp_event *xptr = NULL;
128 int highest_event = 0;
130 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
131 if (xptr->event_seq > XMPP->last_event_processed) {
133 switch(xptr->event_type) {
136 case XMPP_EVT_LOGOUT:
137 if (xptr->session_which_generated_this_event != CC->cs_pid) {
138 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
143 if (xptr->event_seq > highest_event) {
144 highest_event = xptr->event_seq;
149 XMPP->last_event_processed = highest_event;