Browse code

textopsx: exported msg_set_buffer(data) function

- it was available to KEMI

Daniel-Constantin Mierla authored on 24/09/2021 10:16:37
Showing 1 changed files
... ...
@@ -46,6 +46,7 @@
46 46
 MODULE_VERSION
47 47
 
48 48
 static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2);
49
+static int msg_set_buffer_f(sip_msg_t *msg, char *p1data, char *p2);
49 50
 
50 51
 static int change_reply_status_f(sip_msg_t *, char *, char *);
51 52
 static int change_reply_status_fixup(void **param, int param_no);
... ...
@@ -121,6 +122,9 @@ static pv_export_t mod_pvs[] = {
121 122
 static cmd_export_t cmds[] = {
122 123
 	{"msg_apply_changes", (cmd_function)msg_apply_changes_f, 0, 0, 0,
123 124
 			REQUEST_ROUTE | ONREPLY_ROUTE},
125
+	{"msg_set_buffer", (cmd_function)msg_set_buffer_f, 1,
126
+			fixup_spve_null, fixup_free_spve_null,
127
+			REQUEST_ROUTE | ONREPLY_ROUTE},
124 128
 	{"change_reply_status", change_reply_status_f, 2,
125 129
 			change_reply_status_fixup, 0, ONREPLY_ROUTE},
126 130
 	{"change_reply_status_code", change_reply_status_code_f, 1,
... ...
@@ -242,6 +246,21 @@ static int ki_msg_set_buffer(sip_msg_t *msg, str *obuf)
242 246
 	return ki_msg_update_buffer(msg, obuf);
243 247
 }
244 248
 
249
+/**
250
+ *
251
+ */
252
+static int msg_set_buffer_f(sip_msg_t *msg, char *p1data, char *p2)
253
+{
254
+	str data = STR_NULL;
255
+
256
+	if(fixup_get_svalue(msg, (gparam_t*)p1data, &data) < 0) {
257
+		LM_ERR("could not get string param value\n");
258
+		return -1;
259
+	}
260
+
261
+	return ki_msg_set_buffer(msg, &data);
262
+}
263
+
245 264
 /**
246 265
  *
247 266
  */
Browse code

textopsx: added kemi functions to return hf iterator name and body

Daniel-Constantin Mierla authored on 18/08/2021 05:54:05
Showing 1 changed files
... ...
@@ -2277,6 +2277,47 @@ static int w_hf_iterator_insert(sip_msg_t *msg, char *piname, char *phtext)
2277 2277
 	return ki_hf_iterator_insert(msg, &iname, &htext);
2278 2278
 }
2279 2279
 
2280
+/**
2281
+ *
2282
+ */
2283
+static sr_kemi_xval_t _sr_kemi_hf_iterator_xval = {0};
2284
+
2285
+/**
2286
+ *
2287
+ */
2288
+static sr_kemi_xval_t* ki_hf_iterator_hname(sip_msg_t *msg, str *iname)
2289
+{
2290
+	int k;
2291
+
2292
+	memset(&_sr_kemi_hf_iterator_xval, 0, sizeof(sr_kemi_xval_t));
2293
+	k = ki_hf_iterator_index(msg, iname);
2294
+	if(k<0 || _hf_iterators[k].it==NULL) {
2295
+		sr_kemi_xval_null(&_sr_kemi_hf_iterator_xval, 0);
2296
+		return &_sr_kemi_hf_iterator_xval;
2297
+	}
2298
+	_sr_kemi_hf_iterator_xval.vtype = SR_KEMIP_STR;
2299
+	_sr_kemi_hf_iterator_xval.v.s = _hf_iterators[k].it->name;
2300
+	return &_sr_kemi_hf_iterator_xval;
2301
+}
2302
+
2303
+/**
2304
+ *
2305
+ */
2306
+static sr_kemi_xval_t* ki_hf_iterator_hbody(sip_msg_t *msg, str *iname)
2307
+{
2308
+	int k;
2309
+
2310
+	memset(&_sr_kemi_hf_iterator_xval, 0, sizeof(sr_kemi_xval_t));
2311
+	k = ki_hf_iterator_index(msg, iname);
2312
+	if(k<0 || _hf_iterators[k].it==NULL) {
2313
+		sr_kemi_xval_null(&_sr_kemi_hf_iterator_xval, 0);
2314
+		return &_sr_kemi_hf_iterator_xval;
2315
+	}
2316
+	_sr_kemi_hf_iterator_xval.vtype = SR_KEMIP_STR;
2317
+	_sr_kemi_hf_iterator_xval.v.s = _hf_iterators[k].it->body;
2318
+	return &_sr_kemi_hf_iterator_xval;
2319
+}
2320
+
2280 2321
 /**
2281 2322
  *
2282 2323
  */
... ...
@@ -3347,6 +3388,16 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
3347 3388
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3348 3389
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3349 3390
 	},
