summaryrefslogtreecommitdiff
path: root/src/rexmpp_console.c
blob: 2c9b19aecbe1ceeeca3e12f9a644abb6edcb91f6 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**
   @file rexmpp_console.c
   @brief A console module
   @author defanor <defanor@uberspace.net>
   @date 2020
   @copyright MIT license.

   The "console" is supposed to provide a few common and basic
   commands, and to be easily embeddable into programs, similarly to
   an XML console.
*/

#include <string.h>

#include "rexmpp.h"
#include "rexmpp_openpgp.h"
#include "rexmpp_console.h"


void rexmpp_console_printf (rexmpp_t *s, const char *format, ...)
{
  va_list args;
  if (s->console_print_cb != NULL) {
    va_start(args, format);
    s->console_print_cb (s, format, args);
    va_end(args);
  }
}

char *rexmpp_console_message_string (rexmpp_t *s, xmlNodePtr node) {
  char *ret = NULL;
  xmlNodePtr openpgp =
    rexmpp_xml_find_child(node, "urn:xmpp:openpgp:0", "openpgp");
  if (openpgp != NULL) {
    int valid;
    xmlNodePtr elem = rexmpp_openpgp_decrypt_verify_message(s, node, &valid);
    if (! valid) {
      rexmpp_console_printf(s, "An invalid OpenPGP message!\n");
    }

    if (elem != NULL) {
      xmlNodePtr payload =
        rexmpp_xml_find_child(elem, "urn:xmpp:openpgp:0", "payload");
      if (payload != NULL) {
        xmlNodePtr pl_body =
          rexmpp_xml_find_child(payload, "jabber:client", "body");
        if (pl_body != NULL) {
          ret = xmlNodeGetContent(pl_body);
        }
      }
      xmlFreeNode(elem);
    }
  }
  if (ret == NULL) {
    xmlNodePtr body = rexmpp_xml_find_child(node, "jabber:client", "body");
    ret = xmlNodeGetContent(body);
  }
  return ret;
}

void rexmpp_console_on_send (rexmpp_t *s, xmlNodePtr node) {
  if (rexmpp_xml_match(node, "jabber:client", "message")) {
    char *to = xmlGetProp(node, "to");
    if (to != NULL) {
      /* "from" should be set for verification. */
      char *from = xmlGetProp(node, "from");
      xmlAttrPtr fromProp = NULL;
      if (from == NULL) {
        fromProp = xmlNewProp(node, "from", to);
      }
      char *str = rexmpp_console_message_string(s, node);
      if (fromProp != NULL) {
        xmlRemoveProp(fromProp);
      }
      if (str != NULL) {
        rexmpp_console_printf(s, "You tell %s: %s\n", to, str);
        free(str);
      }
      free(to);
    }
  }
  if (rexmpp_xml_match(node, "jabber:client", "presence")) {
    char *presence_type = xmlGetProp(node, "type");
    char *presence_to = xmlGetProp(node, "to");
    if (presence_to == NULL) {
      rexmpp_console_printf(s, "Becoming %s\n",
                            (presence_type == NULL) ?
                            "available" :
                            presence_type);
    } else {
      if (presence_type != NULL && ! strcmp(presence_type, "subscribe")) {
        rexmpp_console_printf(s,
                              "Requesting a subscription to %s's presence.\n",
                              presence_to);
      }
      if (presence_type != NULL && ! strcmp(presence_type, "subscribed")) {
        rexmpp_console_printf(s,
                              "Approving %s's presence subscription request.\n",
                              presence_to);
      }
      if (presence_type != NULL && ! strcmp(presence_type, "unsubscribed")) {
        rexmpp_console_printf(s,
                              "Denying %s's presence subscription request.\n",
                              presence_to);
      }
      free(presence_to);
    }
    if (presence_type != NULL) {
      free(presence_type);
    }
  }
}

void rexmpp_console_on_recv (rexmpp_t *s, xmlNodePtr node) {
  if (rexmpp_xml_match(node, "jabber:client", "message")) {
    char *from = xmlGetProp(node, "from");
    if (from != NULL) {
      char *str = rexmpp_console_message_string(s, node);
      if (str != NULL) {
        rexmpp_console_printf(s, "%s tells you: %s\n", from, str);
        free(str);
      }
      free(from);
    }
  }
  if (rexmpp_xml_match(node, "jabber:client", "presence")) {
    char *presence_type = xmlGetProp(node, "type");
    char *from = xmlGetProp(node, "from");
    if (presence_type != NULL && ! strcmp(presence_type, "subscribe")) {
      rexmpp_console_printf(s, "%s requests a presence subscription\n", from);
    } else if (presence_type != NULL && ! strcmp(presence_type, "subscribed")) {
      rexmpp_console_printf(s, "%s approves a presence subscription\n", from);
    } else if (presence_type != NULL && ! strcmp(presence_type, "unsubscribed")) {
      rexmpp_console_printf(s, "%s denies a presence subscription\n", from);
    } else {
      rexmpp_console_printf(s, "%s is %s\n", from,
                            (presence_type == NULL) ?
                            "available" :
                            presence_type);
    }
    if (presence_type != NULL) {
      free(presence_type);
    }
    if (from != NULL) {
      free(from);
    }
  }
}


