1/*!
2 * @file File: monitor.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 "monitor.h"
44#include "stdio.h"
45#include "string.h"
46
47static int _monitor_log_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
48static int _monitor_show_microblog(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
49
50void monitor_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
51 if(argc > 0 && strcmp("stanza", argv[0]) == 0) {
52 xmpp_handler_add(xmppc->conn, _monitor_log_stanza, NULL, NULL, NULL, NULL);
53#ifdef XMPPC_DEVELOPMENT
54 } else if (argc > 0 && strcmp("microblog", argv[0]) == 0) {
55 xmpp_handler_add(xmppc->conn, _monitor_show_microblog, NULL, NULL, NULL, NULL);
56#endif
57 } else {
58 printf("Command unbekannt!");
59 xmpp_disconnect(xmppc->conn);
60 }
61 // presence
62 xmpp_stanza_t *presence = xmpp_presence_new(xmppc->ctx);
63 char* id = xmpp_uuid_gen(xmppc->ctx);
64 xmpp_stanza_set_id(presence, id);
65 xmpp_send(xmppc->conn, presence);
66 xmpp_stanza_release(presence);
67
68 // XEP-0280: Message Carbons
69 xmpp_stanza_t *carbons = xmpp_iq_new(xmppc->ctx, "set", xmpp_uuid_gen(xmppc->ctx));
70 xmpp_stanza_t* enable = xmpp_stanza_new(xmppc->ctx);
71 xmpp_stanza_set_name(enable, "enable");
72 xmpp_stanza_set_ns(enable, "urn:xmpp:carbons:2");
73 xmpp_stanza_add_child(carbons,enable);
74 xmpp_send(xmppc->conn,carbons);
75}
76
77int _monitor_log_stanza(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata){
78 char* s;
79 size_t len;
80 xmpp_stanza_to_text(stanza,&s,&len);
81
82 char* color = "\x1b[m";
83 if(strcmp(xmpp_stanza_get_name(stanza), "iq") == 0 ) color = ANSI_COLOR_YELLOW;
84 if(strcmp(xmpp_stanza_get_name(stanza), "message") == 0 ) {
85 color = ANSI_COLOR_BLUE;
86 if(strcmp("headline", xmpp_stanza_get_type(stanza)) == 0) {
87 color = ANSI_COLOR_CYAN;
88 }
89 }
90
91 if(strcmp(xmpp_stanza_get_name(stanza), "presence") == 0 ) color = ANSI_COLOR_GREEN;
92 if(xmpp_stanza_get_type(stanza) != NULL)
93 if(strcmp(xmpp_stanza_get_type(stanza), "error") == 0 ) color = ANSI_COLOR_B_RED;
94
95 printf("%s%s\x1b[m\n", color, s);
96 return 1;
97}
98
99/* XEP-0277: Microblogging over XMPP */
100int _monitor_show_microblog(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata){
101 xmpp_stanza_t *log = stanza;
102 if(strcmp(xmpp_stanza_get_name(stanza), "message") == 0 ) {
103 xmpp_stanza_t* event = xmpp_stanza_get_child_by_name(stanza, "event");
104 if(event != NULL &&
105 strcmp(xmpp_stanza_get_ns(event), "http://jabber.org/protocol/pubsub#event") ==0) {
106 char* body_text = xmpp_message_get_body(stanza);
107 printf("Event: %s\n", body_text);
108 xmpp_stanza_t * items = xmpp_stanza_get_child_by_name(event,"items");
109 if( items != NULL && strcmp("urn:xmpp:microblog:0", xmpp_stanza_get_attribute(items, "node" )) == 0) {
110 printf("urn:xmpp:microblog:0 items found!\n");
111 }
112 }
113 }
114
115 if(log) {
116 _monitor_log_stanza(conn, stanza, userdata);
117 }
118
119 return 1;
120}
121