* More license declarations
[citadel.git] / citadel / modules / jabber / xmpp_queue.c
1 /*
2  * $Id$ 
3  *
4  * XMPP event queue
5  *
6  * Copyright (c) 2007-2009 by Art Cancro
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <ctype.h>
49 #include <expat.h>
50 #include <libcitadel.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "internet_addressing.h"
57 #include "md5.h"
58 #include "ctdl_module.h"
59 #include "serv_xmpp.h"
60
61 int queue_event_seq = 0;
62
63 void xmpp_queue_event(int event_type, char *email_addr) {
64
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;
70
71         CtdlLogPrintf(CTDL_DEBUG, "xmpp_queue_event(%d, %s)\n", event_type, email_addr);
72
73         /* Purge events more than a minute old */
74         begin_critical_section(S_XMPP_QUEUE);
75         do {
76                 purged_something = 0;
77                 if (xmpp_queue != NULL) {
78                         if ((time(NULL) - xmpp_queue->event_time) > 60) {
79                                 xptr = xmpp_queue->next;
80                                 free(xmpp_queue);
81                                 xmpp_queue = xptr;
82                                 purged_something = 1;
83                         }
84                 }
85         } while(purged_something);
86         end_critical_section(S_XMPP_QUEUE);
87
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);
96
97         /* Add it to the list */
98         begin_critical_section(S_XMPP_QUEUE);
99         if (xmpp_queue == NULL) {
100                 xmpp_queue = new_event;
101         }
102         else {
103                 for (xptr = xmpp_queue; xptr != NULL; xptr = xptr->next) {
104                         if (xptr->next == NULL) {
105                                 last = xptr;
106                         }
107                 }
108                 last->next = new_event;
109         }
110         end_critical_section(S_XMPP_QUEUE);
111
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                         cptr->async_waiting = 1;
117                 }
118         }
119         end_critical_section(S_SESSION_TABLE);
120 }
121
122
123 /* 
124  * Are we interested in anything from the queue?  (Called in async loop)
125  */
126 void xmpp_process_events(void) {
127         struct xmpp_event *xptr = NULL;
128         int highest_event = 0;
129
130         for (xptr=xmpp_queue; xptr!=NULL; xptr=xptr->next) {
131                 if (xptr->event_seq > XMPP->last_event_processed) {
132
133                         switch(xptr->event_type) {
134
135                                 case XMPP_EVT_LOGIN:
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);
139                                         }
140                                         break;
141                         }
142
143                         if (xptr->event_seq > highest_event) {
144                                 highest_event = xptr->event_seq;
145                         }
146                 }
147         }
148
149         XMPP->last_event_processed = highest_event;
150 }