app_perl_samples.xml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?xml version="1.0" encoding='ISO-8859-1'?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
  4. <!-- Include general documentation entities -->
  5. <!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
  6. %docentities;
  7. ]>
  8. <!-- Module Sample descriptions -->
  9. <chapter>
  10. <chapterinfo>
  11. </chapterinfo>
  12. <title>Perl samples</title>
  13. <section>
  14. <title>Sample directory</title>
  15. <para>
  16. There are a number of example scripts in the <quote>samples/</quote>. They are
  17. documented well. Read them, it will explain a lot to you :)
  18. </para>
  19. <para>
  20. If you want to use any of these scripts directly in your implementation, you
  21. can use Perl's <quote>require</quote> mechanism to import them (just remember
  22. that you need to use quotes when require'ing .pl files).
  23. </para>
  24. <section>
  25. <title>Script descriptions</title>
  26. <para>
  27. The included sample scripts are described below:
  28. </para>
  29. <section>
  30. <title>branches.pl</title>
  31. <para>
  32. The minimal function in branches.pl demonstrates that you can access the "append_branch"
  33. function from within perl, just as you would have done from your normal configuration file.
  34. You'll find documentation on the concepts of branching in the Kamailio documentation.
  35. </para>
  36. </section>
  37. <section>
  38. <title>firstline.pl</title>
  39. <para>
  40. Message's first_line structure may be evaluated. Message can be either of
  41. SIP_REQUEST or SIP_REPLY. Depending on that, different information can be received.
  42. This script demonstrates these functions.
  43. </para>
  44. </section>
  45. <section>
  46. <title>flags.pl</title>
  47. <para>
  48. The perl module provides access to Kamailio's flagging mechanism. The flag names available
  49. for Kamailio modules are made available through the Kamailio::Constants package, so you can
  50. flag messages as "green", "magenta" etc.
  51. </para>
  52. <para>
  53. The first function, setflag, demonstrates how the "green" flag is set. In the second function,
  54. readflag, the "green" and "magenta" flags are evaluated.
  55. </para>
  56. </section>
  57. <section>
  58. <title>functions.pl</title>
  59. <para>
  60. This sample script demonstrates different things related to calling functions from within perl,
  61. and the different types of functions you can offer for Kamailio access.
  62. </para>
  63. <para>
  64. <quote>exportedfuncs</quote> simply demonstrates that you can use the moduleFunction method
  65. to call functions offered by other modules. The results are equivalent to calling these
  66. functions from your config file. In the demonstrated case, telephone calls with a destination
  67. number beginning with 555... are rejected with an internal server error. Other destination
  68. addresses are passed to the alias_db module.
  69. </para>
  70. <para>
  71. Please note that the moduleFunction method is not fully available in Kamailio 1.2. See the method's
  72. documentation for details.
  73. </para>
  74. <para>
  75. <quote>paramfunc</quote> shows that you can pass arbitrary strings to perl functions. Do with
  76. them whatever you want :)
  77. </para>
  78. <para>
  79. <quote>autotest</quote> demonstrates that unknown functions in Kamailio::Message objects are
  80. automatically transformed into calls to module functions.
  81. </para>
  82. <para>
  83. The <quote>diefunc</quote>s show that dying perl scripts - by "manual" dying, or because of script
  84. errors - are handled by the Kamailio package. The error message is logged through Kamailio's logging
  85. mechanism. Please note that this only works correctly if you do NOT overwrite the default die handler.
  86. Oh, yes, that works for warnings, too.
  87. </para>
  88. </section>
  89. <section>
  90. <title>headers.pl</title>
  91. <para>
  92. Header extraction is among the most crucial functionalities while processing SIP messages. This
  93. sample script demonstrates access to header names and values within two sample functions.
  94. </para>
  95. <para>
  96. <quote>headernames</quote> extracts all header names and logs their names.
  97. </para>
  98. <para>
  99. <quote>someheaders</quote> logs the contents of the two headers, <quote>To</quote> and
  100. <quote>WWW-Contact</quote>. As you can see, headers that occur more than once are retrieved
  101. as an array, which may be accessed by Perl's array accessing methods.
  102. </para>
  103. </section>
  104. <section>
  105. <title>logging.pl</title>
  106. <para>
  107. For debugging purposes, you probably want to write messages to the syslog. The <quote>logdemo</quote>
  108. shows three ways to access the Kamailio log function: it is available through the Kamailio class as well
  109. as through the Kamailio::Message class.
  110. </para>
  111. <para>
  112. Remember that you can use exported functions from other modules. You may thus as well use the
  113. <quote>xlog</quote> module and it's xlog function.
  114. </para>
  115. <para>
  116. The L_INFO, L_DBG, L_ERR, L_CRIT... constants are available through the Kamailio::Constants package.
  117. </para>
  118. </section>
  119. <section>
  120. <title>messagedump.pl</title>
  121. <para>
  122. This script demonstrates how to access the whole message header of the current message. Please note that
  123. modifications on the message made by earlier function calls in your configuration script may NOT be
  124. reflected in this dump.
  125. </para>
  126. </section>
  127. <section>
  128. <title>persistence.pl</title>
  129. <para>
  130. When processing SIP messages, you may want to use persistent data across multiple calls to your
  131. Perl functions. Your first option is to use global variables in your script. Unfortunately,
  132. these globals are not visible from the mulitple instances of Kamailio. You may want to use a
  133. mechanism such as the IPC::Shareable shared memory access package to correct this.
  134. </para>
  135. </section>
  136. <section>
  137. <title>phonenumbers.pl</title>
  138. <para>
  139. The Kamailio::Utils::PhoneNumbers package provides two methods for the transformation of local to
  140. canonical telephone numbers, and vice versa. This script demonstrates it's use.
  141. </para>
  142. </section>
  143. <section>
  144. <title>pseudovars.pl</title>
  145. <para>
  146. This script demonstrates the Perl module's <quote>pseudoVar</quote> method. It may be used to
  147. retrieve the values of current pseudo variables.
  148. </para>
  149. <para>
  150. You might notice that there is no particular function for setting pseudo variables; you may use
  151. the exported functions from the avpops module, though.
  152. </para>
  153. </section>
  154. </section>
  155. </section>
  156. <!-- section>
  157. <title>Other examples</title>
  158. <para>
  159. <programlisting format="linespecific">
  160. ...
  161. if (perl_exec_msg("ldap_alias")) {
  162. route[1];
  163. }
  164. ...
  165. </programlisting>
  166. </para>
  167. </section -->
  168. </chapter>