tmrec_mod.c

00001 
00025 #include <stdio.h>
00026 #include <unistd.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 
00030 #include "../../sr_module.h"
00031 #include "../../dprint.h"
00032 #include "../../ut.h"
00033 #include "../../pvar.h"
00034 #include "../../mod_fix.h"
00035 #include "../../lib/srutils/tmrec.h"
00036 #include "period.h"
00037 
00038 
00039 MODULE_VERSION
00040 
00041 static int  mod_init(void);
00042 static int  child_init(int);
00043 static void mod_destroy(void);
00044 
00045 static int w_tmrec_match(struct sip_msg* msg, char* rec, char* t);
00046 static int fixup_tmrec_match(void** param, int param_no);
00047 static int w_is_leap_year(struct sip_msg* msg, char* t, char* p2);
00048 static int fixup_is_leap_year(void** param, int param_no);
00049 static int fixup_time_period_match(void** param, int param_no);
00050 static int w_time_period_match(struct sip_msg* msg, char* period, char* t);
00051 
00052 int tmrec_wday = 0;
00053 char tmrec_separator = '|';
00054 char *tmrec_separator_param = NULL;
00055 
00056 static cmd_export_t cmds[]={
00057         {"tmrec_match", (cmd_function)w_tmrec_match, 1, fixup_tmrec_match,
00058                 0, ANY_ROUTE},
00059         {"tmrec_match", (cmd_function)w_tmrec_match, 2, fixup_tmrec_match,
00060                 0, ANY_ROUTE},
00061         {"is_leap_year", (cmd_function)w_is_leap_year, 0, fixup_is_leap_year,
00062                 0, ANY_ROUTE},
00063         {"is_leap_year", (cmd_function)w_is_leap_year, 1, fixup_is_leap_year,
00064                 0, ANY_ROUTE},
00065         {"time_period_match", (cmd_function)w_time_period_match, 1, fixup_time_period_match,
00066                 0, ANY_ROUTE},
00067         {"time_period_match", (cmd_function)w_time_period_match, 2, fixup_time_period_match,
00068                 0, ANY_ROUTE},
00069         {0, 0, 0, 0, 0, 0}
00070 };
00071 
00072 static param_export_t params[]={
00073         {"wday",                INT_PARAM,   &tmrec_wday},
00074         {"separator",   STR_PARAM,   &tmrec_separator_param},
00075         {0, 0, 0}
00076 };
00077 
00078 struct module_exports exports = {
00079         "tmrec",
00080         DEFAULT_DLFLAGS, /* dlopen flags */
00081         cmds,
00082         params,
00083         0,
00084         0,              /* exported MI functions */
00085         0,              /* exported pseudo-variables */
00086         0,              /* extra processes */
00087         mod_init,       /* module initialization function */
00088         0,              /* response function */
00089         mod_destroy,    /* destroy function */
00090         child_init      /* per child init function */
00091 };
00092 
00093 
00094 
00098 static int mod_init(void)
00099 {
00100         if(tmrec_separator_param!=NULL)
00101                 tmrec_separator = tmrec_separator_param[0];
00102         return 0;
00103 }
00104 
00108 static int child_init(int rank)
00109 {
00110         if (rank!=PROC_MAIN)
00111                 return 0;
00112 
00113         return 0;
00114 }
00118 static void mod_destroy(void)
00119 {
00120         return;
00121 }
00122 
00123 static int w_is_leap_year(struct sip_msg* msg, char* t, char* str2)
00124 {
00125         time_t tv;
00126         struct tm *tb;
00127         int y;
00128         
00129         if(msg==NULL)
00130                 return -1;
00131 
00132         if(t!=NULL)
00133         {
00134                 if(fixup_get_ivalue(msg, (gparam_t*)t, &y)!=0)
00135                 {
00136                         LM_ERR("invalid time parameter value\n");
00137                         return -1;
00138                 }
00139         } else {
00140                 tv = time(NULL);
00141                 tb = localtime(&tv);
00142                 y = 1900 + tb->tm_year;
00143         }
00144 
00145         if(tr_is_leap_year(y))
00146                 return 1;
00147         return -1;
00148 }
00149 
00150 static int fixup_is_leap_year(void** param, int param_no)
00151 {
00152         if(param_no==1)
00153                 return fixup_igp_null(param, param_no);
00154 
00155         return 0;
00156 }
00157 
00158 static int w_tmrec_match(struct sip_msg* msg, char* rec, char* t)
00159 {
00160         str rv;
00161         time_t tv;
00162         int ti;
00163         ac_tm_t act;
00164         tmrec_t tmr;
00165 
00166         if(msg==NULL)
00167                 return -1;
00168 
00169         if(fixup_get_svalue(msg, (gparam_t*)rec, &rv)!=0)
00170         {
00171                 LM_ERR("invalid time recurrence parameter value\n");
00172                 return -1;
00173         }
00174 
00175         if(t!=NULL)
00176         {
00177                 if(fixup_get_ivalue(msg, (gparam_t*)t, &ti)!=0)
00178                 {
00179                         LM_ERR("invalid time stamp parameter value\n");
00180                         return -1;
00181                 }
00182                 tv = (time_t)ti;
00183         } else {
00184                 tv = time(NULL);
00185         }
00186 
00187         memset(&act, 0, sizeof(act));
00188         memset(&tmr, 0, sizeof(tmr));
00189 
00190         /* parse time recurrence definition */
00191         if(tr_parse_recurrence_string(&tmr, rv.s, tmrec_separator)<0)
00192                 return -1;
00193 
00194         /* if there is no dstart, timerec is valid */
00195         if (tmr.dtstart==0)
00196                 goto done;
00197 
00198         /* set current time */
00199         if (ac_tm_set_time(&act, tv)<0)
00200                 goto error;
00201 
00202         /* match the specified recurence */
00203         if (tr_check_recurrence(&tmr, &act, 0)!=0)
00204                 goto error;
00205 
00206 done:
00207         tmrec_destroy(&tmr);
00208         ac_tm_destroy(&act);
00209         return 1;
00210 
00211 error:
00212         tmrec_destroy(&tmr);
00213         ac_tm_destroy(&act);
00214         return -1;
00215 }
00216 
00217 static int fixup_tmrec_match(void** param, int param_no)
00218 {
00219         if(param_no==1)
00220         {
00221                 if(fixup_spve_null(param, 1)<0)
00222                         return -1;
00223                 return 0;
00224         } else if(param_no==2) {
00225                 if(fixup_igp_null(param, 1)<0)
00226                         return -1;
00227         }
00228         return 0;
00229 }
00230 
00231 static int fixup_time_period_match(void** param, int param_no)
00232 {
00233         if(param_no==1)
00234         {
00235                 if(fixup_spve_null(param, 1)<0)
00236                         return -1;
00237                 return 0;
00238         } else if(param_no==2) {
00239                 if(fixup_igp_null(param, 1)<0)
00240                         return -1;
00241         }
00242         return 0;
00243 }
00244 
00245 static int w_time_period_match(struct sip_msg* msg, char* period, char* t)
00246 {
00247         str rv;
00248         time_t tv;
00249         int ti;
00250 
00251         if(msg==NULL)
00252                 return -2;
00253 
00254         if(fixup_get_svalue(msg, (gparam_t*)period, &rv)!=0)
00255         {
00256                 LM_ERR("invalid period parameter value\n");
00257                 return -3;
00258         }
00259 
00260         if(t!=NULL)
00261         {
00262                 if(fixup_get_ivalue(msg, (gparam_t*)t, &ti)!=0)
00263                 {
00264                         LM_ERR("invalid time stamp parameter value\n");
00265                         return -4;
00266                 }
00267                 tv = (time_t)ti;
00268         } else {
00269                 tv = time(NULL);
00270         }
00271 
00272         if (in_period(tv, rv.s))
00273                 return 1;
00274         return -1;
00275 }