discovery.c

  1/*!
  2 * @file File: discovery.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 "discovery.h"
 44#include "stdio.h"
 45#include "string.h"
 46
 47static void _discovery_get_info(xmppc_t *xmppc, char* to);
 48
 49static void _discovery_get_item(xmppc_t *xmppc, char* to);
 50
 51static int _discovery_info_result_handle(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
 52                  void *const userdata);
 53
 54static int _discovery_item_result_handle(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
 55                  void *const userdata);
 56
 57void discovery_execute_command(xmppc_t *xmppc, int argc, char *argv[]) {
 58  if( argc == 2 && ( strcmp("info", argv[0] ) == 0)) {
 59    _discovery_get_info(xmppc, argv[1]);
 60  } else if( argc == 2 && ( strcmp("item", argv[0] ) == 0)) {
 61    _discovery_get_item(xmppc, argv[1]);
 62  } else {
 63    logError(xmppc, "Befehl unbekannt");
 64  }
 65}
 66
 67static void _discovery_get_info(xmppc_t *xmppc, char* to) {
 68  char* id = xmpp_uuid_gen(xmppc->ctx);
 69  xmpp_stanza_t *iq = xmpp_iq_new(xmppc->ctx, "get", id);
 70  xmpp_stanza_set_to(iq,to);
 71  xmpp_stanza_set_from(iq, xmpp_conn_get_jid(xmppc->conn));
 72  xmpp_stanza_t *query = xmpp_stanza_new(xmppc->ctx);
 73  xmpp_stanza_set_name(query,"query");
 74  xmpp_stanza_set_ns(query,"http://jabber.org/protocol/disco#info");
 75  xmpp_stanza_add_child(iq, query);
 76  xmpp_id_handler_add(xmppc->conn, _discovery_info_result_handle, id, xmppc);
 77  xmpp_send(xmppc->conn, iq);  
 78}
 79
 80static void _discovery_get_item(xmppc_t *xmppc, char* to) {
 81  char* id = xmpp_uuid_gen(xmppc->ctx);
 82  xmpp_stanza_t *iq = xmpp_iq_new(xmppc->ctx, "get", id);
 83  xmpp_stanza_set_to(iq,to);
 84  xmpp_stanza_set_from(iq, xmpp_conn_get_jid(xmppc->conn));
 85  xmpp_stanza_t *query = xmpp_stanza_new(xmppc->ctx);
 86  xmpp_stanza_set_name(query,"query");
 87  xmpp_stanza_set_ns(query,"http://jabber.org/protocol/disco#items");
 88  xmpp_stanza_add_child(iq, query);
 89  xmpp_id_handler_add(xmppc->conn, _discovery_item_result_handle, id, xmppc);
 90  xmpp_send(xmppc->conn, iq);  
 91}
 92
 93static int _discovery_item_result_handle(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza,
 94                  void *const userdata) {
 95  xmppc_t *xmppc = (xmppc_t *)userdata;
 96 
 97  logInfo(xmppc,"Discover Item for: %s\n", xmpp_stanza_get_from(stanza));
 98
 99  if (strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
100    char* text;
101    size_t textlen;
102    xmpp_stanza_to_text(stanza,&text, &textlen);
103    logError(xmppc, "%s%s%s\n", ANSI_COLOR_RED, text, ANSI_COLOR_RESET);
104  }
105
106  xmpp_stanza_t* query = xmpp_stanza_get_child_by_name(stanza, "query");
107  if(query != NULL ) {
108    xmpp_stanza_t* c = xmpp_stanza_get_children(query);
109    while (c) {
110      if( strcmp( xmpp_stanza_get_name(c), "item" ) == 0) {
111        printf("%-40s - %-40s\n", xmpp_stanza_get_attribute(c, "jid"),  xmpp_stanza_get_attribute(c, "name"));
112      } 
113      c = xmpp_stanza_get_next(c);
114    }
115  }
116  xmpp_disconnect(xmppc->conn);
117  return 0;
118}
119
120static int _discovery_info_result_handle(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata){
121  xmppc_t *xmppc = (xmppc_t *)userdata;
122  
123  if (strcmp(xmpp_stanza_get_type(stanza), "error") == 0) {
124    char* text;
125    size_t textlen;
126    xmpp_stanza_to_text(stanza,&text, &textlen);
127    logError(xmppc, "%s%s%s\n", ANSI_COLOR_RED, text, ANSI_COLOR_RESET);
128    return 0;
129  }
130
131  logInfo(xmppc,"Discover Info for:\t%s\n", xmpp_stanza_get_from(stanza) );
132
133  xmpp_stanza_t* query = xmpp_stanza_get_child_by_name(stanza, "query");
134  if(query != NULL ) {
135    xmpp_stanza_t* c = xmpp_stanza_get_children(query);
136    
137    while (c) {
138      if( strcmp( xmpp_stanza_get_name(c), "identity" ) == 0) {
139        printf("%-10s - %-20s - %-40s\n", xmpp_stanza_get_attribute(c, "type"),xmpp_stanza_get_attribute(c, "category"), xmpp_stanza_get_attribute(c, "name")  );
140      } else if( strcmp( xmpp_stanza_get_name(c), "feature" ) == 0) {
141        if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_passwordprotected") == 0  ) printf("\tpassword protected\n");
142        else if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_hidden") == 0  ) printf("\tmuc_hidden\n");
143        else if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_temporary") == 0  ) printf("\ttemporary\n");
144        else if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_open") == 0  ) printf("\topen\n");
145        else if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_unmoderated") == 0  ) printf("\tunmoderated\n");
146        else if ( strcmp( xmpp_stanza_get_attribute(c, "var"), "muc_nonanonymous") == 0  ) printf("\tnonanonymous\n");
147        else printf("\t%-10s\n", xmpp_stanza_get_attribute(c, "var"));
148      }
149      c = xmpp_stanza_get_next(c);
150    }
151    
152  }
153
154  xmpp_disconnect(xmppc->conn);
155
156  return 0;
157}
158