3391
+	{ str_init("textopsx"), str_init("hf_iterator_hname"),
3392
+		SR_KEMIP_XVAL, ki_hf_iterator_hname,
3393
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3394
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3395
+	},
3396
+	{ str_init("textopsx"), str_init("hf_iterator_hbody"),
3397
+		SR_KEMIP_XVAL, ki_hf_iterator_hbody,
3398
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3399
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3400
+	},
3350 3401
 	{ str_init("textopsx"), str_init("bl_iterator_start"),
3351 3402
 		SR_KEMIP_INT, ki_bl_iterator_start,
3352 3403
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

textopsx: kemi functions to get body line iterator value

Daniel-Constantin Mierla authored on 09/08/2021 18:54:32
Showing 1 changed files
... ...
@@ -2718,6 +2718,32 @@ static int w_bl_iterator_insert(sip_msg_t *msg, char *piname, char *ptext)
2718 2718
 	return ki_bl_iterator_insert(msg, &iname, &text);
2719 2719
 }
2720 2720
 
2721
+/**
2722
+ *
2723
+ */
2724
+static sr_kemi_xval_t _sr_kemi_bl_iterator_xval = {0};
2725
+
2726
+/**
2727
+ *
2728
+ */
2729
+static sr_kemi_xval_t* ki_bl_iterator_value(sip_msg_t *msg, str *iname)
2730
+{
2731
+	int k;
2732
+
2733
+	memset(&_sr_kemi_bl_iterator_xval, 0, sizeof(sr_kemi_xval_t));
2734
+	k = ki_bl_iterator_index(msg, iname);
2735
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2736
+		sr_kemi_xval_null(&_sr_kemi_bl_iterator_xval, 0);
2737
+		return &_sr_kemi_bl_iterator_xval;
2738
+	}
2739
+	if(_bl_iterators[k].it.s==NULL) {
2740
+		sr_kemi_xval_null(&_sr_kemi_bl_iterator_xval, 0);
2741
+		return &_sr_kemi_bl_iterator_xval;
2742
+	}
2743
+	_sr_kemi_bl_iterator_xval.vtype = SR_KEMIP_STR;
2744
+	_sr_kemi_bl_iterator_xval.v.s = _bl_iterators[k].it;
2745
+	return &_sr_kemi_bl_iterator_xval;
2746
+}
2721 2747
 
