Browse code

modules_k/textops: added starts_with() function

Added starts_with() function in order to avoid use of regexs for
this common test.

Juha Heinanen authored on 29/03/2010 11:28:17
Showing 4 changed files
... ...
@@ -17,7 +17,13 @@ Daniel-Constantin Mierla
17 17
 
18 18
    <miconda@gmail.com>
19 19
 
20
-   Copyright � 2003 FhG FOKUS
20
+Edited by
21
+
22
+Juha Heinanen
23
+
24
+   <jh@tutpro.com>
25
+
26
+   Copyright © 2003 FhG FOKUS
21 27
    Revision History
22 28
    Revision $Revision$ $Date$
23 29
      __________________________________________________________________
... ...
@@ -69,7 +75,8 @@ Daniel-Constantin Mierla
69 75
               3.30. is_privacy(privacy_type)
70 76
               3.31. cmp_str(str1, str2)
71 77
               3.32. cmp_istr(str1, str2)
72
-              3.33. msg_apply_changes()
78
+              3.33. starts_with(str1, str2)
79
+              3.34. msg_apply_changes()
73 80
 
74 81
         4. Known Limitations
75 82
 
... ...
@@ -113,7 +120,8 @@ Daniel-Constantin Mierla
113 120
    1.30. is_privacy usage
114 121
    1.31. cmp_str usage
115 122
    1.32. cmp_str usage
116
-   1.33. msg_apply_changes() usage
123
+   1.33. starts_with usage
124
+   1.34. msg_apply_changes() usage
117 125
 
118 126
 Chapter 1. Admin Guide
119 127
 
... ...
@@ -162,7 +170,8 @@ Chapter 1. Admin Guide
162 170
         3.30. is_privacy(privacy_type)
163 171
         3.31. cmp_str(str1, str2)
164 172
         3.32. cmp_istr(str1, str2)
165
-        3.33. msg_apply_changes()
173
+        3.33. starts_with(str1, str2)
174
+        3.34. msg_apply_changes()
166 175
 
167 176
    4. Known Limitations
168 177
 
... ...
@@ -179,7 +188,7 @@ Chapter 1. Admin Guide
179 188
 
180 189
 1.1. Known Limitations
181 190
 
182
-   search ignores folded lines. For example, search("(From|f):.*@foo.bar")
191
+   search ignores folded lines. For example, search(“(From|f):.*@foo.bar”)
183 192
    doesn't match the following From header field:
184 193
 From: medabeda
185 194
  <sip:medameda@foo.bar>;tag=1234
... ...
@@ -234,9 +243,10 @@ From: medabeda
234 243
    3.30. is_privacy(privacy_type)
235 244
    3.31. cmp_str(str1, str2)
236 245
    3.32. cmp_istr(str1, str2)
237
-   3.33. msg_apply_changes()
246
+   3.33. starts_with(str1, str2)
247
+   3.34. msg_apply_changes()
238 248
 
239
-3.1. search(re)
249
+3.1.  search(re)
240 250
 
241 251
    Searches for the re in the message.
242 252
 
... ...
@@ -251,7 +261,7 @@ From: medabeda
251 261
 if ( search("[Ss][Ii][Pp]") ) { /*....*/ };
252 262
 ...
253 263
 
254
-3.2. search_body(re)
264
+3.2.  search_body(re)
255 265
 
256 266
    Searches for the re in the body of the message.
257 267
 
... ...
@@ -266,7 +276,7 @@ if ( search("[Ss][Ii][Pp]") ) { /*....*/ };
266 276
 if ( search_body("[Ss][Ii][Pp]") ) { /*....*/ };
267 277
 ...
268 278
 
269
-3.3. search_append(re, txt)
279
+3.3.  search_append(re, txt)
270 280
 
271 281
    Searches for the first match of re and appends txt after it.
272 282
 
... ...
@@ -282,7 +292,7 @@ if ( search_body("[Ss][Ii][Pp]") ) { /*....*/ };
282 292
 search_append("[Oo]pen[Ss]er", " SIP Proxy");
283 293
 ...
284 294
 
285
-3.4. search_append_body(re, txt)
295
+3.4.  search_append_body(re, txt)
286 296
 
287 297
    Searches for the first match of re in the body of the message and
288 298
    appends txt after it.
... ...
@@ -299,7 +309,7 @@ search_append("[Oo]pen[Ss]er", " SIP Proxy");
299 309
 search_append_body("[Oo]pen[Ss]er", " SIP Proxy");
300 310
 ...
301 311
 
302
-3.5. replace(re, txt)
312
+3.5.  replace(re, txt)
303 313
 
