jabberd2  2.2.16
sm/mod_iq_time.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "sm.h"
00022 
00030 #ifdef ENABLE_SUPERSEDED
00031 static int ns_TIME = 0;
00032 #endif
00033 static int ns_URN_TIME = 0;
00034 
00035 #ifdef HAVE_TZNAME
00036 extern char *tzname[];
00037 #endif
00038 
00039 static mod_ret_t _iq_time_pkt_sm(mod_instance_t mi, pkt_t pkt)
00040 {
00041     time_t t;
00042     struct tm *tm;
00043     char buf[64];
00044     char *c;
00045 
00046     /* we only want to play with iq:time gets */
00047 #ifdef ENABLE_SUPERSEDED
00048     if(pkt->type != pkt_IQ || (pkt->ns != ns_TIME && pkt->ns != ns_URN_TIME))
00049 #else
00050     if(pkt->type != pkt_IQ || pkt->ns != ns_URN_TIME)
00051 #endif
00052         return mod_PASS;
00053 
00054     t = time(NULL);
00055     tm = localtime(&t);
00056 #ifdef HAVE_TZSET
00057     tzset();
00058 #endif
00059 
00060 #ifdef ENABLE_SUPERSEDED
00061     if(pkt->ns == ns_TIME) {
00062         datetime_out(t, dt_LEGACY, buf, 64);
00063         nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "utc", buf);
00064 
00065         strcpy(buf, asctime(tm));
00066         c = strchr(buf, '\n');
00067         if(c != NULL)
00068             *c = '\0';
00069         nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "display", buf);
00070 #if defined(HAVE_STRUCT_TM_TM_ZONE)
00071         nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tz", (char *) tm->tm_zone);
00072 #elif defined(HAVE_TZNAME)
00073         nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tz", tzname[0]);
00074 #endif
00075     } else {
00076 #endif /* ENABLE_SUPERSEDED */
00077 
00078     datetime_out(t, dt_DATETIME, buf, 64);
00079     nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "utc", buf);
00080 #ifdef HAVE_TZSET
00081     snprintf(buf, 64, "%+03d:%02d", -((int)timezone)/(60*60), -((int)timezone)%(60*60));
00082 #else
00083     snprintf(buf, 64, "%+03d:%02d", (int) tm->tm_gmtoff/(60*60), (int) tm->tm_gmtoff%(60*60));
00084 #endif
00085     nad_insert_elem(pkt->nad, 2, NAD_ENS(pkt->nad, 1), "tzo", buf);
00086 
00087 #ifdef ENABLE_SUPERSEDED
00088     }
00089 #endif
00090     /* tell them */
00091     nad_set_attr(pkt->nad, 1, -1, "type", "result", 6);
00092     pkt_router(pkt_tofrom(pkt));
00093 
00094     return mod_HANDLED;
00095 }
00096 
00097 static void _iq_time_free(module_t mod) {
00098      sm_unregister_ns(mod->mm->sm, uri_TIME);
00099      feature_unregister(mod->mm->sm, uri_TIME);
00100 }
00101 
00102 DLLEXPORT int module_init(mod_instance_t mi, char *arg) {
00103     module_t mod = mi->mod;
00104 
00105     if(mod->init) return 0;
00106 
00107     mod->pkt_sm = _iq_time_pkt_sm;
00108     mod->free = _iq_time_free;
00109 
00110 #ifdef ENABLE_SUPERSEDED
00111     ns_TIME = sm_register_ns(mod->mm->sm, uri_TIME);
00112     feature_register(mod->mm->sm, uri_TIME);
00113 #endif
00114     ns_URN_TIME = sm_register_ns(mod->mm->sm, urn_TIME);
00115     feature_register(mod->mm->sm, urn_TIME);
00116 
00117     return 0;
00118 }