qsa_interface.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "qsa_interface.h"
  2. #include "pdomain.h"
  3. #include "pa_mod.h"
  4. #include "dlist.h"
  5. #include "auth.h"
  6. #include <presence/qsa.h>
  7. #include <presence/notifier.h>
  8. #include <cds/logger.h>
  9. #include <cds/memory.h>
  10. #include <cds/list.h>
  11. #include <presence/pres_doc.h>
  12. static notifier_domain_t *domain = NULL;
  13. static notifier_t *notifier = NULL;
  14. static qsa_content_type_t *ct_presence_info = NULL;
  15. /*static qsa_content_type_t *ct_pidf_xml = NULL;*/
  16. /* static str_t notifier_name = { s: "pa", len: 2 }; */
  17. static int pa_subscribe(notifier_t *n, qsa_subscription_t *subscription);
  18. static void pa_unsubscribe(notifier_t *n, qsa_subscription_t *subscription);
  19. extern dlist_t* root; /* ugly !!!!! */
  20. int accept_internal_subscriptions = 0;
  21. /* QSA interface initialization */
  22. int pa_qsa_interface_init()
  23. {
  24. static str presence_info = STR_STATIC_INIT(CT_PRESENCE_INFO);
  25. static str_t presence_package = { s: "presence", len: 8 };
  26. domain = qsa_get_default_domain();
  27. if (!domain) {
  28. ERR("can't register notifier domain\n");
  29. return -1;
  30. }
  31. /* DBG("QSA (pa) domain: %p\n", domain); */
  32. notifier = register_notifier(domain, &presence_package,
  33. pa_subscribe, pa_unsubscribe, NULL);
  34. if (!notifier) {
  35. ERR("can't register notifier\n");
  36. return -1;
  37. }
  38. ct_presence_info = register_content_type(domain,
  39. &presence_info, (destroy_function_f)free_presentity_info);
  40. if (!ct_presence_info) {
  41. ERR("can't register QSA content type\n");
  42. return -1;
  43. }
  44. else TRACE("PA_CONTENT_TYPE: %p\n", ct_presence_info);
  45. /* DBG("pa_qsa_interface_init(): created notifier %.*s\n", FMT_STR(notifier_name)); */
  46. return 0;
  47. }
  48. void pa_qsa_interface_destroy()
  49. {
  50. if (domain && notifier) unregister_notifier(domain, notifier);
  51. notifier = NULL;
  52. /* no new qsa subscriptions will be created now - now can be
  53. * released all existing ones */
  54. if (domain) qsa_release_domain(domain);
  55. /* no QSA operations should be done there (don't send
  56. * notification messages, ...) only subscriptions may
  57. * be released */
  58. domain = NULL;
  59. }
  60. /* notifier functions */
  61. static int add_internal_subscription(presentity_t *p, internal_pa_subscription_t *is)
  62. {
  63. if (is->status != WS_ACTIVE)
  64. is->status = authorize_internal_watcher(p, is);
  65. if (is->status == WS_REJECTED) return -1;
  66. DOUBLE_LINKED_LIST_ADD(p->first_qsa_subscription,
  67. p->last_qsa_subscription, is);
  68. return 0;
  69. }
  70. internal_pa_subscription_t *create_internal_subscription(qsa_subscription_t *s)
  71. {
  72. internal_pa_subscription_t *ss = cds_malloc(sizeof(internal_pa_subscription_t));
  73. if (!ss) return ss;
  74. ss->subscription = s;
  75. ss->status = WS_PENDING;
  76. ss->prev = NULL;
  77. ss->next = NULL;
  78. return ss;
  79. }
  80. void free_internal_subscription(internal_pa_subscription_t *is)
  81. {
  82. if (is) cds_free(is);
  83. }
  84. static int pa_subscribe(notifier_t *n, qsa_subscription_t *subscription)
  85. {
  86. dlist_t *dl;
  87. presentity_t *p = NULL;
  88. internal_pa_subscription_t *ss;
  89. str uid = STR_NULL;
  90. str *record_id = NULL;
  91. xcap_query_params_t xcap_params;
  92. if (!accept_internal_subscriptions) return 0; /* do not accept subscriptions */
  93. record_id = get_record_id(subscription);
  94. if (!record_id) {
  95. ERR("BUG: subscription to empty record\n");
  96. return -1;
  97. }
  98. DBG("SUBSCRIBE to PA for %.*s [%.*s]\n",
  99. FMT_STR(*record_id),
  100. FMT_STR(subscription->package->name));
  101. if (pres_uri2uid(&uid, record_id) != 0) {
  102. /* can't convert uri to uid */
  103. INFO("can't convert URI to UID for internal PA subscription\n");
  104. return -1;
  105. }
  106. /* DBG("SUBSCRIBE to uid: %.*s\n", FMT_STR(uid)); */
  107. dl = root; /* FIXME: ugly and possibly unsafe (locking needed?) */
  108. while (dl) {
  109. /* create new server subscription */
  110. ss = create_internal_subscription(subscription);
  111. if (!ss) {
  112. ERROR_LOG("can't allocate memory for internal pa subscription\n");
  113. break;
  114. }
  115. lock_pdomain(dl->d);
  116. if (find_presentity_uid(dl->d, &uid, &p) != 0) p = NULL;
  117. if (!p) {
  118. memset(&xcap_params, 0, sizeof(xcap_params));
  119. if (fill_xcap_params) fill_xcap_params(NULL, &xcap_params);
  120. if (new_presentity(dl->d, record_id, &uid, &xcap_params, &p) < 0) {
  121. ERR("can't create presentity\n");
  122. }
  123. }
  124. if (p) {
  125. /* add server subscription to p */
  126. if (add_internal_subscription(p, ss) == 0) {
  127. p->flags |= PFLAG_WATCHERINFO_CHANGED;
  128. notify_internal_watcher(p, ss);
  129. }
  130. else {
  131. /* error adding subscription to p (auth failed, ...) */
  132. free_internal_subscription(ss);
  133. }
  134. }
  135. unlock_pdomain(dl->d);
  136. dl = dl->next;
  137. }
  138. str_free_content(&uid);
  139. DBG("finished SUBSCRIBE to PA for %.*s [%.*s]\n",
  140. FMT_STR(*record_id),
  141. FMT_STR(subscription->package->name));
  142. return 0;
  143. }
  144. static void remove_internal_subscription(presentity_t *p, qsa_subscription_t *s)
  145. {
  146. internal_pa_subscription_t *ss;
  147. ss = p->first_qsa_subscription;
  148. while (ss) {
  149. if (s == ss->subscription) {
  150. DOUBLE_LINKED_LIST_REMOVE(p->first_qsa_subscription, p->last_qsa_subscription, ss);
  151. free_internal_subscription(ss);
  152. break; /* may be only once */
  153. }
  154. ss = ss->next;
  155. }
  156. }
  157. static void pa_unsubscribe(notifier_t *n, qsa_subscription_t *subscription)
  158. {
  159. dlist_t *dl;
  160. presentity_t *p = NULL;
  161. str uid = STR_NULL;
  162. str *record_id = NULL;
  163. if (!accept_internal_subscriptions) return; /* do not accept subscriptions */
  164. record_id = get_record_id(subscription);
  165. if (!record_id) {
  166. ERR("BUG: unsubscription to empty record\n");
  167. return;
  168. }
  169. if (pres_uri2uid(&uid, record_id) != 0) {
  170. /* can't convert uri to uid */
  171. ERR("can't convert URI to UID for internal PA unsubscription\n");
  172. return;
  173. }
  174. /* DBG("UNBSCRIBE from PA for %.*s [%.*s]\n",
  175. FMT_STR(subscription->record_id),
  176. FMT_STR(subscription->package->name)); */
  177. dl = root; /* FIXME: ugly and possibly unsafe (locking needed?) */
  178. while (dl) {
  179. lock_pdomain(dl->d);
  180. if (find_presentity_uid(dl->d, &uid, &p) != 0) p = NULL;
  181. if (!p) continue;
  182. remove_internal_subscription(p, subscription);
  183. p->flags |= PFLAG_WATCHERINFO_CHANGED;
  184. unlock_pdomain(dl->d);
  185. dl = dl->next;
  186. }
  187. str_free_content(&uid);
  188. }
  189. int notify_internal_watcher(presentity_t *p, internal_pa_subscription_t *ss)
  190. {
  191. presentity_info_t *pinfo;
  192. /* notify only accepted watchers */
  193. switch (ss->status) {
  194. case WS_PENDING:
  195. return notify_subscriber(ss->subscription, notifier,
  196. ct_presence_info, NULL, qsa_subscription_pending);
  197. case WS_REJECTED:
  198. return notify_subscriber(ss->subscription, notifier,
  199. ct_presence_info, NULL, qsa_subscription_rejected);
  200. case WS_PENDING_TERMINATED:
  201. case WS_TERMINATED:
  202. return notify_subscriber(ss->subscription, notifier,
  203. ct_presence_info, NULL, qsa_subscription_terminated);
  204. case WS_ACTIVE:
  205. pinfo = dup_presentity_info(&p->data);
  206. if (!pinfo) {
  207. ERROR_LOG("can't create presentity info from presentity!\n");
  208. return -1;
  209. }
  210. return notify_subscriber(ss->subscription, notifier,
  211. ct_presence_info, pinfo, qsa_subscription_active);
  212. }
  213. return 0;
  214. }
  215. int notify_qsa_watchers(presentity_t *p)
  216. {
  217. internal_pa_subscription_t *ss;
  218. int res = 0;
  219. /* DBG("notify_qsa_watchers for %.*s\n", FMT_STR(p->uri)); */
  220. ss = p->first_qsa_subscription;
  221. while (ss) {
  222. if (notify_internal_watcher(p, ss) < 0) res = -1;
  223. ss = ss->next;
  224. }
  225. return res;
  226. }
  227. int subscribe_to_user(presentity_t *_p)
  228. {
  229. static str package = STR_STATIC_INIT("presence");
  230. clear_subscription_data(&_p->presence_subscription_data);
  231. /* ??? FIXME msg queue */ _p->presence_subscription_data.dst = &_p->mq;
  232. _p->presence_subscription_data.record_id = _p->data.uri;
  233. _p->presence_subscription_data.subscriber_id = pa_subscription_uri;
  234. _p->presence_subscription_data.subscriber_data = _p;
  235. _p->presence_subscription = subscribe(domain,
  236. &package, &_p->presence_subscription_data);
  237. if (_p->presence_subscription) return 0;
  238. else return -1;
  239. }
  240. int unsubscribe_to_user(presentity_t *_p)
  241. {
  242. unsubscribe(domain, _p->presence_subscription);
  243. /* TODO ? clean messages ? they will be freed automaticaly ... */
  244. return 0;
  245. }