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