void rexmpp_console_roster_deleted (rexmpp_t *s,
                                    xmlNodePtr req,
                                    xmlNodePtr response,
                                    int success)
{
  (void)response;
  xmlNodePtr item =
    rexmpp_xml_find_child(rexmpp_xml_find_child(req,
                                                "jabber:iq:roster",
                                                "query"),
                          "jabber:iq:roster", "item");
  char *jid = xmlGetProp(item, "jid");
  if (success) {
    rexmpp_console_printf(s, "Deleted %s from the roster.\n", jid);
  } else {
    rexmpp_console_printf(s, "Failed to delete %s from the roster.\n", jid);
  }
  free(jid);
}

void rexmpp_console_roster_added (rexmpp_t *s,
                                  xmlNodePtr req,
                                  xmlNodePtr response,
                                  int success)
{
  (void)response;
  xmlNodePtr item =
    rexmpp_xml_find_child(rexmpp_xml_find_child(req,
                                                "jabber:iq:roster",
                                                "query"),
                          "jabber:iq:roster", "item");
  char *jid = xmlGetProp(item, "jid");
  if (success) {
    rexmpp_console_printf(s, "Added %s into the roster.\n", jid);
  } else {
    rexmpp_console_printf(s, "Failed to add %s into the roster.\n", jid);
  }
  free(jid);
}

void rexmpp_console_on_run (rexmpp_t *s, rexmpp_err_t result) {
  if (result == REXMPP_SUCCESS) {
    rexmpp_console_printf(s, "Done.\n");
    return;
  }
}