2722 2748
 /**
2723 2749
  *
... ...
@@ -3351,6 +3377,11 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
3351 3377
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3352 3378
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3353 3379
 	},
3380
+	{ str_init("textopsx"), str_init("bl_iterator_value"),
3381
+		SR_KEMIP_XVAL, ki_bl_iterator_value,
3382
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3383
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3384
+	},
3354 3385
 
3355 3386
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
3356 3387
 };
Browse code

textopsx: bl iterator functions to append/insert text

Daniel-Constantin Mierla authored on 04/08/2021 07:07:36
Showing 1 changed files
... ...
@@ -85,6 +85,8 @@ static int w_bl_iterator_start(sip_msg_t *msg, char *piname, char *p2);
85 85
 static int w_bl_iterator_next(sip_msg_t *msg, char *piname, char *p2);
86 86
 static int w_bl_iterator_end(sip_msg_t *msg, char *piname, char *p2);
87 87
 static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
88
+static int w_bl_iterator_append(sip_msg_t *msg, char *piname, char *ptext);
89
+static int w_bl_iterator_insert(sip_msg_t *msg, char *piname, char *ptext);
88 90
 
89 91
 static int bind_textopsx(textopsx_api_t *tob);
90 92
 
... ...
@@ -168,6 +170,10 @@ static cmd_export_t cmds[] = {
168 170
 			fixup_free_spve_null, ANY_ROUTE},
169 171
 	{"bl_iterator_rm", w_bl_iterator_rm, 1, fixup_spve_null,
170 172
 			fixup_free_spve_null, ANY_ROUTE},
173
+	{"bl_iterator_append", w_bl_iterator_append, 2, fixup_spve_spve,
174
+			fixup_free_spve_spve, ANY_ROUTE},
175
+	{"bl_iterator_insert", w_bl_iterator_insert, 2, fixup_spve_spve,
176
+			fixup_free_spve_spve, ANY_ROUTE},
171 177
 
172 178
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
173 179
 
... ...
@@ -2599,6 +2605,120 @@ static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2)
2599 2605
 	return ki_bl_iterator_rm(msg, &iname);
2600 2606
 }
2601 2607
 
2608
+/**
2609
+ *
2610
+ */
2611
+static int ki_bl_iterator_append(sip_msg_t *msg, str *iname, str *text)
2612
+{
2613
+	int k;
2614
+	sr_lump_t *anchor;
2615
+	str sval = STR_NULL;
2616
+
2617
+	k = ki_bl_iterator_index(msg, iname);
2618
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2619
+		return -1;
2620
+	}
2621
+	anchor = anchor_lump(msg, _bl_iterators[k].it.s
2622
+			+ _bl_iterators[k].it.len - msg->buf, 0, 0);
2623
+	if (anchor==0) {
2624
+		LM_ERR("cannot append text after %.*s\n", _bl_iterators[k].it.len,
2625
+				_bl_iterators[k].it.s);
2626
+		return -1;
2627
+	}
2628
+	sval.s = (char*)pkg_malloc(text->len + 1);
2629
+	if(sval.s==NULL) {
2630
+		LM_ERR("failed append text after %.*s\n", _bl_iterators[k].it.len,
2631
+				_bl_iterators[k].it.s);
2632
+		return -1;
2633
+	}
2634
+	memcpy(sval.s, text->s, text->len);
2635
+	sval.len = text->len;
2636
+	sval.s[sval.len] = '\0';
2637
+
2638
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2639
+		LM_ERR("cannot insert lump\n");
2640
+		pkg_free(sval.s);
2641
+		return -1;
2642
+	}
2643
+	return 1;
2644
+}
2645
+
2646
+/**
2647
+ *
2648
+ */
2649
+static int w_bl_iterator_append(sip_msg_t *msg, char *piname, char *ptext)
2650
+{
2651
+	str iname = STR_NULL;
2652
+	str text = STR_NULL;
2653
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2654
+		LM_ERR("failed to get iterator name\n");
2655
+		return -1;
2656
+	}
2657
+	if(fixup_get_svalue(msg, (gparam_t*)ptext, &text)<0) {
2658
+		LM_ERR("failed to get text\n");
2659
+		return -1;
2660
+	}
2661
+
2662
+	return ki_bl_iterator_append(msg, &iname, &text);
2663
+}
2664
+
2665
+/**
2666
+ *
2667
+ */
2668
+static int ki_bl_iterator_insert(sip_msg_t *msg, str *iname, str *text)
2669
+{
2670
+	int k;
2671
+	sr_lump_t *anchor;
2672
+	str sval = STR_NULL;
2673
+
2674
+	k = ki_bl_iterator_index(msg, iname);
2675
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2676
+		return -1;
2677
+	}
2678
+	anchor = anchor_lump(msg, _bl_iterators[k].it.s - msg->buf, 0, 0);
2679
+	if (anchor==0) {
2680
+		LM_ERR("cannot insert text after %.*s\n", _bl_iterators[k].it.len,
2681
+				_bl_iterators[k].it.s);
2682
+		return -1;
2683
+	}
2684
+	sval.s = (char*)pkg_malloc(text->len + 1);
2685
+	if(sval.s==NULL) {
2686
+		LM_ERR("failed to insert text after %.*s\n", _bl_iterators[k].it.len,
2687
+				_bl_iterators[k].it.s);
2688
+		return -1;
2689
+	}
2690
+	memcpy(sval.s, text->s, text->len);
2691
+	sval.len = text->len;
2692
+	sval.s[sval.len] = '\0';
2693
+
2694
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2695
+		LM_ERR("cannot insert lump\n");
2696
+		pkg_free(sval.s);
2697
+		return -1;
2698
+	}
2699
+	return 1;
2700
+}
2701
+
2702
+/**
2703
+ *
2704
+ */
2705
+static int w_bl_iterator_insert(sip_msg_t *msg, char *piname, char *ptext)
2706
+{
2707
+	str iname = STR_NULL;
2708
+	str text = STR_NULL;
2709
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2710
+		LM_ERR("failed to get iterator name\n");
2711
+		return -1;
2712
+	}
2713
+	if(fixup_get_svalue(msg, (gparam_t*)ptext, &text)<0) {
2714
+		LM_ERR("failed to get text\n");
2715
+		return -1;
2716
+	}
2717
+
2718
+	return ki_bl_iterator_insert(msg, &iname, &text);
2719
+}
2720
+
2721
+
2602 2722
 /**
2603 2723
  *
2604 2724
  */
... ...
@@ -3221,6 +3341,16 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
3221 3341
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3222 3342
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3223 3343
 	},
3344
+	{ str_init("textopsx"), str_init("bl_iterator_append"),
3345
+		SR_KEMIP_INT, ki_bl_iterator_append,
3346
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3347
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3348
+	},
3349
+	{ str_init("textopsx"), str_init("bl_iterator_insert"),
3350
+		SR_KEMIP_INT, ki_bl_iterator_insert,
3351
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
3352
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3353
+	},
3224 3354
 
3225 3355
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
3226 3356
 };
Browse code

textopsx: added body line iterator functions

- iterate through lines of message body, get the value via
$blitval(iname)

Daniel-Constantin Mierla authored on 30/07/2021 10:24:02
Showing 1 changed files
... ...
@@ -81,24 +81,36 @@ static int w_hf_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
81 81
 static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext);
82 82
 static int w_hf_iterator_insert(sip_msg_t *msg, char *piname, char *phtext);
83 83
 
84
+static int w_bl_iterator_start(sip_msg_t *msg, char *piname, char *p2);
85
+static int w_bl_iterator_next(sip_msg_t *msg, char *piname, char *p2);
86
+static int w_bl_iterator_end(sip_msg_t *msg, char *piname, char *p2);
87
+static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
88
+
84 89
 static int bind_textopsx(textopsx_api_t *tob);
85 90
 
86 91
 static int mod_init(void);
87 92
 
88 93
 extern select_row_t sel_declaration[];
89 94
 
90
-void hf_iterator_init(void);
95
+static void hf_iterator_init(void);
91 96
 static int pv_parse_hf_iterator_name(pv_spec_t *sp, str *in);
92 97
 static int pv_get_hf_iterator_hname(sip_msg_t *msg, pv_param_t *param,
93 98
 		pv_value_t *res);
94 99
 static int pv_get_hf_iterator_hbody(sip_msg_t *msg, pv_param_t *param,
95 100
 		pv_value_t *res);
96 101
 
102
+static void bl_iterator_init(void);
103
+static int pv_parse_bl_iterator_name(pv_spec_t *sp, str *in);
104
+static int pv_get_bl_iterator_value(sip_msg_t *msg, pv_param_t *param,
105
+		pv_value_t *res);
106
+
97 107
 static pv_export_t mod_pvs[] = {
98 108
 	{ {"hfitname", sizeof("hfitname")-1}, PVT_OTHER, pv_get_hf_iterator_hname, 0,
99 109
 		pv_parse_hf_iterator_name, 0, 0, 0 },
100 110
 	{ {"hfitbody", sizeof("hfitbody")-1}, PVT_OTHER, pv_get_hf_iterator_hbody, 0,
101 111
 		pv_parse_hf_iterator_name, 0, 0, 0 },
112
+	{ {"blitval", sizeof("blitval")-1}, PVT_OTHER, pv_get_bl_iterator_value, 0,
113
+		pv_parse_bl_iterator_name, 0, 0, 0 },
102 114
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
103 115
 };
104 116
 
... ...
@@ -148,6 +160,14 @@ static cmd_export_t cmds[] = {
148 160
 			fixup_free_spve_spve, ANY_ROUTE},
149 161
 	{"hf_iterator_insert", w_hf_iterator_insert, 2, fixup_spve_spve,
150 162
 			fixup_free_spve_spve, ANY_ROUTE},
163
+	{"bl_iterator_start", w_bl_iterator_start, 1, fixup_spve_null,
164
+			fixup_free_spve_null, ANY_ROUTE},
165
+	{"bl_iterator_next", w_bl_iterator_next, 1, fixup_spve_null,
166
+			fixup_free_spve_null, ANY_ROUTE},
167
+	{"bl_iterator_end", w_bl_iterator_end, 1, fixup_spve_null,
168
+			fixup_free_spve_null, ANY_ROUTE},
169
+	{"bl_iterator_rm", w_bl_iterator_rm, 1, fixup_spve_null,
170
+			fixup_free_spve_null, ANY_ROUTE},
151 171
 
152 172
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
153 173
 
... ...
@@ -180,6 +200,7 @@ static int mod_init(void)
180 200
 #endif
181 201
 	register_select_table(sel_declaration);
182 202
 	hf_iterator_init();
203
+	bl_iterator_init();
183 204
 
184 205
 	return 0;
185 206
 }
... ...
@@ -1848,7 +1869,7 @@ static hf_iterator_t _hf_iterators[HF_ITERATOR_SIZE];
1848 1869
 /**
1849 1870
  *
1850 1871
  */
1851
-void hf_iterator_init(void)
1872
+static void hf_iterator_init(void)
1852 1873
 {
1853 1874
 	memset(_hf_iterators, 0, HF_ITERATOR_SIZE*sizeof(hf_iterator_t));
1854 1875
 }
