1Presence Module
2
3Anca-Maria Vamanu
4
5   Voice Sistem SRL
6
7Juha Heinanen
8
9   TutPro Inc.
10
11Edited by
12
13Anca-Maria Vamanu
14
15Juha Heinanen
16
17   Copyright © 2006 Voice Sistem SRL
18
19   Copyright © 2009 Juha Heinanen
20     __________________________________________________________________
21
22   Table of Contents
23
24   1. Admin Guide
25
26        1. Overview
27        2. Dependencies
28
29              2.1. Kamailio Modules
30              2.2. External Libraries or Applications
31
32        3. Parameters
33
34              3.1. db_url(str)
35              3.2. presentity_table(str)
36              3.3. active_watchers_table(str)
37              3.4. watchers_table(str)
38              3.5. clean_period (int)
39              3.6. cseq_offset (int)
40              3.7. db_update_period (int)
41              3.8. waitn_time (int)
42              3.9. notifier_poll_rate (int)
43              3.10. notifier_processes (int)
44              3.11. force_delete (int)
45              3.12. startup_mode (int)
46              3.13. expires_offset (int)
47              3.14. max_expires (int)
48              3.15. min_expires (int)
49              3.16. min_expires_action (int)
50              3.17. server_address (str)
51              3.18. subs_db_mode (int)
52              3.19. publ_cache (int)
53              3.20. subs_htable_size (int)
54              3.21. pres_htable_size (int)
55              3.22. send_fast_notify (int)
56              3.23. enable_sphere_check (int)
57              3.24. timeout_rm_subs (int)
58              3.25. fetch_rows (integer)
59              3.26. db_table_lock_type (integer)
60              3.27. local_log_level (int)
61              3.28. local_log_facility (int)
62              3.29. subs_remove_match (int)
63              3.30. xavp_cfg (str)
64              3.31. retrieve_order (int)
65              3.32. retrieve_order_by (str)
66              3.33. sip_uri_match (int)
67              3.34. enable_dmq (integer)
68              3.35. pres_subs_mode (integer)
69              3.36. delete_same_subs (integer)
70              3.37. timer_mode (integer)
71
72        4. Functions
73
74              4.1. handle_publish([sender_uri])
75              4.2. handle_subscribe([watcher_uri])
76              4.3. pres_auth_status(watcher_uri, presentity_uri)
77              4.4. pres_has_subscribers(presentity_uri, event)
78              4.5. pres_refresh_watchers(uri, event, type[, file_uri,
79                      filename])
80
81              4.6. pres_update_watchers(uri, event)
82
83        5. RPC Commands
84
85              5.1. presence.cleanup
86              5.2. presence.refreshWatchers
87              5.3. presence.updateWatchers
88              5.4. presence.presentity_list
89
90        6. Exported Variables
91
92              6.1. $subs(attr)
93              6.2. $notify_reply(attr)
94
95        7. Events
96
97              7.1. presence:notify-reply
98
99        8. Installation
100
101   2. Developer Guide
102
103        1. bind_presence(presence_api_t* api)
104        2. add_event
105        3. get_rules_doc
106        4. get_auth_status
107        5. apply_auth_nbody
108        6. agg_nbody
109        7. free_body
110        8. aux_body_processing
111        9. aux_free_body
112        10. evs_publ_handl
113        11. evs_subs_handl
114        12. contains_event
115        13. get_event_list
116        14. update_watchers_status
117        15. get_sphere
118        16. get_presentity
119        17. free_presentity
120
121   List of Examples
122
123   1.1. Set db_url parameter
124   1.2. Set presentity_table parameter
125   1.3. Set active_watchers_table parameter
126   1.4. Set watchers_table parameter
127   1.5. Set clean_period parameter
128   1.6. Set cseq_offset parameter
129   1.7. Set db_update_period parameter
130   1.8. Set waitn_time parameter
131   1.9. Set notifier_poll_rate parameter
132   1.10. Set notifier_processes parameter
133   1.11. Set force_delete parameter
134   1.12. Set startup_mode parameter
135   1.13. Set expires_offset parameter
136   1.14. Set max_expires parameter
137   1.15. Set min_expires parameter
138   1.16. Set min_expires parameter
139   1.17. Set server_address parameter
140   1.18. Set subs_db_mode parameter
141   1.19. Set publ_cache parameter
142   1.20. Set subs_htable_size parameter
143   1.21. Set pres_htable_size parameter
144   1.22. Set send_fast_notify parameter
145   1.23. Set enable_sphere_check parameter
146   1.24. Set timeout_rm_subs parameter
147   1.25. Set fetch_rows parameter
148   1.26. Set db_table_lock_type parameter
149   1.27. Set local_log_level parameter
150   1.28. Set local_log_facility parameter
151   1.29. Set subs_remove_match parameter
152   1.30. Set xavp_cfg parameter
153   1.31. Set retrieve_order parameter
154   1.32. Set retrieve_order_by parameter
155   1.33. Set sip_uri_match parameter
156   1.34. Set enable_dmq parameter
157   1.35. Set pres_subs_mode parameter
158   1.36. Set delete_same_subs parameter
159   1.37. Set timer_mode parameter
160   1.38. handle_publish usage
161   1.39. handle_subscribe usage
162   1.40. pres_auth_status usage
163   1.41. pres_has_subscribers usage
164   1.42. pres_refresh_watchers usage
165   1.43. pres_update_watchers usage
166   1.44. $subs(name) usage
167   1.45. $notify_reply(name) usage
168   1.46. $notify_reply(name) usage
169   2.1. presence_api_t structure
170
171Chapter 1. Admin Guide
172
173   Table of Contents
174
175   1. Overview
176   2. Dependencies
177
178        2.1. Kamailio Modules
179        2.2. External Libraries or Applications
180
181   3. Parameters
182
183        3.1. db_url(str)
184        3.2. presentity_table(str)
185        3.3. active_watchers_table(str)
186        3.4. watchers_table(str)
187        3.5. clean_period (int)
188        3.6. cseq_offset (int)
189        3.7. db_update_period (int)
190        3.8. waitn_time (int)
191        3.9. notifier_poll_rate (int)
192        3.10. notifier_processes (int)
193        3.11. force_delete (int)
194        3.12. startup_mode (int)
195        3.13. expires_offset (int)
196        3.14. max_expires (int)
197        3.15. min_expires (int)
198        3.16. min_expires_action (int)
199        3.17. server_address (str)
200        3.18. subs_db_mode (int)
201        3.19. publ_cache (int)
202        3.20. subs_htable_size (int)
203        3.21. pres_htable_size (int)
204        3.22. send_fast_notify (int)
205        3.23. enable_sphere_check (int)
206        3.24. timeout_rm_subs (int)
207        3.25. fetch_rows (integer)
208        3.26. db_table_lock_type (integer)
209        3.27. local_log_level (int)
210        3.28. local_log_facility (int)
211        3.29. subs_remove_match (int)
212        3.30. xavp_cfg (str)
213        3.31. retrieve_order (int)
214        3.32. retrieve_order_by (str)
215        3.33. sip_uri_match (int)
216        3.34. enable_dmq (integer)
217        3.35. pres_subs_mode (integer)
218        3.36. delete_same_subs (integer)
219        3.37. timer_mode (integer)
220
221   4. Functions
222
223        4.1. handle_publish([sender_uri])
224        4.2. handle_subscribe([watcher_uri])
225        4.3. pres_auth_status(watcher_uri, presentity_uri)
226        4.4. pres_has_subscribers(presentity_uri, event)
227        4.5. pres_refresh_watchers(uri, event, type[, file_uri, filename])
228
229        4.6. pres_update_watchers(uri, event)
230
231   5. RPC Commands
232
233        5.1. presence.cleanup
234        5.2. presence.refreshWatchers
235        5.3. presence.updateWatchers
236        5.4. presence.presentity_list
237
238   6. Exported Variables
239
240        6.1. $subs(attr)
241        6.2. $notify_reply(attr)
242
243   7. Events
244
245        7.1. presence:notify-reply
246
247   8. Installation
248
2491. Overview
250
251   The Presence module implements the core functionality of SIP event
252   notification. It handles PUBLISH and SUBSCRIBE messages and generates
253   NOTIFY messages in a general, event independent way. It is extensible
254   and allows registering events to it from other Kamailio modules.
255   Supported SIP event packages are presence, presence.winfo, dialog;sla
256   from the presence_xml module and message-summary from the presence_mwi
257   module.
258
259   The module can use database and memory storage (to improve
260   performance). For subscriptions it supports the 4 storage modes: Memory
261   Only, Write Back, Write Through and DB Only. For publishes, it stores
262   the state documents in database only(because of the large size) and it
263   can store a publish cache in memory to avoid unnecessary database
264   queries. Read the subs_db_mode and publ_cache parameter sections to
265   decide which is the best storage configuration for you.
266
267   The module implements several API functions, that can be used by other
268   modules. In fact, it can be used only as a resource module, or
269   "library". This mode of operation is enabled if the db_url parameter is
270   not set to any value.
271
272   The Kamailio Presence module implements the specifications in: RFC3265,
273   RFC3856, RFC3857, RFC3858.
274
2752. Dependencies
276
277   2.1. Kamailio Modules
278   2.2. External Libraries or Applications
279
2802.1. Kamailio Modules
281
282   The following modules must be loaded before this module:
283     * a database module.
284     * sl.
285     * tm.
286     * dmq (only if replication is enabled).
287
2882.2. External Libraries or Applications
289
290     * libxml.
291
2923. Parameters
293
294   3.1. db_url(str)
295   3.2. presentity_table(str)
296   3.3. active_watchers_table(str)
297   3.4. watchers_table(str)
298   3.5. clean_period (int)
299   3.6. cseq_offset (int)
300   3.7. db_update_period (int)
301   3.8. waitn_time (int)
302   3.9. notifier_poll_rate (int)
303   3.10. notifier_processes (int)
304   3.11. force_delete (int)
305   3.12. startup_mode (int)
306   3.13. expires_offset (int)
307   3.14. max_expires (int)
308   3.15. min_expires (int)
309   3.16. min_expires_action (int)
310   3.17. server_address (str)
311   3.18. subs_db_mode (int)
312   3.19. publ_cache (int)
313   3.20. subs_htable_size (int)
314   3.21. pres_htable_size (int)
315   3.22. send_fast_notify (int)
316   3.23. enable_sphere_check (int)
317   3.24. timeout_rm_subs (int)
318   3.25. fetch_rows (integer)
319   3.26. db_table_lock_type (integer)
320   3.27. local_log_level (int)
321   3.28. local_log_facility (int)
322   3.29. subs_remove_match (int)
323   3.30. xavp_cfg (str)
324   3.31. retrieve_order (int)
325   3.32. retrieve_order_by (str)
326   3.33. sip_uri_match (int)
327   3.34. enable_dmq (integer)
328   3.35. pres_subs_mode (integer)
329   3.36. delete_same_subs (integer)
330   3.37. timer_mode (integer)
331
3323.1. db_url(str)
333
334   The database url.
335
336   If set, the module is a fully operational presence server. Otherwise,
337   it is used as a 'library', for its exported functions.
338
339   Default value is “NULL”.
340
341   Example 1.1. Set db_url parameter
342...
343modparam("presence", "db_url",
344        "mysql://kamailio:kamailiorw@localhost/kamailio")
345...
346
3473.2. presentity_table(str)
348
349   The name of the db table where PUBLISH presence information is stored.
350
351   Default value is “presentity”.
352
353   Example 1.2. Set presentity_table parameter
354...
355modparam("presence", "presentity_table", "presentity")
356...
357
3583.3. active_watchers_table(str)
359
360   The name of the db table where active subscription information is
361   stored.
362
363   Default value is “active_watchers”.
364
365   Example 1.3. Set active_watchers_table parameter
366...
367modparam("presence", "active_watchers_table", "active_watchers")
368...
369
3703.4. watchers_table(str)
371
372   The name of the db table where subscription states are stored.
373
374   Default value is “watchers”.
375
376   Example 1.4. Set watchers_table parameter
377...
378modparam("presence", "watchers_table", "watchers")
379...
380
3813.5. clean_period (int)
382
383   The period in seconds between checks if there are expired messages
384   stored in database.
385
386   Default value is “100”. A zero or negative value disables this
387   activity.
388
389   Example 1.5. Set clean_period parameter
390...
391modparam("presence", "clean_period", 100)
392...
393
3943.6. cseq_offset (int)
395
396   The allowed offset between server and client cseq.
397
398   Default value is “0”.
399
400   Example 1.6. Set cseq_offset parameter
401...
402modparam("presence", "cseq_offset", 1)
403...
404
4053.7. db_update_period (int)
406
407   The period at which to synchronize cached subscriber info with the
408   database.
409
410   Default value is “100”. A zero or negative value disables
411   synchronization.
412
413   Example 1.7. Set db_update_period parameter
414...
415modparam("presence", "db_update_period", 100)
416...
417
4183.8. waitn_time (int)
419
420   The maximum time period that NOTIFY requests will be buffered for. The
421   server will attempt to send NOTIFY requests within many seconds of a
422   change occurring.
423
424   Note: this parameter is only used when notifier_processes is greater
425   than 0. When notifier_processes is less than or equal to 0 NOTIFY
426   requests are sent immediately.
427
428   Default value is “5”.
429
430   Example 1.8. Set waitn_time parameter
431...
432modparam("presence", "waitn_time", 10)
433...
434
4353.9. notifier_poll_rate (int)
436
437   The number of times per second that the notifier processes should check
438   for work. Approximately 1/(waitn_time * notifier_poll_rate *
439   notifier_processes) of the pending updates will be sent each time a
440   notifier process runs.
441
442   Separate notifier processes are only run when subs_db_mode is 3 (DB
443   only mode).
444
445   Default value is “10”.
446
447   Example 1.9. Set notifier_poll_rate parameter
448...
449modparam("presence", "notifier_poll_rate", 20)
450...
451
4523.10. notifier_processes (int)
453
454   The number of notifier processes that should be started.
455
456   Separate notifier processes are only run when subs_db_mode is 3 (DB
457   only mode).
458
459   Note: setting this parameter to 0 when subs_db_mode is 3 keeps the old
460   behaviour (sending NOTIFY requests immediately). This (old) behaviour
461   is disabled by default in DB only mode because under load, when lots of
462   NOTIFY requests can be sent on a dialog at the same time, there are
463   race conditions which result in CSeq re-use.
464
465   Default value is “1”.
466
467   Example 1.10. Set notifier_processes parameter
468...
469modparam("presence", "notifier_processes", 2)
470...
471
4723.11. force_delete (int)
473
474   Enabling this parameter will delete expired presentity records without
475   updating watchers.
476
477   Set this parameter to “1” to enable.
478
479   Default value is “0”.
480
481   Example 1.11. Set force_delete parameter
482...
483modparam("presence", "force_delete", 1)
484...
485
4863.12. startup_mode (int)
487
488   Setting this parameter to 0 will provide startup related backward
489   compatibility for some modules. Setting to 0 fixes presentity requests
490   with low expires (e.g. time() + 1)
491
492   Set this parameter to “0” to enable backward compatibility.
493
494   Default value is “1”.
495
496   Example 1.12. Set startup_mode parameter
497...
498modparam("presence", "startup_mode", 0)
499...
500
5013.13. expires_offset (int)
502
503   The value in seconds that should be subtracted from the expires value
504   when sending a 200OK for a publish. It is used for forcing the client
505   to send an update before the old publish expires.
506
507   Default value is “0”.
508
509   Example 1.13. Set expires_offset parameter
510...
511modparam("presence", "expires_offset", 10)
512...
513
5143.14. max_expires (int)
515
516   The maximum admissible expires value for PUBLISH/SUBSCRIBE message (in
517   seconds).
518
519   Default value is “3600”.
520
521   Example 1.14. Set max_expires parameter
522...
523modparam("presence", "max_expires", 3600)
524...
525
5263.15. min_expires (int)
527
528   The minimum admissible expires value for PUBLISH/SUBSCRIBE message (in
529   seconds).
530
531   If > 0 then min_expires_action parameter determines the response.
532
533   Default value is “0”.
534
535   Example 1.15. Set min_expires parameter
536...
537modparam("presence", "min_expires", 1800)
538...
539
5403.16. min_expires_action (int)
541
542   The action to take when UA sends a expires value less then min_expires.
543
544   Possible Values
545     * 1 : RFC Compliant, returns “423 Interval Too Brief”
546     * 2 : forces the min_expires value in the subscription
547
548   If > 0 then min_expires_action parameter determines the response.
549
550   Default value is “1”.
551
552   Example 1.16. Set min_expires parameter
553            ...
554            modparam("presence", "min_expires", 1800)
555            ...
556
5573.17. server_address (str)
558
559   The presence server address which will become the value of Contact
560   header filed for 200 OK replies to SUBSCRIBE and PUBLISH and in NOTIFY
561   messages.
562
563   Example 1.17. Set server_address parameter
564...
565modparam("presence", "server_address", "sip:10.10.10.10:5060")
566...
567
5683.18. subs_db_mode (int)
569
570   The presence module can utilize database for persistent subscription
571   storage. If you use database, your subscriptions will survive machine
572   restarts or SW crashes. The disadvantage is that accessing database can
573   be time consuming. Therefore, presence module implements four database
574   accessing modes:
575     * 0 - This disables database completely. Only memory will be used.
576       Subscriptions will not survive restart. Use this value if you need
577       a really fast presence module and subscription persistence is not
578       necessary or is provided by other means.
579     * 1 - Write-Through scheme. Subscriptions are updated synchronously
580       in database and in memory(used for read operations). Use this
581       scheme if speed is not top priority, but it's important that no
582       subscriptions will be lost during crash or reboot or if you have an
583       external application that reads the state of the subscriptions from
584       database and they need to be updated synchronously.
585     * 2 - Write-Back scheme. This is a combination of previous two
586       schemes. All changes are made to memory and database
587       synchronization is done in the timer. The timer deletes all expired
588       contacts and flushes all modified or new subscriptions to database.
589       Use this scheme if you encounter high-load peaks and want them to
590       process as fast as possible. Latency of this mode is much lower
591       than latency of mode 1, but slightly higher than latency of mode 0.
592       To control the interval at which data is flushed to database, set
593       the db_update_period parameter.
594     * 3 - DB-Only scheme. No memory cache is kept, all operations being
595       directly performed with the database. The timer deletes all expired
596       subscriptions from database. The mode is useful if you configure
597       more servers sharing the same DB without any replication at SIP
598       level. The mode may be slower due the high number of DB operation.
599
600   Default value is 2 (Write-Back scheme).
601
602   Example 1.18. Set subs_db_mode parameter
603...
604modparam("presence", "subs_db_mode", 1)
605...
606
6073.19. publ_cache (int)
608
609   To improve performance, the presence module can operate in a couple of
610   modes related to how PUBLISH data is stored. If publ_cache is 0, then
611   no information is store in memory.
612
613   If publ_cache is 1, then the module keeps in memory an index of the
614   records stored in database, In this mode it keeps only the list of URIs
615   and events, so it does not use much memory. The cache is used when a
616   Subscription is received to check if there is any published state in
617   database. This way unnecessary queries in presentity table are avoided.
618
619   If publ_cache is 2, then the module keeps everything related to PUBLISH
620   requests in memory, not storing anything in the database.
621
622   Setting this parameter to 0 will disable the usage of the publish
623   cache. This is desirable when you have more servers sharing the same
624   database or there are other external entities inserting data into the
625   presentity table.
626
627   Default value is “1”.
628
629   Example 1.19. Set publ_cache parameter
630...
631modparam("presence", "publ_cache", 0)
632...
633
6343.20. subs_htable_size (int)
635
636   The size of the in-memory hash table to store subscription dialogs.
637   This parameter will be used as the power of 2 when computing table
638   size.
639
640   Default value is “9 (512)”.
641
642   Example 1.20. Set subs_htable_size parameter
643...
644modparam("presence", "subs_htable_size", 11)
645...
646
6473.21. pres_htable_size (int)
648
649   The size of the in-memory hash table to store publish records. This
650   parameter will be used as the power of 2 when computing table size.
651
652   Default value is “9 (512)”.
653
654   Example 1.21. Set pres_htable_size parameter
655...
656modparam("presence", "pres_htable_size", 11)
657...
658
6593.22. send_fast_notify (int)
660
661   This parameter enables or disables the sending of an initial empty
662   NOTIFY after a SUBSCRIBE/reSUBSCRIBE. This caused problems for MWI
663   application, because some CPEs (like Samsung) fail to understand an
664   empty NOTIFY to an message-summary event. This parameter is enabled by
665   default, thus adhering to the standard.
666
667   Default value is “1 ”.
668
669   Example 1.22. Set send_fast_notify parameter
670...
671modparam("presence", "send_fast_notify", 0)
672...
673
6743.23. enable_sphere_check (int)
675
676   This parameter is a flag that should be set if permission rules include
677   sphere checking. The sphere information is expected to be present in
678   the RPID body published by the presentity. The flag is introduced as
679   this check requires extra processing that should be avoided if this
680   feature is not supported by the clients.
681
682   Default value is “0 ”.
683
684   Example 1.23. Set enable_sphere_check parameter
685...
686modparam("presence", "enable_sphere_check", 1)
687...
688
6893.24. timeout_rm_subs (int)
690
691   This parameter is a flag that should be set if subscriptions should be
692   removed from the active_watchers when a NOTIFY times out. RFC3265
693   section 3.2.2 defines this behaviour as a SHOULD, so by default it is
694   on. Disabling this will keep subscriptions active on unreliable
695   networks.
696
697   Default value is “1”.
698
699   Example 1.24. Set timeout_rm_subs parameter
700...
701modparam("presence", "timeout_rm_subs", 0)
702...
703
7043.25. fetch_rows (integer)
705
706   Number of rows to be loaded in one step from database.
707
708   Default value is 500.
709
710   Example 1.25. Set fetch_rows parameter
711...
712modparam("presence", "fetch_rows", 1000)
713...
714
7153.26. db_table_lock_type (integer)
716
717   Enable (=1) or disable (=0) the Locks for table during an transaction.
718   Locking only the "current" table causes problems with a MySQL-Databases
719   in "DB-Only" mode.
720
721   In order to use the Presence-Module in "DB_ONLY"-mode with a
722   MySQL-Backend, set this parameter to "0", otherwise the
723   MySQL-Operations will fail. The Presence-Module will generate a "500
724   Server error" due to the failed MySQL-queries.
725
726   Default value is 1 (Write Lock for the Tables).
727
728   Example 1.26. Set db_table_lock_type parameter
729...
730modparam("presence", "db_table_lock_type", 0)
731...
732
7333.27. local_log_level (int)
734
735   Control log level for some debug messages inside the module.
736
737   Default value is 2 (L_INFO).
738
739   Example 1.27. Set local_log_level parameter
740...
741modparam("presence", "local_log_level", 3)
742...
743
7443.28. local_log_facility (int)
745
746   Control syslog facility for some debug messages inside the module.
747
748   Default value is taken from the core log_facility configuration
749   parameter.
750
751   Example 1.28. Set local_log_facility parameter
752...
753modparam("presence", "local_log_facility", "LOG_LOCAL3")
754...
755
7563.29. subs_remove_match (int)
757
758   Control how to match the subscriptions to remove from memory. If set to
759   0, then the match is done on To-Tag (local generated), if set to 1,
760   then the match is done on all dialog attributes (Call-Id, From-Tag,
761   To-Tag).
762
763   Default value is 0.
764
765   Example 1.29. Set subs_remove_match parameter
766...
767modparam("presence", "subs_remove_match", 1)
768...
769
7703.30. xavp_cfg (str)
771
772   The name of the xavp to be used to specify attributes for internal
773   processing of presence module.
774
775   Inner attributes inside xavp can be:
776     * priority - integer value to set the priority of the presence
777       document (higher value, higher priority). It can set the order of
778       the aggregated presence documents sent by NOTIFY (first the
779       document with higher priority). If xavp_cfg parameter is set but
780       this attribute is not in the avp, the priority of the presence
781       document is based on timestamp, so newer documents have higher
782       priority.
783     * delete_subscription - integer value to give extra control of
784       deleting the subscription after processing of
785       event_route[presence:notify-reply]. If value = 1, it deletes the
786       subscription. If xavp_cfg parameter is set but this attribute is
787       not in the avp, the subscription is not deleted. this does not
788       apply for codes 404, 481 and 408 (when timeout_rm_subs = 1) where
789       subscription is deleted.
790
791   Default value is empty (not set).
792
793   Example 1.30. Set xavp_cfg parameter
794...
795modparam("presence", "xavp_cfg", "pres")
796...
797if(is_method("PUBLISH")) {
798    $xavp(pres=>priority) = 100;
799}
800...
801
8023.31. retrieve_order (int)
803
804   If set to 0, presentity records are retrieve by received_time order. If
805   set to 1, presentity records are retrieve by the value of
806   retrieve_order_by parameter.
807
808   Default value is 0.
809
810   Example 1.31. Set retrieve_order parameter
811...
812modparam("presence", "retrieve_order", 1)
813...
814
8153.32. retrieve_order_by (str)
816
817   Used to set the order-by of the db query for fetching the presence
818   records when retrieve_order is set to 1.
819
820   Default value is “priority”.
821
822   Example 1.32. Set retrieve_order_by parameter
823...
824modparam("presence", "retrieve_order_by", "priority, received_time")
825...
826
8273.33. sip_uri_match (int)
828
829   The mode used when comparing uris.
830
831   Possible Values
832     * 0 : case sensitive
833     * 1 : case insensitive
834
835   Default value is “0”.
836
837   Example 1.33. Set sip_uri_match parameter
838...
839modparam("presence", "sip_uri_match", 1)
840...
841
8423.34. enable_dmq (integer)
843
844   If set to 1, will enable DMQ replication of presentities between nodes.
845   Use this instead of a shared DB to share state across a cluster and
846   update local watchers in realtime (subs_db_mode < 3) or on next
847   notifier run (subs_db_mode = 3).
848
849   If this parameter is enabled, the DMQ module must be loaded first -
850   otherwise, startup will fail.
851
852   Default value is 0.
853
854   Example 1.34. Set enable_dmq parameter
855...
856modparam("presence", "enable_dmq", 1)
857...
858
8593.35. pres_subs_mode (integer)
860
861   Allow disabling cloning subscription structure for pv $subs(...),
862   saving the pkg memory and copy operations for all its fields. If 1 the
863   cloning is done; if 0, no cloning and $subs(...) returns $null.
864
865   Default value is 1.
866
867   Example 1.35. Set pres_subs_mode parameter
868...
869modparam("presence", "pres_subs_mode", 0)
870...
871
8723.36. delete_same_subs (integer)
873
874   Enable deleting of subscriptions with the same presence uri and callid.
875
876   Default value is 0 (disabled behavior).
877
878   Example 1.36. Set delete_same_subs parameter
879...
880modparam("presence", "delete_same_subs", 1)
881...
882
8833.37. timer_mode (integer)
884
885   Specify what timer process to be used. If set to 0, the core main timer
886   is used. If set to 1, the core secondary timer is used.
887
888   Default value is 1.
889
890   Example 1.37. Set timer_mode parameter
891...
892modparam("presence", "timer_mode", 0)
893...
894
8954. Functions
896
897   4.1. handle_publish([sender_uri])
898   4.2. handle_subscribe([watcher_uri])
899   4.3. pres_auth_status(watcher_uri, presentity_uri)
900   4.4. pres_has_subscribers(presentity_uri, event)
901   4.5. pres_refresh_watchers(uri, event, type[, file_uri, filename])
902   4.6. pres_update_watchers(uri, event)
903
9044.1.  handle_publish([sender_uri])
905
906   Handles PUBLISH requests by storing and updating published information
907   in memory cache and database, then calls functions to send NOTIFY
908   messages when changes in the published information occur. It takes one
909   argument -> sender_uri. The parameter was added for enabling BLA
910   implementation. If present, notification of a change in published state
911   is not sent to the respective uri even though a subscription exists. It
912   should be taken from the Sender header. It was left at the decision of
913   the administrator whether or not to transmit the content of this header
914   as parameter for handle_publish, to prevent security problems.
915
916   This function can be used from REQUEST_ROUTE.
917
918   Return code:
919     * 1 - if success.
920     * -1 - if error.
921
922   The module sends an appropriate stateless reply in all cases.
923
924   Example 1.38. handle_publish usage
925...
926        if(is_method("PUBLISH"))
927        {
928                if($hdr(Sender)!= NULL)
929                        handle_publish("$hdr(Sender)");
930                else
931                        handle_publish();
932                t_release();
933        }
934...
935
9364.2.  handle_subscribe([watcher_uri])
937
938   The function which handles SUBSCRIBE requests. It stores or updates
939   information in memory and database and calls functions to send NOTIFY
940   messages when a SUBSCRIBE which initiate a dialog is received.
941
942   By default this function uses the From: URI from the SUBSCRIBE request
943   as the Watcher URI. The optional watcher_uri parameter can be used to
944   specify a different Watcher URI, possibly taken from a SIP header like
945   P-Asserted-Identity:.
946
947   This function can be used from REQUEST_ROUTE.
948
949   Return code:
950     * 1 - if success.
951     * -1 - if error.
952
953   The module sends an appropriate stateless reply in all cases.
954
955   Example 1.39. handle_subscribe usage
956...
957if(method=="SUBSCRIBE")
958    handle_subscribe();
959...
960
9614.3.  pres_auth_status(watcher_uri, presentity_uri)
962
963   The function checks if watcher URI is authorized to subscribe event
964   'presence' of presentity URI. Both watcher_uri and presentity_uri can
965   be static strings or contain pseudo variables.
966
967   The function returns ACTIVE_STATUS, if subscription is allowed, and
968   PENDING_STATUS, TERMINATED_STATUS, or WAITING_STATUS otherwise. See
969   presence/subscribe.h for the corresponding integer codes. In case of
970   error, function returns -1.
971
972   This function can be used from REQUEST_ROUTE.
973
974   Example 1.40. pres_auth_status usage
975...
976if (method=="MESSAGE") {
977    pres_auth_status("$fu", $ru");
978    if ($retcode == 1) {
979        t_relay();
980    } else {
981        send_reply("403", "Forbidden");
982    }
983}
984...
985
9864.4.  pres_has_subscribers(presentity_uri, event)
987
988   Allows to check if presentity has any subscribers of event.
989
990   This function can be used from ANY_ROUTE.
991
992   Example 1.41. pres_has_subscribers usage
993...
994if(pres_has_subscribers($var(uri), "message-summary"))
995    # do something...;
996...
997
9984.5.  pres_refresh_watchers(uri, event, type[, file_uri, filename])
999
1000   The function can be used in configuration to trigger notifies to
1001   watchers if a change in watchers authorization or in published state
1002   occurred (i.e., updates of xcap documents).
1003
1004   Parameters:
1005     * uri - the uri of the user who made the change and whose watchers
1006       should be informed.
1007     * event - the event package.
1008     * type - it distinguishes between the three different types of events
1009       that can trigger the refresh, depending on its value:
1010          + 0 - a change in watchers authentication.
1011          + 1 - a statical update in published state through direct update
1012            in db table.
1013          + 2 - a statical update in published state by modifying the pidf
1014            manipulation document.
1015     * file_uri - the uri of the pidf-manipulation file on the XCAP server
1016       (only used for type 2).
1017     * filename - the name of the pidf-manipulation file on the XCAP
1018       server (only used for type 2).
1019
1020   This function can be used from ANY_ROUTE.
1021
1022   Example 1.42. pres_refresh_watchers usage
1023...
1024pres_refresh_watchers("sip:test@kamailio.org", "presence", 1);
1025...
1026
10274.6.  pres_update_watchers(uri, event)
1028
1029   The function can be used in configuration to triger updates to watchers
1030   status if a change in watchers authorization state occurred (i.e.,
1031   updates of xcap documents change state from pending to active).
1032
1033   Parameters:
1034     * uri - the uri of the user who made the change and whose watchers
1035       should be informed. Can be PV.
1036     * event - the event package (e.g., presence).
1037
1038   This function can be used from ANY_ROUTE.
1039
1040   Example 1.43. pres_update_watchers usage
1041...
1042pres_update_watchers("sip:test@kamailio.org", "presence");
1043...
1044
10455. RPC Commands
1046
1047   5.1. presence.cleanup
1048   5.2. presence.refreshWatchers
1049   5.3. presence.updateWatchers
1050   5.4. presence.presentity_list
1051
10525.1. presence.cleanup
1053
1054   Manually triggers the cleanup functions for the active_watchers,
1055   presentity, and watchers tables. Useful if you have set clean_period
1056   and/or db_update_period to zero or less.
1057
1058   Name: presence.cleanup
1059
1060   Parameters: none
1061
1062   RPC Command Format:
1063...
1064kamcmd presence.cleanup
1065...
1066
10675.2. presence.refreshWatchers
1068
1069   Triggers sending Notify messages to watchers if a change in watchers
1070   authorization or in published state occurred.
1071
1072   Name: presence.refreshWatchers
1073
1074   Parameters:
1075     * uri - the uri of the user who made the change and whose watchers
1076       should be informed
1077     * event - the event package.
1078     * type - it distinguishes between the three different types of events
1079       that can trigger the refresh, depending on its value:
1080          + 0 - a change in watchers authentication.
1081          + 1 - a statical update in published state through direct update
1082            in db table.
1083          + 2 - a statical update in published state by modifying the pidf
1084            manipulation document.
1085     * file_uri - the uri of the pidf-manipulation file on the XCAP server
1086       (only used for type 2).
1087     * filename - the name of the pidf-manipulation file on the XCAP
1088       server (only used for type 2).
1089
1090   RPC Command Format:
1091...
1092kamcmd presence.refreshWatchers sip:test@kamailio.org presence 1
1093...
1094
10955.3. presence.updateWatchers
1096
1097   Manually triggers updates to watchers (for example, after the contents
1098   of XCAP docs has been updated).
1099
1100   Name: presence.updateWatchers
1101
1102   Parameters:
1103     * uri - the uri of the user who made the changes and whose watchers
1104       should be updated.
1105     * event - the event package (e.g. presence).
1106
1107   RPC Command Format:
1108...
1109kamcmd presence.updateWatchers sip:alice@domain.com presence
1110...
1111
11125.4. presence.presentity_list
1113
1114   List the presentity records stored in memory.
1115
1116   Name: presence.presentity_list
1117
1118   Parameters:
1119     * mode - (optional) it can be 'full' to print all the fields of
1120       presentity record. If missine, only a selected set of fields are
1121       printed in the response.
1122
1123   RPC Command Format:
1124...
1125kamctl rpc presence.presentity_list
1126kamctl rpc presence.presentity_list full
1127...
1128
11296. Exported Variables
1130
1131   6.1. $subs(attr)
1132   6.2. $notify_reply(attr)
1133
11346.1. $subs(attr)
1135
1136   Access the attributes of handled subscription. It must be used after a
1137   successful call of “handle_subscription()” or in the following events.
1138     * tm:local-request - before notify is sent
1139     * presence:notify-reply - after notify is sent
1140
1141   The “attr” can be:
1142     * uri - subscription presentity uri
1143     * pres_uri - alias for presentity uri
1144     * to_user
1145     * to_domain
1146     * from_user
1147     * from_domain
1148     * watcher_username
1149     * watcher_domain
1150     * event
1151     * event_id
1152     * to_tag
1153     * from_tag
1154     * callid
1155     * remote_cseq
1156     * local_cseq
1157     * contact
1158     * local_contact
1159     * record_route
1160     * expires
1161     * status
1162     * reason
1163     * version
1164     * flags
1165     * user_agent
1166
1167   Example 1.44. $subs(name) usage
1168...
1169if(handle_subscription())
1170{
1171  xlog("presentity=$subs(uri)\n");
1172}
1173...
1174
11756.2. $notify_reply(attr)
1176
1177   Access the reply message received when notifying subscriber. It must be
1178   used in the following events.
1179     * presence:notify-reply - after notify is sent
1180
1181   The “attr” can be any pseudo var that accesses attributes of msg
1182
1183   Example 1.45. $notify_reply(name) usage
1184...
1185event_route[presence:notify-reply]
1186{
1187  xlog("received message = $notify_reply($mb)\n");
1188}
1189...
1190
11917. Events
1192
1193   7.1. presence:notify-reply
1194
11957.1. presence:notify-reply
1196
1197   Fired after notify reply is received or timeout.
1198
1199   Example 1.46. $notify_reply(name) usage
1200...
1201event_route[presence:notify-reply]
1202{
1203  xlog("received message = $notify_reply($mb)\n");
1204}
1205...
1206
12078. Installation
1208
1209   The module requires 3 tables in the Kamailio database: "presentity",
1210   "active_watchers" and "watchers". The SQL syntax to create them can be
1211   found in presence-create.sql script in the database directories in the
1212   kamailio/scripts folder. You can also find the complete database
1213   documentation on the project webpage,
1214   https://www.kamailio.org/docs/db-tables/kamailio-db-devel.html.
1215
1216Chapter 2. Developer Guide
1217
1218   Table of Contents
1219
1220   1. bind_presence(presence_api_t* api)
1221   2. add_event
1222   3. get_rules_doc
1223   4. get_auth_status
1224   5. apply_auth_nbody
1225   6. agg_nbody
1226   7. free_body
1227   8. aux_body_processing
1228   9. aux_free_body
1229   10. evs_publ_handl
1230   11. evs_subs_handl
1231   12. contains_event
1232   13. get_event_list
1233   14. update_watchers_status
1234   15. get_sphere
1235   16. get_presentity
1236   17. free_presentity
1237
1238   The module provides the following functions that can be used in other
1239   Kamailio modules.
1240
12411.  bind_presence(presence_api_t* api)
1242
1243   This function binds the presence modules and fills the structure with
1244   one exported function -> add_event, which when called adds a new event
1245   to be handled by presence.
1246
1247   Example 2.1. presence_api_t structure
1248...
1249typedef struct presence_api {
1250        add_event_t add_event;
1251        contains_event_t contains_event;
1252        search_event_t search_event;
1253        get_event_list_t get_event_list;
1254
1255        update_watchers_t update_watchers_status;
1256
1257        /* subs hash table handling functions */
1258        new_shtable_t new_shtable;
1259        destroy_shtable_t destroy_shtable;
1260        insert_shtable_t insert_shtable;
1261        search_shtable_t search_shtable;
1262        delete_shtable_t delete_shtable;
1263        update_shtable_t update_shtable;
1264        /* function to duplicate a subs structure*/
1265        mem_copy_subs_t  mem_copy_subs;
1266        /* function used for update in database*/
1267        update_db_subs_t update_db_subs_timer;
1268        /* function to extract dialog information from a
1269        SUBSCRIBE message */
1270        extract_sdialog_info_t extract_sdialog_info;
1271        /* function to request sphere definition for a presentity */
1272        pres_get_sphere_t get_sphere;
1273
1274}presence_api_t;
1275...
1276
12772.  add_event
1278
1279   Field type:
1280...
1281typedef int (*add_event_t)(pres_ev_t* event);
1282...
1283
1284   This function receives as a parameter a structure with event specific
1285   information and adds it to presence event list.
1286
1287   The structure received as a parameter:
1288...
1289typedef struct pres_ev
1290{
1291        str name;
1292        event_t* evp;
1293        str content_type;
1294        int default_expires;
1295        int type;
1296        int etag_not_new;
1297        /*
1298         *  0 - the standard mechanism (allocating new etag
1299                        for each Publish)
1300         *  1 - allocating an etag only
1301                        for an initial Publish
1302        */
1303        int req_auth;
1304        get_rules_doc_t* get_rules_doc;
1305        apply_auth_t*  apply_auth_nbody;
1306        is_allowed_t*  get_auth_status;
1307
1308        /* an agg_body_t function should be registered
1309         * if the event permits having multiple published
1310         * states and requires an aggregation of the information
1311         * otherwise, this field should be NULL and the last
1312         * published state is taken when constructing Notify msg
1313         */
1314        agg_nbody_t* agg_nbody;
1315        publ_handling_t  * evs_publ_handl;
1316        subs_handling_t  * evs_subs_handl;
1317        free_body_t* free_body;
1318    /* sometimes it is necessary that a module make changes for a body for each
1319     * active watcher (e.g. setting the "version" parameter in an XML document.
1320     * If a module registers the aux_body_processing callback, it gets called fo
1321r
1322     * each watcher. It either gets the body received by the PUBLISH, or the bod
1323y
1324     * generated by the agg_nbody function.
1325     * The module can deceide if it makes a copy of the original body, which is
1326then
1327     * manipulated, or if it works directly in the original body. If the module
1328makes a
1329     * copy of the original body, it also has to register the aux_free_body() to
1330     * free this "per watcher" body.
1331     */
1332    aux_body_processing_t* aux_body_processing;
1333    free_body_t* aux_free_body;
1334        struct pres_ev* wipeer;
1335        struct pres_ev* next;
1336
1337}pres_ev_t;
1338...
1339
13403.  get_rules_doc
1341
1342   Filed type:
1343...
1344typedef int (get_rules_doc_t)(str* user, str* domain, str** rules_doc);
1345...
1346
1347   This function returns the authorization rules document that will be
1348   used in obtaining the status of the subscription and processing the
1349   notified body. A reference to the document should be put in the
1350   auth_rules_doc of the subs_t structure given as a parameter to the
1351   functions described below.
1352
13534.  get_auth_status
1354
1355   This filed is a function to be called for a subscription request to
1356   return the state for that subscription according to authorization
1357   rules. In the auth_rules_doc field of the subs_t structure received as
1358   a parameter should contain the rules document of the presentity in
1359   case, if it exists.
1360
1361   It is called only if the req_auth field is not 0.
1362
1363   Filed type:
1364...
1365typedef int (is_allowed_t)(struct subscription* subs);
1366...
1367
13685.  apply_auth_nbody
1369
1370   This parameter should be a function to be called for an event that
1371   requires authorization, when constructing final body. The authorization
1372   document is taken from the auth_rules_doc field of the subs_t structure
1373   given as a parameter. It is called only if the req_auth field is not 0.
1374
1375   Filed type:
1376...
1377typedef int (apply_auth_t)(str* , struct subscription*, str** );
1378...
1379
13806.  agg_nbody
1381
1382   If present, this field marks that the events requires aggregation of
1383   states. This function receives a body array and should return the final
1384   body. If not present, it is considered that the event does not require
1385   aggregation and the most recent published information is used when
1386   constructing Notifies.
1387
1388   Filed type:
1389...
1390typedef str* (agg_nbody_t)(str* pres_user, str* pres_domain,
1391str** body_array, int n, int off_index);
1392..
1393
13947.  free_body
1395
1396   This field must be field in if subsequent processing is performed on
1397   the info from database before being inserted in Notify message body(if
1398   agg_nbody or apply_auth_nbody fields are filled in). It should match
1399   the allocation function used when processing the body.
1400
1401   Filed type:
1402...
1403typedef void(free_body_t)(char* body);
1404..
1405
14068.  aux_body_processing
1407
1408   This field must be set if the module needs to manipulate the NOTIFY
1409   body for each watcher. E.g. if the XML body includes a 'version'
1410   parameter which will be increased for each NOTIFY, on a "per watcher"
1411   basis. The module can either allocate a new buffer for the new body an
1412   return it (aux_free_body function must be set too) or it manipulates
1413   the original body directly and returns NULL.
1414
1415   Filed type:
1416...
1417typedef str* (aux_body_processing_t)(struct subscription *subs, str* body);
1418..
1419
14209.  aux_free_body
1421
1422   This field must be set if the module registers the aux_body_processing
1423   function and allocates memory for the new modified body. Then, this
1424   function will be used to free the pointer returned by the
1425   aux_body_processing function. If the module does use the
1426   aux_body_processing, but does not allocate new memory, but manipulates
1427   directly the original body buffer, then the aux_body_processing must
1428   return NULL and this field should not be set.
1429
1430   Filed type:
1431...
1432typedef void(free_body_t)(char* body);
1433..
1434
143510.  evs_publ_handl
1436
1437   This function is called when handling Publish requests. Most contain
1438   body correctness check.
1439
1440...
1441typedef int (publ_handling_t)(struct sip_msg*);
1442..
1443
144411.  evs_subs_handl
1445
1446   It is not compulsory. Should contain event specific handling for
1447   Subscription requests.
1448
1449   Filed type:
1450...
1451typedef int (subs_handling_t)(struct sip_msg*);
1452..
1453
145412.  contains_event
1455
1456   Field type:
1457..
1458typedef pres_ev_t* (*contains_event_t)(str* name,
1459event_t* parsed_event);
1460...
1461
1462   The function parses the event name received as a parameter and searches
1463   the result in the list. It returns the found event or NULL, if not
1464   found. If the second argument is an allocated event_t* structure it
1465   fills it with the result of the parsing.
1466
146713.  get_event_list
1468
1469   Field type:
1470...
1471typedef int (*get_event_list_t) (str** ev_list);
1472...
1473
1474   This function returns a string representation of the events registered
1475   in presence module.( used for Allowed-Events header).
1476
147714.  update_watchers_status
1478
1479   Field type:
1480...
1481typedef int (*update_watchers_t)(str pres_uri, pres_ev_t* ev,
1482str* rules_doc);
1483...
1484
1485   This function is an external command that can be used to announce a
1486   change in authorization rules for a presentity. It updates the stored
1487   status and sends a Notify to the watchers whose status has changes.
1488   (used by presence_xml module when notified through an RPC command of a
1489   change in an xcap document).
1490
149115.  get_sphere
1492
1493   Field type:
1494...
1495typedef char* (*pres_get_sphere_t)(str* pres_uri);
1496...
1497
1498   This function searches for a sphere definition in the published
1499   information if this has type RPID. If not found returns NULL. (the
1500   return value is allocated in private memory and should be freed)
1501
150216.  get_presentity
1503
1504   Field type:
1505...
1506typedef str* (*pres_get_presentity_t)(str pres_uri, pres_ev_t *ev, str *etag, st
1507r *contact);
1508...
1509
1510   This function returns a pointer to a str containing an XML document
1511   with all of the matching presentities. If no matching presentities are
1512   found the function returns NULL.
1513
1514   The etag and contact parameters are optional and may be set to NULL.
1515   Once you are finished with the presentity document you must call
1516   free_presentity to free the allocated memory.
1517
151817.  free_presentity
1519
1520   Field type:
1521...
1522typedef void (*pres_free_presentity_t)(str *presentity, pres_ev_t *ev);
1523...
1524
1525   This function frees memory allocated by a call to get_presentity. The
1526   ev parameter MUST point to the same pres_ev_t data-structure that was
1527   used in the call to get_presentity.
1528