void rexmpp_console_feed (rexmpp_t *s, char *str, ssize_t str_len) {
  /* todo: buffering */
  (void)str_len;                /* Unused for now (todo). */
  char *words_save_ptr;
  xmlNodePtr presence;
  char *word, *jid_str, *msg_text;
  struct rexmpp_jid jid;
  word = strtok_r(str, " ", &words_save_ptr);
  if (word == NULL) {
    return;
  }

  const char *help =
    "Available commands:\n"
    "help\n"
    "quit\n"
    "tell <jid> <message>\n"
    "signcrypt <jid> <message>\n"
    "publish-key <fingerprint>\n"
    "join <conference> [as] <nick>\n"
    "roster list\n"
    "roster add <jid>\n"
    "roster delete <jid>\n"
    "subscription request <jid>\n"
    "subscription approve <jid>\n"
    "subscription deny <jid>\n"
    ;

  if (! strcmp(word, "help")) {
    rexmpp_console_printf(s, help);
  }

  if (! strcmp(word, "quit")) {
    rexmpp_console_printf(s, "Quitting.\n");
    rexmpp_stop(s);
    return;
  }

  if (! strcmp(word, "publish-key")) {
    char *fingerprint = strtok_r(NULL, " ", &words_save_ptr);
    rexmpp_openpgp_publish_key(s, fingerprint);
  }

  if (! strcmp(word, "tell")) {
    jid_str = strtok_r(NULL, " ", &words_save_ptr);
    if (jid_str == NULL || rexmpp_jid_parse(jid_str, &jid)) {
      return;
    }
    msg_text = jid_str + strlen(jid_str) + 1;
    xmlNodePtr msg = rexmpp_xml_add_id(s, xmlNewNode(NULL, "message"));
    xmlNewProp(msg, "to", jid.full);
    xmlNewProp(msg, "type", "chat");
    xmlNewTextChild(msg, NULL, "body", msg_text);
    rexmpp_send(s, msg);
  }

  if (! strcmp(word, "signcrypt")) {
    jid_str = strtok_r(NULL, " ", &words_save_ptr);
    if (jid_str == NULL || rexmpp_jid_parse(jid_str, &jid)) {
      return;
    }
    msg_text = jid_str + strlen(jid_str) + 1;
    xmlNodePtr body = xmlNewNode(NULL, "body");
    xmlNewNs(body, "jabber:client", NULL);
    xmlNodeAddContent(body, msg_text);
    const char *rcpt[2];
    rcpt[0] = jid.full;
    rcpt[1] = NULL;
    char *b64 = rexmpp_openpgp_encrypt_sign(s, body, rcpt);
    xmlNodePtr openpgp = xmlNewNode(NULL, "openpgp");
    openpgp->ns = xmlNewNs(openpgp, "urn:xmpp:openpgp:0", NULL);
    xmlNodeAddContent(openpgp, b64);
    free(b64);

    xmlNodePtr msg = rexmpp_xml_add_id(s, xmlNewNode(NULL, "message"));
    xmlNewProp(msg, "to", jid.full);
    xmlNewProp(msg, "type", "chat");
    xmlAddChild(msg, openpgp);

    body = xmlNewNode(NULL, "body");
    xmlNewNs(body, "jabber:client", NULL);
    xmlNodeAddContent(body, "This is a secret message.");
    xmlAddChild(msg, body);

    rexmpp_send(s, msg);
  }

  if (! strcmp(word, "join")) {
    jid_str = strtok_r(NULL, " ", &words_save_ptr);
    if (jid_str == NULL || rexmpp_jid_parse(jid_str, &jid)) {
      return;
    }
    word = strtok_r(NULL, " ", &words_save_ptr);
    if (! strcmp(word, "as")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
    }
    if (word == NULL) {
      return;
    }
    char *full_jid = malloc(strlen(jid.bare) + strlen(word) + 2);
    snprintf(full_jid, strlen(jid_str) + strlen(word) + 2, "%s/%s",
             jid.bare, word);
    presence = rexmpp_xml_add_id(s, xmlNewNode(NULL, "presence"));
    xmlNewProp(presence, "from", s->assigned_jid.full);
    xmlNewProp(presence, "to", full_jid);
    xmlNodePtr x = xmlNewNode(NULL, "x");
    xmlNewNs(x, "http://jabber.org/protocol/muc", NULL);
    xmlAddChild(presence, x);
    rexmpp_send(s, presence);
  }

  if (! strcmp(word, "roster")) {
    word = strtok_r(NULL, " ", &words_save_ptr);
    if (word == NULL) {
      return;
    }
    if (! strcmp(word, "list")) {
      xmlNodePtr item;
      for (item = s->roster_items;
           item != NULL;
           item = xmlNextElementSibling(item)) {
        char *item_jid = xmlGetProp(item, "jid");
        char *item_ask = xmlGetProp(item, "ask");
        char *item_subscription = xmlGetProp(item, "subscription");
        rexmpp_console_printf(s, "%s: subscription = %s, ask = %s\n",
                              item_jid, item_subscription, item_ask);
        if (item_jid != NULL) {
          free(item_jid);
        }
        if (item_ask != NULL) {
          free(item_ask);
        }
        if (item_subscription != NULL) {
          free(item_subscription);
        }
      }
    } else if (! strcmp(word, "delete")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
      if (word == NULL) {
        return;
      }
      xmlNodePtr delete_item = xmlNewNode(NULL, "item");
      delete_item->ns = xmlNewNs(delete_item, "jabber:iq:roster", NULL);
      xmlNewProp(delete_item, "jid", word);
      xmlNewProp(delete_item, "subscription", "remove");
      xmlNodePtr delete_query = xmlNewNode(NULL, "query");
      delete_query->ns = xmlNewNs(delete_query, "jabber:iq:roster", NULL);
      xmlAddChild(delete_query, delete_item);
      rexmpp_iq_new(s, "set", NULL, delete_query,
                    rexmpp_console_roster_deleted);
    } else if (! strcmp(word, "add")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
      if (word == NULL) {
        return;
      }
      xmlNodePtr delete_item = xmlNewNode(NULL, "item");
      delete_item->ns = xmlNewNs(delete_item, "jabber:iq:roster", NULL);
      xmlNewProp(delete_item, "jid", word);
      xmlNodePtr delete_query = xmlNewNode(NULL, "query");
      delete_query->ns = xmlNewNs(delete_query, "jabber:iq:roster", NULL);
      xmlAddChild(delete_query, delete_item);
      rexmpp_iq_new(s, "set", NULL, delete_query,
                    rexmpp_console_roster_added);
    }
  }

  if (! strcmp(word, "subscription")) {
    word = strtok_r(NULL, " ", &words_save_ptr);
    if (word == NULL) {
      return;
    }
    if (! strcmp(word, "request")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
      if (word == NULL) {
        return;
      }
      presence = xmlNewNode(NULL, "presence");
      xmlNewProp(presence, "to", word);
      xmlNewProp(presence, "type", "subscribe");
      rexmpp_send(s, presence);
    }
    if (! strcmp(word, "approve")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
      if (word == NULL) {
        return;
      }
      presence = xmlNewNode(NULL, "presence");
      xmlNewProp(presence, "to", word);
      xmlNewProp(presence, "type", "subscribed");
      rexmpp_send(s, presence);
    }
    if (! strcmp(word, "deny")) {
      word = strtok_r(NULL, " ", &words_save_ptr);
      if (word == NULL) {
        return;
      }
      presence = xmlNewNode(NULL, "presence");
      xmlNewProp(presence, "to", word);
      xmlNewProp(presence, "type", "unsubscribed");
      rexmpp_send(s, presence);
    }
  }
}