jabberd2  2.3.6
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  || (!(pkt->type & pkt_PRESENCE) && jid_compare_full(pkt->from, sess->jid) != 0)) {
35  if(pkt->from != NULL)
36  jid_free(pkt->from);
37 
38  pkt->from = jid_dup(sess->jid);
39  nad_set_attr(pkt->nad, 1, -1, "from", jid_full(pkt->from), 0);
40  }
41 
42  /* no to address means its to us */
43  if(pkt->to == NULL) {
44  /* drop iq-result packets */
45  /* user client is confirming all iq-set, but we usually do not track these
46  * confirmations and we need to drop it here, not loop back to client */
47  if(pkt->type == pkt_IQ_RESULT) {
48  pkt_free(pkt);
49  return mod_HANDLED;
50  }
51 
52  /* iq packets without to should have been already handled by modules */
53  if(pkt->type & pkt_IQ) {
55  }
56 
57  /* supplant user jid as 'to' */
58  pkt->to = jid_dup(sess->jid);
59  nad_set_attr(pkt->nad, 1, -1, "to", jid_full(pkt->to), 0);
60  }
61 
62  /* let it go on the wire */
63  pkt_router(pkt);
64 
65  return mod_HANDLED;
66 }
67 
69 {
70  sess_t sess;
71 
72  /* if there's a resource, send it direct */
73  if(*pkt->to->resource != '\0') {
74  /* find the session */
75  sess = sess_match(user, pkt->to->resource);
76 
77  /* and send it straight there */
78  if(sess != NULL) {
79  pkt_sess(pkt, sess);
80  return mod_HANDLED;
81  }
82 
83  /* no session */
84  if(pkt->type & pkt_PRESENCE) {
85  pkt_free(pkt);
86  return mod_HANDLED;
87 
88  } else if(pkt->type & pkt_IQ)
90 
91  /* unmatched messages will fall through (XMPP-IM r20 s11 rule 2) */
92  }
93 
94  return mod_PASS;
95 }
96 
97 DLLEXPORT int module_init(mod_instance_t mi, const char *arg) {
98  module_t mod = mi->mod;
99 
100  if(mod->init) return 0;
101 
102  mod->in_sess = _deliver_in_sess;
104 
105  feature_register(mod->mm->sm, "message");
106 
107  return 0;
108 }
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:68
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:97
#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:403
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
int jid_compare_full(jid_t a, jid_t b)
compare two full jids
Definition: jid.c:364
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