TT#41650 Development of Kamailio module/function for parsing iCal structure

- parsing ical from buffer and converting to tmrec using libical implementation.

Change-Id: Ifd823a576ed4eb1d95aa3c72f34226b6100c4c21
changes/01/22701/49
Roman Romanchenko 7 years ago
parent be206b457a
commit 2e1778866e

1
debian/control vendored

@ -19,6 +19,7 @@ Build-Depends:
libexpat1-dev,
libmaxminddb-dev,
libhiredis-dev (>= 0.10.0),
libical-dev,
libjansson-dev,
libjson-c-dev,
libldap2-dev,

@ -44,3 +44,4 @@ sipwise/tt37692.patch
sipwise/dtmf-blocking.patch
sipwise/media-blocking.patch
upstream/core_cfg_reset_routename_properly.patch
sipwise/tt41650.patch

@ -0,0 +1,184 @@
--- /dev/null
+++ b/src/modules/tmrec/ical.c
@@ -0,0 +1,45 @@
+#include "ical.h"
+#include <libical/ical.h>
+#include <libical/icalss.h>
+#include <stdlib.h>
+
+#define PERIOD 24*3600*7 // one week
+
+void check_time_frame(icalcomponent *comp, struct icaltime_span *span, void *data)
+{
+ callback_data_t *d = (callback_data_t *)data;
+ if (span->start <= d->time && d->time <= span->end)
+ {
+ *(d->rc) = 1;
+ }
+}
+
+int match_ical_event(icalcomponent *c, time_t time)
+{
+ int rc = -1;
+
+ for(icalcomponent *event = icalcomponent_get_first_component(c, ICAL_VEVENT_COMPONENT); event != 0; event = icalcomponent_get_next_component(c, ICAL_VEVENT_COMPONENT))
+ {
+ struct icaltimetype start;
+ struct icaltimetype end;
+
+ icaltimezone *utc = icaltimezone_get_utc_timezone();
+ start = icaltime_from_timet_with_zone(time, 0, utc);
+ end = icaltime_from_timet_with_zone(time + PERIOD, 0, utc);
+ icaltime_normalize(end);
+
+ callback_data_t data = {&rc, time};
+ icalcomponent_foreach_recurrence(event, start, end, check_time_frame, &data);
+ }
+
+ icalcomponent_free(c);
+
+ return rc;
+}
+
+icalcomponent *create_from_buf(const char *buf)
+{
+ icalcomponent *c = icalparser_parse_string(buf);
+ return c;
+}
+
--- /dev/null
+++ b/src/modules/tmrec/ical.h
@@ -0,0 +1,17 @@
+#ifndef _ICAL_H_
+#define _ICAL_H_
+
+#include <libical/ical.h>
+#include <time.h>
+
+void check_time_frame(icalcomponent *comp, struct icaltime_span *span, void *data);
+int match_ical_event(icalcomponent *c, time_t t);
+icalcomponent *create_from_buf(const char *buf);
+
+typedef struct callback_data
+{
+ int *rc;
+ time_t time;
+} callback_data_t;
+
+#endif
--- a/src/modules/tmrec/tmrec_mod.c
+++ b/src/modules/tmrec/tmrec_mod.c
@@ -41,6 +41,7 @@
#include "../../core/kemi.h"
#include "../../lib/srutils/tmrec.h"
#include "period.h"
+#include "ical.h"
MODULE_VERSION
@@ -50,7 +51,9 @@ static int child_init(int);
static void mod_destroy(void);
static int w_tmrec_match(struct sip_msg* msg, char* rec, char* t);
+static int w_tmrec_match_ical(struct sip_msg* msg, char* rec, char* t);
static int fixup_tmrec_match(void** param, int param_no);
+static int fixup_ical(void** param, int param_no);
static int w_is_leap_year(struct sip_msg* msg, char* t, char* p2);
static int fixup_is_leap_year(void** param, int param_no);
static int fixup_time_period_match(void** param, int param_no);
@@ -65,6 +68,10 @@ static cmd_export_t cmds[]={
0, ANY_ROUTE},
{"tmrec_match", (cmd_function)w_tmrec_match, 2, fixup_tmrec_match,
0, ANY_ROUTE},
+ {"tmrec_match_ical", (cmd_function)w_tmrec_match_ical, 1, fixup_ical,
+ 0, ANY_ROUTE},
+ {"tmrec_match_ical", (cmd_function)w_tmrec_match_ical, 2, fixup_ical,
+ 0, ANY_ROUTE},
{"is_leap_year", (cmd_function)w_is_leap_year, 0, fixup_is_leap_year,
0, ANY_ROUTE},
{"is_leap_year", (cmd_function)w_is_leap_year, 1, fixup_is_leap_year,
@@ -256,6 +263,45 @@ static int w_tmrec_match(struct sip_msg*
return ki_tmrec_match_timestamp(msg, &rv, ti);
}
+static int w_tmrec_match_ical(struct sip_msg* msg, char* ical_rec, char* t)
+{
+ str rv;
+ time_t tv;
+ int ti;
+
+ if(msg==NULL)
+ return -2;
+
+ if(fixup_get_svalue(msg, (gparam_t*)ical_rec, &rv)!=0)
+ {
+ LM_ERR("w_tmrec_match_ical: invalid input");
+ return -1;
+ }
+
+ if(t!=NULL)
+ {
+ if(fixup_get_ivalue(msg, (gparam_t*)t, &ti)!=0)
+ {
+ LM_ERR("invalid time stamp parameter value\n");
+ return -4;
+ }
+ tv = (time_t)ti;
+ } else {
+ tv = time(NULL);
+ }
+
+ char time_string[20];
+ strftime(time_string, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv));
+
+ icalcomponent *event = create_from_buf(rv.s);
+ LM_DBG("w_tmrec_match_ical: checking ical: %s, %s\n", time_string, rv.s);
+
+ int rc = match_ical_event(event, tv);
+
+ LM_DBG("match_ical_event return %d\n", rc);
+ return rc;
+}
+
static int fixup_tmrec_match(void** param, int param_no)
{
if(param_no==1)
@@ -270,6 +316,20 @@ static int fixup_tmrec_match(void** para
return 0;
}
+static int fixup_ical(void** param, int param_no)
+{
+ if(param_no==1)
+ {
+ if(fixup_spve_null(param, 1)<0)
+ return -1;
+ return 0;
+ } else if(param_no==2) {
+ if(fixup_igp_null(param, 1)<0)
+ return -1;
+ }
+ return 0;
+}
+
static int fixup_time_period_match(void** param, int param_no)
{
if(param_no==1)
@@ -379,4 +439,4 @@ int mod_register(char *path, int *dlflag
{
sr_kemi_modules_add(sr_kemi_tmrec_exports);
return 0;
-}
\ No newline at end of file
+}
--- a/src/modules/tmrec/Makefile
+++ b/src/modules/tmrec/Makefile
@@ -6,7 +6,7 @@ include ../../Makefile.defs
auto_gen=
NAME=tmrec.so
DEFS +=
-LIBS +=
+LIBS += -L$(LOCALBASE)/lib -lical -licalss
DEFS+=-DKAMAILIO_MOD_INTERFACE
Loading…
Cancel
Save