mcabber/mcabber/xmpp_helper.c
author Mikael Berthe <mikael@lilotux.net>
Sun, 05 Jun 2016 20:06:46 +0200
changeset 2268 f5402d705f67
parent 2210 ea90906cb691
child 2303 4f3821bda633
permissions -rw-r--r--
Fix FSF addresses in all files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     1
/*
1599
dcd5d4c75199 Update/Add headers
Mikael Berthe <mikael@lilotux.net>
parents: 1598
diff changeset
     2
 * xmpp_helper.c    -- Jabber protocol helper functions
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     3
 *
1901
84bb3e893586 Update some file headers
Mikael Berthe <mikael@lilotux.net>
parents: 1896
diff changeset
     4
 * Copyright (C) 2008-2010 Frank Zschockelt <mcabber@freakysoft.de>
84bb3e893586 Update some file headers
Mikael Berthe <mikael@lilotux.net>
parents: 1896
diff changeset
     5
 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     6
 *
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     7
 * This program is free software; you can redistribute it and/or modify
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     8
 * it under the terms of the GNU General Public License as published by
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
     9
 * the Free Software Foundation; either version 2 of the License, or (at
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    10
 * your option) any later version.
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    11
 *
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    15
 * General Public License for more details.
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    16
 *
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License
2268
f5402d705f67 Fix FSF addresses in all files
Mikael Berthe <mikael@lilotux.net>
parents: 2210
diff changeset
    18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    19
 */
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    20
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    21
#include <string.h>
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    22
#include <stdlib.h>
2020
46a21258ad91 Fix some compiler warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1999
diff changeset
    23