... ...
@@ -2008,7 +2029,7 @@ static int ki_hf_iterator_prev(sip_msg_t *msg, str *iname)
2008 2029
 			if(hf->next->next) {
2009 2030
 				if(_hf_iterators[k].it==hf->next->next) {
2010 2031
 					_hf_iterators[k].it = hf->next;
2011
-					_hf_iterators[k].prev = it;
2032
+					_hf_iterators[k].prev = hf;
2012 2033
 					return 1;
2013 2034
 				}
2014 2035
 			} else {
... ...
@@ -2329,6 +2350,303 @@ static int pv_get_hf_iterator_hbody(sip_msg_t *msg, pv_param_t *param, pv_value_
2329 2350
 	return pv_get_strval(msg, param, res, &_hf_iterators[i].it->body);
2330 2351
 }
2331 2352
 
2353
+
2354
+/*** body line iterator */
2355
+#define BL_ITERATOR_SIZE	4
2356
+#define BL_ITERATOR_NAME_SIZE	32
2357
+
2358
+typedef struct bl_iterator {
2359
+	str name;
2360
+	char bname[HF_ITERATOR_NAME_SIZE];
2361
+	str body;
2362
+	str it;
2363
+	int eob;
2364
+} bl_iterator_t;
2365
+
2366
+static bl_iterator_t _bl_iterators[BL_ITERATOR_SIZE];
2367
+
2368
+/**
2369
+ *
2370
+ */
2371
+static void bl_iterator_init(void)
2372
+{
2373
+	memset(_bl_iterators, 0, BL_ITERATOR_SIZE*sizeof(bl_iterator_t));
2374
+}
2375
+
2376
+/**
2377
+ *
2378
+ */
2379
+static int ki_bl_iterator_start(sip_msg_t *msg, str *iname)
2380
+{
2381
+	int i;
2382
+	int k;
2383
+
2384
+	k = -1;
2385
+	for(i=0; i<BL_ITERATOR_SIZE; i++) {
2386
+		if(_bl_iterators[i].name.len>0) {
2387
+			if(_bl_iterators[i].name.len==iname->len
2388
+					&& strncmp(_bl_iterators[i].name.s, iname->s, iname->len)==0) {
2389
+				k = i;
2390
+				break;
2391
+			}
2392
+		} else {
2393
+			if(k==-1) k = i;
2394
+		}
2395
+	}
2396
+	if(k==-1) {
2397
+		LM_ERR("no iterator available - max number is %d\n", BL_ITERATOR_SIZE);
2398
+		return -1;
2399
+	}
2400
+	if(_bl_iterators[k].name.len<=0) {
2401
+		if(iname->len>=BL_ITERATOR_NAME_SIZE) {
2402
+			LM_ERR("iterator name is too big [%.*s] (max %d)\n",
2403
+					iname->len, iname->s, BL_ITERATOR_NAME_SIZE);
2404
+			return -1;
2405
+		}
2406
+		strncpy(_bl_iterators[k].bname, iname->s, iname->len);
2407
+		_bl_iterators[k].bname[iname->len] = '\0';
2408
+		_bl_iterators[k].name.len = iname->len;
2409
+		_bl_iterators[k].name.s = _bl_iterators[k].bname;
2410
+	}
2411
+	_bl_iterators[k].it.s = NULL;
2412
+	_bl_iterators[k].it.len = 0;
2413
+	_bl_iterators[k].eob = 0;
2414
+	_bl_iterators[k].body.s = get_body(msg);
2415
+	if(_bl_iterators[k].body.s==NULL) {
2416
+		LM_DBG("no message body\n");
2417
+		return -1;
2418
+	}
2419
+	_bl_iterators[k].body.len = msg->buf + msg->len - _bl_iterators[k].body.s;
2420
+	return 1;
2421
+}
2422
+
2423
+/**
2424
+ *
2425
+ */
2426
+static int w_bl_iterator_start(sip_msg_t *msg, char *piname, char *p2)
2427
+{
2428
+	str iname = STR_NULL;
2429
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2430
+		LM_ERR("failed to get iterator name\n");
2431
+		return -1;
2432
+	}
2433
+	return ki_bl_iterator_start(msg, &iname);
2434
+}
2435
+
2436
+/**
2437
+ *
2438
+ */
2439
+static int ki_bl_iterator_next(sip_msg_t *msg, str *iname)
2440
+{
2441
+	int i;
2442
+	int k;
2443
+	char *p;
2444
+
2445
+	k = -1;
2446
+	for(i=0; i<BL_ITERATOR_SIZE; i++) {
2447
+		if(_bl_iterators[i].name.len>0) {
2448
+			if(_bl_iterators[i].name.len==iname->len
2449
+					&& strncmp(_bl_iterators[i].name.s, iname->s, iname->len)==0) {
2450
+				k = i;
2451
+				break;
2452
+			}
2453
+		}
2454
+	}
2455
+	if(k==-1) {
2456
+		LM_ERR("iterator not available [%.*s]\n", iname->len, iname->s);
2457
+		return -1;
2458
+	}
2459
+	if(_bl_iterators[k].eob == 1) {
2460
+		return -1;
2461
+	}
2462
+
2463
+	if(_bl_iterators[k].it.s==NULL) {
2464
+		_bl_iterators[k].it.s = _bl_iterators[k].body.s;
2465
+	}
2466
+	p = _bl_iterators[k].it.s + _bl_iterators[k].it.len;
2467
+	if(p>=_bl_iterators[k].body.s + _bl_iterators[k].body.len) {
2468
+		_bl_iterators[k].it.s = NULL;
2469
+		_bl_iterators[k].it.len = 0;
2470
+		_bl_iterators[k].eob = 1;
2471
+		return -1;
2472
+	}
2473
+	_bl_iterators[k].it.s = p;
2474
+	while(p < _bl_iterators[k].body.s + _bl_iterators[k].body.len) {
2475
+		if(*p=='\n') {
2476
+			break;
2477
+		}
2478
+		p++;
2479
+	}
2480
+	_bl_iterators[k].it.len = p - _bl_iterators[k].it.s + 1;
2481
+
2482
+	return 1;
2483
+}
2484
+
2485
+/**
2486
+ *
2487
+ */
2488
+static int w_bl_iterator_next(sip_msg_t *msg, char *piname, char *p2)
2489
+{
2490
+	str iname = STR_NULL;
2491
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2492
+		LM_ERR("failed to get iterator name\n");
2493
+		return -1;
2494
+	}
2495
+	return ki_bl_iterator_next(msg, &iname);
2496
+}
2497
+
2498
+/**
2499
+ *
2500
+ */
2501
+static int ki_bl_iterator_end(sip_msg_t *msg, str *iname)
2502
+{
2503
+	int i;
2504
+	int k;
2505
+
2506
+	k = -1;
2507
+	for(i=0; i<BL_ITERATOR_SIZE; i++) {
2508
+		if(_bl_iterators[i].name.len>0) {
2509
+			if(_bl_iterators[i].name.len==iname->len
2510
+					&& strncmp(_bl_iterators[i].name.s, iname->s, iname->len)==0) {
2511
+				k = i;
2512
+				break;
2513
+			}
2514
+		}
2515
+	}
2516
+	if(k==-1) {
2517
+		LM_ERR("iterator not available [%.*s]\n", iname->len, iname->s);
2518
+		return -1;
2519
+	}
2520
+	_bl_iterators[k].it.s = NULL;
2521
+	_bl_iterators[k].it.len = 0;
2522
+	_bl_iterators[k].body.s = NULL;
2523
+	_bl_iterators[k].body.len = 0;
2524
+	_bl_iterators[k].eob = 0;
2525
+	return 1;
2526
+}
2527
+
2528
+/**
2529
+ *
2530
+ */
2531
+static int w_bl_iterator_end(sip_msg_t *msg, char *piname, char *p2)
2532
+{
2533
+	str iname = STR_NULL;
2534
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2535
+		LM_ERR("failed to get iterator name\n");
2536
+		return -1;
2537
+	}
2538
+	return ki_bl_iterator_end(msg, &iname);
2539
+}
2540
+
2541
+/**
2542
+ *
2543
+ */
2544
+static int ki_bl_iterator_index(sip_msg_t *msg, str *iname)
2545
+{
2546
+	int i;
2547
+	int k;
2548
+
2549
+	k = -1;
2550
+	for(i=0; i<BL_ITERATOR_SIZE; i++) {
2551
+		if(_bl_iterators[i].name.len>0) {
2552
+			if(_bl_iterators[i].name.len==iname->len
2553
+					&& strncmp(_bl_iterators[i].name.s, iname->s, iname->len)==0) {
2554
+				k = i;
2555
+				break;
2556
+			}
2557
+		}
2558
+	}
2559
+	if(k==-1) {
2560
+		LM_ERR("iterator not available [%.*s]\n", iname->len, iname->s);
2561
+		return -1;
2562
+	}
2563
+
2564
+	return k;
2565
+}
2566
+
2567
+/**
2568
+ *
2569
+ */
2570
+static int ki_bl_iterator_rm(sip_msg_t *msg, str *iname)
2571
+{
2572
+	int k;
2573
+	sr_lump_t *anchor;
2574
+
2575
+	k = ki_bl_iterator_index(msg, iname);
2576
+	if(k<0 || _bl_iterators[k].it.s==NULL || _bl_iterators[k].it.len<=0) {
2577
+		return -1;
2578
+	}
2579
+	anchor = del_lump(msg, _bl_iterators[k].it.s - msg->buf,
2580
+			_bl_iterators[k].it.len, 0);
2581
+	if (anchor==0) {
2582
+		LM_ERR("cannot remove line %.*s\n", _bl_iterators[k].it.len,
2583
+				_bl_iterators[k].it.s);
2584
+		return -1;
2585
+	}
2586
+	return 1;
2587
+}
2588
+
2589
+/**
2590
+ *
2591
+ */
2592
+static int w_bl_iterator_rm(sip_msg_t *msg, char *piname, char *p2)
2593
+{
2594
+	str iname = STR_NULL;
2595
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2596
+		LM_ERR("failed to get iterator name\n");
2597
+		return -1;
2598
+	}
2599
+	return ki_bl_iterator_rm(msg, &iname);
2600
+}
2601
+
2602
+/**
2603
+ *
2604
+ */
2605
+static int pv_parse_bl_iterator_name(pv_spec_t *sp, str *in)
2606
+{
2607
+	if(in->len<=0) {
2608
+		return -1;
2609
+	}
2610
+
2611
+	sp->pvp.pvn.u.isname.name.s.s = in->s;
2612
+	sp->pvp.pvn.u.isname.name.s.len = in->len;
2613
+	sp->pvp.pvn.u.isname.type = 0;
2614
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
2615
+
2616
+	return 0;
2617
+}
2618
+
2619
+/**
2620
+ *
2621
+ */
2622
+static int pv_get_bl_iterator_value(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
2623
+{
2624
+	int i;
2625
+	int k;
2626
+	str *iname;
2627
+
2628
+	iname = &param->pvn.u.isname.name.s;
2629
+	k = -1;
2630
+	for(i=0; i<BL_ITERATOR_SIZE; i++) {
2631
+		if(_bl_iterators[i].name.len>0) {
2632
+			if(_bl_iterators[i].name.len==iname->len
2633
+					&& strncmp(_bl_iterators[i].name.s, iname->s, iname->len)==0) {
2634
+				k = i;
2635
+				break;
2636
+			}
2637
+		}
2638
+	}
2639
+	if(k==-1) {
2640
+		LM_ERR("iterator not available [%.*s]\n", iname->len, iname->s);
2641
+		return pv_get_null(msg, param, res);
2642
+	}
2643
+
2644
+	if(_bl_iterators[i].it.s==NULL) {
2645
+		return pv_get_null(msg, param, res);
2646
+	}
2647
+	return pv_get_strval(msg, param, res, &_bl_iterators[i].it);
2648
+}
2649
+
2332 2650
 /* select implementation */
2333 2651
 static int sel_hf_value(str *res, select_t *s, struct sip_msg *msg)
2334 2652
 { /* dummy */
... ...
@@ -2883,6 +3201,26 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
2883 3201
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
2884 3202
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2885 3203
 	},
3204
+	{ str_init("textopsx"), str_init("bl_iterator_start"),
3205
+		SR_KEMIP_INT, ki_bl_iterator_start,
3206
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3207
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3208
+	},
3209
+	{ str_init("textopsx"), str_init("bl_iterator_end"),
3210
+		SR_KEMIP_INT, ki_bl_iterator_end,
3211
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3212
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3213
+	},
3214
+	{ str_init("textopsx"), str_init("bl_iterator_next"),
3215
+		SR_KEMIP_INT, ki_bl_iterator_next,
3216
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3217
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3218
+	},
3219
+	{ str_init("textopsx"), str_init("bl_iterator_rm"),
3220
+		SR_KEMIP_INT, ki_bl_iterator_rm,
3221
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
3222
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
3223
+	},
2886 3224
 
2887 3225
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
2888 3226
 };
