summaryrefslogtreecommitdiff
path: root/src/rexmpp_roster.c
blob: 3deb5b72a2e104fcc56c2eb2438a531031ee65f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
   @file rexmpp_roster.c
   @brief Roster-related functions.
   @author defanor <defanor@uberspace.net>
   @date 2020
   @copyright MIT license.
*/

#include "rexmpp.h"
#include "rexmpp_xml.h"
#include <syslog.h>
#include <string.h>
#include <stdlib.h>

rexmpp_xml_t *
rexmpp_roster_find_item (rexmpp_t *s,
                         const char *jid,
                         rexmpp_xml_t **prev_item)
{
  rexmpp_xml_t *prev = NULL, *cur = s->roster_items;
  while (cur != NULL) {
    const char *cur_jid = rexmpp_xml_find_attr_val(cur, "jid");
    if (cur_jid == NULL) {
      rexmpp_log(s, LOG_ALERT, "No jid found in a roster item.");
      return NULL;
    }
    int match = (strcmp(cur_jid, jid) == 0);
    if (match) {
      if (prev_item != NULL) {
        *prev_item = prev;
      }
      return cur;
    }
    prev = cur;
    cur = rexmpp_xml_next_elem_sibling(cur);
  }
  return NULL;
}

rexmpp_err_t rexmpp_modify_roster (rexmpp_t *s, rexmpp_xml_t *item) {
  rexmpp_err_t ret = REXMPP_SUCCESS;
  if (! rexmpp_xml_match(item, "jabber:iq:roster", "item")) {
    rexmpp_log(s, LOG_ERR, "No roster item.");
    return REXMPP_E_PARAM;
  }
  const char *subscription = rexmpp_xml_find_attr_val(item, "subscription");
  const char *jid = rexmpp_xml_find_attr_val(item, "jid");
  if (subscription != NULL && strcmp(subscription, "remove") == 0) {
    /* Delete the item. */
    rexmpp_xml_t *prev, *cur;
    cur = rexmpp_roster_find_item(s, jid, &prev);
    if (cur != NULL) {
      if (prev != NULL) {
        prev->next = rexmpp_xml_next_elem_sibling(cur);
      } else {
        s->roster_items = rexmpp_xml_next_elem_sibling(cur);
      }
      rexmpp_xml_free(cur);
    } else {
      ret = REXMPP_E_ROSTER_ITEM_NOT_FOUND;
    }
  } else {
    /* Add or modify the item. */
    rexmpp_xml_t *cur, *prev;
    cur = rexmpp_roster_find_item(s, jid, &prev);
    /* Remove the item if it was in the roster before. */
    if (cur != NULL) {
      if (prev != NULL) {
        prev->next = rexmpp_xml_next_elem_sibling(cur);
      } else {
        s->roster_items = rexmpp_xml_next_elem_sibling(cur);
      }
      rexmpp_xml_free(cur);
    }
    /* Add the new item. */
    rexmpp_xml_t *new_item = rexmpp_xml_clone(item);
    new_item->next = s->roster_items;
    s->roster_items = new_item;
  }
  if (s->roster_modify_cb != NULL) {
    s->roster_modify_cb(s, item);
  }
  return ret;
}

void rexmpp_roster_set (rexmpp_t *s, rexmpp_xml_t *query) {
  if (s->roster_items != NULL) {
    rexmpp_xml_free_list(s->roster_items);
  }
  if (s->roster_ver != NULL) {
    free(s->roster_ver);
  }
  const char *roster_ver = rexmpp_xml_find_attr_val(query, "ver");
  s->roster_ver = NULL;
  if (roster_ver != NULL) {
    s->roster_ver = strdup(roster_ver);
  }
  s->roster_items = rexmpp_xml_clone_list(rexmpp_xml_first_elem_child(query));
  if (s->roster_modify_cb != NULL) {
    rexmpp_xml_t *item;
    for (item = rexmpp_xml_first_elem_child(query);
         item != NULL;
         item = rexmpp_xml_next_elem_sibling(item))
      {
        s->roster_modify_cb(s, item);
      }
  }
}

void rexmpp_roster_cache_read (rexmpp_t *s) {
  if (s->roster_cache_file == NULL) {
    rexmpp_log(s, LOG_WARNING, "No roster cache file path is set.");
    return;
  }
  rexmpp_xml_t *query = rexmpp_xml_read_file(s->roster_cache_file);
  if (query != NULL) {
    rexmpp_roster_set(s, query);
    rexmpp_xml_free(query);
  }
}

void rexmpp_roster_cache_write (rexmpp_t *s) {
  if (s->roster_cache_file == NULL) {
    rexmpp_log(s, LOG_WARNING, "No roster cache file path is set.");
    return;
  }

  rexmpp_xml_t *query = rexmpp_xml_new_elem("query", "jabber:iq:roster");
  if (s->roster_ver != NULL) {
    rexmpp_xml_add_attr(query, "ver", s->roster_ver);
  }
  if (s->roster_items != NULL) {
    rexmpp_xml_add_child(query, rexmpp_xml_clone_list(s->roster_items));
  }
  rexmpp_xml_write_file(s->roster_cache_file, query);
  rexmpp_xml_free(query);
}

void rexmpp_iq_roster_get (rexmpp_t *s,
                           void *ptr,
                           rexmpp_xml_t *req,
                           rexmpp_xml_t *response,
                           int success)
{
  (void)ptr;
  (void)req;     /* Nothing interesting in the request. */
  if (! success) {
    rexmpp_log(s, LOG_ERR, "Roster loading failed.");
    return;
  }
  rexmpp_xml_t *query =
    rexmpp_xml_find_child(response, "jabber:iq:roster", "query");
  if (query == NULL) {
    rexmpp_log(s, LOG_DEBUG, "No roster query in reply.");
    return;
  }
  rexmpp_roster_set(s, query);
  if (s->roster_cache_file != NULL) {
    rexmpp_roster_cache_write(s);
  }
}