mcabber/mcabber/hooks.c
author Mikael Berthe <mikael@lilotux.net>
Mon, 22 Mar 2010 21:47:22 +0100
changeset 1812 5dbb3ebbc466
parent 1811 e6d355e50d7a
child 1813 1c3efa9bc422
permissions -rw-r--r--
Add hk_unread_list_change() (Suggested by isbear)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     1
/*
699
ee03b56b93ee Update Copyright (2006)
Mikael Berthe <mikael@lilotux.net>
parents: 675
diff changeset
     2
 * hooks.c      -- Hooks layer
393
f8f3c7493457 Whitespace cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 391
diff changeset
     3
 *
1729
e6e89b1d7831 Minor style and header updates
Mikael Berthe <mikael@lilotux.net>
parents: 1683
diff changeset
     4
 * Copyright (C) 2005-2010 Mikael Berthe <mikael@lilotux.net>
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     5
 *
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     6
 * This program is free software; you can redistribute it and/or modify
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     7
 * it under the terms of the GNU General Public License as published by
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     8
 * the Free Software Foundation; either version 2 of the License, or (at
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
     9
 * your option) any later version.
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    10
 *
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    11
 * This program is distributed in the hope that it will be useful, but
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    14
 * General Public License for more details.
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    15
 *
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    17
 * along with this program; if not, write to the Free Software
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    19
 * USA
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    20
 */
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    21
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
    22
#include <loudmouth/loudmouth.h>
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
    23
#include <stdlib.h>
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
    24
#include <string.h>
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
    25
#include <sys/types.h>
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
    26
#include <unistd.h>
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    27
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    28
#include "hooks.h"
378
2e6c7b1440d1 Improve debugging/logging
Mikael Berthe <mikael@lilotux.net>
parents: 374
diff changeset
    29
#include "screen.h"
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    30
#include "roster.h"
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
    31
#include "histolog.h"
325
ff6fb51bfd78 Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents: 318
diff changeset
    32
#include "hbuf.h"
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
    33
#include "settings.h"
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
    34
#include "utils.h"
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
    35
#include "utf8.h"
1352
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
    36
#include "commands.h"
1653
fca9a4c17432 Improve UI latency and CPU usage
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1642
diff changeset
    37
