This repository has been archived by the owner on Mar 26, 2023. It is now read-only.
/
usercast.c
225 lines (192 loc) · 7.44 KB
/
usercast.c
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
/*
* Copyright (c) 2013 Pavlo Lavrenenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "usercast.h"
#include <conversation.h>
#include <signals.h>
#include <gtkconv.h>
#include <pluginpref.h>
#include <prefs.h>
#include <version.h>
#include <debug.h>
#include <string.h>
static void
cast_nick(PidginConversation* conv, const gchar* nick)
{
Policy prefix_policy = purple_prefs_get_int(USERCAST_CONF_PREFIX_POLICY);
Policy postfix_policy = purple_prefs_get_int(USERCAST_CONF_POSTFIX_POLICY);
const gchar* user_prefix = purple_prefs_get_string(USERCAST_CONF_PREFIX);
const gchar* user_postfix = purple_prefs_get_string(USERCAST_CONF_POSTFIX);
gchar* user_cast = NULL;
gchar* user_part = NULL;
gint cursor_position = 0;
gint text_length = 0;
gchar* text = NULL;
g_object_get(conv->entry_buffer, "cursor-position", &cursor_position, NULL);
g_object_get(conv->entry_buffer, "text", &text, NULL);
text_length = strlen(text);
if (prefix_policy == POLICY_ALWAYS ||
(prefix_policy == POLICY_FIRST_WORD && cursor_position == 0) ||
(prefix_policy == POLICY_LAST_WORD && cursor_position == text_length))
user_part = g_strdup_printf("%s%s", user_prefix, nick);
else
user_part = g_strdup_printf("%s", nick);
if (postfix_policy == POLICY_ALWAYS ||
(postfix_policy == POLICY_FIRST_WORD && cursor_position == 0) ||
(postfix_policy == POLICY_LAST_WORD && cursor_position == text_length))
{
user_cast = g_strdup_printf("%s%s", user_part, user_postfix);
g_free(user_part);
}
else
user_cast = user_part;
gtk_text_buffer_insert_at_cursor(conv->entry_buffer, user_cast, -1);
g_free(user_cast);
}
static gboolean
conversation_nick_clicked(PurpleConversation* conv, gchar* nick, guint button)
{
GdkEvent* next_event = NULL;
PidginConversation* pidgin_conv = NULL;
gint event_timeout = 100; /* milliseconds */
if (purple_conversation_get_type(conv) != PURPLE_CONV_TYPE_CHAT)
{
purple_debug_warning(USERCAST_PLUGIN_NAME, "Conversation `%s' is not a chat\n", conv->name);
return FALSE;
}
if (!PIDGIN_IS_PIDGIN_CONVERSATION(conv))
{
purple_debug_warning(USERCAST_PLUGIN_NAME, "Conversation `%s' is not a pidgin conversation\n",
conv->name);
return FALSE;
}
if (button != 1)
return FALSE;
/* Wait for either GDK_2BUTTON_EVENT or something else to arrive */
for (; event_timeout > 0; event_timeout--)
{
if ((next_event = gdk_event_peek()))
break;
g_usleep(1000); /* 1 millisecond */
}
/* Pingin handles GDK_2BUTTON_PRESS separately */
if (next_event && next_event->type == GDK_2BUTTON_PRESS)
{
/* Remove GDK_2BUTTON_PRESS from event queue */
gdk_event_free(next_event);
gdk_event_free(gdk_event_get());
pidgin_conv = PIDGIN_CONVERSATION(conv);
cast_nick(pidgin_conv, nick);
gtk_widget_grab_focus(pidgin_conv->entry);
purple_debug_info(USERCAST_PLUGIN_NAME, "Casted user `%s' to `%s'\n", nick, conv->name);
return TRUE;
}
return FALSE;
}
static gboolean
plugin_load(PurplePlugin* plugin)
{
purple_signal_connect(pidgin_conversations_get_handle(), "chat-nick-clicked",
plugin, PURPLE_CALLBACK(conversation_nick_clicked), NULL);
return TRUE;
}
static gboolean
plugin_unload(PurplePlugin* plugin)
{
purple_signals_disconnect_by_handle(plugin);
return TRUE;
}
static PurplePluginPrefFrame*
get_plugin_pref_frame(PurplePlugin* plugin) {
PurplePluginPrefFrame* frame = purple_plugin_pref_frame_new();
PurplePluginPref* pref = NULL;
pref = purple_plugin_pref_new_with_label("Prefix preferences");
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(USERCAST_CONF_PREFIX, "Nick prefix");
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(USERCAST_CONF_PREFIX_POLICY, "Prefix use policy");
purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE);
purple_plugin_pref_add_choice(pref, "Always", GINT_TO_POINTER(POLICY_ALWAYS));
purple_plugin_pref_add_choice(pref, "First word", GINT_TO_POINTER(POLICY_FIRST_WORD));
purple_plugin_pref_add_choice(pref, "Last word", GINT_TO_POINTER(POLICY_LAST_WORD));
purple_plugin_pref_add_choice(pref, "Never", GINT_TO_POINTER(POLICY_NEVER));
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_label("Postfix preferences");
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(USERCAST_CONF_POSTFIX, "Nick postfix");
purple_plugin_pref_frame_add(frame, pref);
pref = purple_plugin_pref_new_with_name_and_label(USERCAST_CONF_POSTFIX_POLICY, "Postfix use policy");
purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE);
purple_plugin_pref_add_choice(pref, "Always", GINT_TO_POINTER(POLICY_ALWAYS));
purple_plugin_pref_add_choice(pref, "First word", GINT_TO_POINTER(POLICY_FIRST_WORD));
purple_plugin_pref_add_choice(pref, "Last word", GINT_TO_POINTER(POLICY_LAST_WORD));
purple_plugin_pref_add_choice(pref, "Never", GINT_TO_POINTER(POLICY_NEVER));
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,
USERCAST_PLUGIN_ID,
USERCAST_PLUGIN_NAME,
USERCAST_PLUGIN_VERSION,
"Paste username into chat conversation",
"Double-click username to paste it into chat input area",
"Pavlo Lavrenenko <santa.ssh@gmail.com>",
"https://github.com/santa01/usercast",
plugin_load,
plugin_unload,
NULL,
NULL,
NULL,
&prefs_info,
NULL,
NULL,
NULL,
NULL,
NULL
};
static void
init_plugin(PurplePlugin* plugin)
{
purple_prefs_add_none(USERCAST_CONF_ROOT);
purple_prefs_add_string(USERCAST_CONF_PREFIX, "Hey, ");
purple_prefs_add_string(USERCAST_CONF_POSTFIX, ", ");
purple_prefs_add_int(USERCAST_CONF_PREFIX_POLICY, POLICY_FIRST_WORD);
purple_prefs_add_int(USERCAST_CONF_POSTFIX_POLICY, POLICY_ALWAYS);
}
PURPLE_INIT_PLUGIN(usercast, init_plugin, info)