jabberd2  2.6.1
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, const 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 }
pkt_type_t type
packet type
Definition: sm.h:138
data structures and prototypes for the session manager
subscribe request
Definition: sm.h:102
const char * jid_user(jid_t jid)
expand and return the user
Definition: jid.c:338
const char * jid_full(jid_t jid)
expand and return the full
Definition: jid.c:346
jid_t jid_new(const char *id, int len)
make a new jid
Definition: jid.c:81
pkt_t pkt_tofrom(pkt_t pkt)
swap a packet's to and from attributes
Definition: pkt.c:57
single instance of a module in a chain
Definition: sm.h:446
int init
number of times the module intialiser has been called
Definition: sm.h:416
char * resource
Definition: jid.h:46
static mod_ret_t _echo_pkt_sm(mod_instance_t mi, pkt_t pkt)
Definition: mod_echo.c:30
mm_t mm
module manager
Definition: sm.h:404
#define DLLEXPORT
Definition: c2s.h:47
sm_t sm
sm context
Definition: sm.h:366
jid_t jid_reset_components(jid_t jid, const char *node, const char *domain, const char *resource)
build a jid from components
Definition: jid.c:281
module_t mod
module that this is an instance of
Definition: sm.h:449
jid_t from
packet addressing (not used for routing)
Definition: sm.h:140
void * private
module private data
Definition: sm.h:418
packet summary data wrapper
Definition: sm.h:129
void jid_free(jid_t jid)
free a jid
Definition: jid.c:286
char * domain
Definition: jid.h:45
Definition: jid.h:42
void pkt_router(pkt_t pkt)
Definition: pkt.c:379
#define log_debug(...)
Definition: log.h:65
packet was unhandled, should be passed to the next module
Definition: sm.h:340
packet was handled (and freed)
Definition: sm.h:339
mod_ret_t(* pkt_sm)(mod_instance_t mi, pkt_t pkt)
pkt-sm handler
Definition: sm.h:429
message
Definition: sm.h:95
jid_t to
Definition: sm.h:140
#define ZONE
Definition: mio_impl.h:76
DLLEXPORT int module_init(mod_instance_t mi, const char *arg)
Definition: mod_echo.c:58
data for a single module
Definition: sm.h:403
presence (probe)
Definition: sm.h:101
pkt_t pkt_create(sm_t sm, const char *elem, const char *type, const char *to, const char *from)
Definition: pkt.c:328
mod_ret_t
module return values
Definition: sm.h:338
char * node
Definition: jid.h:44