jabberd2  2.2.17
mod_echo.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "sm.h"
22 
31 {
32  jid_t jid;
33  char *resource = (char *) mi->mod->private;
34 
35  /* answer to probes and subscription requests */
36  if(pkt->type == pkt_PRESENCE_PROBE || pkt->type == pkt_S10N) {
37  log_debug(ZONE, "answering presence probe/sub from %s with /echo resource", jid_full(pkt->from));
38 
39  /* send presence */
40  jid = jid_new(jid_user(pkt->to), -1);
41  jid_reset_components(jid, jid->node, jid->domain, resource);
42  pkt_router(pkt_create(mi->mod->mm->sm, "presence", NULL, jid_user(pkt->from), jid_full(jid)));
43  jid_free(jid);
44  }
45 
46  /* we want messages addressed to /echo */
47  if(!(pkt->type & pkt_MESSAGE) || strcmp(pkt->to->resource, "echo") != 0)
48  return mod_PASS;
49 
50  log_debug(ZONE, "echo request from %s", jid_full(pkt->from));
51 
52  /* swap to and from and return it */
53  pkt_router(pkt_tofrom(pkt));
54 
55  return mod_HANDLED;
56 }
57 
58 DLLEXPORT int module_init(mod_instance_t mi, char *arg) {
59  module_t mod = mi->mod;
60 
61  if(mod->init) return 0;
62 
63  /* store /echo resource for use when answering probes */
64  mod->private = "echo";
65 
66  mod->pkt_sm = _echo_pkt_sm;
67  /* data is static so nothing to free */
68  /* mod->free = _echo_free; */
69 
70  return 0;
71 }