304 314
    Replaces the first occurrence of re with txt.
305 315
 
... ...
@@ -315,7 +325,7 @@ search_append_body("[Oo]pen[Ss]er", " SIP Proxy");
315 325
 replace("openser", "Kamailio SIP Proxy");
316 326
 ...
317 327
 
318
-3.6. replace_body(re, txt)
328
+3.6.  replace_body(re, txt)
319 329
 
320 330
    Replaces the first occurrence of re in the body of the message with
321 331
    txt.
... ...
@@ -332,7 +342,7 @@ replace("openser", "Kamailio SIP Proxy");
332 342
 replace_body("openser", "Kamailio SIP Proxy");
333 343
 ...
334 344
 
335
-3.7. replace_all(re, txt)
345
+3.7.  replace_all(re, txt)
336 346
 
337 347
    Replaces all occurrence of re with txt.
338 348
 
... ...
@@ -348,7 +358,7 @@ replace_body("openser", "Kamailio SIP Proxy");
348 358
 replace_all("openser", "Kamailio SIP Proxy");
349 359
 ...
350 360
 
351
-3.8. replace_body_all(re, txt)
361
+3.8.  replace_body_all(re, txt)
352 362
 
353 363
    Replaces all occurrence of re in the body of the message with txt.
354 364
    Matching is done on a per-line basis.
... ...
@@ -365,7 +375,7 @@ replace_all("openser", "Kamailio SIP Proxy");
365 375
 replace_body_all("openser", "Kamailio SIP Proxy");
366 376
 ...
367 377
 
368
-3.9. replace_body_atonce(re, txt)
378
+3.9.  replace_body_atonce(re, txt)
369 379
 
370 380
    Replaces all occurrence of re in the body of the message with txt.
371 381
    Matching is done over the whole body.
... ...
@@ -384,7 +394,7 @@ if(has_body() && replace_body_atonce("^.+$", ""))
384 394
         remove_hf("Content-Type");
385 395
 ...
386 396
 
387
-3.10. subst('/re/repl/flags')
397
+3.10.  subst('/re/repl/flags')
388 398
 
389 399
    Replaces re with repl (sed or perl like).
390 400
 
... ...
@@ -410,7 +420,7 @@ if ( subst('/^To:(.*)sip:[^@]*@[a-zA-Z0-9.]+(.*)$/t:\1$avp(sip_address)\2/ig') )
410 420
 
411 421
 ...
412 422
 
413
-3.11. subst_uri('/re/repl/flags')
423
+3.11.  subst_uri('/re/repl/flags')
414 424
 
415 425
    Runs the re substitution on the message uri (like subst but works only
416 426
    on the uri)