#include <stdio.h>   // snprintf
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    24
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    25
#include "xmpp_helper.h"
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    26
#include "settings.h"
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    27
#include "utils.h"
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    28
#include "caps.h"
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    29
#include "logprint.h"
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    30
#include "config.h"
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    31
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    32
time_t iqlast; // last message/status change time
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
    33
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    34
extern char *imstatus_showmap[];
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    35
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    36
struct xmpp_error xmpp_errors[] = {
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    37
  {XMPP_ERROR_REDIRECT,              "302",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    38
    "Redirect",              "redirect",                "modify"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    39
  {XMPP_ERROR_BAD_REQUEST,           "400",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    40
    "Bad Request",           "bad-request",             "modify"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    41
  {XMPP_ERROR_NOT_AUTHORIZED,        "401",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    42
    "Not Authorized",        "not-authorized",          "auth"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    43
  {XMPP_ERROR_PAYMENT_REQUIRED,      "402",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    44
    "Payment Required",      "payment-required",        "auth"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    45
  {XMPP_ERROR_FORBIDDEN,             "403",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    46
    "Forbidden",             "forbidden",               "auth"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    47
  {XMPP_ERROR_NOT_FOUND,             "404",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    48
    "Not Found",             "item-not-found",          "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    49
  {XMPP_ERROR_NOT_ALLOWED,           "405",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    50
    "Not Allowed",           "not-allowed",             "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    51
  {XMPP_ERROR_NOT_ACCEPTABLE,        "406",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    52
    "Not Acceptable",        "not-acceptable",          "modify"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    53
  {XMPP_ERROR_REGISTRATION_REQUIRED, "407",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    54
    "Registration required", "registration-required",   "auth"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    55
  {XMPP_ERROR_REQUEST_TIMEOUT,       "408",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    56
    "Request Timeout",       "remote-server-timeout",   "wait"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    57
  {XMPP_ERROR_CONFLICT,              "409",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    58
    "Conflict",               "conflict",               "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    59
  {XMPP_ERROR_INTERNAL_SERVER_ERROR, "500",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    60
    "Internal Server Error", "internal-server-error",   "wait"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    61
  {XMPP_ERROR_NOT_IMPLEMENTED,       "501",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    62
    "Not Implemented",       "feature-not-implemented", "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    63
  {XMPP_ERROR_REMOTE_SERVER_ERROR,   "502",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    64
    "Remote Server Error",   "service-unavailable",     "wait"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    65
  {XMPP_ERROR_SERVICE_UNAVAILABLE,   "503",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    66
    "Service Unavailable",   "service-unavailable",     "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    67
  {XMPP_ERROR_REMOTE_SERVER_TIMEOUT, "504",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    68
    "Remote Server Timeout", "remote-server-timeout",   "wait"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    69
  {XMPP_ERROR_DISCONNECTED,          "510",
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    70
    "Disconnected",          "service-unavailable",     "cancel"},
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    71
  {0, NULL, NULL, NULL, NULL}
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    72
};
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
    73
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    74
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    75
static GSList *xmpp_additional_features = NULL;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    76
static char *ver, *ver_notavail;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    77
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
    78
void xmpp_add_feature(const char *xmlns)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    79
{
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    80
  if (xmlns) {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    81
    ver = NULL;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    82
    ver_notavail = NULL;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    83
    xmpp_additional_features = g_slist_append(xmpp_additional_features,
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
    84
                                              g_strdup(xmlns));
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    85
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    86
}
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    87
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
    88
void xmpp_del_feature(const char *xmlns)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    89
{
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    90
  GSList *feature = xmpp_additional_features;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    91
  while (feature) {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    92
    if (!strcmp(feature->data, xmlns)) {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    93
      ver = NULL;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    94
      ver_notavail = NULL;
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
    95
      g_free(feature->data);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    96
      xmpp_additional_features = g_slist_delete_link(xmpp_additional_features,
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    97
                                                     feature);
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    98
      return;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    99
    }
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
   100
    feature = g_slist_next(feature);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   101
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   102
}
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   103
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   104
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   105
const gchar* lm_message_node_get_child_value(LmMessageNode *node,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   106
                                             const gchar *child)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   107
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   108
  LmMessageNode *tmp;
2210
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   109
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   110
  if (G_UNLIKELY(!node || !child)) return NULL;
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   111
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   112
  tmp = lm_message_node_find_child(node, child);
1896
c5ab9cf3819a [MUC] Fix handling of empty room topic
Mikael Berthe <mikael@lilotux.net>
parents: 1857
diff changeset
   113
  if (tmp) {
c5ab9cf3819a [MUC] Fix handling of empty room topic
Mikael Berthe <mikael@lilotux.net>
parents: 1857
diff changeset
   114
    const gchar *val = lm_message_node_get_value(tmp);
c5ab9cf3819a [MUC] Fix handling of empty room topic
Mikael Berthe <mikael@lilotux.net>
parents: 1857
diff changeset
   115
    return (val ? val : "");
c5ab9cf3819a [MUC] Fix handling of empty room topic
Mikael Berthe <mikael@lilotux.net>
parents: 1857
diff changeset
   116
  }
c5ab9cf3819a [MUC] Fix handling of empty room topic
Mikael Berthe <mikael@lilotux.net>
parents: 1857
diff changeset
   117
  return NULL;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   118
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   119
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   120
static LmMessageNode *hidden = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   121
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   122
void lm_message_node_hide(LmMessageNode *node)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   123
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   124
  LmMessageNode *parent = node->parent, *prev_sibling = node->prev;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   125
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   126
  if (hidden) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   127
    hidden->children = hidden->next = hidden->prev = hidden->parent = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   128
    lm_message_node_unref(hidden);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   129
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   130
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   131
  if (parent->children == node)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   132
    parent->children = node->next;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   133
  if (prev_sibling)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   134
    prev_sibling->next = node->next;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   135
  if (node->next)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   136
    node->next->prev = prev_sibling;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   137
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   138
1729
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1704
diff changeset
   139
// Maybe not a good idea, because it uses internals of loudmouth...
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1704
diff changeset
   140
// It's used for rosternotes/bookmarks
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   141
LmMessageNode *lm_message_node_new(const gchar *name, const gchar *xmlns)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   142
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   143
  LmMessageNode *node;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   144
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   145
  node = g_new0 (LmMessageNode, 1);
2210
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   146
  if (G_UNLIKELY(!node)) return NULL;
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   147
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   148
  node->name       = g_strdup (name);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   149
  node->value      = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   150
  node->raw_mode   = FALSE;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   151
  node->attributes = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   152
  node->next       = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   153
  node->prev       = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   154
  node->parent     = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   155
  node->children   = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   156
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   157
  node->ref_count  = 1;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   158
  lm_message_node_set_attribute(node, "xmlns", xmlns);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   159
  return node;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   160
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   161
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   162
void lm_message_node_insert_childnode(LmMessageNode *node,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   163
                                      LmMessageNode *child)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   164
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   165
  LmMessageNode *x;
2210
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   166
  if (G_UNLIKELY(!node)) return;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   167
  lm_message_node_deep_ref(child);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   168
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   169
  if (node->children == NULL)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   170
    node->children = child;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   171
  else {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   172
    for (x = node->children; x->next; x = x->next)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   173
      ;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   174
    x->next = child;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   175
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   176
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   177
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   178
void lm_message_node_deep_ref(LmMessageNode *node)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   179
{
2210
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   180
  if (G_UNLIKELY(!node)) return;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   181
  lm_message_node_ref(node);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   182
  lm_message_node_deep_ref(node->next);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   183
  lm_message_node_deep_ref(node->children);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   184
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   185
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   186
const gchar* lm_message_get_from(LmMessage *m)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   187
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   188
  return lm_message_node_get_attribute(m->node, "from");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   189
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   190
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   191
const gchar* lm_message_get_id(LmMessage *m)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   192
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   193
  return lm_message_node_get_attribute(m->node, "id");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   194
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   195
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   196
LmMessage *lm_message_new_iq_from_query(LmMessage *m,
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   197
                                        LmMessageSubType type)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   198
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   199
  LmMessage *new;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   200
  const char *from = lm_message_node_get_attribute(m->node, "from");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   201
  const char *id = lm_message_node_get_attribute(m->node, "id");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   202
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   203
  new = lm_message_new_with_sub_type(from, LM_MESSAGE_TYPE_IQ,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   204
                                     type);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   205
  if (id)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   206
    lm_message_node_set_attribute(new->node, "id", id);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   207
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   208
  return new;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   209
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   210
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   211
//  entity_version(enum imstatus status)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   212
// Return a static version string for Entity Capabilities.
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   213
// It should be specific to the client version, please change the id
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   214
// if you alter mcabber's disco support (or add something to the version
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   215
// number) so that it doesn't conflict with the official client.
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   216
const char *entity_version(enum imstatus status)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   217
{
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   218
#ifndef MODULES_ENABLE
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   219
  static char *ver, *ver_notavail;
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   220
#endif
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   221
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   222
  if (ver && (status != notavail))
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   223
    return ver;
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   224
  if (ver_notavail)
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   225
    return ver_notavail;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   226
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   227
  caps_add("");
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   228
  caps_set_identity("", "client", PACKAGE_STRING, "pc");
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   229
  caps_add_feature("", NS_DISCO_INFO);
1999
51f032d5ca22 Add support for XEP-0115 Entity Capabilities, with offline cache
Hermitifier
parents: 1972
diff changeset
   230
  caps_add_feature("", NS_CAPS);
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   231
  caps_add_feature("", NS_MUC);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   232
  // advertise ChatStates only if they aren't disabled
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   233
  if (!settings_opt_get_int("disable_chatstates"))
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
   234
    caps_add_feature("", NS_CHATSTATES);
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   235
  caps_add_feature("", NS_TIME);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   236
  caps_add_feature("", NS_XMPP_TIME);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   237
  caps_add_feature("", NS_VERSION);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   238
  caps_add_feature("", NS_PING);
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   239
  caps_add_feature("", NS_COMMANDS);
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1600
diff changeset
   240
  caps_add_feature("", NS_RECEIPTS);
1951
1a01a7ef4e43 Add support for XEP-0249 / Direct MUC Invitations (Myhailo Danylenko)
Mikael Berthe <mikael@lilotux.net>
parents: 1901
diff changeset
   241
  caps_add_feature("", NS_X_CONFERENCE);
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   242
  if (!settings_opt_get_int("iq_last_disable") &&
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   243
      (!settings_opt_get_int("iq_last_disable_when_notavail") ||
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   244
       status != notavail))
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
   245
    caps_add_feature("", NS_LAST);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   246
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   247
  {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   248
    GSList *el = xmpp_additional_features;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   249
    while (el) {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   250
      caps_add_feature("", el->data);
1857
189c2a5a4e66 Fix coding style in xmpp_helper
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1811
diff changeset
   251
      el = g_slist_next(el);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   252
    }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   253
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   254
#endif
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   255
1600
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   256
  if (status == notavail) {
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   257
    ver_notavail = caps_generate();
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   258
    return ver_notavail;
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   259
  }
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   260
c5ee395fbc8c Updated Entity Capabilities support (XEP-0115)
franky
parents: 1599
diff changeset
   261
  ver = caps_generate();
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   262
  return ver;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   263
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   264
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   265
LmMessageNode *lm_message_node_find_xmlns(LmMessageNode *node,
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   266
                                          const char *xmlns)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   267
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   268
  LmMessageNode *x;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   269
  const char *p;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   270
2210
ea90906cb691 Add a few more checks
Mikael Berthe <mikael@lilotux.net>
parents: 2209
diff changeset
   271
  if (G_UNLIKELY(!node)) return NULL;
2209
412322678d59 Fix segfault when receiving a <private> Carbons message tag
Mikael Berthe <mikael@lilotux.net>
parents: 2033
diff changeset
   272
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   273
  for (x = node->children ; x; x = x->next) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   274
    if ((p = lm_message_node_get_attribute(x, "xmlns")) && !strcmp(p, xmlns))
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   275
      break;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   276
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   277
  return x;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   278
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   279
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   280
time_t lm_message_node_get_timestamp(LmMessageNode *node)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   281
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   282
  LmMessageNode *x;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   283
  const char *p;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   284
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   285
  x = lm_message_node_find_xmlns(node, NS_XMPP_DELAY);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   286
  if (x && (!strcmp(x->name, "delay")) &&
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   287
      (p = lm_message_node_get_attribute(x, "stamp")) != NULL)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   288
    return from_iso8601(p, 1);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   289
  x = lm_message_node_find_xmlns(node, NS_DELAY);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   290
  if (x && (p = lm_message_node_get_attribute(x, "stamp")) != NULL)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   291
    return from_iso8601(p, 1);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   292
  return 0;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   293
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   294
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   295
//  lm_message_new_presence(status, recipient, message)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   296
// Create an xmlnode with default presence attributes
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   297
// Note: the caller must free the node after use
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   298
LmMessage *lm_message_new_presence(enum imstatus st,
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   299
                                   const char *recipient,
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   300
                                   const char *msg)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   301
{
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   302
  unsigned int prio;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   303
  LmMessage *x = lm_message_new(recipient, LM_MESSAGE_TYPE_PRESENCE);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   304
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   305
  switch(st) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   306
    case away:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   307
    case notavail:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   308
    case dontdisturb:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   309
    case freeforchat:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   310
        lm_message_node_add_child(x->node, "show", imstatus_showmap[st]);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   311
        break;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   312
1972
45f0f0f60656 Remove deprecated status invisible
Mikael Berthe <mikael@lilotux.net>
parents: 1951
diff changeset
   313
#ifdef WITH_DEPRECATED_STATUS_INVISIBLE
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   314
    case invisible:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   315
        lm_message_node_set_attribute(x->node, "type", "invisible");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   316
        break;
1972
45f0f0f60656 Remove deprecated status invisible
Mikael Berthe <mikael@lilotux.net>
parents: 1951
diff changeset
   317
#endif
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   318
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   319
    case offline:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   320
        lm_message_node_set_attribute(x->node, "type", "unavailable");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   321
        break;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   322
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   323
    default:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   324
        break;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   325
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   326
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   327
  if (st == away || st == notavail)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   328
    prio = settings_opt_get_int("priority_away");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   329
  else
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   330
    prio = settings_opt_get_int("priority");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   331
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   332
  if (prio) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   333
    char strprio[8];
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   334
    snprintf(strprio, 8, "%d", (int)prio);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   335
    lm_message_node_add_child(x->node, "priority", strprio);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   336
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   337
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   338
  if (msg)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   339
    lm_message_node_add_child(x->node, "status", msg);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   340
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   341
  return x;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   342
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   343
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   344
static const char *defaulterrormsg(guint code)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   345
{
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   346
  int i;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   347
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   348
  for (i = 0; xmpp_errors[i].code; ++i) {
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   349
    if (xmpp_errors[i].code == code)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   350
      return xmpp_errors[i].meaning;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   351
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   352
  return NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   353
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   354
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   355
//  display_server_error(x)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   356
// Display the error to the user
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   357
// x: error tag xmlnode pointer
1800
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   358
void display_server_error(LmMessageNode *x, const char *from)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   359
{
1800
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   360
  const char *desc = NULL, *errname = NULL, *s;
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   361
  char *sdesc, *tmp;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   362
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   363
  if (!x) return;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   364
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   365
  /* RFC3920:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   366
   *    The <error/> element:
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   367
   *       o  MUST contain a child element corresponding to one of the defined
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   368
   *          stanza error conditions specified below; this element MUST be
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   369
   *          qualified by the 'urn:ietf:params:xml:ns:xmpp-stanzas' namespace.
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   370
   */
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   371
  if (x->children)
1704
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   372
    errname = x->children->name;
1800
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   373
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   374
  if (from)
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   375
    scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s] from <%s>",
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   376
                 (errname ? errname : ""), from);
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   377
  else
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   378
    scr_LogPrint(LPRINT_LOGNORM, "Received error packet [%s]",
d2747442918a When displaying a server error, show the sender JID
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   379
                 (errname ? errname : ""));
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   380
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   381
  // For backward compatibility
1704
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   382
  if (!errname && ((s = lm_message_node_get_attribute(x, "code")) != NULL)) {
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   383
    // Default message
1704
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   384
    desc = defaulterrormsg(atoi(s));
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   385
  }
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   386
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   387
  // Error tag data is better, if available
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   388
  s = lm_message_node_get_value(x);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   389
  if (s && *s) desc = s;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   390
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   391
  // And sometimes there is a text message
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   392
  s = lm_message_node_get_child_value(x, "text");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   393
  if (s && *s) desc = s;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   394
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   395
  // If we still have no description, let's give up
1704
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   396
  if (!desc || !*desc)
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   397
    return;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   398
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   399
  // Strip trailing newlines
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   400
  sdesc = g_strdup(desc);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   401
  for (tmp = sdesc; *tmp; tmp++) ;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   402
  if (tmp > sdesc)
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   403
    tmp--;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   404
  while (tmp >= sdesc && (*tmp == '\n' || *tmp == '\r'))
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   405
    *tmp-- = '\0';
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   406
1704
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   407
  if (*sdesc)
ab502d645378 Update display_server_error()
Mikael Berthe <mikael@lilotux.net>
parents: 1697
diff changeset
   408
    scr_LogPrint(LPRINT_LOGNORM, "Error message from server: %s", sdesc);
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   409
  g_free(sdesc);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   410
}
a087125d8fc8 Replace libjabber with loudmouth
franky
parents:
diff changeset
   411
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1800
diff changeset
   412
/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */