mcabber/modules/urlregex/urlregex.c
author Mikael Berthe <mikael@lilotux.net>
Wed, 27 Jan 2016 10:34:44 +0100
changeset 2241 d703d6b42b32
parent 1975 a23799244335
permissions -rw-r--r--
Ready for release 1.0.1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1975
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 *  Module "urlregex"   -- Display received URL in the log window
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * Copyright 2011 Mikael Berthe <mikael@lilotux.net>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * Copyright 2008 Frank Zschockelt <mcabber@freakysoft.de>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 *
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * This module is free software; you can redistribute it and/or modify
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 * it under the terms of the GNU General Public License as published by
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * the Free Software Foundation; either version 2 of the License, or (at
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * your option) any later version.
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 *
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * This program is distributed in the hope that it will be useful, but
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * General Public License for more details.
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 *
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
 */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
#include <string.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
#include <mcabber/logprint.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
#include <mcabber/commands.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
#include <mcabber/compl.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
#include <mcabber/hooks.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
#include <mcabber/screen.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
#include <mcabber/settings.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
#include <mcabber/modules.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
#include <mcabber/config.h>
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
static void urlregex_init   (void);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
static void urlregex_uninit (void);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
/* Module description */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
module_info_t info_urlregex = {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
        .branch          = MCABBER_BRANCH,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
        .api             = MCABBER_API_VERSION,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
        .version         = MCABBER_VERSION,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
        .description     = "Simple URL extractor\n"
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
                           " Displays URL in the log window.",
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
        .requires        = NULL,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
        .init            = urlregex_init,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
        .uninit          = urlregex_uninit,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
        .next            = NULL,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
};
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
static guint urlregex_hid = 0;  /* Hook handler id */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    49
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    50
#ifdef HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    51
static GRegex *url_regex = NULL;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    52
#endif
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    54
/* Helper function */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    55
#ifdef HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    56
static inline void scr_log_urls(const gchar *string)
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
{
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
  GMatchInfo *match_info;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    59
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    60
  g_regex_match_full(url_regex, string, -1, 0, 0, &match_info, NULL);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    61
  while (g_match_info_matches(match_info)) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    62
    gchar *url = g_match_info_fetch(match_info, 0);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    63
    scr_print_logwindow(url);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    64
    g_free(url);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    65
    g_match_info_next(match_info, NULL);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    66
  }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    67
  g_match_info_free(match_info);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    68
}
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    69
#endif
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    70
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    71
/* Event handler */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    72
static guint urlregex_hh(const gchar *hookname, hk_arg_t *args, gpointer userdata)
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    73
{
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    74
#ifdef HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    75
  if (url_regex) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    76
    int i;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    77
    const char *msg = NULL;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    78
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    79
    for (i = 0; args[i].name; i++) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    80
      if (!strcmp(args[i].name, "message")) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    81
        msg = args[i].value;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    82
        break;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    83
      }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    84
    }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    85
    if (msg)
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    86
      scr_log_urls(msg);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    87
  }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    88
#endif
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    89
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    90
  /* We're done, let the other handlers do their job! */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    91
  return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    92
}
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    93
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    94
/* Initialization */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    95
static void urlregex_init(void)
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    96
{
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    97
  if (settings_opt_get("url_regex")) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    98
#ifdef HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    99
    url_regex = g_regex_new(settings_opt_get("url_regex"),
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   100
                            G_REGEX_OPTIMIZE, 0, NULL);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   101
    /* Add handler
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   102
     * We are only interested in incoming message events
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   103
     */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   104
    urlregex_hid = hk_add_handler(urlregex_hh, HOOK_POST_MESSAGE_IN,
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   105
                                  G_PRIORITY_DEFAULT_IDLE, NULL);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   106
#else
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   107
    scr_log_print(LPRINT_LOGNORM, "ERROR: Your glib version is too old, "
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   108
                  "cannot use url_regex.");
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   109
#endif // HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   110
  }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   111
}
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   112
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   113
/* Uninitialization */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   114
static void urlregex_uninit(void)
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   115
{
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   116
  /* Unregister event handler */
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   117
  hk_del_handler(HOOK_POST_MESSAGE_IN, urlregex_hid);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   118
#ifdef HAVE_GLIB_REGEX
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   119
  if (url_regex) {
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   120
    g_regex_unref(url_regex);
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   121
    url_regex = NULL;
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   122
  }
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   123
#endif
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   124
}
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   125
a23799244335 Add missing files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
   126
/* vim: set et cindent cinoptions=>2\:2(0 ts=2 sw=2:  For Vim users... */