Browse code

textopsx: added hf_iterator_prev() function

Daniel-Constantin Mierla authored on 30/07/2021 07:10:16
Showing 1 changed files
... ...
@@ -75,6 +75,7 @@ static int assign_hf_value2_fixup(void **param, int param_no);
75 75
 
76 76
 static int w_hf_iterator_start(sip_msg_t *msg, char *piname, char *p2);
77 77
 static int w_hf_iterator_next(sip_msg_t *msg, char *piname, char *p2);
78
+static int w_hf_iterator_prev(sip_msg_t *msg, char *piname, char *p2);
78 79
 static int w_hf_iterator_end(sip_msg_t *msg, char *piname, char *p2);
79 80
 static int w_hf_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
80 81
 static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext);
... ...
@@ -137,6 +138,8 @@ static cmd_export_t cmds[] = {
137 138
 			fixup_free_spve_null, ANY_ROUTE},
138 139
 	{"hf_iterator_next", w_hf_iterator_next, 1, fixup_spve_null,
139 140
 			fixup_free_spve_null, ANY_ROUTE},
141
+	{"hf_iterator_prev", w_hf_iterator_prev, 1, fixup_spve_null,
142
+			fixup_free_spve_null, ANY_ROUTE},
140 143
 	{"hf_iterator_end", w_hf_iterator_end, 1, fixup_spve_null,
141 144
 			fixup_free_spve_null, ANY_ROUTE},
