comparison mcabber/modules/urlregex/urlregex.c @ 1975:a23799244335

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