jabberd2  2.6.1
mod_iq_time.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 
30 #ifdef ENABLE_SUPERSEDED
31 static int ns_TIME = 0;
32 #endif
33 static int ns_URN_TIME = 0;
34 
35 #ifdef HAVE_TZNAME
36 extern char *tzname[];
37 #endif
38 
40 {
41  time_t t;
42  struct tm *tm;
43  char buf[64];
44  char *c;
45 
46  /* we only want to play with iq:time gets */
47 #ifdef ENABLE_SUPERSEDED
48  if(pkt->type != pkt_IQ || (pkt->ns != ns_TIME && pkt->ns != ns_URN_TIME))
49 #else
50  if(pkt->type != pkt_IQ || pkt->ns != ns_URN_TIME)
51 #endif
52  return mod_PASS;
53 
54  t = time(NULL);
55  tm = localtime(&t);
56 #ifdef HAVE_TZSET
57  tzset();
58 #endif
59 
60 #ifdef ENABLE_SUPERSEDED
61  if(pkt->ns == ns_TIME) {
62  datetime_out(t, dt_LEGACY, buf, 64);
63  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "utc", buf);
64 
65  strcpy(buf, asctime(tm));
66  c = strchr(buf, '\n');
67  if(c != NULL)
68  *c = '\0';
69  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "display", buf);
70 #if defined(HAVE_STRUCT_TM_TM_ZONE)
71  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tz", (char *) tm->tm_zone);
72 #elif defined(HAVE_TZNAME)
73  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tz", tzname[0]);
74 #endif
75  } else {
76 #endif /* ENABLE_SUPERSEDED */
77 
78  datetime_out(t, dt_DATETIME, buf, 64);
79  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "utc", buf);
80 #ifdef HAVE_TZSET
81  snprintf(buf, 64, "%+03d:%02d", -((int)timezone)/(60*60), -((int)timezone)%(60*60));
82 #else
83  snprintf(buf, 64, "%+03d:%02d", (int) tm->tm_gmtoff/(60*60), (int) tm->tm_gmtoff%(60*60));
84 #endif
85  nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tzo", buf);
86 
87 #ifdef ENABLE_SUPERSEDED
88  }
89 #endif
90  /* tell them */
91  nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
92  pkt_router(pkt_tofrom(pkt));
93 
94  return mod_HANDLED;
95 }
96 
97 static void _iq_time_free(module_t mod) {
100 }
101 
102 DLLEXPORT int module_init(mod_instance_t mi, const char *arg) {
103  module_t mod = mi->mod;
104 
105  if(mod->init) return 0;
106 
107  mod->pkt_sm = _iq_time_pkt_sm;
108  mod->free = _iq_time_free;
109 
110 #ifdef ENABLE_SUPERSEDED
111  ns_TIME = sm_register_ns(mod->mm->sm, uri_TIME);
112  feature_register(mod->mm->sm, uri_TIME);
113 #endif
115  feature_register(mod->mm->sm, urn_TIME);
116 
117  return 0;
118 }
pkt_type_t type
packet type
Definition: sm.h:138
int sm_register_ns(sm_t sm, const char *uri)
register a new global ns
Definition: sm.c:324
data structures and prototypes for the session manager
void sm_unregister_ns(sm_t sm, const char *uri)
unregister a global ns
Definition: sm.c:338
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
void nad_set_attr(nad_t nad, unsigned 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:407
int init
number of times the module intialiser has been called
Definition: sm.h:416
int nad_insert_elem(nad_t nad, unsigned int parent, int ns, const char *name, const char *cdata)
shove in a new child elem after the given one
Definition: nad.c:437
mm_t mm
module manager
Definition: sm.h:404
#define DLLEXPORT
Definition: c2s.h:47
sm_t sm
sm context
Definition: sm.h:366
module_t mod
module that this is an instance of
Definition: sm.h:449
void feature_unregister(sm_t sm, const char *feature)
unregister feature
Definition: feature.c:45
packet summary data wrapper
Definition: sm.h:129
nad_t nad
nad of the entire packet
Definition: sm.h:146
void datetime_out(time_t t, datetime_t type, char *date, int datelen)
Definition: datetime.c:114
#define uri_TIME
Definition: uri.h:67
static mod_ret_t _iq_time_pkt_sm(mod_instance_t mi, pkt_t pkt)
Definition: mod_iq_time.c:39
DLLEXPORT int module_init(mod_instance_t mi, const char *arg)
Definition: mod_iq_time.c:102
void pkt_router(pkt_t pkt)
Definition: pkt.c:379
void feature_register(sm_t sm, const char *feature)
register a feature
Definition: feature.c:37
info/query (get)
Definition: sm.h:106
packet was unhandled, should be passed to the next module
Definition: sm.h:340
int ns
iq sub-namespace
Definition: sm.h:142
packet was handled (and freed)
Definition: sm.h:339
static void _iq_time_free(module_t mod)
Definition: mod_iq_time.c:97
static int ns_URN_TIME
Definition: mod_iq_time.c:33
mod_ret_t(* pkt_sm)(mod_instance_t mi, pkt_t pkt)
pkt-sm handler
Definition: sm.h:429
#define urn_TIME
Definition: uri.h:68
void(* free)(module_t mod)
called when module is freed
Definition: sm.h:442
data for a single module
Definition: sm.h:403
mod_ret_t
module return values
Definition: sm.h:338
#define NAD_ENS(N, E)
Definition: nad.h:196