1/*!
2 * @file File: mam.c
3 *
4 * vim: expandtab:ts=2:sts=2:sw=2
5 *
6 * @copyright
7 *
8 * Copyright (C) 2020 Anoxinon e.V.
9 *
10 * This file is part of xmppc.
11 *
12 * xmppc is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * xmppc is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * German
26 *
27 * Diese Datei ist Teil von xmppc.
28 *
29 * xmppc ist Freie Software: Sie können es unter den Bedingungen
30 * der GNU General Public License, wie von der Free Software Foundation,
31 * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
32 * veröffentlichten Version, weiter verteilen und/oder modifizieren.
33 *
34 * xmppc wird in der Hoffnung, dass es nützlich sein wird, aber
35 * OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
36 * Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
37 * Siehe die GNU General Public License für weitere Details.
38 *
39 * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
40 * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
41 */
42
43#include "mam.h"
44#include "stdio.h"
45#include "string.h"
46
47static void _mam_request(xmppc_t *xmppc, char* to);
48static int _mam_show(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
49static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
50
51void mam_execute_command (xmppc_t *xmppc, int argc, char *argv[]) {
52 if(argc == 2 && strcmp("list", argv[0]) == 0) {
53 _mam_request(xmppc, argv[1]);
54 } else {
55 printf("Command unbekannt!");
56 xmpp_disconnect(xmppc->conn);
57 }
58}
59
60static void _mam_request(xmppc_t *xmppc, char* to) {
61
62 xmpp_handler_add (xmppc->conn, _mam_display_message, NULL,"message",NULL, xmppc);
63
64 char* id = xmpp_uuid_gen(xmppc->ctx);
65 xmpp_stanza_t *iq = xmpp_iq_new(xmppc->ctx, "set", id);
66 // query
67 xmpp_stanza_t* query = xmpp_stanza_new(xmppc->ctx);
68 xmpp_stanza_set_name(query, "query");
69 xmpp_stanza_set_ns(query, "urn:xmpp:mam:2");
70
71
72 xmpp_stanza_t* x = xmpp_stanza_new(xmppc->ctx);
73 xmpp_stanza_set_name(x,"x");
74 xmpp_stanza_set_ns(x, "jabber:x:data"),
75 xmpp_stanza_set_type(x,"submit");
76
77 xmpp_stanza_add_child(query,x);
78
79 xmpp_stanza_t* f = xmpp_stanza_new(xmppc->ctx);
80 xmpp_stanza_set_name(f,"field");
81 xmpp_stanza_set_type(f,"hidden");
82 xmpp_stanza_set_attribute(f, "var", "FORM_TYPE");
83 xmpp_stanza_t* v = xmpp_stanza_new(xmppc->ctx);
84 xmpp_stanza_set_name(v,"value");
85 xmpp_stanza_t* b = xmpp_stanza_new(xmppc->ctx);
86 xmpp_stanza_set_text(b, "urn:xmpp:mam:2");
87
88 xmpp_stanza_add_child(x,f);
89 xmpp_stanza_add_child(f,v);
90 xmpp_stanza_add_child(v,b);
91
92 f = xmpp_stanza_new(xmppc->ctx);
93 xmpp_stanza_set_name(f,"field");
94 xmpp_stanza_set_attribute(f, "var", "with");
95 v = xmpp_stanza_new(xmppc->ctx);
96 xmpp_stanza_set_name(v,"value");
97 b = xmpp_stanza_new(xmppc->ctx);
98 xmpp_stanza_set_text(b, to);
99 //xmpp_stanza_set_text(b, xmpp_conn_get_jid (xmppc->conn));
100
101 xmpp_stanza_add_child(x,f);
102 xmpp_stanza_add_child(f,v);
103 xmpp_stanza_add_child(v,b);
104
105 xmpp_stanza_add_child(iq,query);
106 xmpp_id_handler_add(xmppc->conn, _mam_show, id, xmppc);
107 xmpp_send(xmppc->conn,iq);
108
109}
110
111int _mam_show(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata){
112 xmpp_stanza_t *fin = xmpp_stanza_get_child_by_name(stanza,"fin");
113 if(fin) {
114 xmpp_disconnect(conn);
115 }
116 return 1;
117}
118
119
120static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) {
121 xmpp_stanza_t* result = xmpp_stanza_get_child_by_name(stanza,"result");
122 if( result && strcmp("urn:xmpp:mam:2", xmpp_stanza_get_ns(result)) == 0 ) {
123 char* s;
124 size_t len;
125 xmpp_stanza_to_text(xmpp_stanza_get_children(result) ,&s,&len);
126 printf("%s%s\x1b[m\n", ANSI_COLOR_YELLOW, s);
127 }
128 return 1;
129}