#include "main.h"
1426
a64778f5f26b Implement FIFO named command pipe
Mikael Berthe <mikael@lilotux.net>
parents: 1425
diff changeset
    38
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    39
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    40
#include <glib.h>
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    41
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    42
typedef struct {
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    43
  hk_handler_t handler;
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    44
  gint      priority;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    45
  gpointer  userdata;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    46
  guint     hid;
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    47
} hook_list_data_t;
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    48
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    49
static GHashTable *hk_handler_hash = NULL;
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    50
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    51
//  _new_hook_id()
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    52
// Return a unique Hook Id
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    53
static guint _new_hook_id(void)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    54
{
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    55
  static guint hidcounter;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    56
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    57
  return ++hidcounter;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    58
}
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    59
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    60
//  _new_hook_queue(hookname)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    61
// Create a new hash table entry with a GSList pointer for the specified hook
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    62
static GSList **_new_hook_queue(const gchar *hookname)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    63
{
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    64
  GSList **p;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    65
  // Create the hash table if needed.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    66
  if (!hk_handler_hash) {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    67
    hk_handler_hash = g_hash_table_new_full(&g_str_hash, &g_str_equal,
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    68
                                            &g_free, &g_free);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    69
    if (!hk_handler_hash) {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    70
      scr_log_print(LPRINT_LOGNORM, "Couldn't create hook hash table!");
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    71
      return NULL;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    72
    }
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    73
  }
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    74
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    75
  // Add a queue for the requested hook
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    76
  p = g_new(GSList*, 1);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    77
  *p = NULL;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    78
  g_hash_table_insert(hk_handler_hash, g_strdup(hookname), p);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    79
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    80
  return p;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    81
}
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    82
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    83
static gint _hk_compare_prio(hook_list_data_t *a, hook_list_data_t *b)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    84
{
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    85
  if (a->priority > b->priority)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    86
    return 1;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    87
  return 0;
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    88
}
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    89
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    90
//  hk_add_handler(handler, hookname, priority, userdata)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    91
// Create a hook handler and a hook hash entry if needed.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    92
// Return the handler id.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    93
guint hk_add_handler(hk_handler_t handler, const gchar *hookname,
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    94
                     gint priority, gpointer userdata)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
    95
{
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    96
  GSList **hqueue = NULL;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    97
  hook_list_data_t *h = g_new(hook_list_data_t, 1);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    98
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
    99
  h->handler  = handler;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   100
  h->priority = priority;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   101
  h->userdata = userdata;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   102
  h->hid      = _new_hook_id();
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   103
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   104
  if (hk_handler_hash)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   105
    hqueue = g_hash_table_lookup(hk_handler_hash, hookname);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   106
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   107
  if (!hqueue)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   108
    hqueue = _new_hook_queue(hookname);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   109
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   110
  if (!hqueue)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   111
    return 0;
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   112
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   113
  *hqueue = g_slist_insert_sorted(*hqueue, h, (GCompareFunc)_hk_compare_prio);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   114
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   115
  return h->hid;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   116
}
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   117
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   118
static gint _hk_queue_search_cb(hook_list_data_t *a, guint *hid)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   119
{
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   120
  if (a->hid == *hid)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   121
    return 0;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   122
  return 1;
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   123
}
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   124
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   125
//  hk_del_handler(hookname, hook_id)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   126
// Remove the handler with specified hook id from the hookname queue.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   127
// The hash entry is removed if the queue is empty.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   128
void hk_del_handler(const gchar *hookname, guint hid)
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   129
{
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   130
  GSList **hqueue;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   131
  GSList *el;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   132
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   133
  if (!hid)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   134
    return;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   135
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   136
  hqueue = g_hash_table_lookup(hk_handler_hash, hookname);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   137
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   138
  if (!hqueue) {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   139
    scr_log_print(LPRINT_LOGNORM, "*ERROR*: Couldn't remove hook handler!");
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   140
    return;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   141
  }
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   142
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   143
  el = g_slist_find_custom(*hqueue, &hid,
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   144
                           (GCompareFunc)_hk_queue_search_cb);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   145
  if (el) {
1787
1f913c92c9b2 Add urgent flag to the hook-message-in hook
Mikael Berthe <mikael@lilotux.net>
parents: 1785
diff changeset
   146
    g_free(el->data);
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   147
    *hqueue = g_slist_delete_link(*hqueue, el);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   148
    // Remove hook hash table entry if the hook queue is empty
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   149
    if (!*hqueue)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   150
      g_hash_table_remove(hk_handler_hash, hookname);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   151
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   152
}
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   153
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   154
//  hk_run_handlers(hookname, args)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   155
// Process all hooks for the "hookname" event.
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   156
// Note that the processing is interrupted as soon as one of the handlers
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   157
// do not return HOOK_HANDLER_RESULT_ALLOW_MORE_HOOKS (i.e. 0).
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   158
guint hk_run_handlers(const gchar *hookname, hk_arg_t *args)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   159
{
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   160
  GSList **hqueue;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   161
  GSList *h;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   162
  guint ret = 0;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   163
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   164
  if (!hk_handler_hash)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   165
    return 0;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   166
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   167
  hqueue = g_hash_table_lookup(hk_handler_hash, hookname);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   168
  if (!hqueue)
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   169
    return 0; // Should we use a special code?
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   170
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   171
  for (h = *hqueue; h; h = g_slist_next(h)) {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   172
    hook_list_data_t *data = h->data;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   173
    ret = (data->handler)(hookname, args, data->userdata);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   174
    if (ret) break;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   175
  }
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   176
  return ret;
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   177
}
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   178
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   179
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   180
static char *extcmd;
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   181
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   182
static const char *COMMAND_ME = "/me ";
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   183
1398
f8321420ed7a Remove useless inline's
Mikael Berthe <mikael@lilotux.net>
parents: 1359
diff changeset
   184
void hk_message_in(const char *bjid, const char *resname,
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   185
                   time_t timestamp, const char *msg, LmMessageSubType type,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   186
                   guint encrypted)
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   187
{
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   188
  int new_guy = FALSE;
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   189
  int is_groupchat = FALSE; // groupchat message
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   190
  int is_room = FALSE;      // window is a room window
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   191
  int log_muc_conf = FALSE;
1129
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   192
  int active_window = FALSE;
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   193
  int message_flags = 0;
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   194
  guint rtype = ROSTER_TYPE_USER;
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   195
  char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL;
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   196
  GSList *roster_usr;
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1210
diff changeset
   197
  unsigned mucnicklen = 0;
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   198
  const char *ename = NULL;
1805
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   199
  gboolean attention = FALSE, mucprivmsg = FALSE;
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   200
1484
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   201
  if (encrypted == ENCRYPTED_PGP)
1055
6eb1efea75d0 PGP: Visual encryption flag
Mikael Berthe <mikael@lilotux.net>
parents: 1010
diff changeset
   202
    message_flags |= HBB_PREFIX_PGPCRYPT;
1484
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   203
  else if (encrypted == ENCRYPTED_OTR)
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   204
    message_flags |= HBB_PREFIX_OTRCRYPT;
1055
6eb1efea75d0 PGP: Visual encryption flag
Mikael Berthe <mikael@lilotux.net>
parents: 1010
diff changeset
   205
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   206
  if (type == LM_MESSAGE_SUB_TYPE_GROUPCHAT) {
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   207
    rtype = ROSTER_TYPE_ROOM;
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   208
    is_groupchat = TRUE;
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   209
    log_muc_conf = settings_opt_get_int("log_muc_conf");
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   210
    if (!resname) {
648
16cd6c858a5d MUC: No new_message flag for server messages
Mikael Berthe <mikael@lilotux.net>
parents: 646
diff changeset
   211
      message_flags = HBB_PREFIX_INFO | HBB_PREFIX_NOFLAG;
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   212
      resname = "";
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   213
      wmsg = bmsg = g_strdup_printf("~ %s", msg);
646
a8cb28ca5ff4 MUC: Server messages are prefixed with "~ " instead of "<> "
Mikael Berthe <mikael@lilotux.net>
parents: 625
diff changeset
   214
    } else {
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   215
      wmsg = bmsg = g_strdup_printf("<%s> %s", resname, msg);
1290
e42f48103609 Drawing the MUC nicks by a different color
Michal 'vorner' Vaner <vorner@ucw.cz>
parents: 1210
diff changeset
   216
      mucnicklen = strlen(resname) + 2;
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   217
      if (!strncmp(msg, COMMAND_ME, strlen(COMMAND_ME)))
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   218
        wmsg = mmsg = g_strdup_printf("*%s %s", resname, msg+4);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   219
    }
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   220
  } else {
1116
14e458020e23 Fix a bug in the history logging (incoming "/me " messages)
Mikael Berthe <mikael@lilotux.net>
parents: 1109
diff changeset
   221
    bmsg = g_strdup(msg);
1188
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   222
    if (!strncmp(msg, COMMAND_ME, strlen(COMMAND_ME))) {
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   223
      gchar *shortid = g_strdup(bjid);
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   224
      if (settings_opt_get_int("buddy_me_fulljid") == FALSE) {
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   225
        gchar *p = strchr(shortid, '@'); // Truncate the jid
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   226
        if (p)
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   227
          *p = '\0';
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   228
      }
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   229
      wmsg = mmsg = g_strdup_printf("*%s %s", shortid, msg+4);
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   230
      g_free(shortid);
ef40688d87bd Add option 'buddy_me_fulljid'
Mikael Berthe <mikael@lilotux.net>
parents: 1167
diff changeset
   231
    } else
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   232
      wmsg = (char*) msg;
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   233
  }
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   234
1797
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   235
#ifdef MODULES_ENABLE
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   236
  {
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   237
    guint h_result;
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   238
    hk_arg_t args[] = {
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   239
      { "jid", bjid },
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   240
      { "resource", resname },
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   241
      { "message", wmsg },
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   242
      { "groupchat", is_groupchat ? "true" : "false" },
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   243
      { NULL, NULL },
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   244
    };
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   245
    h_result = hk_run_handlers(HOOK_PRE_MESSAGE_IN, args);
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   246
    if (h_result == HOOK_HANDLER_RESULT_NO_MORE_HOOK_DROP_DATA) {
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   247
      scr_LogPrint(LPRINT_DEBUG, "Message dropped (hook result).");
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   248
      g_free(bmsg);
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   249
      g_free(mmsg);
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   250
      return;
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   251
    }
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   252
  }
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   253
#endif
246c5d359c97 Add "hook-pre-message-in" hook
Mikael Berthe <mikael@lilotux.net>
parents: 1796
diff changeset
   254
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   255
  // If this user isn't in the roster, we add it
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   256
  roster_usr = roster_find(bjid, jidsearch, 0);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   257
  if (!roster_usr) {
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   258
    new_guy = TRUE;
1355
9716cf8a0726 Add on_server flag
Mikael Berthe <mikael@lilotux.net>
parents: 1352
diff changeset
   259
    roster_usr = roster_add_user(bjid, NULL, NULL, rtype, sub_none, -1);
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   260
    if (!roster_usr) { // Shouldn't happen...
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   261
      scr_LogPrint(LPRINT_LOGNORM, "ERROR: unable to add buddy!");
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   262
      g_free(bmsg);
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   263
      g_free(mmsg);
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   264
      return;
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   265
    }
489
21ab22a60bcb Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents: 485
diff changeset
   266
  } else if (is_groupchat) {
21ab22a60bcb Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents: 485
diff changeset
   267
    // Make sure the type is ROOM
21ab22a60bcb Auto-convert a roster item to "room" type when receiving a groupchat message
Mikael Berthe <mikael@lilotux.net>
parents: 485
diff changeset
   268
    buddy_settype(roster_usr->data, ROSTER_TYPE_ROOM);
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   269
  }
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   270
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   271
  is_room = !!(buddy_gettype(roster_usr->data) & ROSTER_TYPE_ROOM);
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   272
728
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   273
  if (is_room) {
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   274
    if (!is_groupchat) {
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   275
      // This is a private message from a room participant
1108
a0e9eaa9f5b4 Fix small memory leak in hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 1107
diff changeset
   276
      g_free(bmsg);
734
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   277
      if (!resname) {
728
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   278
        resname = "";
734
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   279
        wmsg = bmsg = g_strdup(msg);
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   280
      } else {
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   281
        wmsg = bmsg = g_strdup_printf("PRIV#<%s> %s", resname, msg);
1108
a0e9eaa9f5b4 Fix small memory leak in hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 1107
diff changeset
   282
        if (!strncmp(msg, COMMAND_ME, strlen(COMMAND_ME))) {
a0e9eaa9f5b4 Fix small memory leak in hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 1107
diff changeset
   283
          g_free(mmsg);
734
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   284
          wmsg = mmsg = g_strdup_printf("PRIV#*%s %s", resname, msg+4);
1108
a0e9eaa9f5b4 Fix small memory leak in hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 1107
diff changeset
   285
        }
1805
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   286
        mucprivmsg = TRUE;
734
1d9f0efcfcee MUC: Do not mark error messages as private
Mikael Berthe <mikael@lilotux.net>
parents: 729
diff changeset
   287
      }
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   288
      message_flags |= HBB_PREFIX_HLIGHT;
728
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   289
    } else {
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   290
      // This is a regular chatroom message.
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   291
      const char *nick = buddy_getnickname(roster_usr->data);
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   292
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   293
      if (nick) {
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   294
        // Let's see if we are the message sender, in which case we'll
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   295
        // highlight it.
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   296
        if (resname && !strcmp(resname, nick)) {
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   297
          message_flags |= HBB_PREFIX_HLIGHT_OUT;
1787
1f913c92c9b2 Add urgent flag to the hook-message-in hook
Mikael Berthe <mikael@lilotux.net>
parents: 1785
diff changeset
   298
        } else {
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   299
          // We're not the sender.  Can we see our nick?
1207
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   300
          const char *msgptr = msg;
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   301
          while ((msgptr = strcasestr(msgptr, nick)) != NULL) {
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   302
            const char *leftb, *rightb;
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   303
            // The message contains our nick.  Let's check it's not
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   304
            // in the middle of another word (i.e. preceded/followed
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   305
            // immediately by an alphanumeric character or an underscore.
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   306
            rightb = msgptr+strlen(nick);
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   307
            if (msgptr == msg)
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   308
              leftb = NULL;
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   309
            else
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   310
              leftb = prev_char((char*)msgptr, msg);
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   311
            msgptr = next_char((char*)msgptr);
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   312
            // Check left boundary
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   313
            if (leftb && (iswalnum(get_char(leftb)) || get_char(leftb) == '_'))
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   314
              continue;
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   315
            // Check right boundary
8f0af3b88cef MUC: improve nickname detection and add option 'muc_disable_nick_hl'
Mikael Berthe <mikael@lilotux.net>
parents: 1205
diff changeset
   316
            if (!iswalnum(get_char(rightb)) && get_char(rightb) != '_')
1801
07e73049f7c5 Replace "urgent" with "attention" in HOOK_POST_MESSAGE_IN
Mikael Berthe <mikael@lilotux.net>
parents: 1797
diff changeset
   317
              attention = TRUE;
07e73049f7c5 Replace "urgent" with "attention" in HOOK_POST_MESSAGE_IN
Mikael Berthe <mikael@lilotux.net>
parents: 1797
diff changeset
   318
            if (attention && !settings_opt_get_int("muc_disable_nick_hl"))
1204
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   319
              message_flags |= HBB_PREFIX_HLIGHT;
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   320
          }
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   321
        }
e802ec0c02d2 Basic support for nick highlighting in MUC rooms (the whole line is colored)
Mikael Berthe <mikael@lilotux.net>
parents: 1188
diff changeset
   322
      }
728
421b337dc6d2 Chat window: Highlight the flag in our messages
Mikael Berthe <mikael@lilotux.net>
parents: 699
diff changeset
   323
    }
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   324
  }
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   325
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   326
  if (type  == LM_MESSAGE_SUB_TYPE_ERROR) {
325
ff6fb51bfd78 Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents: 318
diff changeset
   327
    message_flags = HBB_PREFIX_ERR | HBB_PREFIX_IN;
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   328
    scr_LogPrint(LPRINT_LOGNORM, "Error message received from <%s>", bjid);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   329
  }
325
ff6fb51bfd78 Handle "error" message type
Mikael Berthe <mikael@lilotux.net>
parents: 318
diff changeset
   330
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 160
diff changeset
   331
  // Note: the hlog_write should not be called first, because in some
1783
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   332
  // cases scr_write_incoming_message() will load the history and we'd
178
cfefae4b6de9 [/trunk] Changeset 190 by mikael
mikael
parents: 160
diff changeset
   333
  // have the message twice...
1783
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   334
  scr_write_incoming_message(bjid, wmsg, timestamp, message_flags, mucnicklen);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   335
1805
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   336
  // Set urgent (a.k.a. "attention") flag
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   337
  {
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   338
    guint uip;
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   339
    if (is_groupchat) {
1808
7a1c40549291 Fix attention sign for MUC private messages
Mikael Berthe <mikael@lilotux.net>
parents: 1805
diff changeset
   340
      if (attention)      uip = ROSTER_UI_PRIO_MUC_HL_MESSAGE;
1805
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   341
      else                uip = ROSTER_UI_PRIO_MUC_MESSAGE;
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   342
    } else {
1808
7a1c40549291 Fix attention sign for MUC private messages
Mikael Berthe <mikael@lilotux.net>
parents: 1805
diff changeset
   343
      if (mucprivmsg)     uip = ROSTER_UI_PRIO_MUC_PRIV_MESSAGE;
7a1c40549291 Fix attention sign for MUC private messages
Mikael Berthe <mikael@lilotux.net>
parents: 1805
diff changeset
   344
      else if (attention) uip = ROSTER_UI_PRIO_ATTENTION_MESSAGE;
7a1c40549291 Fix attention sign for MUC private messages
Mikael Berthe <mikael@lilotux.net>
parents: 1805
diff changeset
   345
      else                uip = ROSTER_UI_PRIO_PRIVATE_MESSAGE;
1805
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   346
    }
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   347
    scr_setattentionflag_if_needed(bjid, FALSE, uip, prio_max);
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   348
  }
48e47198a9db Use urgent flag
Mikael Berthe <mikael@lilotux.net>
parents: 1801
diff changeset
   349
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   350
  // We don't log the modified message, but the original one
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   351
  if (wmsg == mmsg)
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   352
    wmsg = bmsg;
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   353
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   354
  // - We don't log the message if it is an error message
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   355
  // - We don't log the message if it is a private conf. message
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   356
  // - We don't log the message if it is groupchat message and the log_muc_conf
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   357
  //   option is off (and it is not a history line)
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   358
  if (!(message_flags & HBB_PREFIX_ERR) &&
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   359
      (!is_room || (is_groupchat && log_muc_conf && !timestamp)))
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1339
diff changeset
   360
    hlog_write_message(bjid, timestamp, 0, wmsg);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   361
1129
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   362
  if (settings_opt_get_int("events_ignore_active_window") &&
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   363
      current_buddy && scr_get_chatmode()) {
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   364
    gpointer bud = BUDDATA(current_buddy);
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   365
    if (bud) {
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   366
      const char *cjid = buddy_getjid(bud);
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   367
      if (cjid && !strcasecmp(cjid, bjid))
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   368
        active_window = TRUE;
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   369
    }
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   370
  }
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   371
1447
ba706e97d6ec Add option 'eventcmd_use_nickname'
Mikael Berthe <mikael@lilotux.net>
parents: 1444
diff changeset
   372
  if (settings_opt_get_int("eventcmd_use_nickname"))
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   373
    ename = roster_getname(bjid);
1447
ba706e97d6ec Add option 'eventcmd_use_nickname'
Mikael Berthe <mikael@lilotux.net>
parents: 1444
diff changeset
   374
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   375
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   376
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   377
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   378
      { "jid", bjid },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   379
      { "resource", resname },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   380
      { "message", wmsg },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   381
      { "groupchat", is_groupchat ? "true" : "false" },
1801
07e73049f7c5 Replace "urgent" with "attention" in HOOK_POST_MESSAGE_IN
Mikael Berthe <mikael@lilotux.net>
parents: 1797
diff changeset
   382
      { "attention", attention ? "true" : "false" },
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   383
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   384
    };
1796
5e2db25fdb17 Rename "hook-message-in" to "hook-post-message-in"
Mikael Berthe <mikael@lilotux.net>
parents: 1795
diff changeset
   385
    hk_run_handlers(HOOK_POST_MESSAGE_IN, args);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   386
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   387
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   388
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   389
  // External command
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   390
  // - We do not call hk_ext_cmd() for history lines in MUC
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   391
  // - We do call hk_ext_cmd() for private messages in a room
1129
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   392
  // - We do call hk_ext_cmd() for messages to the current window
1a109ebf3f24 Add option "events_ignore_active_window"
Mikael Berthe <mikael@lilotux.net>
parents: 1116
diff changeset
   393
  if (!active_window && ((is_groupchat && !timestamp) || !is_groupchat))
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   394
    hk_ext_cmd(ename ? ename : bjid, (is_groupchat ? 'G' : 'M'), 'R', wmsg);
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   395
808
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   396
  // Display the sender in the log window
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   397
  if ((!is_groupchat) && !(message_flags & HBB_PREFIX_ERR) &&
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   398
      settings_opt_get_int("log_display_sender")) {
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   399
    const char *name = roster_getname(bjid);
808
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   400
    if (!name) name = "";
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   401
    scr_LogPrint(LPRINT_NORMAL, "Message received from %s <%s/%s>",
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   402
                 name, bjid, (resname ? resname : ""));
808
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   403
  }
4a6ce276ffca New option "log_display_sender"
Mikael Berthe <mikael@lilotux.net>
parents: 774
diff changeset
   404
1210
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   405
  // Beep, if enabled:
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   406
  // - if it's a private message
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   407
  // - if it's a public message and it's highlighted
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   408
  if (settings_opt_get_int("beep_on_message")) {
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   409
    if ((!is_groupchat && !(message_flags & HBB_PREFIX_ERR)) ||
7c8672bbede5 Use beep_on_message's value for highlighted MUC room messages
Mikael Berthe <mikael@lilotux.net>
parents: 1207
diff changeset
   410
        (is_groupchat  && (message_flags & HBB_PREFIX_HLIGHT)))
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   411
      scr_beep();
887
df4bb1535ef8 Fix beep_on_message in rooms
Mikael Berthe <mikael@lilotux.net>
parents: 856
diff changeset
   412
  }
675
ae7016aae014 New "beep_on_message" option
Mikael Berthe <mikael@lilotux.net>
parents: 656
diff changeset
   413
1010
54405d09b15a Add a call to buddylist_build()
Mikael Berthe <mikael@lilotux.net>
parents: 967
diff changeset
   414
  // We need to update the roster if the sender is unknown or
1311
0dda8238af21 Implement "/roster display"
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   415
  // if the sender is offline/invisible and a filter is set.
146
50f23c38743a [/trunk] Changeset 158 by mikael
mikael
parents: 141
diff changeset
   416
  if (new_guy ||
477
d11e5ce2582c Improve hk_message_in()
Mikael Berthe <mikael@lilotux.net>
parents: 468
diff changeset
   417
      (buddy_getstatus(roster_usr->data, NULL) == offline &&
1311
0dda8238af21 Implement "/roster display"
Mikael Berthe <mikael@lilotux.net>
parents: 1290
diff changeset
   418
       buddylist_isset_filter()))
146
50f23c38743a [/trunk] Changeset 158 by mikael
mikael
parents: 141
diff changeset
   419
  {
141
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   420
    update_roster = TRUE;
6533a231a65e [/trunk] Changeset 153 by mikael
mikael
parents: 120
diff changeset
   421
  }
447
03bb57383cea Initial Multi-User Chat support
Mikael Berthe <mikael@lilotux.net>
parents: 438
diff changeset
   422
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   423
  g_free(bmsg);
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   424
  g_free(mmsg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   425
}
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   426
484
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   427
//  hk_message_out()
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   428
// nick should be set for private messages in a chat room, and null for
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   429
// normal messages.
1398
f8321420ed7a Remove useless inline's
Mikael Berthe <mikael@lilotux.net>
parents: 1359
diff changeset
   430
void hk_message_out(const char *bjid, const char *nick,
1602
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   431
                    time_t timestamp, const char *msg,
f4a2c6f767d1 Message Receipts support (XEP-0184)
franky
parents: 1599
diff changeset
   432
                    guint encrypted, gpointer xep184)
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   433
{
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   434
  char *wmsg = NULL, *bmsg = NULL, *mmsg = NULL;
1484
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   435
  guint cryptflag = 0;
484
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   436
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   437
  if (nick) {
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   438
    wmsg = bmsg = g_strdup_printf("PRIV#<%s> %s", nick, msg);
1109
7e383aa48b3d Fix a small mistake introduced in changeset 1db3d045e95c
Mikael Berthe <mikael@lilotux.net>
parents: 1108
diff changeset
   439
    if (!strncmp(msg, COMMAND_ME, strlen(COMMAND_ME))) {
7e383aa48b3d Fix a small mistake introduced in changeset 1db3d045e95c
Mikael Berthe <mikael@lilotux.net>
parents: 1108
diff changeset
   440
      const char *mynick = roster_getnickname(bjid);
7e383aa48b3d Fix a small mistake introduced in changeset 1db3d045e95c
Mikael Berthe <mikael@lilotux.net>
parents: 1108
diff changeset
   441
      wmsg = mmsg = g_strdup_printf("PRIV#<%s> *%s %s", nick,
7e383aa48b3d Fix a small mistake introduced in changeset 1db3d045e95c
Mikael Berthe <mikael@lilotux.net>
parents: 1108
diff changeset
   442
                                    (mynick ? mynick : "me"), msg+4);
7e383aa48b3d Fix a small mistake introduced in changeset 1db3d045e95c
Mikael Berthe <mikael@lilotux.net>
parents: 1108
diff changeset
   443
    }
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   444
  } else {
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   445
    wmsg = (char*)msg;
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   446
    if (!strncmp(msg, COMMAND_ME, strlen(COMMAND_ME))) {
1611
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1607
diff changeset
   447
      char *myid = jid_get_username(settings_opt_get("jid"));
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1607
diff changeset
   448
      if (myid) {
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1607
diff changeset
   449
        wmsg = mmsg = g_strdup_printf("*%s %s", myid, msg+4);
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1607
diff changeset
   450
        g_free(myid);
f9bf561e54d0 Use the username for authentication, added jid_get_username() to utils.c
franky
parents: 1607
diff changeset
   451
      }
567
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   452
    }
6f490c725999 Handle "/me " in messages
Mikael Berthe <mikael@lilotux.net>
parents: 541
diff changeset
   453
  }
484
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   454
485
55aa45eb7ece Fix a small display bug
Mikael Berthe <mikael@lilotux.net>
parents: 484
diff changeset
   455
  // Note: the hlog_write should not be called first, because in some
1783
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   456
  // cases scr_write_outgoing_message() will load the history and we'd
485
55aa45eb7ece Fix a small display bug
Mikael Berthe <mikael@lilotux.net>
parents: 484
diff changeset
   457
  // have the message twice...
1484
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   458
  if (encrypted == ENCRYPTED_PGP)
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   459
    cryptflag = HBB_PREFIX_PGPCRYPT;
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   460
  else if (encrypted == ENCRYPTED_OTR)
7b36b91a4388 New UI message flag (O) when OTR is used
Mikael Berthe <mikael@lilotux.net>
parents: 1450
diff changeset
   461
    cryptflag = HBB_PREFIX_OTRCRYPT;
1783
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   462
  scr_write_outgoing_message(bjid, wmsg, cryptflag, xep184);
485
55aa45eb7ece Fix a small display bug
Mikael Berthe <mikael@lilotux.net>
parents: 484
diff changeset
   463
55aa45eb7ece Fix a small display bug
Mikael Berthe <mikael@lilotux.net>
parents: 484
diff changeset
   464
  // We don't log private messages
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   465
  if (!nick)
1342
06441b6cc23a History logfile: Use a special code (MI) for local MUC info messages
Mikael Berthe <mikael@lilotux.net>
parents: 1339
diff changeset
   466
    hlog_write_message(bjid, timestamp, 1, msg);
485
55aa45eb7ece Fix a small display bug
Mikael Berthe <mikael@lilotux.net>
parents: 484
diff changeset
   467
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   468
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   469
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   470
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   471
      { "jid", bjid },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   472
      { "message", wmsg },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   473
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   474
    };
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   475
    hk_run_handlers(HOOK_MESSAGE_OUT, args);
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   476
    // TODO: check (and use) return value
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   477
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   478
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   479
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   480
  // External command
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   481
  hk_ext_cmd(bjid, 'M', 'S', NULL);
484
00e2d3821a5b Display PRIV for private messages in a room when using /say_to
Mikael Berthe <mikael@lilotux.net>
parents: 483
diff changeset
   482
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   483
  g_free(bmsg);
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   484
  g_free(mmsg);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   485
}
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   486
1398
f8321420ed7a Remove useless inline's
Mikael Berthe <mikael@lilotux.net>
parents: 1359
diff changeset
   487
void hk_statuschange(const char *bjid, const char *resname, gchar prio,
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   488
                     time_t timestamp, enum imstatus status,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   489
                     const char *status_msg)
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   490
{
516
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   491
  int st_in_buf;
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   492
  enum imstatus oldstat;
1450
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   493
  char *bn;
655
de6837908702 Use "(...)" when cutting a multi-line status message in the log window
Mikael Berthe <mikael@lilotux.net>
parents: 648
diff changeset
   494
  char *logsmsg;
614
d3020b2c4da2 Show empty resources
Mikael Berthe <mikael@lilotux.net>
parents: 603
diff changeset
   495
  const char *rn = (resname ? resname : "");
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   496
  const char *ename = NULL;
1447
ba706e97d6ec Add option 'eventcmd_use_nickname'
Mikael Berthe <mikael@lilotux.net>
parents: 1444
diff changeset
   497
ba706e97d6ec Add option 'eventcmd_use_nickname'
Mikael Berthe <mikael@lilotux.net>
parents: 1444
diff changeset
   498
  if (settings_opt_get_int("eventcmd_use_nickname"))
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   499
    ename = roster_getname(bjid);
510
6c3cd3aa4a91 New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents: 489
diff changeset
   500
1450
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   501
  oldstat = roster_getstatus(bjid, resname);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   502
516
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   503
  st_in_buf = settings_opt_get_int("show_status_in_buffer");
1450
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   504
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   505
  if (settings_opt_get_int("log_display_presence")) {
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   506
    int buddy_format = settings_opt_get_int("buddy_format");
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   507
    bn = NULL;
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   508
    if (buddy_format) {
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   509
      const char *name = roster_getname(bjid);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   510
      if (name && strcmp(name, bjid)) {
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   511
        if (buddy_format == 1)
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   512
          bn = g_strdup_printf("%s <%s/%s>", name, bjid, rn);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   513
        else if (buddy_format == 2)
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   514
          bn = g_strdup_printf("%s/%s", name, rn);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   515
        else if (buddy_format == 3)
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   516
          bn = g_strdup_printf("%s", name);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   517
      }
510
6c3cd3aa4a91 New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents: 489
diff changeset
   518
    }
1450
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   519
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   520
    if (!bn)
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   521
      bn = g_strdup_printf("<%s/%s>", bjid, rn);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   522
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   523
    logsmsg = g_strdup(status_msg ? status_msg : "");
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   524
    replace_nl_with_dots(logsmsg);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   525
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   526
    scr_LogPrint(LPRINT_LOGNORM, "Buddy status has changed: [%c>%c] %s %s",
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   527
                 imstatus2char[oldstat], imstatus2char[status], bn, logsmsg);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   528
    g_free(logsmsg);
c6ebf66a4f80 Add option 'log_display_presence'
Mikael Berthe <mikael@lilotux.net>
parents: 1449
diff changeset
   529
    g_free(bn);
510
6c3cd3aa4a91 New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents: 489
diff changeset
   530
  }
6c3cd3aa4a91 New 'buddy_format' option
Mikael Berthe <mikael@lilotux.net>
parents: 489
diff changeset
   531
516
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   532
  if (st_in_buf == 2 ||
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   533
      (st_in_buf == 1 && (status == offline || oldstat == offline))) {
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   534
    // Write the status change in the buddy's buffer, only if it already exists
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   535
    if (scr_buddy_buffer_exists(bjid)) {
516
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   536
      bn = g_strdup_printf("Buddy status has changed: [%c>%c] %s",
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   537
                           imstatus2char[oldstat], imstatus2char[status],
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   538
                           ((status_msg) ? status_msg : ""));
1783
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   539
      scr_write_incoming_message(bjid, bn, timestamp,
af3de54ef986 Convert scr_LogPrint() to scr_log_print()
Mikael Berthe <mikael@lilotux.net>
parents: 1780
diff changeset
   540
                                 HBB_PREFIX_INFO|HBB_PREFIX_NOFLAG, 0);
516
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   541
      g_free(bn);
e5008032edb8 Add option 'show_status_in_buffer'
Mikael Berthe <mikael@lilotux.net>
parents: 514
diff changeset
   542
    }
514
979eb0fe2969 Show status changes in buddy window, if it is open
Mikael Berthe <mikael@lilotux.net>
parents: 510
diff changeset
   543
  }
979eb0fe2969 Show status changes in buddy window, if it is open
Mikael Berthe <mikael@lilotux.net>
parents: 510
diff changeset
   544
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   545
  roster_setstatus(bjid, rn, prio, status, status_msg, timestamp,
625
028c01940490 Show presence timestamps in "/info" and "/room whois"
Mikael Berthe <mikael@lilotux.net>
parents: 614
diff changeset
   546
                   role_none, affil_none, NULL);
120
cfd3df636d5f [/trunk] Changeset 133 by mikael
mikael
parents: 118
diff changeset
   547
  buddylist_build();
1780
e4378fbab5d7 Major API cleanup - bump API to 4
Mikael Berthe <mikael@lilotux.net>
parents: 1729
diff changeset
   548
  scr_draw_roster();
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   549
  hlog_write_status(bjid, timestamp, status, status_msg);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   550
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   551
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   552
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   553
    char os[2] = " \0";
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   554
    char ns[2] = " \0";
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   555
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   556
      { "jid", bjid },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   557
      { "resource", rn },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   558
      { "old_status", os },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   559
      { "new_status", ns },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   560
      { "message", status_msg ? status_msg : "" },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   561
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   562
    };
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   563
    os[0] = imstatus2char[oldstat];
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   564
    ns[0] = imstatus2char[status];
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   565
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   566
    hk_run_handlers(HOOK_STATUS_CHANGE, args);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   567
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   568
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   569
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   570
  // External command
