* Restored the '(none)' and '(nothing)' displays in the summary screen when the Tasks...
[citadel.git] / citadel / modules / jabber / xmpp_queue.c
blobb97f5333b742eef0543ccd824c618426ed8eb686
1 /*
2 * $Id$
4 * XMPP event queue
6 * Copyright (c) 2007 by Art Cancro
7 * This code is released under the terms of the GNU General Public License.
9 */
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 # include <sys/time.h>
27 # else
28 # include <time.h>
29 # endif
30 #endif
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <ctype.h>
36 #include <expat.h>
37 #include <libcitadel.h>
38 #include "citadel.h"
39 #include "server.h"
40 #include "citserver.h"
41 #include "support.h"
42 #include "config.h"
43 #include "internet_addressing.h"
44 #include "md5.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);
62 do {
63 purged_something = 0;
64 if (xmpp_queue != NULL) {
65 if ((time(NULL) - xmpp_queue->event_time) > 60) {
66 xptr = xmpp_queue->next;
67 free(xmpp_queue);
68 xmpp_queue = xptr;
69 purged_something = 1;
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;
89 else {
90 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
91 if (xptr->next == NULL) {
92 last = xptr;
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) {
122 case XMPP_EVT_LOGIN:
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);
127 break;
130 if (xptr->event_seq > highest_event) {
131 highest_event = xptr->event_seq;
136 XMPP->last_event_processed = highest_event;