2 * jabberd - Jabber Open Source Server
3 * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4 * Ryan Eatmon, Robert Norris
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
22 /** ! ! ! Patched version to disable offline storage for jabberd-2.0s4 ! ! ! */
26 /** @file sm/mod_offline.c
27 * @brief offline storage
28 * @author Robert Norris
33 typedef struct _mod_offline_st {
35 int dropsubscriptions;
38 static mod_ret_t _offline_in_sess(mod_instance_t mi, sess_t sess, pkt_t pkt) {
46 char cttl[15], cstamp[18];
49 /* if they're becoming available for the first time */
50 if(pkt->type == pkt_PRESENCE && pkt->to == NULL && sess->user->top == NULL) {
52 ret = storage_get(pkt->sm->st, "queue", jid_user(sess->jid), NULL, &os);
53 if(ret != st_SUCCESS) {
54 log_debug(ZONE, "storage_get returned %d", ret);
60 o = os_iter_object(os);
62 if(os_object_get(o, "xml", (void **) &nad, &ot)) {
63 queued = pkt_new(pkt->sm, nad_copy(nad));
65 log_debug(ZONE, "invalid queued packet, not delivering");
67 /* check expiry as necessary */
68 if((ns = nad_find_scoped_namespace(queued->nad, uri_EXPIRE, NULL)) >= 0 &&
69 (elem = nad_find_elem(queued->nad, 1, ns, "x", 1)) >= 0 &&
70 (attr = nad_find_attr(queued->nad, elem, -1, "seconds", NULL)) >= 0) {
71 snprintf(cttl, 15, "%.*s", NAD_AVAL_L(queued->nad, attr), NAD_AVAL(queued->nad, attr));
74 /* it should have a x:delay stamp, because we stamp everything we store */
75 if((ns = nad_find_scoped_namespace(queued->nad, uri_DELAY, NULL)) >= 0 &&
76 (elem = nad_find_elem(queued->nad, 1, ns, "x", 1)) >= 0 &&
77 (attr = nad_find_attr(queued->nad, elem, -1, "stamp", NULL)) >= 0) {
78 snprintf(cstamp, 18, "%.*s", NAD_AVAL_L(queued->nad, attr), NAD_AVAL(queued->nad, attr));
79 stamp = datetime_in(cstamp);
81 if(stamp + ttl <= time(NULL)) {
82 log_debug(ZONE, "queued packet has expired, dropping");
89 log_debug(ZONE, "delivering queued packet to %s", jid_full(sess->jid));
90 pkt_sess(queued, sess);
93 } while(os_iter_next(os));
98 storage_delete(pkt->sm->st, "queue", jid_user(sess->jid), NULL);
101 /* pass it so that other modules and mod_presence can get it */
105 static mod_ret_t _offline_pkt_user(mod_instance_t mi, user_t user, pkt_t pkt) {
106 mod_offline_t offline = (mod_offline_t) mi->mod->private;
112 /* send messages and s10ns to the top session */
113 if(user->top != NULL && (pkt->type & pkt_MESSAGE || pkt->type & pkt_S10N)) {
114 pkt_sess(pkt, user->top);
118 /* save messages and s10ns for later */
119 if((pkt->type & pkt_MESSAGE && !offline->dropmessages) ||
120 (pkt->type & pkt_S10N && !offline->dropsubscriptions)) {
121 log_debug(ZONE, "saving message for later");
123 pkt_delay(pkt, time(NULL), user->sm->id);
127 o = os_object_new(os);
129 os_object_put(o, "xml", pkt->nad, os_type_NAD);
132 switch(storage_put(user->sm->st, "queue", jid_user(user->jid), os)) {
135 return -stanza_err_INTERNAL_SERVER_ERROR;
139 return -stanza_err_SERVICE_UNAVAILABLE; /* xmpp-im 9.5#4 */
144 /* send offline events if they asked for it */
145 if((ns = nad_find_scoped_namespace(pkt->nad, uri_EVENT, NULL)) >= 0 &&
146 (elem = nad_find_elem(pkt->nad, 1, ns, "x", 1)) >= 0 &&
147 nad_find_elem(pkt->nad, elem, ns, "offline", 1) >= 0) {
149 event = pkt_create(user->sm, "message", NULL, jid_full(pkt->from), jid_full(pkt->to));
151 attr = nad_find_attr(pkt->nad, 1, -1, "type", NULL);
153 nad_set_attr(event->nad, 1, -1, "type", NAD_AVAL(pkt->nad, attr), NAD_AVAL_L(pkt->nad, attr));
155 ns = nad_add_namespace(event->nad, uri_EVENT, NULL);
156 nad_append_elem(event->nad, ns, "x", 2);
157 nad_append_elem(event->nad, ns, "offline", 3);
159 nad_append_elem(event->nad, ns, "id", 3);
160 attr = nad_find_attr(pkt->nad, 1, -1, "id", NULL);
162 nad_append_cdata(event->nad, NAD_AVAL(pkt->nad, attr), NAD_AVAL_L(pkt->nad, attr), 4);
175 static void _offline_user_delete(mod_instance_t mi, jid_t jid) {
182 char cttl[15], cstamp[18];
185 log_debug(ZONE, "deleting queue for %s", jid_user(jid));
187 /* bounce the queue */
188 if(storage_get(mi->mod->mm->sm->st, "queue", jid_user(jid), NULL, &os) == st_SUCCESS) {
189 if(os_iter_first(os))
191 o = os_iter_object(os);
193 if(os_object_get(o, "xml", (void **) &nad, &ot)) {
194 queued = pkt_new(mi->mod->mm->sm, nad);
196 log_debug(ZONE, "invalid queued packet, not delivering");
198 /* check expiry as necessary */
199 if((ns = nad_find_scoped_namespace(queued->nad, uri_EXPIRE, NULL)) >= 0 &&
200 (elem = nad_find_elem(queued->nad, 1, ns, "x", 1)) >= 0 &&
201 (attr = nad_find_attr(queued->nad, elem, -1, "seconds", NULL)) >= 0) {
202 snprintf(cttl, 15, "%.*s", NAD_AVAL_L(queued->nad, attr), NAD_AVAL(queued->nad, attr));
205 /* it should have a x:delay stamp, because we stamp everything we store */
206 if((ns = nad_find_scoped_namespace(queued->nad, uri_DELAY, NULL)) >= 0 &&
207 (elem = nad_find_elem(queued->nad, 1, ns, "x", 1)) >= 0 &&
208 (attr = nad_find_attr(queued->nad, elem, -1, "stamp", NULL)) >= 0) {
209 snprintf(cstamp, 18, "%.*s", NAD_AVAL_L(queued->nad, attr), NAD_AVAL(queued->nad, attr));
210 stamp = datetime_in(cstamp);
212 if(stamp + ttl <= time(NULL)) {
213 log_debug(ZONE, "queued packet has expired, dropping");
220 log_debug(ZONE, "bouncing queued packet from %s", jid_full(queued->from));
221 pkt_router(pkt_error(queued, stanza_err_ITEM_NOT_FOUND));
224 } while(os_iter_next(os));
229 storage_delete(mi->sm->st, "queue", jid_user(jid), NULL);
232 static void _offline_free(module_t mod) {
233 mod_offline_t offline = (mod_offline_t) mod->private;
238 int offline_init(mod_instance_t mi, char *arg) {
239 module_t mod = mi->mod;
241 mod_offline_t offline;
242 int dropmessages = 0;
243 int dropsubscriptions = 0;
245 if(mod->init) return 0;
247 configval = config_get_one(mod->mm->sm->config, "offline.dropmessages", 0);
248 if (configval != NULL)
250 configval = config_get_one(mod->mm->sm->config, "offline.dropsubscriptions", 0);
251 if (configval != NULL)
252 dropsubscriptions = 1;
254 offline = (mod_offline_t) malloc(sizeof(struct _mod_offline_st));
255 offline->dropmessages = dropmessages;
256 offline->dropsubscriptions = dropsubscriptions;
258 mod->private = offline;
260 mod->in_sess = _offline_in_sess;
261 mod->pkt_user = _offline_pkt_user;
262 mod->user_delete = _offline_user_delete;
263 mod->free = _offline_free;