1449
a9a1770e4578 Fix a bug introduced in changeset ba706e97d6ec
Mikael Berthe <mikael@lilotux.net>
parents: 1448
diff changeset
   571
  hk_ext_cmd(ename ? ename : bjid, 'S', imstatus2char[status], NULL);
113
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   572
}
8ac67e951eab [/trunk] Changeset 127 by mikael
mikael
parents:
diff changeset
   573
1398
f8321420ed7a Remove useless inline's
Mikael Berthe <mikael@lilotux.net>
parents: 1359
diff changeset
   574
void hk_mystatuschange(time_t timestamp, enum imstatus old_status,
536
56f641155579 Status change was not displayed when same status but message removed
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
   575
                              enum imstatus new_status, const char *msg)
116
1e7e59775f12 [/trunk] Changeset 130 by mikael
mikael
parents: 115
diff changeset
   576
{
541
87165382e4ce Fix error introduced in changeset 56f641155579
Mikael Berthe <mikael@lilotux.net>
parents: 536
diff changeset
   577
  scr_LogPrint(LPRINT_LOGNORM, "Your status has been set: [%c>%c] %s",
536
56f641155579 Status change was not displayed when same status but message removed
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
   578
               imstatus2char[old_status], imstatus2char[new_status],
56f641155579 Status change was not displayed when same status but message removed
Mikael Berthe <mikael@lilotux.net>
parents: 524
diff changeset
   579
               (msg ? msg : ""));
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   580
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   581
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   582
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   583
    char ns[2] = " \0";
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   584
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   585
      { "new_status", ns },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   586
      { "message", msg ? msg : "" },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   587
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   588
    };
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   589
    ns[0] = imstatus2char[new_status];
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   590
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   591
    hk_run_handlers(HOOK_MY_STATUS_CHANGE, args);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   592
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   593
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   594
116
1e7e59775f12 [/trunk] Changeset 130 by mikael
mikael
parents: 115
diff changeset
   595
  //hlog_write_status(NULL, 0, status);