... ...
@@ -438,7 +448,7 @@ if (subst_uri('/^sip:([0-9]+)@(.*)$/sip:$avp(uri_prefix)\1@\2;orig_uri=\0/i')){$
438 448
 
439 449
 ...
440 450
 
441
-3.12. subst_user('/re/repl/flags')
451
+3.12.  subst_user('/re/repl/flags')
442 452
 
443 453
    Runs the re substitution on the message uri (like subst_uri but works
444 454
    only on the user portion of the uri)
... ...
@@ -465,7 +475,7 @@ if (subst_user('/(.*)3642$/$avp(user_prefix)\13642/')){$
465 475
 
466 476
 ...
467 477
 
468
-3.13. subst_body('/re/repl/flags')
478
+3.13.  subst_body('/re/repl/flags')
469 479
 
470 480
    Replaces re with repl (sed or perl like) in the body of the message.
471 481
 
... ...
@@ -486,7 +496,7 @@ if ( subst_body('/^o=(.*) /o=$fU /') ) {};
486 496
 
487 497
 ...
488 498
 
489
-3.14. set_body(txt,content_type)
499
+3.14.  set_body(txt,content_type)
490 500
 
491 501
    Set body to a SIP message.
492 502
 
... ...
@@ -503,7 +513,7 @@ if ( subst_body('/^o=(.*) /o=$fU /') ) {};
503 513
 set_body("test", "text/plain");
504 514
 ...
505 515
 
506
-3.15. set_reply_body(txt,content_type)
516
+3.15.  set_reply_body(txt,content_type)
507 517
 
508 518
    Set body to a SIP reply to be generated by Kamailio.
509 519
 
... ...
@@ -520,7 +530,7 @@ set_body("test", "text/plain");
520 530
 set_reply_body("test", "text/plain");
521 531
 ...
522 532
 
523
-3.16. filter_body(content_type)
533
+3.16.  filter_body(content_type)
524 534
 
525 535
    Filters multipart/mixed body by leaving out all other body parts except
526 536
    the first body part of given type.
... ...
@@ -543,7 +553,7 @@ if (has_body("multipart/mixed")) {
543 553
 }
544 554
 ...
545 555
 
546
-3.17. append_to_reply(txt)
556
+3.17.  append_to_reply(txt)
547 557
 
548 558
    Append txt as header to the reply.
549 559
 
... ...
@@ -559,7 +569,7 @@ append_to_reply("Foo: bar\r\n");
559 569
 append_to_reply("Foo: $rm at $Ts\r\n");
560 570
 ...
561 571
 
562
-3.18. append_hf(txt)
572
+3.18.  append_hf(txt)
563 573
 
564 574
    Appends 'txt' as header after the last header field.
565 575
 
... ...
@@ -582,7 +592,7 @@ append_hf("P-hint: VOICEMAIL\r\n");
582 592
 append_hf("From-username: $fU\r\n");
583 593
 ...
584 594
 
585
-3.19. append_hf(txt, hdr)
595
+3.19.  append_hf(txt, hdr)
586 596
 
587 597
    Appends 'txt' as header after first 'hdr' header field.
588 598
 
... ...
@@ -600,7 +610,7 @@ append_hf("P-hint: VOICEMAIL\r\n", "Call-ID");
600 610
 append_hf("From-username: $fU\r\n", "Call-ID");
601 611
 ...
602 612
 
603
-3.20. insert_hf(txt)
613
+3.20.  insert_hf(txt)
604 614
 
605 615
    Inserts 'txt' as header before the first header field.
606 616
 
... ...
@@ -617,7 +627,7 @@ insert_hf("P-hint: VOICEMAIL\r\n");
617 627
 insert_hf("To-username: $tU\r\n");
618 628
 ...
619 629
 
620
-3.21. insert_hf(txt, hdr)
630
+3.21.  insert_hf(txt, hdr)
621 631
 
622 632
    Inserts 'txt' as header before first 'hdr' header field.
623 633
 
... ...
@@ -635,7 +645,7 @@ insert_hf("P-hint: VOICEMAIL\r\n", "Call-ID");
635 645
 insert_hf("To-username: $tU\r\n", "Call-ID");
636 646
 ...
637 647
 
638
-3.22. append_urihf(prefix, suffix)
648
+3.22.  append_urihf(prefix, suffix)
639 649
 
640 650
    Append header field name with original Request-URI in middle.
641 651
 
... ...
@@ -651,14 +661,14 @@ insert_hf("To-username: $tU\r\n", "Call-ID");
651 661
 append_urihf("CC-Diversion: ", "\r\n");
652 662
 ...
653 663
 
654
-3.23. is_present_hf(hf_name)
664
+3.23.  is_present_hf(hf_name)
655 665
 
656 666
    Return true if a header field is present in message.
657 667
 
658 668
 Note
659 669
 
660 670
    The function is also able to distinguish the compact names. For exmaple
661
-   "From" will match with "f"
671
+   “From” will match with “f”
662 672
 
663 673
    Meaning of the parameters is as follows:
664 674
      * hf_name - Header field name.(long or compact form)
... ...
@@ -671,7 +681,7 @@ Note
671 681
 if (is_present_hf("From")) log(1, "From HF Present");
672 682
 ...
673 683
 
674
-3.24. is_present_hf_re(hf_name_re)
684
+3.24.  is_present_hf_re(hf_name_re)
675 685
 
676 686
    Return true if a header field whose name matches regular expression
677 687
    'hf_name_re' is present in message.
... ...
@@ -687,12 +697,12 @@ if (is_present_hf("From")) log(1, "From HF Present");
687 697
 if (is_present_hf_re("^P-")) log(1, "There are headers starting with P-\n");
688 698
 ...
689 699
 
690
-3.25. append_time()
700
+3.25.  append_time()
691 701
 
692 702
    Adds a time header to the reply of the request. You must use it before
693 703
    functions that are likely to send a reply, e.g., save() from
694
-   'registrar' module. Header format is: "Date: %a, %d %b %Y %H:%M:%S
695
-   GMT", with the legend:
704
+   'registrar' module. Header format is: “Date: %a, %d %b %Y %H:%M:%S
705
+   GMT”, with the legend:
696 706
      * %a abbreviated week of day name (locale)
697 707
      * %d day of month as decimal number
698 708
      * %b abbreviated month name (locale)
... ...
@@ -711,7 +721,7 @@ if (is_present_hf_re("^P-")) log(1, "There are headers starting with P-\n");
711 721
 append_time();
712 722
 ...
713 723
 
714
-3.26. is_method(name)
724
+3.26.  is_method(name)
715 725
 
716 726
    Check if the method of the message matches the name. If name is a known
717 727
    method (invite, cancel, ack, bye, options, info, update, register,
... ...
@@ -748,9 +758,9 @@ if(is_method("OPTION|UPDATE"))
748 758
 }
749 759
 ...
750 760
 
751
-3.27. remove_hf(hname)
761
+3.27.  remove_hf(hname)
752 762
 
753
-   Remove from message all headers with name "hname"
763
+   Remove from message all headers with name “hname”
754 764
 
755 765
    Returns true if at least one header is found and removed.
756 766
 
... ...
@@ -768,10 +778,10 @@ if(remove_hf("User-Agent"))
768 778
 }
769 779
 ...
770 780
 
771
-3.28. remove_hf_re(re)
781
+3.28.  remove_hf_re(re)
772 782
 
773 783
    Remove from message all headers with name matching regular expression
774
-   "re"
784
+   “re”
775 785
 
776 786
    Returns true if at least one header is found and removed.
777 787
 
... ...
@@ -789,16 +799,16 @@ if(remove_hf_re("^P-"))
789 799
 }