142 145
 	{"hf_iterator_rm", w_hf_iterator_rm, 1, fixup_spve_null,
... ...
@@ -1836,6 +1839,7 @@ typedef struct hf_iterator {
1836 1839
 	str name;
1837 1840
 	char bname[HF_ITERATOR_NAME_SIZE];
1838 1841
 	hdr_field_t *it;
1842
+	hdr_field_t *prev;
1839 1843
 	int eoh;
1840 1844
 } hf_iterator_t;
1841 1845
 
... ...
@@ -1886,6 +1890,7 @@ static int ki_hf_iterator_start(sip_msg_t *msg, str *iname)
1886 1890
 		_hf_iterators[k].name.s = _hf_iterators[k].bname;
1887 1891
 	}
1888 1892
 	_hf_iterators[k].it = NULL;
1893
+	_hf_iterators[k].prev = NULL;
1889 1894
 	_hf_iterators[k].eoh = 0;
1890 1895
 	if(parse_headers(msg, HDR_EOH_F, 0) == -1) {
1891 1896
 		LM_ERR("failed parsing message\n");
... ...
@@ -1940,6 +1945,7 @@ static int ki_hf_iterator_next(sip_msg_t *msg, str *iname)
1940 1945
 	if(_hf_iterators[k].it == NULL) {
1941 1946
 		_hf_iterators[k].it = msg->headers;
1942 1947
 	} else {
1948
+		_hf_iterators[k].prev = _hf_iterators[k].it;
1943 1949
 		_hf_iterators[k].it = _hf_iterators[k].it->next;
1944 1950
 	}
1945 1951
 	if(_hf_iterators[k].it == NULL) {
... ...
@@ -1962,6 +1968,74 @@ static int w_hf_iterator_next(sip_msg_t *msg, char *piname, char *p2)
1962 1968
 	return ki_hf_iterator_next(msg, &iname);
1963 1969
 }
1964 1970
 
1971
+/**
1972
+ *
1973
+ */
1974
+static int ki_hf_iterator_prev(sip_msg_t *msg, str *iname)
1975
+{
1976
+	hdr_field_t *hf;
1977
+	int i;
1978
+	int k;
1979
+
1980
+	k = -1;
1981
+	for(i=0; i<HF_ITERATOR_SIZE; i++) {
1982
+		if(_hf_iterators[i].name.len>0) {
1983
+			if(_hf_iterators[i].name.len==iname->len
1984
+					&& strncmp(_hf_iterators[i].name.s, iname->s, iname->len)==0) {
1985
+				k = i;
1986
+				break;
1987
+			}
1988
+		}
1989
+	}
1990
+	if(k==-1) {
1991
+		LM_ERR("iterator not available [%.*s]\n", iname->len, iname->s);
1992
+		return -1;
1993
+	}
1994
+	if(_hf_iterators[k].eoh == 1) {
1995
+		return -1;
1996
+	}
1997
+
1998
+	if(_hf_iterators[k].prev==NULL) {
1999
+		return ki_hf_iterator_start(msg, iname);
2000
+	}
2001
+
2002
+	if(_hf_iterators[k].prev!=_hf_iterators[k].it) {
2003
+		_hf_iterators[k].it = _hf_iterators[k].prev;
2004
+		return 1;
2005
+	}
2006
+	for(hf=msg->headers; hf; hf=hf->next) {
2007
+		if(hf->next) {
2008
+			if(hf->next->next) {
2009
+				if(_hf_iterators[k].it==hf->next->next) {
2010
+					_hf_iterators[k].it = hf->next;
2011
+					_hf_iterators[k].prev = it;
2012
+					return 1;
2013
+				}
2014
+			} else {
2015
+				if(_hf_iterators[k].it==hf->next) {
2016
+					_hf_iterators[k].it = hf;
2017
+					_hf_iterators[k].prev = NULL;
2018
+					return 1;
2019
+				}
2020
+			}
2021
+		}
2022
+	}
2023
+	return ki_hf_iterator_start(msg, iname);
2024
+}
2025
+
2026
+/**
2027
+ *
2028
+ */
2029
+static int w_hf_iterator_prev(sip_msg_t *msg, char *piname, char *p2)
2030
+{
2031
+	str iname = STR_NULL;
2032
+	if(fixup_get_svalue(msg, (gparam_t*)piname, &iname)<0) {
2033
+		LM_ERR("failed to get iterator name\n");
2034
+		return -1;
2035
+	}
2036
+	return ki_hf_iterator_prev(msg, &iname);
2037
+}
2038
+
1965 2039
 /**
1966 2040
  *
1967 2041
  */
... ...
@@ -2789,6 +2863,11 @@ static sr_kemi_t sr_kemi_textopsx_exports[] = {
2789 2863
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2790 2864
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2791 2865
 	},
2866
+	{ str_init("textopsx"), str_init("hf_iterator_prev"),
2867
+		SR_KEMIP_INT, ki_hf_iterator_prev,
2868
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
2869
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2870
+	},
2792 2871
 	{ str_init("textopsx"), str_init("hf_iterator_rm"),
2793 2872
 		SR_KEMIP_INT, ki_hf_iterator_rm,
2794 2873
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
Browse code

textopsx: added hf_iterator_insert() function

Daniel-Constantin Mierla authored on 29/07/2021 07:01:25
Showing 1 changed files
... ...
@@ -78,6 +78,7 @@ static int w_hf_iterator_next(sip_msg_t *msg, char *piname, char *p2);
78 78
 static int w_hf_iterator_end(sip_msg_t *msg, char *piname, char *p2);
79 79
 static int w_hf_iterator_rm(sip_msg_t *msg, char *piname, char *p2);
80 80
 static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext);
81
+static int w_hf_iterator_insert(sip_msg_t *msg, char *piname, char *phtext);
81 82
 
82 83
 static int bind_textopsx(textopsx_api_t *tob);
83 84
 
... ...
@@ -142,6 +143,8 @@ static cmd_export_t cmds[] = {
142 143
 			fixup_free_spve_null, ANY_ROUTE},
143 144
 	{"hf_iterator_append", w_hf_iterator_append, 2, fixup_spve_spve,
144 145
 			fixup_free_spve_spve, ANY_ROUTE},
146
+	{"hf_iterator_insert", w_hf_iterator_insert, 2, fixup_spve_spve,
147
+			fixup_free_spve_spve, ANY_ROUTE},
145 148
 
146 149
 	{"bind_textopsx", (cmd_function)bind_textopsx, 1, 0, 0, ANY_ROUTE},
147 150
 
... ...
@@ -2117,6 +2120,62 @@ static int w_hf_iterator_append(sip_msg_t *msg, char *piname, char *phtext)
2117 2120
 	return ki_hf_iterator_append(msg, &iname, &htext);
2118 2121
 }
