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;
60 struct xmpp_event *xmpp_queue = NULL;
62 void xmpp_queue_event(int event_type, char *email_addr) {
64 struct xmpp_event *xptr = NULL;
65 struct xmpp_event *new_event = NULL;
66 struct xmpp_event *last = NULL;
67 int purged_something = 0;
68 struct CitContext *cptr;
70 XMPP_syslog(LOG_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
72 /* Purge events more than a minute old */
73 begin_critical_section(S_XMPP_QUEUE);
76 if (xmpp_queue != NULL) {
77 if ((time(NULL) - xmpp_queue->event_time) > 60) {
78 xptr = xmpp_queue->next;
84 } while(purged_something);
85 end_critical_section(S_XMPP_QUEUE);
87 /* Create a new event */
88 new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
89 new_event->next = NULL;
90 new_event->event_time = time(NULL);
91 new_event->event_seq = ++queue_event_seq;
92 new_event->event_type = event_type;
93 new_event->session_which_generated_this_event = CC->cs_pid;
94 safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
96 /* Add it to the list */
97 begin_critical_section(S_XMPP_QUEUE);
98 if (xmpp_queue == NULL) {
99 xmpp_queue = new_event;
102 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
103 if (xptr->next == NULL) {
107 last->next = new_event;
109 end_critical_section(S_XMPP_QUEUE);
111 /* Tell the sessions that something is happening */
112 begin_critical_section(S_SESSION_TABLE);
113 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
114 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
115 set_async_waiting(cptr);
118 end_critical_section(S_SESSION_TABLE);
123 * Are we interested in anything from the queue? (Called in async loop)
125 void xmpp_process_events(void) {
126 struct xmpp_event *xptr = NULL;
127 int highest_event = 0;
129 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
130 if (xptr->event_seq > XMPP->last_event_processed) {
132 switch(xptr->event_type) {
135 case XMPP_EVT_LOGOUT:
136 if (xptr->session_which_generated_this_event != CC->cs_pid) {
137 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
142 if (xptr->event_seq > highest_event) {
143 highest_event = xptr->event_seq;
148 XMPP->last_event_processed = highest_event;
152 void xmpp_cleanup_events(void)
154 struct xmpp_event *ptr, *ptr2;
155 begin_critical_section(S_XMPP_QUEUE);
158 while (ptr != NULL) {
163 end_critical_section(S_XMPP_QUEUE);
167 CTDL_MODULE_INIT(xmpp_queue)
171 CtdlRegisterCleanupHook(xmpp_cleanup_events);
174 /* return our module name for the log */