4 * Copyright (c) 2007-2009 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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
30 #include <sys/types.h>
32 #if TIME_WITH_SYS_TIME
33 # include <sys/time.h>
37 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "citserver.h"
54 #include "internet_addressing.h"
56 #include "ctdl_module.h"
57 #include "serv_xmpp.h"
59 int queue_event_seq = 0;
61 void xmpp_queue_event(int event_type, char *email_addr) {
63 struct xmpp_event *xptr = NULL;
64 struct xmpp_event *new_event = NULL;
65 struct xmpp_event *last = NULL;
66 int purged_something = 0;
67 struct CitContext *cptr;
69 XMPP_syslog(LOG_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
71 /* Purge events more than a minute old */
72 begin_critical_section(S_XMPP_QUEUE);
75 if (xmpp_queue != NULL) {
76 if ((time(NULL) - xmpp_queue->event_time) > 60) {
77 xptr = xmpp_queue->next;
83 } while(purged_something);
84 end_critical_section(S_XMPP_QUEUE);
86 /* Create a new event */
87 new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
88 new_event->next = NULL;
89 new_event->event_time = time(NULL);
90 new_event->event_seq = ++queue_event_seq;
91 new_event->event_type = event_type;
92 new_event->session_which_generated_this_event = CC->cs_pid;
93 safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
95 /* Add it to the list */
96 begin_critical_section(S_XMPP_QUEUE);
97 if (xmpp_queue == NULL) {
98 xmpp_queue = new_event;
101 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
102 if (xptr->next == NULL) {
106 last->next = new_event;
108 end_critical_section(S_XMPP_QUEUE);
110 /* Tell the sessions that something is happening */
111 begin_critical_section(S_SESSION_TABLE);
112 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
113 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
114 set_async_waiting(cptr);
117 end_critical_section(S_SESSION_TABLE);
122 * Are we interested in anything from the queue? (Called in async loop)
124 void xmpp_process_events(void) {
125 struct xmpp_event *xptr = NULL;
126 int highest_event = 0;
128 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
129 if (xptr->event_seq > XMPP->last_event_processed) {
131 switch(xptr->event_type) {
134 case XMPP_EVT_LOGOUT:
135 if (xptr->session_which_generated_this_event != CC->cs_pid) {
136 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
141 if (xptr->event_seq > highest_event) {
142 highest_event = xptr->event_seq;
147 XMPP->last_event_processed = highest_event;
151 void xmpp_cleanup_events(void)
153 struct xmpp_event *ptr, *ptr2;
154 begin_critical_section(S_XMPP_QUEUE);
157 while (ptr != NULL) {
162 end_critical_section(S_XMPP_QUEUE);