2119 2122
 
2123
+/**
2124
+ *
2125
+ */
2126
+static int ki_hf_iterator_insert(sip_msg_t *msg, str *iname, str *htext)
2127
+{
2128
+	int k;
2129
+	sr_lump_t *anchor;
2130
+	str sval = STR_NULL;
2131
+
2132
+	k = ki_hf_iterator_index(msg, iname);
2133
+	if(k<0 || _hf_iterators[k].it==NULL) {
2134
+		return -1;
2135
+	}
2136
+	anchor = anchor_lump(msg, _hf_iterators[k].it->name.s - msg->buf, 0, 0);
2137
+	if (anchor==0) {
2138
+		LM_ERR("cannot insert hdr after %.*s\n", _hf_iterators[k].it->name.len,
2139
+				_hf_iterators[k].it->name.s);
2140
+		return -1;
2141
+	}
2142
+	sval.s = (char*)pkg_malloc(htext->len + 1);
2143
+	if(sval.s==NULL) {
2144
+		LM_ERR("failed to insert hdr after %.*s\n", _hf_iterators[k].it->name.len,
2145
+				_hf_iterators[k].it->name.s);
2146
+		return -1;
2147
+	}
2148
+	memcpy(sval.s, htext->s, htext->len);
2149
+	sval.len = htext->len;
2150
+	sval.s[sval.len] = '\0';
2151
+
2152
+	if (insert_new_lump_before(anchor, sval.s, sval.len, 0) == 0) {
2153
+		LM_ERR("cannot insert lump\n");
2154
+		pkg_free(sval.s);
2155
+		return -1;
2156
+	}
2157
+	return 1;
2158
+}
2159
+
2160
+/**
2161
+ *
2162
+ */