1e7e59775f12 [/trunk] Changeset 130 by mikael
mikael
parents: 115
diff changeset
   596
}
1e7e59775f12 [/trunk] Changeset 130 by mikael
mikael
parents: 115
diff changeset
   597
1683
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   598
void hk_postconnect(void)
1352
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   599
{
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   600
  const char *hook_command;
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   601
  char *cmdline;
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   602
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   603
#ifdef MODULES_ENABLE
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   604
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   605
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   606
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   607
    };
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   608
    hk_run_handlers(HOOK_POST_CONNECT, args);
1607
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   609
  }
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   610
#endif
14690e624e9d Add modules
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1604
diff changeset
   611
1683
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   612
  hook_command = settings_opt_get("hook-post-connect");
1642
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   613
  if (!hook_command)
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   614
    return;
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   615
1683
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   616
  scr_LogPrint(LPRINT_LOGNORM, "Running hook-post-connect...");
1642
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   617
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   618
  cmdline = from_utf8(hook_command);
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   619
  if (process_command(cmdline, TRUE) == 255)
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   620
    mcabber_set_terminate_ui();
cd67b29e04e3 Fix not execution of HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1623
diff changeset
   621
1352
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   622
  g_free(cmdline);
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   623
}
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   624
1683
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   625
void hk_predisconnect(void)
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   626
{
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   627
  const char *hook_command;
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   628
  char *cmdline;
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   629
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   630
#ifdef MODULES_ENABLE
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   631
  {
1795
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   632
    hk_arg_t args[] = {
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   633
      { NULL, NULL },
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   634
    };
47699a09ceb3 Rework the hook system
Mikael Berthe <mikael@lilotux.net>
parents: 1787
diff changeset
   635
    hk_run_handlers(HOOK_PRE_DISCONNECT, args);
1683
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   636
  }
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   637
#endif
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   638
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   639
  hook_command = settings_opt_get("hook-pre-disconnect");
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   640
  if (!hook_command)
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   641
    return;
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   642
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   643
  scr_LogPrint(LPRINT_LOGNORM, "Running hook-pre-disconnect...");
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   644
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   645
  cmdline = from_utf8(hook_command);
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   646
  if (process_command(cmdline, TRUE) == 255)
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   647
    mcabber_set_terminate_ui();
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   648
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   649
  g_free(cmdline);
b09f82f61745 Split HOOK_INTERNAL
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1668
diff changeset
   650
}
1352
61a54e172010 Add internal hooks support
Mikael Berthe <mikael@lilotux.net>
parents: 1342
diff changeset
   651
