| |
1 /** |
| |
2 * @file gntconv.c GNT Conversation API |
| |
3 * @ingroup gntui |
| |
4 * |
| |
5 * finch |
| |
6 * |
| |
7 * Finch is the legal property of its developers, whose names are too numerous |
| |
8 * to list here. Please refer to the COPYRIGHT file distributed with this |
| |
9 * source distribution. |
| |
10 * |
| |
11 * This program is free software; you can redistribute it and/or modify |
| |
12 * it under the terms of the GNU General Public License as published by |
| |
13 * the Free Software Foundation; either version 2 of the License, or |
| |
14 * (at your option) any later version. |
| |
15 * |
| |
16 * This program is distributed in the hope that it will be useful, |
| |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| |
19 * GNU General Public License for more details. |
| |
20 * |
| |
21 * You should have received a copy of the GNU General Public License |
| |
22 * along with this program; if not, write to the Free Software |
| |
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| |
24 */ |
| |
25 #include <string.h> |
| |
26 |
| |
27 #include <cmds.h> |
| |
28 #include <idle.h> |
| |
29 #include <prefs.h> |
| |
30 #include <util.h> |
| |
31 |
| |
32 #include "finch.h" |
| |
33 #include "gntaccount.h" |
| |
34 #include "gntblist.h" |
| |
35 #include "gntconv.h" |
| |
36 #include "gntdebug.h" |
| |
37 #include "gntplugin.h" |
| |
38 #include "gntprefs.h" |
| |
39 #include "gntstatus.h" |
| |
40 |
| |
41 #include "gnt.h" |
| |
42 #include "gntbox.h" |
| |
43 #include "gntentry.h" |
| |
44 #include "gnttextview.h" |
| |
45 |
| |
46 #define PREF_ROOT "/purple/gnt/conversations" |
| |
47 |
| |
48 #include "config.h" |
| |
49 |
| |
50 static void |
| |
51 send_typing_notification(GntWidget *w, FinchConv *ggconv) |
| |
52 { |
| |
53 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry)); |
| |
54 gboolean empty = (!text || !*text); |
| |
55 if (purple_prefs_get_bool("/purple/gnt/conversations/notify_typing")) { |
| |
56 PurpleConversation *conv = ggconv->active_conv; |
| |
57 PurpleConvIm *im = PURPLE_CONV_IM(conv); |
| |
58 if (!empty) { |
| |
59 gboolean send = (purple_conv_im_get_send_typed_timeout(im) == 0); |
| |
60 |
| |
61 purple_conv_im_stop_send_typed_timeout(im); |
| |
62 purple_conv_im_start_send_typed_timeout(im); |
| |
63 if (send || (purple_conv_im_get_type_again(im) != 0 && |
| |
64 time(NULL) > purple_conv_im_get_type_again(im))) { |
| |
65 unsigned int timeout; |
| |
66 timeout = serv_send_typing(purple_conversation_get_gc(conv), |
| |
67 purple_conversation_get_name(conv), |
| |
68 PURPLE_TYPING); |
| |
69 purple_conv_im_set_type_again(im, timeout); |
| |
70 } |
| |
71 } else { |
| |
72 purple_conv_im_stop_send_typed_timeout(im); |
| |
73 |
| |
74 serv_send_typing(purple_conversation_get_gc(conv), |
| |
75 purple_conversation_get_name(conv), |
| |
76 PURPLE_NOT_TYPING); |
| |
77 } |
| |
78 } |
| |
79 } |
| |
80 |
| |
81 static gboolean |
| |
82 entry_key_pressed(GntWidget *w, const char *key, FinchConv *ggconv) |
| |
83 { |
| |
84 if (key[0] == '\r' && key[1] == 0) |
| |
85 { |
| |
86 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry)); |
| |
87 if (*text == '/') |
| |
88 { |
| |
89 PurpleConversation *conv = ggconv->active_conv; |
| |
90 PurpleCmdStatus status; |
| |
91 const char *cmdline = text + 1; |
| |
92 char *error = NULL, *escape; |
| |
93 |
| |
94 escape = g_markup_escape_text(cmdline, -1); |
| |
95 status = purple_cmd_do_command(conv, cmdline, escape, &error); |
| |
96 g_free(escape); |
| |
97 |
| |
98 switch (status) |
| |
99 { |
| |
100 case PURPLE_CMD_STATUS_OK: |
| |
101 break; |
| |
102 case PURPLE_CMD_STATUS_NOT_FOUND: |
| |
103 purple_conversation_write(conv, "", _("No such command."), |
| |
104 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
105 break; |
| |
106 case PURPLE_CMD_STATUS_WRONG_ARGS: |
| |
107 purple_conversation_write(conv, "", _("Syntax Error: You typed the wrong number of arguments " |
| |
108 "to that command."), |
| |
109 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
110 break; |
| |
111 case PURPLE_CMD_STATUS_FAILED: |
| |
112 purple_conversation_write(conv, "", error ? error : _("Your command failed for an unknown reason."), |
| |
113 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
114 break; |
| |
115 case PURPLE_CMD_STATUS_WRONG_TYPE: |
| |
116 if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
| |
117 purple_conversation_write(conv, "", _("That command only works in chats, not IMs."), |
| |
118 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
119 else |
| |
120 purple_conversation_write(conv, "", _("That command only works in IMs, not chats."), |
| |
121 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
122 break; |
| |
123 case PURPLE_CMD_STATUS_WRONG_PRPL: |
| |
124 purple_conversation_write(conv, "", _("That command doesn't work on this protocol."), |
| |
125 PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
126 break; |
| |
127 } |
| |
128 g_free(error); |
| |
129 #if 0 |
| |
130 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
| |
131 _("Commands are not supported yet. Message was NOT sent."), |
| |
132 GNT_TEXT_FLAG_DIM | GNT_TEXT_FLAG_UNDERLINE); |
| |
133 gnt_text_view_next_line(GNT_TEXT_VIEW(ggconv->tv)); |
| |
134 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0); |
| |
135 #endif |
| |
136 } |
| |
137 else |
| |
138 { |
| |
139 char *escape = g_markup_escape_text(text, -1); |
| |
140 char *apos = purple_strreplace(escape, "'", "'"); |
| |
141 g_free(escape); |
| |
142 escape = apos; |
| |
143 switch (purple_conversation_get_type(ggconv->active_conv)) |
| |
144 { |
| |
145 case PURPLE_CONV_TYPE_IM: |
| |
146 purple_conv_im_send_with_flags(PURPLE_CONV_IM(ggconv->active_conv), escape, PURPLE_MESSAGE_SEND); |
| |
147 break; |
| |
148 case PURPLE_CONV_TYPE_CHAT: |
| |
149 purple_conv_chat_send(PURPLE_CONV_CHAT(ggconv->active_conv), escape); |
| |
150 break; |
| |
151 default: |
| |
152 g_free(escape); |
| |
153 g_return_val_if_reached(FALSE); |
| |
154 } |
| |
155 g_free(escape); |
| |
156 purple_idle_touch(); |
| |
157 } |
| |
158 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text); |
| |
159 gnt_entry_clear(GNT_ENTRY(ggconv->entry)); |
| |
160 return TRUE; |
| |
161 } |
| |
162 else if (key[0] == 27) |
| |
163 { |
| |
164 if (strcmp(key, GNT_KEY_DOWN) == 0) |
| |
165 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 1); |
| |
166 else if (strcmp(key, GNT_KEY_UP) == 0) |
| |
167 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), -1); |
| |
168 else if (strcmp(key, GNT_KEY_PGDOWN) == 0) |
| |
169 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), ggconv->tv->priv.height - 2); |
| |
170 else if (strcmp(key, GNT_KEY_PGUP) == 0) |
| |
171 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), -(ggconv->tv->priv.height - 2)); |
| |
172 else |
| |
173 return FALSE; |
| |
174 return TRUE; |
| |
175 } |
| |
176 else |
| |
177 { |
| |
178 } |
| |
179 |
| |
180 return FALSE; |
| |
181 } |
| |
182 |
| |
183 static void |
| |
184 closing_window(GntWidget *window, FinchConv *ggconv) |
| |
185 { |
| |
186 GList *list = ggconv->list; |
| |
187 ggconv->window = NULL; |
| |
188 while (list) { |
| |
189 PurpleConversation *conv = list->data; |
| |
190 list = list->next; |
| |
191 purple_conversation_destroy(conv); |
| |
192 } |
| |
193 } |
| |
194 |
| |
195 static void |
| |
196 size_changed_cb(GntWidget *widget, int width, int height) |
| |
197 { |
| |
198 int w, h; |
| |
199 gnt_widget_get_size(widget, &w, &h); |
| |
200 purple_prefs_set_int(PREF_ROOT "/size/width", w); |
| |
201 purple_prefs_set_int(PREF_ROOT "/size/height", h); |
| |
202 } |
| |
203 |
| |
204 static void |
| |
205 save_position_cb(GntWidget *w, int x, int y) |
| |
206 { |
| |
207 purple_prefs_set_int(PREF_ROOT "/position/x", x); |
| |
208 purple_prefs_set_int(PREF_ROOT "/position/y", y); |
| |
209 } |
| |
210 |
| |
211 static PurpleConversation * |
| |
212 find_conv_with_contact(PurpleConversation *conv) |
| |
213 { |
| |
214 PurpleBlistNode *node; |
| |
215 PurpleBuddy *buddy = purple_find_buddy(conv->account, conv->name); |
| |
216 PurpleConversation *ret = NULL; |
| |
217 |
| |
218 if (!buddy) |
| |
219 return NULL; |
| |
220 |
| |
221 for (node = ((PurpleBlistNode*)buddy)->parent->child; node; node = node->next) { |
| |
222 if (node == (PurpleBlistNode*)buddy) |
| |
223 continue; |
| |
224 if ((ret = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, |
| |
225 ((PurpleBuddy*)node)->name, ((PurpleBuddy*)node)->account)) != NULL) |
| |
226 break; |
| |
227 } |
| |
228 return ret; |
| |
229 } |
| |
230 |
| |
231 static char * |
| |
232 get_conversation_title(PurpleConversation *conv, PurpleAccount *account) |
| |
233 { |
| |
234 return g_strdup_printf(_("%s (%s -- %s)"), purple_conversation_get_title(conv), |
| |
235 purple_account_get_username(account), purple_account_get_protocol_name(account)); |
| |
236 } |
| |
237 |
| |
238 static void |
| |
239 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null) |
| |
240 { |
| |
241 PurpleConversation *conv; |
| |
242 FinchConv *ggc; |
| |
243 PurpleConvIm *im = NULL; |
| |
244 char *title, *str; |
| |
245 |
| |
246 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account); |
| |
247 |
| |
248 if (!conv) |
| |
249 return; |
| |
250 |
| |
251 im = PURPLE_CONV_IM(conv); |
| |
252 ggc = conv->ui_data; |
| |
253 |
| |
254 if (purple_conv_im_get_typing_state(im) == PURPLE_TYPING) { |
| |
255 int scroll; |
| |
256 str = get_conversation_title(conv, account); |
| |
257 title = g_strdup_printf(_("%s [%s]"), str, |
| |
258 gnt_ascii_only() ? "T" : "\342\243\277"); |
| |
259 g_free(str); |
| |
260 |
| |
261 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv)); |
| |
262 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
| |
263 /* Updating is a little buggy. So just remove and add a new one */ |
| |
264 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); |
| |
265 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv), |
| |
266 str, GNT_TEXT_FLAG_DIM, "typing"); |
| |
267 g_free(str); |
| |
268 if (scroll <= 1) |
| |
269 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0); |
| |
270 } else { |
| |
271 title = get_conversation_title(conv, account); |
| |
272 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE); |
| |
273 } |
| |
274 gnt_screen_rename_widget(ggc->window, title); |
| |
275 g_free(title); |
| |
276 } |
| |
277 |
| |
278 static gpointer |
| |
279 finch_conv_get_handle() |
| |
280 { |
| |
281 static int handle; |
| |
282 return &handle; |
| |
283 } |
| |
284 |
| |
285 static void |
| |
286 finch_create_conversation(PurpleConversation *conv) |
| |
287 { |
| |
288 FinchConv *ggc = conv->ui_data; |
| |
289 char *title; |
| |
290 PurpleConversationType type; |
| |
291 PurpleConversation *cc; |
| |
292 PurpleAccount *account; |
| |
293 |
| |
294 if (ggc) |
| |
295 return; |
| |
296 |
| |
297 cc = find_conv_with_contact(conv); |
| |
298 if (cc && cc->ui_data) |
| |
299 ggc = cc->ui_data; |
| |
300 else |
| |
301 ggc = g_new0(FinchConv, 1); |
| |
302 |
| |
303 ggc->list = g_list_prepend(ggc->list, conv); |
| |
304 ggc->active_conv = conv; |
| |
305 conv->ui_data = ggc; |
| |
306 |
| |
307 if (cc && cc->ui_data) { |
| |
308 finch_conversation_set_active(conv); |
| |
309 return; |
| |
310 } |
| |
311 |
| |
312 account = purple_conversation_get_account(conv); |
| |
313 type = purple_conversation_get_type(conv); |
| |
314 title = get_conversation_title(conv, account); |
| |
315 |
| |
316 ggc->window = gnt_box_new(FALSE, TRUE); |
| |
317 gnt_box_set_title(GNT_BOX(ggc->window), title); |
| |
318 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE); |
| |
319 gnt_box_set_pad(GNT_BOX(ggc->window), 0); |
| |
320 gnt_widget_set_name(ggc->window, "conversation-window"); |
| |
321 |
| |
322 ggc->tv = gnt_text_view_new(); |
| |
323 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv); |
| |
324 gnt_widget_set_name(ggc->tv, "conversation-window-textview"); |
| |
325 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"), |
| |
326 purple_prefs_get_int(PREF_ROOT "/size/height")); |
| |
327 |
| |
328 ggc->entry = gnt_entry_new(NULL); |
| |
329 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry); |
| |
330 gnt_widget_set_name(ggc->entry, "conversation-window-entry"); |
| |
331 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1); |
| |
332 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE); |
| |
333 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE); |
| |
334 |
| |
335 g_signal_connect_after(G_OBJECT(ggc->entry), "key_pressed", G_CALLBACK(entry_key_pressed), ggc); |
| |
336 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc); |
| |
337 |
| |
338 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"), |
| |
339 purple_prefs_get_int(PREF_ROOT "/position/y")); |
| |
340 gnt_widget_show(ggc->window); |
| |
341 |
| |
342 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL); |
| |
343 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL); |
| |
344 |
| |
345 if (type == PURPLE_CONV_TYPE_IM) { |
| |
346 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc); |
| |
347 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(), |
| |
348 PURPLE_CALLBACK(update_buddy_typing), NULL); |
| |
349 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(), |
| |
350 PURPLE_CALLBACK(update_buddy_typing), NULL); |
| |
351 } |
| |
352 |
| |
353 g_free(title); |
| |
354 } |
| |
355 |
| |
356 static void |
| |
357 finch_destroy_conversation(PurpleConversation *conv) |
| |
358 { |
| |
359 /* do stuff here */ |
| |
360 FinchConv *ggc = conv->ui_data; |
| |
361 ggc->list = g_list_remove(ggc->list, conv); |
| |
362 if (ggc->list && conv == ggc->active_conv) |
| |
363 ggc->active_conv = ggc->list->data; |
| |
364 |
| |
365 if (ggc->list == NULL) { |
| |
366 gnt_widget_destroy(ggc->window); |
| |
367 g_free(ggc); |
| |
368 } |
| |
369 } |
| |
370 |
| |
371 static void |
| |
372 finch_write_common(PurpleConversation *conv, const char *who, const char *message, |
| |
373 PurpleMessageFlags flags, time_t mtime) |
| |
374 { |
| |
375 FinchConv *ggconv = conv->ui_data; |
| |
376 char *strip, *newline; |
| |
377 GntTextFormatFlags fl = 0; |
| |
378 int pos; |
| |
379 gboolean notify; |
| |
380 |
| |
381 g_return_if_fail(ggconv != NULL); |
| |
382 |
| |
383 if (ggconv->active_conv != conv) { |
| |
384 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) |
| |
385 finch_conversation_set_active(conv); |
| |
386 else |
| |
387 return; |
| |
388 } |
| |
389 |
| |
390 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv)); |
| |
391 |
| |
392 notify = !!gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE); |
| |
393 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL); |
| |
394 |
| |
395 /* Unnecessary to print the timestamp for delayed message */ |
| |
396 if (!(flags & PURPLE_MESSAGE_DELAYED) && |
| |
397 purple_prefs_get_bool("/purple/gnt/conversations/timestamps")) |
| |
398 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
| |
399 purple_utf8_strftime("(%H:%M:%S) ", localtime(&mtime)), GNT_TEXT_FLAG_DIM); |
| |
400 |
| |
401 if (flags & PURPLE_MESSAGE_AUTO_RESP) |
| |
402 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
| |
403 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD); |
| |
404 |
| |
405 if (who && *who && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))) |
| |
406 { |
| |
407 char * name = NULL; |
| |
408 |
| |
409 if (purple_message_meify((char*)message, -1)) |
| |
410 name = g_strdup_printf("*** %s ", who); |
| |
411 else |
| |
412 name = g_strdup_printf("%s: ", who); |
| |
413 |
| |
414 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
| |
415 name, GNT_TEXT_FLAG_BOLD); |
| |
416 g_free(name); |
| |
417 } |
| |
418 else |
| |
419 fl = GNT_TEXT_FLAG_DIM; |
| |
420 |
| |
421 if (flags & PURPLE_MESSAGE_ERROR) |
| |
422 fl |= GNT_TEXT_FLAG_BOLD; |
| |
423 if (flags & PURPLE_MESSAGE_NICK) |
| |
424 fl |= GNT_TEXT_FLAG_UNDERLINE; |
| |
425 |
| |
426 /* XXX: Remove this workaround when textview can parse messages. */ |
| |
427 newline = purple_strdup_withhtml(message); |
| |
428 strip = purple_markup_strip_html(newline); |
| |
429 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), |
| |
430 strip, fl); |
| |
431 |
| |
432 g_free(newline); |
| |
433 g_free(strip); |
| |
434 |
| |
435 if (notify) { |
| |
436 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_name(conv)); |
| |
437 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv), |
| |
438 strip, GNT_TEXT_FLAG_DIM, "typing"); |
| |
439 g_free(strip); |
| |
440 } |
| |
441 |
| |
442 if (pos <= 1) |
| |
443 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0); |
| |
444 |
| |
445 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR)) |
| |
446 gnt_widget_set_urgent(ggconv->tv); |
| |
447 } |
| |
448 |
| |
449 static void |
| |
450 finch_write_chat(PurpleConversation *conv, const char *who, const char *message, |
| |
451 PurpleMessageFlags flags, time_t mtime) |
| |
452 { |
| |
453 purple_conversation_write(conv, who, message, flags, mtime); |
| |
454 } |
| |
455 |
| |
456 static void |
| |
457 finch_write_im(PurpleConversation *conv, const char *who, const char *message, |
| |
458 PurpleMessageFlags flags, time_t mtime) |
| |
459 { |
| |
460 PurpleAccount *account = purple_conversation_get_account(conv); |
| |
461 if (flags & PURPLE_MESSAGE_SEND) |
| |
462 { |
| |
463 who = purple_connection_get_display_name(purple_account_get_connection(account)); |
| |
464 if (!who) |
| |
465 who = purple_account_get_alias(account); |
| |
466 if (!who) |
| |
467 who = purple_account_get_username(account); |
| |
468 } |
| |
469 else if (flags & PURPLE_MESSAGE_RECV) |
| |
470 { |
| |
471 PurpleBuddy *buddy; |
| |
472 who = purple_conversation_get_name(conv); |
| |
473 buddy = purple_find_buddy(account, who); |
| |
474 if (buddy) |
| |
475 who = purple_buddy_get_contact_alias(buddy); |
| |
476 } |
| |
477 |
| |
478 purple_conversation_write(conv, who, message, flags, mtime); |
| |
479 } |
| |
480 |
| |
481 static void |
| |
482 finch_write_conv(PurpleConversation *conv, const char *who, const char *alias, |
| |
483 const char *message, PurpleMessageFlags flags, time_t mtime) |
| |
484 { |
| |
485 const char *name; |
| |
486 if (alias && *alias) |
| |
487 name = alias; |
| |
488 else if (who && *who) |
| |
489 name = who; |
| |
490 else |
| |
491 name = NULL; |
| |
492 |
| |
493 finch_write_common(conv, name, message, flags, mtime); |
| |
494 } |
| |
495 |
| |
496 static void |
| |
497 finch_chat_add_users(PurpleConversation *conv, GList *users, gboolean new_arrivals) |
| |
498 { |
| |
499 FinchConv *ggc = conv->ui_data; |
| |
500 GntEntry *entry = GNT_ENTRY(ggc->entry); |
| |
501 |
| |
502 if (!new_arrivals) |
| |
503 { |
| |
504 /* Print the list of users in the room */ |
| |
505 GString *string = g_string_new(_("List of users:\n")); |
| |
506 GList *iter; |
| |
507 |
| |
508 for (iter = users; iter; iter = iter->next) |
| |
509 { |
| |
510 PurpleConvChatBuddy *cbuddy = iter->data; |
| |
511 char *str; |
| |
512 |
| |
513 if ((str = cbuddy->alias) == NULL) |
| |
514 str = cbuddy->name; |
| |
515 g_string_append_printf(string, "[ %s ]", str); |
| |
516 } |
| |
517 |
| |
518 purple_conversation_write(conv, NULL, string->str, |
| |
519 PURPLE_MESSAGE_SYSTEM, time(NULL)); |
| |
520 g_string_free(string, TRUE); |
| |
521 } |
| |
522 |
| |
523 for (; users; users = users->next) |
| |
524 { |
| |
525 PurpleConvChatBuddy *cbuddy = users->data; |
| |
526 gnt_entry_add_suggest(entry, cbuddy->name); |
| |
527 gnt_entry_add_suggest(entry, cbuddy->alias); |
| |
528 } |
| |
529 } |
| |
530 |
| |
531 static void |
| |
532 finch_chat_rename_user(PurpleConversation *conv, const char *old, const char *new_n, const char *new_a) |
| |
533 { |
| |
534 /* Update the name for string completion */ |
| |
535 FinchConv *ggc = conv->ui_data; |
| |
536 GntEntry *entry = GNT_ENTRY(ggc->entry); |
| |
537 gnt_entry_remove_suggest(entry, old); |
| |
538 gnt_entry_add_suggest(entry, new_n); |
| |
539 gnt_entry_add_suggest(entry, new_a); |
| |
540 } |
| |
541 |
| |
542 static void |
| |
543 finch_chat_remove_user(PurpleConversation *conv, GList *list) |
| |
544 { |
| |
545 /* Remove the name from string completion */ |
| |
546 FinchConv *ggc = conv->ui_data; |
| |
547 GntEntry *entry = GNT_ENTRY(ggc->entry); |
| |
548 for (; list; list = list->next) |
| |
549 gnt_entry_remove_suggest(entry, list->data); |
| |
550 } |
| |
551 |
| |
552 static void |
| |
553 finch_chat_update_user(PurpleConversation *conv, const char *user) |
| |
554 { |
| |
555 } |
| |
556 |
| |
557 static PurpleConversationUiOps conv_ui_ops = |
| |
558 { |
| |
559 .create_conversation = finch_create_conversation, |
| |
560 .destroy_conversation = finch_destroy_conversation, |
| |
561 .write_chat = finch_write_chat, |
| |
562 .write_im = finch_write_im, |
| |
563 .write_conv = finch_write_conv, |
| |
564 .chat_add_users = finch_chat_add_users, |
| |
565 .chat_rename_user = finch_chat_rename_user, |
| |
566 .chat_remove_users = finch_chat_remove_user, |
| |
567 .chat_update_user = finch_chat_update_user, |
| |
568 .present = NULL, |
| |
569 .has_focus = NULL, |
| |
570 .custom_smiley_add = NULL, |
| |
571 .custom_smiley_write = NULL, |
| |
572 .custom_smiley_close = NULL |
| |
573 }; |
| |
574 |
| |
575 PurpleConversationUiOps *finch_conv_get_ui_ops() |
| |
576 { |
| |
577 return &conv_ui_ops; |
| |
578 } |
| |
579 |
| |
580 /* Xerox */ |
| |
581 static PurpleCmdRet |
| |
582 say_command_cb(PurpleConversation *conv, |
| |
583 const char *cmd, char **args, char **error, void *data) |
| |
584 { |
| |
585 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
| |
586 purple_conv_im_send(PURPLE_CONV_IM(conv), args[0]); |
| |
587 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) |
| |
588 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), args[0]); |
| |
589 |
| |
590 return PURPLE_CMD_RET_OK; |
| |
591 } |
| |
592 |
| |
593 /* Xerox */ |
| |
594 static PurpleCmdRet |
| |
595 me_command_cb(PurpleConversation *conv, |
| |
596 const char *cmd, char **args, char **error, void *data) |
| |
597 { |
| |
598 char *tmp; |
| |
599 |
| |
600 tmp = g_strdup_printf("/me %s", args[0]); |
| |
601 |
| |
602 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM) |
| |
603 purple_conv_im_send(PURPLE_CONV_IM(conv), tmp); |
| |
604 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT) |
| |
605 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), tmp); |
| |
606 |
| |
607 g_free(tmp); |
| |
608 return PURPLE_CMD_RET_OK; |
| |
609 } |
| |
610 |
| |
611 /* Xerox */ |
| |
612 static PurpleCmdRet |
| |
613 debug_command_cb(PurpleConversation *conv, |
| |
614 const char *cmd, char **args, char **error, void *data) |
| |
615 { |
| |
616 char *tmp, *markup; |
| |
617 PurpleCmdStatus status; |
| |
618 |
| |
619 if (!g_ascii_strcasecmp(args[0], "version")) { |
| |
620 tmp = g_strdup_printf("me is using %s.", VERSION); |
| |
621 markup = g_markup_escape_text(tmp, -1); |
| |
622 |
| |
623 status = purple_cmd_do_command(conv, tmp, markup, error); |
| |
624 |
| |
625 g_free(tmp); |
| |
626 g_free(markup); |
| |
627 return status; |
| |
628 } else { |
| |
629 purple_conversation_write(conv, NULL, _("Supported debug options are: version"), |
| |
630 PURPLE_MESSAGE_NO_LOG|PURPLE_MESSAGE_ERROR, time(NULL)); |
| |
631 return PURPLE_CMD_STATUS_OK; |
| |
632 } |
| |
633 } |
| |
634 |
| |
635 /* Xerox */ |
| |
636 static PurpleCmdRet |
| |
637 clear_command_cb(PurpleConversation *conv, |
| |
638 const char *cmd, char **args, char **error, void *data) |
| |
639 { |
| |
640 FinchConv *ggconv = conv->ui_data; |
| |
641 gnt_text_view_clear(GNT_TEXT_VIEW(ggconv->tv)); |
| |
642 return PURPLE_CMD_STATUS_OK; |
| |
643 } |
| |
644 |
| |
645 /* Xerox */ |
| |
646 static PurpleCmdRet |
| |
647 help_command_cb(PurpleConversation *conv, |
| |
648 const char *cmd, char **args, char **error, void *data) |
| |
649 { |
| |
650 GList *l, *text; |
| |
651 GString *s; |
| |
652 |
| |
653 if (args[0] != NULL) { |
| |
654 s = g_string_new(""); |
| |
655 text = purple_cmd_help(conv, args[0]); |
| |
656 |
| |
657 if (text) { |
| |
658 for (l = text; l; l = l->next) |
| |
659 if (l->next) |
| |
660 g_string_append_printf(s, "%s\n", (char *)l->data); |
| |
661 else |
| |
662 g_string_append_printf(s, "%s", (char *)l->data); |
| |
663 } else { |
| |
664 g_string_append(s, _("No such command (in this context).")); |
| |
665 } |
| |
666 } else { |
| |
667 s = g_string_new(_("Use \"/help <command>\" for help on a specific command.\n" |
| |
668 "The following commands are available in this context:\n")); |
| |
669 |
| |
670 text = purple_cmd_list(conv); |
| |
671 for (l = text; l; l = l->next) |
| |
672 if (l->next) |
| |
673 g_string_append_printf(s, "%s, ", (char *)l->data); |
| |
674 else |
| |
675 g_string_append_printf(s, "%s.", (char *)l->data); |
| |
676 g_list_free(text); |
| |
677 } |
| |
678 |
| |
679 purple_conversation_write(conv, NULL, s->str, PURPLE_MESSAGE_NO_LOG, time(NULL)); |
| |
680 g_string_free(s, TRUE); |
| |
681 |
| |
682 return PURPLE_CMD_STATUS_OK; |
| |
683 } |
| |
684 |
| |
685 static PurpleCmdRet |
| |
686 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data) |
| |
687 { |
| |
688 void (*callback)() = data; |
| |
689 callback(); |
| |
690 return PURPLE_CMD_STATUS_OK; |
| |
691 } |
| |
692 |
| |
693 void finch_conversation_init() |
| |
694 { |
| |
695 purple_prefs_add_none(PREF_ROOT); |
| |
696 purple_prefs_add_none(PREF_ROOT "/size"); |
| |
697 purple_prefs_add_int(PREF_ROOT "/size/width", 70); |
| |
698 purple_prefs_add_int(PREF_ROOT "/size/height", 20); |
| |
699 purple_prefs_add_none(PREF_ROOT "/position"); |
| |
700 purple_prefs_add_int(PREF_ROOT "/position/x", 0); |
| |
701 purple_prefs_add_int(PREF_ROOT "/position/y", 0); |
| |
702 |
| |
703 /* Xerox the commands */ |
| |
704 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT, |
| |
705 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
706 say_command_cb, _("say <message>: Send a message normally as if you weren't using a command."), NULL); |
| |
707 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT, |
| |
708 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
709 me_command_cb, _("me <action>: Send an IRC style action to a buddy or chat."), NULL); |
| |
710 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT, |
| |
711 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
712 debug_command_cb, _("debug <option>: Send various debug information to the current conversation."), NULL); |
| |
713 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT, |
| |
714 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
715 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL); |
| |
716 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT, |
| |
717 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL, |
| |
718 help_command_cb, _("help <command>: Help on a specific command."), NULL); |
| |
719 |
| |
720 /* Now some commands to bring up some other windows */ |
| |
721 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT, |
| |
722 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
723 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all); |
| |
724 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT, |
| |
725 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
726 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show); |
| |
727 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT, |
| |
728 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
729 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all); |
| |
730 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT, |
| |
731 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
732 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show); |
| |
733 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT, |
| |
734 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
735 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all); |
| |
736 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT, |
| |
737 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL, |
| |
738 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all); |
| |
739 } |
| |
740 |
| |
741 void finch_conversation_uninit() |
| |
742 { |
| |
743 } |
| |
744 |
| |
745 void finch_conversation_set_active(PurpleConversation *conv) |
| |
746 { |
| |
747 FinchConv *ggconv = conv->ui_data; |
| |
748 PurpleAccount *account; |
| |
749 char *title; |
| |
750 |
| |
751 g_return_if_fail(ggconv); |
| |
752 g_return_if_fail(g_list_find(ggconv->list, conv)); |
| |
753 |
| |
754 ggconv->active_conv = conv; |
| |
755 account = purple_conversation_get_account(conv); |
| |
756 title = get_conversation_title(conv, account); |
| |
757 gnt_screen_rename_widget(ggconv->window, title); |
| |
758 g_free(title); |
| |
759 } |
| |
760 |