jabberd2  2.3.4
mod_deliver.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  /* ensure from is set correctly if not already by client */
33  if(pkt->from == NULL || jid_compare_user(pkt->from, sess->jid) != 0) {
34  if(pkt->from != NULL)
35  jid_free(pkt->from);
36 
37  pkt->from = jid_dup(sess->jid);
38  nad_set_attr(pkt->nad, 1, -1, "from", jid_full(pkt->from), 0);
39  }
40 
41  /* no to address means its to us */
42  if(pkt->to == NULL) {
43  /* drop iq-result packets */
44  /* user client is confirming all iq-set, but we usually do not track these
45  * confirmations and we need to drop it here, not loop back to client */
46  if(pkt->type == pkt_IQ_RESULT) {
47  pkt_free(pkt);
48  return mod_HANDLED;
49  }
50 
51  /* iq packets without to should have been already handled by modules */
52  if(pkt->type & pkt_IQ) {
54  }
55 
56  /* supplant user jid as 'to' */
57  pkt->to = jid_dup(sess->jid);
58  nad_set_attr(pkt->nad, 1, -1, "to", jid_full(pkt->to), 0);
59  }
60 
61  /* let it go on the wire */
62  pkt_router(pkt);
63 
64  return mod_HANDLED;
65 }
66 
68 {
69  sess_t sess;
70 
71  /* if there's a resource, send it direct */
72  if(*pkt->to->resource != '\0') {
73  /* find the session */
74  sess = sess_match(user, pkt->to->resource);
75 
76  /* and send it straight there */
77  if(sess != NULL) {
78  pkt_sess(pkt, sess);
79  return mod_HANDLED;
80  }
81 
82  /* no session */
83  if(pkt->type & pkt_PRESENCE) {
84  pkt_free(pkt);
85  return mod_HANDLED;
86 
87  } else if(pkt->type & pkt_IQ)
89 
90  /* unmatched messages will fall through (XMPP-IM r20 s11 rule 2) */
91  }
92 
93  return mod_PASS;
94 }
95 
96 DLLEXPORT int module_init(mod_instance_t mi, const char *arg) {
97  module_t mod = mi->mod;
98 
99  if(mod->init) return 0;
100 
101  mod->in_sess = _deliver_in_sess;
103 
104  feature_register(mod->mm->sm, "message");
105 
106  return 0;
107 }
pkt_type_t type
packet type
Definition: sm.h:138
jid_t jid
session jid (user@host/res)
Definition: sm.h:258
data structures and prototypes for the session manager
const char * jid_full(jid_t jid)
expand and return the full
Definition: jid.c:347
single instance of a module in a chain
Definition: sm.h:446
static mod_ret_t _deliver_pkt_user(mod_instance_t mi, user_t user, pkt_t pkt)
Definition: mod_deliver.c:67
int init
number of times the module intialiser has been called
Definition: sm.h:416
#define stanza_err_FEATURE_NOT_IMPLEMENTED
Definition: util.h:369
info/query (result)
Definition: sm.h:108
char * resource
Definition: jid.h:46
mm_t mm
module manager
Definition: sm.h:404
DLLEXPORT int module_init(mod_instance_t mi, const char *arg)
Definition: mod_deliver.c:96
#define DLLEXPORT
Definition: c2s.h:47
void nad_set_attr(nad_t nad, int elem, int ns, const char *name, const char *val, int vallen)
create, update, or zap any matching attr on this elem
Definition: nad.c:375
sm_t sm
sm context
Definition: sm.h:366
mod_ret_t(* in_sess)(mod_instance_t mi, sess_t sess, pkt_t pkt)
in-sess handler
Definition: sm.h:423
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
packet summary data wrapper
Definition: sm.h:129
nad_t nad
nad of the entire packet
Definition: sm.h:146
void jid_free(jid_t jid)
free a jid
Definition: jid.c:286
mod_ret_t(* pkt_user)(mod_instance_t mi, user_t user, pkt_t pkt)
pkt-user handler
Definition: sm.h:430
void pkt_router(pkt_t pkt)
Definition: pkt.c:379
void pkt_free(pkt_t pkt)
Definition: pkt.c:315
void feature_register(sm_t sm, const char *feature)
register a feature
Definition: feature.c:37
info/query (get)
Definition: sm.h:106
static mod_ret_t _deliver_in_sess(mod_instance_t mi, sess_t sess, pkt_t pkt)
Definition: mod_deliver.c:30
sess_t sess_match(user_t user, const char *resource)
match a session by resource
Definition: sess.c:206
presence
Definition: sm.h:99
packet was unhandled, should be passed to the next module
Definition: sm.h:340
packet was handled (and freed)
Definition: sm.h:339
There is one instance of this struct per user who is logged in to this c2s instance.
Definition: c2s.h:74
int jid_compare_user(jid_t a, jid_t b)
compare the user portion of two jids
Definition: jid.c:355
jid_t to
Definition: sm.h:140
jid_t jid_dup(jid_t jid)
duplicate a jid
Definition: jid.c:373
data for a single module
Definition: sm.h:403
mod_ret_t
module return values
Definition: sm.h:338
#define stanza_err_SERVICE_UNAVAILABLE
Definition: util.h:384
void pkt_sess(pkt_t pkt, sess_t sess)
Definition: pkt.c:459
data for a single user
Definition: sm.h:234