mam.c

  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 "stdbool.h"
 46#include "stdlib.h"
 47#include "string.h"
 48
 49static void _mam_request(xmppc_t *xmppc, char* to, bool pretty);
 50static int _mam_show(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
 51static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
 52static int _mam_display_pretty_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
 53
 54void mam_execute_command (xmppc_t *xmppc, int argc, char *argv[]) {
 55  if(argc == 2) {
 56    if (strcmp("list", argv[0]) == 0) {
 57      _mam_request(xmppc, argv[1], false);
 58      return;
 59    } else if (strcmp("pretty", argv[0]) == 0) {
 60      _mam_request(xmppc, argv[1], true);
 61      return;
 62    }
 63  }
 64
 65  printf("Command unbekannt!");
 66  xmpp_disconnect(xmppc->conn);
 67}
 68
 69static void _mam_request(xmppc_t *xmppc, char* to, bool pretty) {
 70
 71  if (pretty) {
 72    xmpp_handler_add (xmppc->conn, _mam_display_pretty_message, NULL,"message",NULL, xmppc);
 73  } else {
 74    xmpp_handler_add (xmppc->conn, _mam_display_message, NULL,"message",NULL, xmppc);
 75  }
 76
 77    char* id = xmpp_uuid_gen(xmppc->ctx);
 78    xmpp_stanza_t *iq = xmpp_iq_new(xmppc->ctx, "set", id);
 79    // query
 80    xmpp_stanza_t* query = xmpp_stanza_new(xmppc->ctx);
 81    xmpp_stanza_set_name(query, "query");
 82    xmpp_stanza_set_ns(query, "urn:xmpp:mam:2");
 83
 84  
 85    xmpp_stanza_t* x = xmpp_stanza_new(xmppc->ctx);
 86    xmpp_stanza_set_name(x,"x");
 87    xmpp_stanza_set_ns(x, "jabber:x:data"),
 88    xmpp_stanza_set_type(x,"submit");
 89    
 90    xmpp_stanza_add_child(query,x);
 91
 92    xmpp_stanza_t* f = xmpp_stanza_new(xmppc->ctx);
 93    xmpp_stanza_set_name(f,"field");
 94    xmpp_stanza_set_type(f,"hidden");
 95    xmpp_stanza_set_attribute(f, "var", "FORM_TYPE");
 96    xmpp_stanza_t* v = xmpp_stanza_new(xmppc->ctx);
 97    xmpp_stanza_set_name(v,"value");
 98    xmpp_stanza_t* b = xmpp_stanza_new(xmppc->ctx);
 99    xmpp_stanza_set_text(b, "urn:xmpp:mam:2");
100    
101    xmpp_stanza_add_child(x,f);
102    xmpp_stanza_add_child(f,v);
103    xmpp_stanza_add_child(v,b);
104
105    f = xmpp_stanza_new(xmppc->ctx);
106    xmpp_stanza_set_name(f,"field");
107    xmpp_stanza_set_attribute(f, "var", "with");
108    v = xmpp_stanza_new(xmppc->ctx);
109    xmpp_stanza_set_name(v,"value");
110    b = xmpp_stanza_new(xmppc->ctx);
111    xmpp_stanza_set_text(b, to);
112    //xmpp_stanza_set_text(b, xmpp_conn_get_jid (xmppc->conn));
113
114    xmpp_stanza_add_child(x,f);
115    xmpp_stanza_add_child(f,v);
116    xmpp_stanza_add_child(v,b);
117
118    xmpp_stanza_add_child(iq,query);
119    xmpp_id_handler_add(xmppc->conn, _mam_show, id, xmppc);
120    xmpp_send(xmppc->conn,iq);
121
122}
123
124int _mam_show(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata){
125  xmpp_stanza_t *fin = xmpp_stanza_get_child_by_name(stanza,"fin");
126  if(fin) {
127    xmpp_disconnect(conn);
128  }
129  return 1;
130}
131
132
133static int _mam_display_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) {
134  xmpp_stanza_t* result = xmpp_stanza_get_child_by_name(stanza,"result");
135  if( result && strcmp("urn:xmpp:mam:2", xmpp_stanza_get_ns(result)) == 0 ) {
136    char* s;
137    size_t len;
138    xmpp_stanza_to_text(xmpp_stanza_get_children(result) ,&s,&len);
139    printf("%s%s\x1b[m\n", ANSI_COLOR_YELLOW, s);
140  }
141  return 1;
142}
143
144static int _mam_display_pretty_message(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata)
145{
146  xmpp_stanza_t* result = xmpp_stanza_get_child_by_name(stanza,"result");
147
148  if( result && strcmp("urn:xmpp:mam:2", xmpp_stanza_get_ns(result)) == 0 ) {
149
150    xmpp_stanza_t* forwarded = xmpp_stanza_get_child_by_ns(result, "urn:xmpp:forward:0");
151    if (forwarded) {
152
153      xmpp_stanza_t* message = xmpp_stanza_get_child_by_name(forwarded, "message");
154      if (message) {
155        const char* const from = xmpp_stanza_get_from(message);
156        xmpp_stanza_t* body = xmpp_stanza_get_child_by_name(message, "body");
157
158        if (body) {
159          char *text = xmpp_stanza_get_text(body);
160          printf("%s%s\x1b[m: %s\n", ANSI_COLOR_YELLOW, from, text);
161          free(text);
162        }
163      }
164    }
165  }
166
167  return 1;
168}