790 800
 ...
791 801
 
792
-3.29. has_body(), has_body(mime)
802
+3.29.  has_body(), has_body(mime)
793 803
 
794 804
    The function returns true if the SIP message has a body attached. The
795
-   checked includes also the "Content-Lenght" header presence and value.
805
+   checked includes also the “Content-Lenght” header presence and value.
796 806
 
797 807
    If a parameter is given, the mime described will be also checked
798
-   against the "Content-Type" header.
808
+   against the “Content-Type” header.
799 809
 
800 810
    Meaning of the parameters is as follows:
801
-     * mime - mime to be checked against the "Content-Type" header. If not
811
+     * mime - mime to be checked against the “Content-Type” header. If not
802 812
        present or 0, this check will be disabled.
803 813
 
804 814
    This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
... ...
@@ -812,7 +822,7 @@ if(has_body("application/sdp"))
812 822
 }
813 823
 ...
814 824
 
815
-3.30. is_privacy(privacy_type)
825
+3.30.  is_privacy(privacy_type)
816 826
 
817 827
    The function returns true if the SIP message has a Privacy header field
818 828
    that includes the given privacy_type among its privacy values. See
... ...
@@ -830,7 +840,7 @@ if(is_privacy("id"))
830 840
 }
831 841
 ...
832 842
 
833
-3.31. cmp_str(str1, str2)
843
+3.31.  cmp_str(str1, str2)
834 844
 
835 845
    The function returns true if the two parameters matches as string case
836 846
    sensitive comparison.
... ...
@@ -846,7 +856,7 @@ if(cmp_str("$rU", "kamailio"))
846 856
 }
847 857
 ...
848 858
 
849
-3.32. cmp_istr(str1, str2)
859
+3.32.  cmp_istr(str1, str2)
850 860
 
851 861
    The function returns true if the two parameters matches as string case
852 862
    insensitive comparison.
... ...
@@ -862,7 +872,23 @@ if(cmp_istr("$rU@you", "kamailio@YOU"))
862 872
 }
863 873
 ...
864 874
 
865
-3.33. msg_apply_changes()
875
+3.33.  starts_with(str1, str2)
876
+
877
+   The function returns true if the first string starts with the second
878
+   string.
879
+
880
+   This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
881
+   FAILURE_ROUTE and BRANCH_ROUTE.
882
+
883
+   Example 1.33. starts_with usage
884
+...
885
+if(cmp_str("$rU", "+358"))
886
+{
887
+    # do interesting stuff here
888
+}
889
+...
890
+
891
+3.34.  msg_apply_changes()
866 892
 
867 893
    Use this function to apply changes done on SIP request content. Be
868 894
    careful when using this function -- due to special handling of changes
... ...
@@ -872,7 +898,7 @@ if(cmp_istr("$rU@you", "kamailio@YOU"))
872 898
 
873 899
    This function can be used from REQUEST_ROUTE.
874 900
 
875
-   Example 1.33. msg_apply_changes() usage
901
+   Example 1.34. msg_apply_changes() usage
876 902
 ...
877 903
 append_hf("My-Header: yes\r\n");
878 904
 if(msg_apply_changes())
... ...
@@ -902,10 +928,10 @@ Chapter 2. Developer Guide
902 928
 
903 929
    1.1. load_textops(*import_structure)
904 930
 
905
-1.1. load_textops(*import_structure)
931
+1.1.  load_textops(*import_structure)
906 932
 