1812
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   652
void hk_unread_list_change(guint unread_count, guint attention_count,
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   653
                           guint muc_unread, guint muc_attention)
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   654
{
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   655
#ifdef MODULES_ENABLE
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   656
  gchar *str_unread = g_strdup_printf("%u", unread_count);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   657
  gchar *str_attention = g_strdup_printf("%u", attention_count);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   658
  gchar *str_muc_unread = g_strdup_printf("%u", muc_unread);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   659
  gchar *str_muc_attention = g_strdup_printf("%u", muc_attention);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   660
  hk_arg_t args[] = {
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   661
    { "unread", str_unread },               // All unread
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   662
    { "attention", str_attention },         // Attention (private)
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   663
    { "muc_unread", str_muc_unread },       // MUC unread
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   664
    { "muc_attention", str_muc_attention }, // MUC attention (highlight)
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   665
    { NULL, NULL },
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   666
  };
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   667
  hk_run_handlers(HOOK_UNREAD_LIST_CHANGE, args);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   668
  g_free(str_unread);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   669
  g_free(str_attention);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   670
  g_free(str_muc_unread);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   671
  g_free(str_muc_attention);
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   672
#endif
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   673
}
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   674
5dbb3ebbc466 Add hk_unread_list_change()
Mikael Berthe <mikael@lilotux.net>
parents: 1811
diff changeset
   675
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   676
/* External commands */
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   677
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   678
//  hk_ext_cmd_init()
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   679
// Initialize external command variable.
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   680
// Can be called with parameter NULL to reset and free memory.
281
f562b9af2de7 Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents: 221
diff changeset
   681
