1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/**
* highlight.c Highlight on customized words.
*
* Copyright (C) 2007-2008 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
/* Pack/Local headers */
#include "../common/pp_internal.h"
#include <plugin.h>
#include <account.h>
#include <blist.h>
#include <cmds.h>
#include <conversation.h>
#include <debug.h>
#include <notify.h>
#include <util.h>
#include <string.h>
#define PREF_PREFIX "/plugins/core/highlight"
#define PREF_WORDS PREF_PREFIX "/words"
#define DELIMS " \t.,;|<>?/\\`~!@#$%^&*()+={}[]:'\""
#define PROP "highlight-words"
static char **words;
static PurpleCmdId cmd;
/**
* History stuff.
*/
/* XXX: SAVE THE NAME AND ACCOUNT OF THE CONVERSATION. OTHERWISE, IT CAUSES
* A CRASH WHEN A HIGHLIGHTED CONVERSATION NO LONGER EXISTS.
*/
static GHashTable *history;
static void
string_destroy(gpointer data)
{
g_string_free(data, TRUE);
}
static void
print_history_from_one_conv(gpointer key, gpointer value, gpointer data)
{
g_string_append_printf(data, "<b>Highlights from %s (%s)<br>%s<br><br><hr>",
purple_conversation_get_name(key),
purple_account_get_username(purple_conversation_get_account(key)),
((GString*)value)->str);
}
static void
print_history()
{
GString *str = g_string_new(NULL);
g_hash_table_foreach(history, print_history_from_one_conv, str);
purple_notify_formatted(NULL, _("Highlight History"), _("Highlight History"),
NULL, str->str, NULL, NULL);
string_destroy(str);
}
static void
clear_history()
{
g_hash_table_destroy(history);
history = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, string_destroy);
}
static void
add_to_history(PurpleConversation *conv, const char *message, const char *who, time_t mtime)
{
GString *str = g_hash_table_lookup(history, conv);
if (str == NULL) {
str = g_string_new(NULL);
g_hash_table_replace(history, conv, str);
}
g_string_append_printf(str, "<br>(%s) <b>%s</b>: %s",
purple_time_format(localtime(&mtime)),
who, message);
}
/* End of history */
static void
casefold_collate_strings(char **string)
{
int i;
for (i = 0; string[i]; i++) {
char *store = string[i];
char *cs = g_utf8_casefold(store, -1);
string[i] = g_utf8_collate_key(cs, -1);
g_free(cs);
g_free(store);
}
}
static void
sort(char **strings, int length)
{
int half;
char **left, **middle, **m, **right;
char **ret, **r;
if (length <= 1)
return;
r = ret = g_new0(char *, length);
half = length / 2;
sort(strings, half);
sort(strings + half, length - half);
left = strings;
middle = m = strings + half;
right = strings + length;
while (left < middle && m < right) {
int comp = strcmp(*left, *m);
if (comp <= 0)
*r++ = *left++;
else
*r++ = *m++;
}
while (left < middle)
*r++ = *left++;
while (m < right)
*r++ = *m++;
for (half = 0; half < length; half++)
strings[half] = ret[half];
g_free(ret);
}
static gboolean
writing_msg_callback(PurpleAccount *account, char *who, char **message, PurpleConversation *conv,
PurpleMessageFlags flags)
{
if (flags & PURPLE_MESSAGE_NICK)
add_to_history(conv, *message, who, time(NULL));
return FALSE;
}
static gboolean
msg_callback(PurpleAccount *account, char **who, char **message, PurpleConversation *conv,
PurpleMessageFlags *flags)
{
char **splits;
int len;
int wl, sl;
const char *me;
if (*flags & PURPLE_MESSAGE_NICK) {
return FALSE; /* this message is already highlighted */
}
if (!words)
return FALSE;
me = purple_connection_get_display_name(purple_account_get_connection(account));
if (me != NULL && g_utf8_collate(*who, me) == 0)
return FALSE;
splits = g_strsplit_set(*message, DELIMS, -1);
if (!splits)
return FALSE;
len = 0;
while (splits[len])
len++;
casefold_collate_strings(splits);
sort(splits, len);
/* this is probably over-engineering. */
for (sl = 0, wl = 0; words[wl] && splits[sl]; ) {
int val = strcmp(words[wl], splits[sl]);
if (val == 0) {
*flags |= PURPLE_MESSAGE_NICK;
break;
}
if (val < 0)
wl++;
else
sl++;
}
g_strfreev(splits);
return FALSE;
}
static void
construct_list()
{
int len = 0;
g_strfreev(words);
words = g_strsplit_set(purple_prefs_get_string(PREF_WORDS), DELIMS, -1);
if (!words)
return;
while (words[len])
len++;
casefold_collate_strings(words);
sort(words, len);
}
static PurpleCmdRet
highlight_cmd(PurpleConversation *conv, const gchar *cmd, gchar **args, gchar **error, void *data)
{
if (g_utf8_collate(args[0], "history") == 0) {
print_history();
} else if (g_utf8_collate(args[0], "clear") == 0) {
clear_history();
} else {
}
return PURPLE_CMD_RET_OK;
}
static gboolean
plugin_load(PurplePlugin *plugin)
{
construct_list();
purple_signal_connect(purple_conversations_get_handle(), "receiving-chat-msg", plugin,
G_CALLBACK(msg_callback), NULL);
purple_signal_connect(purple_conversations_get_handle(), "writing-chat-msg", plugin,
G_CALLBACK(writing_msg_callback), NULL);
purple_prefs_connect_callback(plugin, PREF_WORDS,
(PurplePrefCallback)construct_list, NULL);
cmd = purple_cmd_register("highlight", "ws", PURPLE_CMD_P_DEFAULT,
PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL, highlight_cmd,
_("/highlight history: shows the list of highlighted sentences from the history.\n"
"/highlight clear: clears the history.\n"
"/highlight +<word>: adds <word> to the highlight word list for this conversation only.\n"
"/highlight -<word>: removes <word> from the highlight word list for this conversation only.\n"),
NULL);
history = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, string_destroy);
return TRUE;
}
static gboolean
plugin_unload(PurplePlugin *plugin)
{
if (cmd)
purple_cmd_unregister(cmd);
g_hash_table_destroy(history);
return TRUE;
}
static PurplePluginPrefFrame *
get_plugin_pref_frame(PurplePlugin *plugin)
{
PurplePluginPrefFrame *frame;
PurplePluginPref *pref;
frame = purple_plugin_pref_frame_new();
pref = purple_plugin_pref_new_with_name_and_label(PREF_WORDS, _("Words to highlight on\n"
"(separate words by space)"));
purple_plugin_pref_frame_add(frame, pref);
return frame;
}
static PurplePluginUiInfo prefs_info = {
get_plugin_pref_frame,
0,
NULL,
NULL,
NULL,
NULL,
NULL
};
static PurplePluginInfo info =
{
PURPLE_PLUGIN_MAGIC,
PURPLE_MAJOR_VERSION,
PURPLE_MINOR_VERSION,
PURPLE_PLUGIN_STANDARD,
NULL,
0,
NULL,
PURPLE_PRIORITY_DEFAULT,
"core-plugin_pack-highlight",
NULL,
PP_VERSION,
NULL,
NULL,
"Sadrul H Chowdhury <sadrul@users.sourceforge.net>",
PP_WEBSITE,
plugin_load,
plugin_unload,
NULL,
NULL,
NULL,
&prefs_info,
NULL,
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin *plugin)
{
#ifdef ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, PP_LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
#endif /* ENABLE_NLS */
purple_prefs_add_none(PREF_PREFIX);
purple_prefs_add_string(PREF_WORDS, "");
info.name = _("Highlight");
info.summary = _("Support for highlighting words.");
info.description = _("Support for highlighting words.");
}
PURPLE_INIT_PLUGIN(ignore, init_plugin, info)
|