907 933
    For programmatic use only--import the Textops API.
908 934
 
909 935
    Meaning of the parameters is as follows:
910
-     * import_structure - Pointer to the import structure - see "struct
911
-       textops_binds" in modules/textops/api.h
936
+     * import_structure - Pointer to the import structure - see “struct
937
+       textops_binds” in modules/textops/api.h
... ...
@@ -29,6 +29,11 @@
29 29
 		<surname>Mierla</surname>
30 30
 			<email>miconda@gmail.com</email>
31 31
 		</editor>
32
+		<editor>
33
+		<firstname>Juha</firstname>
34
+		<surname>Heinanen</surname>
35
+			<email>jh@tutpro.com</email>
36
+		</editor>
32 37
 	</authorgroup>
33 38
 	<copyright>
34 39
 		<year>2003</year>
... ...
@@ -1208,6 +1208,31 @@ if(cmp_istr("$rU@you", "kamailio@YOU"))
1208 1208
 		</example>
1209 1209
 	</section>
1210 1210
 
1211
+	<section>
1212
+		<title>
1213
+		<function moreinfo="none">starts_with(str1, str2)</function>
1214
+		</title>
1215
+		<para>
1216
+		The function returns <emphasis>true</emphasis> if 
1217
+		the first string starts with the second string.
1218
+		</para>
1219
+   		<para>
1220
+		This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE, 
1221
+		FAILURE_ROUTE and BRANCH_ROUTE.
1222
+		</para>
1223
+		<example>
1224
+		<title><function>starts_with</function> usage</title>
1225
+		<programlisting format="linespecific">
1226
+...
1227
+if(cmp_str("$rU", "+358"))
1228
+{
1229
+    # do interesting stuff here
1230
+}
1231
+...
1232
+</programlisting>
1233
+		</example>
1234
+	</section>
1235
+
1211 1236
 	<section>
1212 1237
 		<title>
1213 1238
 		<function moreinfo="none">msg_apply_changes()</function>
... ...
@@ -120,6 +120,7 @@ static int has_body_f(struct sip_msg *msg, char *type, char *str2 );
120 120
 static int is_privacy_f(struct sip_msg *msg, char *privacy, char *str2 );
121 121
 static int cmp_str_f(struct sip_msg *msg, char *str1, char *str2 );
122 122
 static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2 );
123
+static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 );
123 124
 static int remove_hf_re_f(struct sip_msg* msg, char* key, char* foo);
124 125
 static int is_present_hf_re_f(struct sip_msg* msg, char* key, char* foo);
125 126
 static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2);
... ...
@@ -242,6 +243,9 @@ static cmd_export_t cmds[]={
242 243
 	{"cmp_istr",  (cmd_function)cmp_istr_f, 2,
243 244
 		fixup_spve_spve, 0,
244 245
 		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
246
+	{"starts_with",  (cmd_function)starts_with_f, 2,
247
+		fixup_spve_spve, 0,
248
+		REQUEST_ROUTE|ONREPLY_ROUTE|FAILURE_ROUTE|BRANCH_ROUTE|LOCAL_ROUTE},
245 249
 	{"msg_apply_changes",      (cmd_function)msg_apply_changes_f,     0,
246 250
 		0, 0,
247 251
 		REQUEST_ROUTE },
... ...
@@ -1847,6 +1851,31 @@ static int cmp_istr_f(struct sip_msg *msg, char *str1, char *str2)
1847 1851
 	return -2;
1848 1852
 }
1849 1853
 
1854
+static int starts_with_f(struct sip_msg *msg, char *str1, char *str2 )
1855
+{
1856
+	str s1;
1857
+	str s2;
1858
+	int ret;
1859
+
1860
+	if(fixup_get_svalue(msg, (gparam_p)str1, &s1)!=0)
1861
+	{
1862
+		LM_ERR("cannot get first parameter\n");
1863
+		return -8;
1864
+	}
1865
+	if(fixup_get_svalue(msg, (gparam_p)str2, &s2)!=0)
1866
+	{
1867
+		LM_ERR("cannot get second parameter\n");
1868
+		return -8;
1869
+	}
1870
+	if (s1.len < s2.len) return -1;
1871
+	ret = strncmp(s1.s, s2.s, s2.len);
1872
+	if(ret==0)
1873
+		return 1;
1874
+	if(ret>0)
1875
+		return -1;
1876
+	return -2;
1877
+}
1878
+
1850 1879
 static int msg_apply_changes_f(sip_msg_t *msg, char *str1, char *str2)
1851 1880
 {
1852 1881
 	struct dest_info dst;