void hk_ext_cmd_init(const char *command)
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   682
{
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   683
  if (extcmd) {
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   684
    g_free(extcmd);
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   685
    extcmd = NULL;
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   686
  }
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   687
  if (command)
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   688
    extcmd = expand_filename(command);
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   689
}
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   690
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   691
//  hk_ext_cmd()
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   692
// Launch an external command (process) for the given event.
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   693
// For now, data should be NULL.
1058
c0d44a9a99bc Code cleanup
Mikael Berthe <mikael@lilotux.net>
parents: 1055
diff changeset
   694
void hk_ext_cmd(const char *bjid, guchar type, guchar info, const char *data)
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   695
{
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   696
  pid_t pid;
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   697
  char *arg_type = NULL;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   698
  char *arg_info = NULL;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   699
  char *arg_data = NULL;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   700
  char status_str[2];
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   701
  char *datafname = NULL;
1444
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   702
  char unread_str[16];
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   703
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   704
  if (!extcmd) return;
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   705
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   706
  // Prepare arg_* (external command parameters)
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   707
  switch (type) {
1444
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   708
    case 'M': /* Normal message */
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   709
        arg_type = "MSG";
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   710
        if (info == 'R')
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   711
          arg_info = "IN";
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   712
        else if (info == 'S')
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   713
          arg_info = "OUT";
453
39e173645f9c External command is called for MUC messages
Mikael Berthe <mikael@lilotux.net>
parents: 447
diff changeset
   714
        break;
1444
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   715
    case 'G': /* Groupchat message */
453
39e173645f9c External command is called for MUC messages
Mikael Berthe <mikael@lilotux.net>
parents: 447
diff changeset
   716
        arg_type = "MSG";
39e173645f9c External command is called for MUC messages
Mikael Berthe <mikael@lilotux.net>
parents: 447
diff changeset
   717
        arg_info = "MUC";
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   718
        break;
1444
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   719
    case 'S': /* Status change */
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   720
        arg_type = "STATUS";
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   721
        if (strchr(imstatus2char, tolower(info))) {
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   722
          status_str[0] = toupper(info);
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   723
          status_str[1] = 0;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   724
          arg_info = status_str;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   725
        }
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   726
        break;
1444
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   727
    case 'U': /* Unread buffer count */
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   728
        arg_type = "UNREAD";
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   729
        g_snprintf(unread_str, sizeof unread_str, "%d", info);
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   730
        arg_info = unread_str;  /* number of remaining unread bjids */
3bf11085c6a5 New external "UNREAD" event (suggested by Viacheslav Chumushuk)
Mikael Berthe <mikael@lilotux.net>
parents: 1426
diff changeset
   731
        break;
355
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   732
    default:
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   733
        return;
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   734
  }
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   735
c5a7a7273986 Add some external actions
Mikael Berthe <mikael@lilotux.net>
parents: 325
diff changeset
   736
  if (!arg_type || !arg_info) return;
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   737
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   738
  if (strchr("MG", type) && data && settings_opt_get_int("event_log_files")) {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   739
    int fd;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   740
    const char *prefix;
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   741
    char *prefix_xp = NULL;
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   742
    char *data_locale;
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   743
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   744
    data_locale = from_utf8(data);
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   745
    prefix = settings_opt_get("event_log_dir");
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   746
    if (prefix)
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   747
      prefix = prefix_xp = expand_filename(prefix);
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   748
    else
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   749
      prefix = ut_get_tmpdir();
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   750
    datafname = g_strdup_printf("%s/mcabber-%d.XXXXXX", prefix, getpid());
1167
9726c78a91f3 Allow '~' (HOME directory) in config options
Mikael Berthe <mikael@lilotux.net>
parents: 1129
diff changeset
   751
    g_free(prefix_xp);
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   752
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   753
    // XXX Some old systems may require us to set umask first.
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   754
    fd = mkstemp(datafname);
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   755
    if (fd == -1) {
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   756
      g_free(datafname);
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   757
      datafname = NULL;
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   758
      scr_LogPrint(LPRINT_LOGNORM,
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   759
                   "Unable to create temp file for external command.");
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   760
    } else {
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   761
      size_t data_locale_len = strlen(data_locale);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   762
      ssize_t a = write(fd, data_locale, data_locale_len);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   763
      ssize_t b = write(fd, "\n", 1);
1604
351427ef0b4b Remove #include's of C files
Mikael Berthe <mikael@lilotux.net>
parents: 1602
diff changeset
   764
      if ((size_t)a != data_locale_len || b != 1) {
1598
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   765
        g_free(datafname);
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   766
        datafname = NULL;
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   767
        scr_LogPrint(LPRINT_LOGNORM,
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   768
                     "Unable to write to temp file for external command.");
a087125d8fc8 Replace libjabber with loudmouth
franky
parents: 1484
diff changeset
   769
      }
1107
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   770
      close(fd);
1db3d045e95c Fix 2 hooks bugs
Mikael Berthe <mikael@lilotux.net>
parents: 1058
diff changeset
   771
      arg_data = datafname;
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   772
    }
772
464be13343a9 Store most data in UTF-8 internally
Mikael Berthe <mikael@lilotux.net>
parents: 735
diff changeset
   773
    g_free(data_locale);
524
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   774
  }
05c0e55c4bb1 Pass message body to external command
Mikael Berthe <mikael@lilotux.net>
parents: 516
diff changeset
   775
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   776
  if ((pid=fork()) == -1) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
   777
    scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command.");
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   778
    g_free(datafname);
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   779
    return;
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   780
  }
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   781
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   782
  if (pid == 0) { // child
656
29a0637c176f Close standard fds before launching the eventcmd process
Mikael Berthe <mikael@lilotux.net>
parents: 655
diff changeset
   783
    // Close standard file descriptors
29a0637c176f Close standard fds before launching the eventcmd process
Mikael Berthe <mikael@lilotux.net>
parents: 655
diff changeset
   784
    close(STDIN_FILENO);
29a0637c176f Close standard fds before launching the eventcmd process
Mikael Berthe <mikael@lilotux.net>
parents: 655
diff changeset
   785
    close(STDOUT_FILENO);
29a0637c176f Close standard fds before launching the eventcmd process
Mikael Berthe <mikael@lilotux.net>
parents: 655
diff changeset
   786
    close(STDERR_FILENO);
1339
1ea5a4cf2ae0 Correct execl() prototype
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
   787
    if (execl(extcmd, extcmd, arg_type, arg_info, bjid, arg_data,
1ea5a4cf2ae0 Correct execl() prototype
Mikael Berthe <mikael@lilotux.net>
parents: 1311
diff changeset
   788
              (char *)NULL) == -1) {
374
bd5638c21834 Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents: 364
diff changeset
   789
      // scr_LogPrint(LPRINT_LOGNORM, "Cannot execute external command.");
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   790
      exit(1);
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   791
    }
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   792
  }
774
46304b773a44 Remove useless checks before g_free() calls
Mikael Berthe <mikael@lilotux.net>
parents: 772
diff changeset
   793
  g_free(datafname);
160
44c6410b4845 [/trunk] Changeset 172 by mikael
mikael
parents: 146
diff changeset
   794
}
576
8b3db0b555a1 Add Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 572
diff changeset
   795
1811
e6d355e50d7a Update Vim modelines
Mikael Berthe <mikael@lilotux.net>
parents: 1808
diff changeset
   796
/* vim: set expandtab cindent cinoptions=>2\:2(0 sw=2 ts=2:  For Vim users... */