File: mod_iq_last.c

package info (click to toggle)
jabberd2 2.7.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,196 kB
  • sloc: ansic: 37,104; sh: 1,100; perl: 656; xml: 561; makefile: 511; python: 238; ruby: 145; sql: 55
file content (166 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * jabberd - Jabber Open Source Server
 * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
 *                    Ryan Eatmon, Robert Norris
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
 */

#include "sm.h"

/** @file sm/mod_iq_last.c
  * @brief last activity
  * @author Robert Norris
  * $Date: 2005/08/17 07:48:28 $
  * $Revision: 1.18 $
  */

#define uri_LAST    "jabber:iq:last"
static int ns_LAST = 0;

static mod_ret_t _iq_last_pkt_sm(mod_instance_t mi, pkt_t pkt) {
    module_t mod = mi->mod;
    char uptime[10];

    /* we only want to play with iq:last gets */
    if(pkt->type != pkt_IQ || pkt->ns != ns_LAST)
        return mod_PASS;

    snprintf(uptime, 10, "%d", (int) (time(NULL) - (time_t) mod->private));
    nad_set_attr(pkt->nad, 2, -1, "seconds", uptime, 0);

    /* tell them */
    nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
    pkt_router(pkt_tofrom(pkt));

    return mod_HANDLED;
}

static mod_ret_t _iq_last_pkt_user(mod_instance_t mi, user_t user, pkt_t pkt) {
    char lasttime[10];
    time_t t;
    os_t os;
    os_object_t o;
    st_ret_t ret;

    /* we only want to play with iq:last gets */
    if(pkt->type != pkt_IQ || pkt->ns != ns_LAST)
        return mod_PASS;

    /* make sure they're allowed */
    if(!pres_trust(user, pkt->from))
        return -stanza_err_FORBIDDEN;

    /* If the IQ was sent to a JID with a resource, then XMPP-IM 11.1.1
     * requires we deliver it if that resource is available
     */
    if (*pkt->to->resource != '\0')
	return mod_PASS;

    /* If they have an available resource, we should return a query element with a
     * seconds value of 0
     */
    if(user->top != NULL)
    {
	nad_set_attr(pkt->nad, 2, -1, "seconds", "0", 0);
	nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
	pkt_router(pkt_tofrom(pkt));

        return mod_HANDLED;
    }

    ret = storage_get(user->sm->st, "logout", jid_user(user->jid), NULL, &os);
    switch(ret) {
        case st_SUCCESS:
            t = 0;

            if(os_iter_first(os)) {
                o = os_iter_object(os);

                os_object_get_time(os, o, "time", &t);
            }

            os_free(os);

            snprintf(lasttime, 10, "%d", (int) (time(NULL) - t));
            nad_set_attr(pkt->nad, 2, -1, "seconds", lasttime, 0);

            nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
            pkt_router(pkt_tofrom(pkt));

            return mod_HANDLED;

        case st_FAILED:
            return -stanza_err_INTERNAL_SERVER_ERROR;

        case st_NOTFOUND:
            return -stanza_err_SERVICE_UNAVAILABLE;

        case st_NOTIMPL:
            return -stanza_err_FEATURE_NOT_IMPLEMENTED;
    }

    /* we never get here */
    return -stanza_err_INTERNAL_SERVER_ERROR;
}

static void _iq_last_sess_end(mod_instance_t mi, sess_t sess) {
    time_t t;
    os_t os;
    os_object_t o;

    /* store their logout time */
    t = time(NULL);

    os = os_new();
    o = os_object_new(os);

    os_object_put_time(o, "time", &t);

    storage_replace(sess->user->sm->st, "logout", jid_user(sess->jid), NULL, os);

    os_free(os);
}

static void _iq_last_user_delete(mod_instance_t mi, jid_t jid) {
    log_debug(ZONE, "deleting logout time for %s", jid_user(jid));

    storage_delete(mi->sm->st, "logout", jid_user(jid), NULL);
}

static void _iq_last_free(module_t mod) {
    sm_unregister_ns(mod->mm->sm, uri_LAST);
    feature_unregister(mod->mm->sm, uri_LAST);
}

DLLEXPORT int module_init(mod_instance_t mi, const char *arg) {
    module_t mod = mi->mod;

    if(mod->init) return 0;

    mod->sess_end = _iq_last_sess_end;
    mod->pkt_user = _iq_last_pkt_user;
    mod->pkt_sm = _iq_last_pkt_sm;
    mod->user_delete = _iq_last_user_delete;
    mod->free = _iq_last_free;

    /* startup time */
    mod->private = (void *) time(NULL);

    ns_LAST = sm_register_ns(mod->mm->sm, uri_LAST);
    feature_register(mod->mm->sm, uri_LAST);

    return 0;
}