6 * Copyright (c) 2007 by Art Cancro
7 * This code is released under the terms of the GNU General Public License.
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
37 #include <libcitadel.h>
40 #include "citserver.h"
43 #include "internet_addressing.h"
45 #include "ctdl_module.h"
46 #include "serv_xmpp.h"
48 int queue_event_seq = 0;
50 void xmpp_queue_event(int event_type, char *email_addr) {
52 struct xmpp_event *xptr = NULL;
53 struct xmpp_event *new_event = NULL;
54 struct xmpp_event *last = NULL;
55 int purged_something = 0;
56 struct CitContext *cptr;
58 CtdlLogPrintf(CTDL_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
60 /* Purge events more than a minute old */
61 begin_critical_section(S_XMPP_QUEUE);
64 if (xmpp_queue != NULL) {
65 if ((time(NULL) - xmpp_queue->event_time) > 60) {
66 xptr = xmpp_queue->next;
72 } while(purged_something);
73 end_critical_section(S_XMPP_QUEUE);
75 /* Create a new event */
76 new_event = (struct xmpp_event *) malloc(sizeof(struct xmpp_event));
77 new_event->next = NULL;
78 new_event->event_time = time(NULL);
79 new_event->event_seq = ++queue_event_seq;
80 new_event->event_type = event_type;
81 new_event->session_which_generated_this_event = CC->cs_pid;
82 safestrncpy(new_event->event_jid, email_addr, sizeof new_event->event_jid);
84 /* Add it to the list */
85 begin_critical_section(S_XMPP_QUEUE);
86 if (xmpp_queue == NULL) {
87 xmpp_queue = new_event;
90 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
91 if (xptr->next == NULL) {
95 last->next = new_event;
97 end_critical_section(S_XMPP_QUEUE);
99 /* Tell the sessions that something is happening */
100 begin_critical_section(S_SESSION_TABLE);
101 for (cptr = ContextList; cptr != NULL; cptr = cptr->next) {
102 if ((cptr->logged_in) && (cptr->h_async_function == xmpp_async_loop)) {
103 cptr->async_waiting = 1;
106 end_critical_section(S_SESSION_TABLE);
111 * Are we interested in anything from the queue? (Called in async loop)
113 void xmpp_process_events(void) {
114 struct xmpp_event *xptr = NULL;
115 int highest_event = 0;
117 for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
118 if (xptr->event_seq > XMPP->last_event_processed) {
120 switch(xptr->event_type) {
123 case XMPP_EVT_LOGOUT:
124 if (xptr->session_which_generated_this_event != CC->cs_pid) {
125 xmpp_presence_notify(xptr->event_jid, xptr->event_type);
130 if (xptr->event_seq > highest_event) {
131 highest_event = xptr->event_seq;
136 XMPP->last_event_processed = highest_event;