watcher.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. * Presence Agent, watcher structure and related functions
  3. *
  4. * $Id$
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. #include "paerrno.h"
  30. #include "../../lib/srdb2/db.h"
  31. #include "../../dprint.h"
  32. #include "../../parser/parse_event.h"
  33. #include "../../mem/shm_mem.h"
  34. #include "../../trim.h"
  35. #include "../../ut.h"
  36. #include "pa_mod.h"
  37. #include "watcher.h"
  38. #include "presentity.h"
  39. #include "auth.h"
  40. #include "ptime.h"
  41. str watcher_status_names[] = {
  42. [WS_PENDING] = STR_STATIC_INIT("pending"),
  43. [WS_ACTIVE] = STR_STATIC_INIT("active"),
  44. [WS_REJECTED] = STR_STATIC_INIT("rejected"),
  45. [WS_TERMINATED] = STR_STATIC_INIT("terminated"),
  46. [WS_PENDING_TERMINATED] = STR_STATIC_INIT("terminated"),
  47. STR_NULL
  48. };
  49. str watcher_event_names[] = {
  50. [WE_SUBSCRIBE] = STR_STATIC_INIT("subscribe"),
  51. [WE_APPROVED] = STR_STATIC_INIT("approved"),
  52. [WE_DEACTIVATED] = STR_STATIC_INIT("deactivated"),
  53. [WE_PROBATION] = STR_STATIC_INIT("probation"),
  54. [WE_REJECTED] = STR_STATIC_INIT("rejected"),
  55. [WE_TIMEOUT] = STR_STATIC_INIT("timeout"),
  56. [WE_GIVEUP] = STR_STATIC_INIT("giveup"),
  57. [WE_NORESOURCE] = STR_STATIC_INIT("noresource"),
  58. STR_NULL
  59. };
  60. const char *event_package2str(int et) /* FIXME: change working with this to enum ?*/
  61. {
  62. /* added due to incorrect package handling */
  63. switch (et) {
  64. case EVENT_PRESENCE: return "presence";
  65. case EVENT_PRESENCE_WINFO: return "presence.winfo";
  66. /*case EVENT_XCAP_CHANGE: return ...; */
  67. default: return "unknown";
  68. }
  69. }
  70. int str2event_package(const char *epname)
  71. {
  72. /* work only with packages supported by PA! */
  73. if (strcmp(epname, "presence") == 0) return EVENT_PRESENCE;
  74. if (strcmp(epname, "presence.winfo") == 0) return EVENT_PRESENCE_WINFO;
  75. return -1; /* unsupported */
  76. }
  77. /*int event_package_from_string(str *epname)
  78. {
  79. int i;
  80. for (i = 0; event_package_name[i]; i++) {
  81. if (strcasecmp(epname->s, event_package_name[i]) == 0) {
  82. return i;
  83. }
  84. }
  85. return 0;
  86. }*/
  87. watcher_status_t watcher_status_from_string(str *wsname)
  88. {
  89. int i;
  90. for (i = 0; watcher_status_names[i].len; i++) {
  91. if (str_nocase_equals(wsname, &watcher_status_names[i]) == 0) {
  92. return i;
  93. }
  94. }
  95. return 0;
  96. }
  97. static watcher_event_t watcher_event_from_string(str *wename)
  98. {
  99. int i;
  100. for (i = 0; watcher_event_names[i].len; i++) {
  101. if (str_nocase_equals(wename, &watcher_event_names[i]) == 0) {
  102. return i;
  103. }
  104. }
  105. return 0;
  106. }
  107. /* be sure s!=NULL */
  108. /* compute a hash value for a string */
  109. unsigned int compute_hash(unsigned int _h, char* s, int len)
  110. {
  111. #define h_inc h+=v^(v>>3);
  112. char* p;
  113. register unsigned v;
  114. register unsigned h = _h;
  115. for(p=s; p<=(s+len-4); p+=4)
  116. {
  117. v=(*p<<24)+(p[1]<<16)+(p[2]<<8)+p[3];
  118. h_inc;
  119. }
  120. v=0;
  121. for(;p<(s+len); p++)
  122. {
  123. v<<=8;
  124. v+=*p;
  125. }
  126. h_inc;
  127. return h;
  128. }
  129. /*
  130. * Create a new watcher structure but do not write to database
  131. */
  132. int new_watcher_no_wb(str* _uri, time_t _e, int event_package,
  133. int doc_type, dlg_t* _dlg, str *_dn, str *server_contact,
  134. str *id, watcher_t** _w)
  135. {
  136. watcher_t* watcher;
  137. int size;
  138. dbid_t dbid;
  139. str sid;
  140. /* Check parameters */
  141. if (!_uri && !_dlg && !_w) {
  142. LOG(L_ERR, "new_watcher(): Invalid parameter value\n");
  143. return -1;
  144. }
  145. if (!id) {
  146. /* generate new database/watcher id (needed for winfo documents!) */
  147. generate_dbid(dbid);
  148. sid.s = dbid_strptr(dbid);
  149. sid.len = dbid_strlen(dbid);
  150. id = &sid;
  151. }
  152. /* Allocate memory buffer for watcher_t structure and uri string */
  153. size = sizeof(watcher_t) + id->len + _uri->len + _dn->len + server_contact->len;
  154. watcher = (watcher_t*)mem_alloc(size);
  155. if (!watcher) {
  156. paerrno = PA_NO_MEMORY;
  157. ERR("No memory left (%d bytes)\n", size);
  158. return -1;
  159. }
  160. memset(watcher, 0, sizeof(watcher_t));
  161. /* Copy ID string */
  162. watcher->id.s = (char*)watcher + sizeof(watcher_t);
  163. str_cpy(&watcher->id, id);
  164. /* Copy uri string */
  165. watcher->uri.s = after_str_ptr(&watcher->id);
  166. str_cpy(&watcher->uri, _uri);
  167. /* Copy display_name string */
  168. watcher->display_name.s = after_str_ptr(&watcher->uri);
  169. str_cpy(&watcher->display_name, _dn);
  170. /* Copy server_contact string */
  171. watcher->server_contact.s = after_str_ptr(&watcher->display_name);
  172. str_cpy(&watcher->server_contact, server_contact);
  173. watcher->document_index = 0;
  174. watcher->event_package = event_package;
  175. watcher->expires = _e; /* Expires value */
  176. watcher->preferred_mimetype = doc_type; /* Accepted document type */
  177. watcher->dialog = _dlg; /* Dialog handle */
  178. watcher->event = WE_SUBSCRIBE;
  179. watcher->status = WS_PENDING;
  180. *_w = watcher;
  181. return 0;
  182. }
  183. static int set_watcher_db_data(presentity_t *_p, watcher_t *watcher,
  184. db_key_t *cols, db_val_t *vals, int *col_cnt,
  185. str *dialog_str /* destination for dialog string -> must be freed after ! */
  186. )
  187. {
  188. int n_cols = 0;
  189. char *package = (char*)event_package2str(watcher->event_package);
  190. str dialog; /* serialized dialog */
  191. str_clear(dialog_str);
  192. if (dlg_func.dlg2str(watcher->dialog, &dialog) != 0) {
  193. LOG(L_ERR, "Error while serializing dialog\n");
  194. return -1;
  195. }
  196. cols[n_cols] = col_w_uri;
  197. vals[n_cols].type = DB_STR;
  198. vals[n_cols].nul = 0;
  199. vals[n_cols].val.str_val = watcher->uri;
  200. n_cols++;
  201. cols[n_cols] = col_package;
  202. vals[n_cols].type = DB_STR;
  203. vals[n_cols].nul = 0;
  204. vals[n_cols].val.str_val.s = package;
  205. vals[n_cols].val.str_val.len = strlen(package);
  206. n_cols++;
  207. cols[n_cols] = col_s_id;
  208. vals[n_cols].type = DB_STR;
  209. vals[n_cols].nul = 0;
  210. vals[n_cols].val.str_val = watcher->id;
  211. n_cols++;
  212. cols[n_cols] = col_status;
  213. vals[n_cols].type = DB_STR;
  214. vals[n_cols].nul = 0;
  215. vals[n_cols].val.str_val = watcher_status_names[watcher->status];
  216. n_cols++;
  217. cols[n_cols] = col_event;
  218. vals[n_cols].type = DB_STR;
  219. vals[n_cols].nul = 0;
  220. vals[n_cols].val.str_val = watcher_event_names[watcher->event];
  221. n_cols++;
  222. cols[n_cols] = col_display_name;
  223. vals[n_cols].type = DB_STR;
  224. vals[n_cols].nul = 0;
  225. vals[n_cols].val.str_val.s = watcher->display_name.s;
  226. vals[n_cols].val.str_val.len = watcher->display_name.len;
  227. n_cols++;
  228. cols[n_cols] = col_accepts;
  229. vals[n_cols].type = DB_INT;
  230. vals[n_cols].nul = 0;
  231. vals[n_cols].val.int_val = watcher->preferred_mimetype;
  232. n_cols++;
  233. cols[n_cols] = col_expires;
  234. vals[n_cols].type = DB_DATETIME;
  235. vals[n_cols].nul = 0;
  236. vals[n_cols].val.time_val = watcher->expires;
  237. n_cols++;
  238. cols[n_cols] = col_dialog;
  239. vals[n_cols].type = DB_BLOB;
  240. vals[n_cols].nul = 0;
  241. vals[n_cols].val.blob_val = dialog;
  242. n_cols++;
  243. cols[n_cols] = col_server_contact;
  244. vals[n_cols].type = DB_STR;
  245. vals[n_cols].nul = 0;
  246. vals[n_cols].val.str_val = watcher->server_contact;
  247. n_cols++;
  248. cols[n_cols] = col_pres_id;
  249. vals[n_cols].type = DB_STR;
  250. vals[n_cols].nul = 0;
  251. vals[n_cols].val.str_val = _p->pres_id;
  252. n_cols++;
  253. cols[n_cols] = col_doc_index;
  254. vals[n_cols].type = DB_INT;
  255. vals[n_cols].nul = 0;
  256. vals[n_cols].val.int_val = watcher->document_index;
  257. n_cols++;
  258. *col_cnt = n_cols;
  259. if (dialog_str) *dialog_str = dialog;
  260. return 0;
  261. }
  262. static int db_add_watcher(presentity_t *_p, watcher_t *watcher)
  263. {
  264. str_t tmp;
  265. db_key_t query_cols[20];
  266. db_val_t query_vals[20];
  267. int n_query_cols = 0;
  268. if (!use_db) return 0;
  269. str_clear(&tmp);
  270. if (pa_dbf.use_table(pa_db, watcherinfo_table) < 0) {
  271. LOG(L_ERR, "db_add_watcher: Error in use_table\n");
  272. return -1;
  273. }
  274. if (set_watcher_db_data(_p, watcher,
  275. query_cols, query_vals, &n_query_cols,
  276. &tmp) != 0) {
  277. return -1;
  278. }
  279. /* insert new record into database */
  280. if (pa_dbf.insert(pa_db, query_cols, query_vals, n_query_cols) < 0) {
  281. LOG(L_ERR, "db_add_watcher: Error while inserting watcher\n");
  282. str_free_content(&tmp);
  283. return -1;
  284. }
  285. str_free_content(&tmp);
  286. return 0;
  287. }
  288. int db_update_watcher(presentity_t *_p, watcher_t *watcher)
  289. {
  290. str tmp;
  291. db_key_t query_cols[20];
  292. db_val_t query_vals[20];
  293. int n_query_cols = 0;
  294. db_key_t keys[] = { col_s_id };
  295. db_op_t ops[] = { OP_EQ };
  296. db_val_t k_vals[] = { { DB_STR, 0, { .str_val = watcher->id } } };
  297. if (!use_db) return 0;
  298. str_clear(&tmp);
  299. if (pa_dbf.use_table(pa_db, watcherinfo_table) < 0) {
  300. LOG(L_ERR, "db_update_watcher: Error in use_table\n");
  301. return -1;
  302. }
  303. if (set_watcher_db_data(_p, watcher,
  304. query_cols, query_vals, &n_query_cols,
  305. &tmp) != 0) {
  306. return -1;
  307. }
  308. if (pa_dbf.update(pa_db, keys, ops, k_vals,
  309. query_cols, query_vals, 1, n_query_cols) < 0) {
  310. LOG(L_ERR, "Error while updating watcher in DB\n");
  311. str_free_content(&tmp);
  312. return -1;
  313. }
  314. str_free_content(&tmp);
  315. return 0;
  316. }
  317. static inline int db_remove_watcher(struct presentity *_p, watcher_t *w)
  318. {
  319. db_key_t keys[] = { col_s_id };
  320. db_op_t ops[] = { OP_EQ };
  321. db_val_t k_vals[] = { { DB_STR, 0, { .str_val = w->id } } };
  322. if (!use_db) return 0;
  323. if (pa_dbf.use_table(pa_db, watcherinfo_table) < 0) {
  324. ERR("Error in use_table\n");
  325. return -1;
  326. }
  327. if (pa_dbf.delete(pa_db, keys, ops, k_vals, 1) < 0) {
  328. ERR("Error while deleting watcher from DB\n");
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. /*
  334. * Read watcherinfo table from database for presentity _p
  335. */
  336. int db_read_watcherinfo(presentity_t *_p, db_con_t* db)
  337. {
  338. db_key_t query_cols[5];
  339. db_op_t query_ops[5];
  340. db_val_t query_vals[5];
  341. str dialog = STR_NULL;
  342. dlg_t *dlg = NULL;
  343. db_key_t result_cols[11];
  344. db_res_t *res;
  345. int r = 0;
  346. int n_query_cols = 1;
  347. int n_result_cols = 0;
  348. int w_uri_col, s_id_col, event_package_col, status_col, watcher_event_col,
  349. display_name_col, accepts_col, expires_col, dialog_col, server_contact_col,
  350. doc_index_col;
  351. if (!use_db) return 0;
  352. /* LOG(L_ERR, "db_read_watcherinfo starting\n");*/
  353. query_cols[0] = col_pres_id;
  354. query_ops[0] = OP_EQ;
  355. query_vals[0].type = DB_STR;
  356. query_vals[0].nul = 0;
  357. query_vals[0].val.str_val = _p->pres_id;
  358. result_cols[w_uri_col = n_result_cols++] = col_w_uri;
  359. result_cols[s_id_col = n_result_cols++] = col_s_id;
  360. result_cols[event_package_col = n_result_cols++] = col_package;
  361. result_cols[status_col = n_result_cols++] = col_status;
  362. result_cols[display_name_col = n_result_cols++] = col_display_name;
  363. result_cols[accepts_col = n_result_cols++] = col_accepts;
  364. result_cols[expires_col = n_result_cols++] = col_expires;
  365. result_cols[watcher_event_col = n_result_cols++] = col_event;
  366. result_cols[dialog_col = n_result_cols++] = col_dialog;
  367. result_cols[server_contact_col = n_result_cols++] = col_server_contact;
  368. result_cols[doc_index_col = n_result_cols++] = col_doc_index;
  369. if (pa_dbf.use_table(db, watcherinfo_table) < 0) {
  370. ERR("Error in use_table\n");
  371. return -1;
  372. }
  373. if (pa_dbf.query (db, query_cols, query_ops, query_vals,
  374. result_cols, n_query_cols, n_result_cols, 0, &res) < 0) {
  375. ERR("Error while querying watcherinfo\n");
  376. return -1;
  377. }
  378. if (res && (res->n > 0)) {
  379. /* fill in tuple structure from database query result */
  380. int i;
  381. dlg = NULL;
  382. for (i = 0; i < res->n; i++) {
  383. db_row_t *row = &res->rows[i];
  384. db_val_t *row_vals = ROW_VALUES(row);
  385. str w_uri = STR_NULL;
  386. str s_id = STR_NULL;
  387. char *event_package_str = NULL;
  388. int event_package = EVENT_PRESENCE;
  389. str watcher_event_str = STR_NULL;
  390. watcher_event_t watcher_event = WE_SUBSCRIBE;
  391. int accepts = row_vals[accepts_col].val.int_val;
  392. time_t expires = row_vals[expires_col].val.time_val;
  393. int doc_index = row_vals[doc_index_col].val.int_val;
  394. str status = STR_NULL;
  395. str display_name = STR_NULL;
  396. str server_contact = STR_NULL;
  397. watcher_t *watcher = NULL;
  398. if (!row_vals[w_uri_col].nul) {
  399. w_uri.s = (char *)row_vals[w_uri_col].val.string_val;
  400. w_uri.len = strlen(w_uri.s);
  401. }
  402. if (!row_vals[s_id_col].nul) {
  403. s_id.s = (char *)row_vals[s_id_col].val.string_val;
  404. s_id.len = strlen(s_id.s);
  405. }
  406. if (!row_vals[event_package_col].nul) {
  407. event_package_str = (char *)row_vals[event_package_col].val.string_val;
  408. event_package = str2event_package(event_package_str);
  409. }
  410. if (!row_vals[status_col].nul) {
  411. status.s = (char *)row_vals[status_col].val.string_val;
  412. status.len = strlen(status.s);
  413. }
  414. if (!row_vals[watcher_event_col].nul) {
  415. watcher_event_str.s = (char *)row_vals[watcher_event_col].val.string_val;
  416. watcher_event_str.len = strlen(watcher_event_str.s);
  417. watcher_event = watcher_event_from_string(&watcher_event_str);
  418. }
  419. if (!row_vals[display_name_col].nul) {
  420. display_name.s = (char *)row_vals[display_name_col].val.string_val;
  421. display_name.len = strlen(display_name.s);
  422. }
  423. if (!row_vals[dialog_col].nul) {
  424. dialog = row_vals[dialog_col].val.blob_val;
  425. dlg = (dlg_t*)mem_alloc(sizeof(*dlg));
  426. if (!dlg) {
  427. LOG(L_ERR, "db_read_watcher: Can't allocate dialog\n");
  428. r = -1;
  429. }
  430. else
  431. if (dlg_func.str2dlg(&dialog, dlg) != 0) {
  432. LOG(L_ERR, "db_read_watcher: Error while deserializing dialog\n");
  433. r = -1;
  434. }
  435. }
  436. if (!row_vals[server_contact_col].nul) {
  437. server_contact.s = (char *)row_vals[server_contact_col].val.string_val;
  438. server_contact.len = strlen(server_contact.s);
  439. }
  440. DBG("creating watcher\n");
  441. if (new_watcher_no_wb(&w_uri, expires,
  442. event_package, accepts, dlg, &display_name,
  443. &server_contact, &s_id, &watcher) == 0) {
  444. watcher->status = watcher_status_from_string(&status);
  445. watcher->event = watcher_event;
  446. watcher->document_index = doc_index;
  447. r = append_watcher(_p, watcher, 0);
  448. if (r < 0) {
  449. ERR("Error while adding watcher\n");
  450. free_watcher(watcher);
  451. }
  452. }
  453. else r = -1;
  454. }
  455. }
  456. pa_dbf.free_result(db, res);
  457. return r;
  458. }
  459. /*
  460. * Release a watcher structure
  461. */
  462. void free_watcher(watcher_t* _w)
  463. {
  464. tmb.free_dlg(_w->dialog);
  465. mem_free(_w);
  466. }
  467. /*
  468. * Update a watcher structure
  469. */
  470. int update_watcher(struct presentity *p, watcher_t* _w, time_t _e, struct sip_msg *m)
  471. {
  472. watcher_status_t old = _w->status; /* old status of subscription */
  473. tmb.dlg_request_uas(_w->dialog, m, IS_TARGET_REFRESH);
  474. _w->expires = _e;
  475. _w->flags |= WFLAG_SUBSCRIPTION_CHANGED;
  476. /* actualize watcher's status according to time */
  477. if (_w->expires <= act_time) {
  478. /* ERR("Updated watcher to expire: %.*s\n", _w->uri.len, _w->uri.s); */
  479. _w->expires = 0;
  480. set_watcher_terminated_status(_w);
  481. }
  482. if ((old != _w->status) && (_w->event_package == EVENT_PRESENCE)) {
  483. /* changed only when presence watcher changes status */
  484. /* FIXME: it could be changed when expires time changes too,
  485. * but we don't send expiration in watcherinf notify thus
  486. * it is worthless */
  487. p->flags |= PFLAG_WATCHERINFO_CHANGED;
  488. }
  489. if (use_db) return db_update_watcher(p, _w);
  490. else return 0;
  491. }
  492. int is_watcher_terminated(watcher_t *w)
  493. {
  494. if (!w) return -1;
  495. if ((w->status == WS_TERMINATED) ||
  496. (w->status == WS_REJECTED) ||
  497. (w->status == WS_PENDING_TERMINATED)) return 1;
  498. return 0;
  499. }
  500. int is_watcher_authorized(watcher_t *w)
  501. {
  502. if (!w) return 0;
  503. switch (w->status) {
  504. case WS_PENDING: ;
  505. case WS_PENDING_TERMINATED: ;
  506. case WS_REJECTED: return 0;
  507. case WS_ACTIVE: ;
  508. case WS_TERMINATED: return 1;
  509. }
  510. return 0;
  511. }
  512. void set_watcher_terminated_status(watcher_t *w)
  513. {
  514. if (!w) return;
  515. switch (w->status) {
  516. case WS_REJECTED: break;
  517. case WS_PENDING: ;
  518. w->status = WS_PENDING_TERMINATED;
  519. break;
  520. case WS_PENDING_TERMINATED: break;
  521. case WS_TERMINATED: break;
  522. case WS_ACTIVE: ;
  523. w->status = WS_TERMINATED;
  524. break;
  525. }
  526. }
  527. int append_watcher(presentity_t *_p, watcher_t *_w, int add_to_db)
  528. {
  529. if (add_to_db && use_db) {
  530. if (db_add_watcher(_p, _w) != 0) {
  531. ERR("Error while adding watcher\n");
  532. paerrno = PA_INTERNAL_ERROR;
  533. return -5;
  534. }
  535. }
  536. if (_w->event_package == EVENT_PRESENCE_WINFO) {
  537. DOUBLE_LINKED_LIST_ADD(_p->first_winfo_watcher,
  538. _p->last_winfo_watcher, _w);
  539. }
  540. else {
  541. DOUBLE_LINKED_LIST_ADD(_p->first_watcher,
  542. _p->last_watcher, _w);
  543. /* changed only when presence watcher added */
  544. _p->flags |= PFLAG_WATCHERINFO_CHANGED;
  545. DEBUG("setting PFLAG_WATCHERINFO_CHANGED\n");
  546. }
  547. return 0;
  548. }
  549. void remove_watcher(presentity_t* _p, watcher_t *w)
  550. {
  551. if (!w) return;
  552. if (use_db) db_remove_watcher(_p, w);
  553. if (w->event_package == EVENT_PRESENCE_WINFO)
  554. DOUBLE_LINKED_LIST_REMOVE(_p->first_winfo_watcher,
  555. _p->last_winfo_watcher, w);
  556. else {
  557. DOUBLE_LINKED_LIST_REMOVE(_p->first_watcher,
  558. _p->last_watcher, w);
  559. _p->flags |= PFLAG_WATCHERINFO_CHANGED;
  560. }
  561. }
  562. /* returns 0 if equal dialog IDs */
  563. static int cmp_dlg_ids(dlg_id_t *a, dlg_id_t *b)
  564. {
  565. if (!a) {
  566. if (!b) return -1;
  567. else return 0;
  568. }
  569. if (!b) return 1;
  570. if (str_case_equals(&a->call_id, &b->call_id) != 0) return 1;
  571. if (str_case_equals(&a->rem_tag, &b->rem_tag) != 0) return 1; /* case sensitive ? */
  572. if (str_case_equals(&a->loc_tag, &b->loc_tag) != 0) return 1; /* case sensitive ? */
  573. return 0;
  574. }
  575. /* Find a watcher in the list via dialog identifier */
  576. int find_watcher_dlg(struct presentity* _p, dlg_id_t *dlg_id, int _et, watcher_t** _w)
  577. {
  578. watcher_t* watcher;
  579. /* first look for watchers */
  580. if (!dlg_id) return -1;
  581. if (_et != EVENT_PRESENCE_WINFO)
  582. watcher = _p->first_watcher;
  583. else
  584. watcher = _p->first_winfo_watcher;
  585. while(watcher) {
  586. if (watcher->dialog) {
  587. if ((cmp_dlg_ids(&watcher->dialog->id, dlg_id) == 0) &&
  588. (watcher->event_package == _et)) {
  589. *_w = watcher;
  590. return 0;
  591. }
  592. }
  593. watcher = watcher->next;
  594. }
  595. return 1;
  596. }