Change Finch to use Pidgin's clientkey/devid when connecting to ICQ.
[pidgin-git.git] / finch / gntconv.c
blob04c882857aefefb3b9eef3f5494bcd5cfdf042b2
1 /**
2 * @file gntconv.c GNT Conversation API
3 * @ingroup finch
4 */
6 /* finch
8 * Finch is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
27 #include <internal.h>
28 #include "finch.h"
30 #include <cmds.h>
31 #include <core.h>
32 #include <idle.h>
33 #include <prefs.h>
34 #include <util.h>
36 #include "gntaccount.h"
37 #include "gntblist.h"
38 #include "gntconv.h"
39 #include "gntdebug.h"
40 #include "gntlog.h"
41 #include "gntplugin.h"
42 #include "gntpounce.h"
43 #include "gntprefs.h"
44 #include "gntrequest.h"
45 #include "gntsound.h"
46 #include "gntstatus.h"
48 #include "gnt.h"
49 #include "gntbox.h"
50 #include "gntentry.h"
51 #include "gntlabel.h"
52 #include "gntmenu.h"
53 #include "gntmenuitem.h"
54 #include "gntmenuitemcheck.h"
55 #include "gntstyle.h"
56 #include "gnttextview.h"
57 #include "gnttree.h"
58 #include "gntutils.h"
59 #include "gntwindow.h"
61 #define PREF_ROOT "/finch/conversations"
62 #define PREF_CHAT PREF_ROOT "/chats"
63 #define PREF_USERLIST PREF_CHAT "/userlist"
65 #include "config.h"
67 static void finch_write_common(PurpleConversation *conv, const char *who,
68 const char *message, PurpleMessageFlags flags, time_t mtime);
69 static void generate_send_to_menu(FinchConv *ggc);
71 static int color_message_receive;
72 static int color_message_send;
73 static int color_message_highlight;
74 static int color_message_action;
75 static int color_timestamp;
77 static PurpleBuddy *
78 find_buddy_for_conversation(PurpleConversation *conv)
80 return purple_find_buddy(purple_conversation_get_account(conv),
81 purple_conversation_get_name(conv));
84 static PurpleChat *
85 find_chat_for_conversation(PurpleConversation *conv)
87 return purple_blist_find_chat(purple_conversation_get_account(conv),
88 purple_conversation_get_name(conv));
91 static PurpleBlistNode *
92 get_conversation_blist_node(PurpleConversation *conv)
94 PurpleBlistNode *node = NULL;
96 switch (purple_conversation_get_type(conv)) {
97 case PURPLE_CONV_TYPE_IM:
98 node = (PurpleBlistNode*)find_buddy_for_conversation(conv);
99 node = node ? purple_blist_node_get_parent(node) : NULL;
100 break;
101 case PURPLE_CONV_TYPE_CHAT:
102 node = (PurpleBlistNode*)find_chat_for_conversation(conv);
103 break;
104 default:
105 break;
107 return node;
110 static void
111 send_typing_notification(GntWidget *w, FinchConv *ggconv)
113 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
114 gboolean empty = (!text || !*text || (*text == '/'));
115 if (purple_prefs_get_bool("/finch/conversations/notify_typing")) {
116 PurpleConversation *conv = ggconv->active_conv;
117 PurpleConvIm *im = PURPLE_CONV_IM(conv);
118 if (!empty) {
119 gboolean send = (purple_conv_im_get_send_typed_timeout(im) == 0);
121 purple_conv_im_stop_send_typed_timeout(im);
122 purple_conv_im_start_send_typed_timeout(im);
123 if (send || (purple_conv_im_get_type_again(im) != 0 &&
124 time(NULL) > purple_conv_im_get_type_again(im))) {
125 unsigned int timeout;
126 timeout = serv_send_typing(purple_conversation_get_gc(conv),
127 purple_conversation_get_name(conv),
128 PURPLE_TYPING);
129 purple_conv_im_set_type_again(im, timeout);
131 } else {
132 purple_conv_im_stop_send_typed_timeout(im);
134 serv_send_typing(purple_conversation_get_gc(conv),
135 purple_conversation_get_name(conv),
136 PURPLE_NOT_TYPING);
141 static void
142 entry_key_pressed(GntWidget *w, FinchConv *ggconv)
144 const char *text = gnt_entry_get_text(GNT_ENTRY(ggconv->entry));
145 if (*text == '/' && *(text + 1) != '/')
147 PurpleConversation *conv = ggconv->active_conv;
148 PurpleCmdStatus status;
149 const char *cmdline = text + 1;
150 char *error = NULL, *escape;
152 escape = g_markup_escape_text(cmdline, -1);
153 status = purple_cmd_do_command(conv, cmdline, escape, &error);
154 g_free(escape);
156 switch (status)
158 case PURPLE_CMD_STATUS_OK:
159 break;
160 case PURPLE_CMD_STATUS_NOT_FOUND:
161 purple_conversation_write(conv, "", _("No such command."),
162 PURPLE_MESSAGE_NO_LOG, time(NULL));
163 break;
164 case PURPLE_CMD_STATUS_WRONG_ARGS:
165 purple_conversation_write(conv, "", _("Syntax Error: You typed the wrong number of arguments "
166 "to that command."),
167 PURPLE_MESSAGE_NO_LOG, time(NULL));
168 break;
169 case PURPLE_CMD_STATUS_FAILED:
170 purple_conversation_write(conv, "", error ? error : _("Your command failed for an unknown reason."),
171 PURPLE_MESSAGE_NO_LOG, time(NULL));
172 break;
173 case PURPLE_CMD_STATUS_WRONG_TYPE:
174 if(purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
175 purple_conversation_write(conv, "", _("That command only works in chats, not IMs."),
176 PURPLE_MESSAGE_NO_LOG, time(NULL));
177 else
178 purple_conversation_write(conv, "", _("That command only works in IMs, not chats."),
179 PURPLE_MESSAGE_NO_LOG, time(NULL));
180 break;
181 case PURPLE_CMD_STATUS_WRONG_PRPL:
182 purple_conversation_write(conv, "", _("That command doesn't work on this protocol."),
183 PURPLE_MESSAGE_NO_LOG, time(NULL));
184 break;
186 g_free(error);
188 else if (!purple_account_is_connected(purple_conversation_get_account(ggconv->active_conv)))
190 purple_conversation_write(ggconv->active_conv, "", _("Message was not sent, because you are not signed on."),
191 PURPLE_MESSAGE_ERROR | PURPLE_MESSAGE_NO_LOG, time(NULL));
193 else
195 char *escape = purple_markup_escape_text((*text == '/' ? text + 1 : text), -1);
196 switch (purple_conversation_get_type(ggconv->active_conv))
198 case PURPLE_CONV_TYPE_IM:
199 purple_conv_im_send_with_flags(PURPLE_CONV_IM(ggconv->active_conv), escape, PURPLE_MESSAGE_SEND);
200 break;
201 case PURPLE_CONV_TYPE_CHAT:
202 purple_conv_chat_send(PURPLE_CONV_CHAT(ggconv->active_conv), escape);
203 break;
204 default:
205 g_free(escape);
206 g_return_if_reached();
208 g_free(escape);
209 purple_idle_touch();
211 gnt_entry_add_to_history(GNT_ENTRY(ggconv->entry), text);
212 gnt_entry_clear(GNT_ENTRY(ggconv->entry));
215 static void
216 closing_window(GntWidget *window, FinchConv *ggconv)
218 GList *list = ggconv->list;
219 ggconv->window = NULL;
220 while (list) {
221 PurpleConversation *conv = list->data;
222 list = list->next;
223 purple_conversation_destroy(conv);
227 static void
228 size_changed_cb(GntWidget *widget, int width, int height)
230 int w, h;
231 gnt_widget_get_size(widget, &w, &h);
232 purple_prefs_set_int(PREF_ROOT "/size/width", w);
233 purple_prefs_set_int(PREF_ROOT "/size/height", h);
236 static void
237 save_position_cb(GntWidget *w, int x, int y)
239 purple_prefs_set_int(PREF_ROOT "/position/x", x);
240 purple_prefs_set_int(PREF_ROOT "/position/y", y);
243 static PurpleConversation *
244 find_conv_with_contact(PurpleAccount *account, const char *name)
246 PurpleBlistNode *node;
247 PurpleBuddy *buddy = purple_find_buddy(account, name);
248 PurpleConversation *ret = NULL;
250 if (!buddy)
251 return NULL;
253 for (node = purple_blist_node_get_first_child(purple_blist_node_get_parent((PurpleBlistNode*)buddy));
254 node; node = purple_blist_node_get_sibling_next(node)) {
255 if (node == (PurpleBlistNode*)buddy)
256 continue;
257 if ((ret = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM,
258 purple_buddy_get_name((PurpleBuddy*)node), purple_buddy_get_account((PurpleBuddy*)node))) != NULL)
259 break;
261 return ret;
264 static char *
265 get_conversation_title(PurpleConversation *conv, PurpleAccount *account)
267 return g_strdup_printf(_("%s (%s -- %s)"), purple_conversation_get_title(conv),
268 purple_account_get_username(account), purple_account_get_protocol_name(account));
271 static void
272 update_buddy_typing(PurpleAccount *account, const char *who, gpointer null)
274 PurpleConversation *conv;
275 FinchConv *ggc;
276 PurpleConvIm *im = NULL;
277 char *title, *str;
279 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, who, account);
281 if (!conv)
282 return;
284 im = PURPLE_CONV_IM(conv);
285 ggc = FINCH_GET_DATA(conv);
287 if (purple_conv_im_get_typing_state(im) == PURPLE_TYPING) {
288 int scroll;
289 str = get_conversation_title(conv, account);
290 title = g_strdup_printf(_("%s [%s]"), str,
291 gnt_ascii_only() ? "T" : "\342\243\277");
292 g_free(str);
294 scroll = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggc->tv));
295 str = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
296 /* Updating is a little buggy. So just remove and add a new one */
297 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", NULL, TRUE);
298 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggc->tv),
299 str, GNT_TEXT_FLAG_DIM, "typing");
300 g_free(str);
301 if (scroll <= 1)
302 gnt_text_view_scroll(GNT_TEXT_VIEW(ggc->tv), 0);
303 } else {
304 title = get_conversation_title(conv, account);
305 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggc->tv), "typing", " ", TRUE);
307 gnt_screen_rename_widget(ggc->window, title);
308 g_free(title);
311 static void
312 chat_left_cb(PurpleConversation *conv, gpointer null)
314 finch_write_common(conv, NULL, _("You have left this chat."),
315 PURPLE_MESSAGE_SYSTEM, time(NULL));
318 static void
319 buddy_signed_on_off(PurpleBuddy *buddy, gpointer null)
321 PurpleConversation *conv = find_conv_with_contact(purple_buddy_get_account(buddy), purple_buddy_get_name(buddy));
322 if (conv == NULL)
323 return;
324 generate_send_to_menu(FINCH_GET_DATA(conv));
327 static void
328 account_signed_on_off(PurpleConnection *gc, gpointer null)
330 GList *list = purple_get_ims();
331 while (list) {
332 PurpleConversation *conv = list->data;
333 PurpleConversation *cc = find_conv_with_contact(
334 purple_conversation_get_account(conv), purple_conversation_get_name(conv));
335 if (cc)
336 generate_send_to_menu(FINCH_GET_DATA(cc));
337 list = list->next;
340 if (PURPLE_CONNECTION_IS_CONNECTED(gc)) {
341 /* We just signed on. Let's see if there's any chat that we have open,
342 * and hadn't left before the disconnect. */
343 list = purple_get_chats();
344 while (list) {
345 PurpleConversation *conv = list->data;
346 PurpleChat *chat;
347 GHashTable *comps = NULL;
349 list = list->next;
350 if (purple_conversation_get_account(conv) != purple_connection_get_account(gc) ||
351 !purple_conversation_get_data(conv, "want-to-rejoin"))
352 continue;
354 chat = find_chat_for_conversation(conv);
355 if (chat == NULL) {
356 PurplePluginProtocolInfo *info = PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc));
357 if (info->chat_info_defaults != NULL)
358 comps = info->chat_info_defaults(gc, purple_conversation_get_name(conv));
359 } else {
360 comps = purple_chat_get_components(chat);
362 serv_join_chat(gc, comps);
363 if (chat == NULL && comps != NULL)
364 g_hash_table_destroy(comps);
369 static void
370 account_signing_off(PurpleConnection *gc)
372 GList *list = purple_get_chats();
373 PurpleAccount *account = purple_connection_get_account(gc);
375 /* We are about to sign off. See which chats we are currently in, and mark
376 * them for rejoin on reconnect. */
377 while (list) {
378 PurpleConversation *conv = list->data;
379 if (!purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv)) &&
380 purple_conversation_get_account(conv) == account) {
381 purple_conversation_set_data(conv, "want-to-rejoin", GINT_TO_POINTER(TRUE));
382 purple_conversation_write(conv, NULL, _("The account has disconnected and you are no "
383 "longer in this chat. You will be automatically rejoined in the chat when "
384 "the account reconnects."),
385 PURPLE_MESSAGE_SYSTEM, time(NULL));
387 list = list->next;
391 static gpointer
392 finch_conv_get_handle(void)
394 static int handle;
395 return &handle;
398 static void
399 cleared_message_history_cb(PurpleConversation *conv, gpointer data)
401 FinchConv *ggc = FINCH_GET_DATA(conv);
402 if (ggc)
403 gnt_text_view_clear(GNT_TEXT_VIEW(ggc->tv));
406 static void
407 clear_scrollback_cb(GntMenuItem *item, gpointer ggconv)
409 FinchConv *ggc = ggconv;
410 purple_conversation_clear_message_history(ggc->active_conv);
413 static void
414 send_file_cb(GntMenuItem *item, gpointer ggconv)
416 FinchConv *ggc = ggconv;
417 serv_send_file(purple_conversation_get_gc(ggc->active_conv),
418 purple_conversation_get_name(ggc->active_conv), NULL);
421 static void
422 add_pounce_cb(GntMenuItem *item, gpointer ggconv)
424 FinchConv *ggc = ggconv;
425 finch_pounce_editor_show(
426 purple_conversation_get_account(ggc->active_conv),
427 purple_conversation_get_name(ggc->active_conv), NULL);
430 static void
431 get_info_cb(GntMenuItem *item, gpointer ggconv)
433 FinchConv *ggc = ggconv;
434 finch_retrieve_user_info(purple_conversation_get_gc(ggc->active_conv),
435 purple_conversation_get_name(ggc->active_conv));
438 static void
439 toggle_timestamps_cb(GntMenuItem *item, gpointer ggconv)
441 purple_prefs_set_bool(PREF_ROOT "/timestamps",
442 !purple_prefs_get_bool(PREF_ROOT "/timestamps"));
445 static void
446 toggle_logging_cb(GntMenuItem *item, gpointer ggconv)
448 FinchConv *fc = ggconv;
449 PurpleConversation *conv = fc->active_conv;
450 gboolean logging = gnt_menuitem_check_get_checked(GNT_MENU_ITEM_CHECK(item));
451 GList *iter;
453 if (logging == purple_conversation_is_logging(conv))
454 return;
456 /* Xerox */
457 if (logging) {
458 /* Enable logging first so the message below can be logged. */
459 purple_conversation_set_logging(conv, TRUE);
461 purple_conversation_write(conv, NULL,
462 _("Logging started. Future messages in this conversation will be logged."),
463 PURPLE_MESSAGE_SYSTEM, time(NULL));
464 } else {
465 purple_conversation_write(conv, NULL,
466 _("Logging stopped. Future messages in this conversation will not be logged."),
467 PURPLE_MESSAGE_SYSTEM, time(NULL));
469 /* Disable the logging second, so that the above message can be logged. */
470 purple_conversation_set_logging(conv, FALSE);
473 /* Each conversation with the same person will have the same logging setting */
474 for (iter = fc->list; iter; iter = iter->next) {
475 if (iter->data == conv)
476 continue;
477 purple_conversation_set_logging(iter->data, logging);
481 static void
482 toggle_sound_cb(GntMenuItem *item, gpointer ggconv)
484 FinchConv *fc = ggconv;
485 PurpleBlistNode *node = get_conversation_blist_node(fc->active_conv);
486 fc->flags ^= FINCH_CONV_NO_SOUND;
487 if (node)
488 purple_blist_node_set_bool(node, "gnt-mute-sound", !!(fc->flags & FINCH_CONV_NO_SOUND));
491 static void
492 send_to_cb(GntMenuItem *m, gpointer n)
494 PurpleAccount *account = g_object_get_data(G_OBJECT(m), "purple_account");
495 gchar *buddy = g_object_get_data(G_OBJECT(m), "purple_buddy_name");
496 PurpleConversation *conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, buddy);
497 finch_conversation_set_active(conv);
500 static void
501 view_log_cb(GntMenuItem *n, gpointer ggc)
503 FinchConv *fc;
504 PurpleConversation *conv;
505 PurpleLogType type;
506 const char *name;
507 PurpleAccount *account;
508 GSList *buddies;
509 GSList *cur;
511 fc = ggc;
512 conv = fc->active_conv;
514 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
515 type = PURPLE_LOG_IM;
516 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
517 type = PURPLE_LOG_CHAT;
518 else
519 return;
521 name = purple_conversation_get_name(conv);
522 account = purple_conversation_get_account(conv);
524 buddies = purple_find_buddies(account, name);
525 for (cur = buddies; cur != NULL; cur = cur->next) {
526 PurpleBlistNode *node = cur->data;
527 if ((node != NULL) &&
528 (purple_blist_node_get_sibling_prev(node) || purple_blist_node_get_sibling_next(node))) {
529 finch_log_show_contact((PurpleContact *)purple_blist_node_get_parent(node));
530 g_slist_free(buddies);
531 return;
534 g_slist_free(buddies);
536 finch_log_show(type, name, account);
539 static void
540 generate_send_to_menu(FinchConv *ggc)
542 GntWidget *sub, *menu = ggc->menu;
543 GntMenuItem *item;
544 GSList *buds;
545 GList *list = NULL;
547 buds = purple_find_buddies(purple_conversation_get_account(ggc->active_conv),
548 purple_conversation_get_name(ggc->active_conv));
549 if (!buds)
550 return;
552 if ((item = ggc->u.im->sendto) == NULL) {
553 item = gnt_menuitem_new(_("Send To"));
554 gnt_menu_add_item(GNT_MENU(menu), item);
555 ggc->u.im->sendto = item;
557 sub = gnt_menu_new(GNT_MENU_POPUP);
558 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
560 for (; buds; buds = g_slist_delete_link(buds, buds)) {
561 PurpleBlistNode *node = PURPLE_BLIST_NODE(purple_buddy_get_contact(PURPLE_BUDDY(buds->data)));
562 for (node = purple_blist_node_get_first_child(node); node != NULL;
563 node = purple_blist_node_get_sibling_next(node)) {
564 PurpleBuddy *buddy = (PurpleBuddy *)node;
565 PurpleAccount *account = purple_buddy_get_account(buddy);
566 if (purple_account_is_connected(account)) {
567 /* Use the PurplePresence to get unique buddies. */
568 PurplePresence *presence = purple_buddy_get_presence(buddy);
569 if (!g_list_find(list, presence))
570 list = g_list_prepend(list, presence);
574 for (list = g_list_reverse(list); list != NULL; list = g_list_delete_link(list, list)) {
575 PurplePresence *pre = list->data;
576 PurpleBuddy *buddy = purple_presence_get_buddy(pre);
577 PurpleAccount *account = purple_buddy_get_account(buddy);
578 gchar *name = g_strdup(purple_buddy_get_name(buddy));
579 gchar *text = g_strdup_printf("%s (%s)", purple_buddy_get_name(buddy), purple_account_get_username(account));
580 item = gnt_menuitem_new(text);
581 g_free(text);
582 gnt_menu_add_item(GNT_MENU(sub), item);
583 gnt_menuitem_set_callback(item, send_to_cb, NULL);
584 g_object_set_data(G_OBJECT(item), "purple_account", account);
585 g_object_set_data_full(G_OBJECT(item), "purple_buddy_name", name, g_free);
589 static void
590 invite_cb(GntMenuItem *item, gpointer ggconv)
592 FinchConv *fc = ggconv;
593 PurpleConversation *conv = fc->active_conv;
594 purple_conv_chat_invite_user(PURPLE_CONV_CHAT(conv), NULL, NULL, TRUE);
597 static void
598 gg_create_menu(FinchConv *ggc)
600 GntWidget *menu, *sub;
601 GntMenuItem *item;
603 ggc->menu = menu = gnt_menu_new(GNT_MENU_TOPLEVEL);
604 gnt_window_set_menu(GNT_WINDOW(ggc->window), GNT_MENU(menu));
606 item = gnt_menuitem_new(_("Conversation"));
607 gnt_menu_add_item(GNT_MENU(menu), item);
609 sub = gnt_menu_new(GNT_MENU_POPUP);
610 gnt_menuitem_set_submenu(item, GNT_MENU(sub));
612 item = gnt_menuitem_new(_("Clear Scrollback"));
613 gnt_menu_add_item(GNT_MENU(sub), item);
614 gnt_menuitem_set_callback(item, clear_scrollback_cb, ggc);
616 item = gnt_menuitem_check_new(_("Show Timestamps"));
617 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
618 purple_prefs_get_bool(PREF_ROOT "/timestamps"));
619 gnt_menu_add_item(GNT_MENU(sub), item);
620 gnt_menuitem_set_callback(item, toggle_timestamps_cb, ggc);
622 if (purple_conversation_get_type(ggc->active_conv) == PURPLE_CONV_TYPE_IM) {
623 PurpleAccount *account = purple_conversation_get_account(ggc->active_conv);
624 PurpleConnection *gc = purple_account_get_connection(account);
625 PurplePluginProtocolInfo *pinfo =
626 gc ? PURPLE_PLUGIN_PROTOCOL_INFO(purple_connection_get_prpl(gc)) : NULL;
628 if (pinfo && pinfo->get_info) {
629 item = gnt_menuitem_new(_("Get Info"));
630 gnt_menu_add_item(GNT_MENU(sub), item);
631 gnt_menuitem_set_callback(item, get_info_cb, ggc);
634 item = gnt_menuitem_new(_("Add Buddy Pounce..."));
635 gnt_menu_add_item(GNT_MENU(sub), item);
636 gnt_menuitem_set_callback(item, add_pounce_cb, ggc);
638 if (pinfo && pinfo->send_file &&
639 (!pinfo->can_receive_file ||
640 pinfo->can_receive_file(gc, purple_conversation_get_name(ggc->active_conv)))) {
641 item = gnt_menuitem_new(_("Send File"));
642 gnt_menu_add_item(GNT_MENU(sub), item);
643 gnt_menuitem_set_callback(item, send_file_cb, ggc);
646 generate_send_to_menu(ggc);
647 } else if (purple_conversation_get_type(ggc->active_conv) == PURPLE_CONV_TYPE_CHAT) {
648 item = gnt_menuitem_new(_("Invite..."));
649 gnt_menu_add_item(GNT_MENU(sub), item);
650 gnt_menuitem_set_callback(item, invite_cb, ggc);
653 item = gnt_menuitem_new(_("View Log..."));
654 gnt_menu_add_item(GNT_MENU(sub), item);
655 gnt_menuitem_set_callback(item, view_log_cb, ggc);
657 item = gnt_menuitem_check_new(_("Enable Logging"));
658 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
659 purple_conversation_is_logging(ggc->active_conv));
660 gnt_menu_add_item(GNT_MENU(sub), item);
661 gnt_menuitem_set_callback(item, toggle_logging_cb, ggc);
663 item = gnt_menuitem_check_new(_("Enable Sounds"));
664 gnt_menuitem_check_set_checked(GNT_MENU_ITEM_CHECK(item),
665 !(ggc->flags & FINCH_CONV_NO_SOUND));
666 gnt_menu_add_item(GNT_MENU(sub), item);
667 gnt_menuitem_set_callback(item, toggle_sound_cb, ggc);
670 static void
671 create_conv_from_userlist(GntWidget *widget, FinchConv *fc)
673 PurpleAccount *account = purple_conversation_get_account(fc->active_conv);
674 PurpleConnection *gc = purple_account_get_connection(account);
675 PurplePluginProtocolInfo *prpl_info = NULL;
676 char *name, *realname;
678 if (!gc) {
679 purple_conversation_write(fc->active_conv, NULL, _("You are not connected."),
680 PURPLE_MESSAGE_SYSTEM, time(NULL));
681 return;
684 name = gnt_tree_get_selection_data(GNT_TREE(widget));
686 prpl_info = PURPLE_PLUGIN_PROTOCOL_INFO(gc->prpl);
687 if (prpl_info && PURPLE_PROTOCOL_PLUGIN_HAS_FUNC(prpl_info, get_cb_real_name))
688 realname = prpl_info->get_cb_real_name(gc, purple_conv_chat_get_id(PURPLE_CONV_CHAT(fc->active_conv)), name);
689 else
690 realname = NULL;
691 purple_conversation_new(PURPLE_CONV_TYPE_IM, account, realname ? realname : name);
692 g_free(realname);
695 static void
696 gained_focus_cb(GntWindow *window, FinchConv *fc)
698 GList *iter;
699 for (iter = fc->list; iter; iter = iter->next) {
700 purple_conversation_set_data(iter->data, "unseen-count", 0);
701 purple_conversation_update(iter->data, PURPLE_CONV_UPDATE_UNSEEN);
705 static void
706 completion_cb(GntEntry *entry, const char *start, const char *end)
708 if (start == entry->start && *start != '/')
709 gnt_widget_key_pressed(GNT_WIDGET(entry), ": ");
712 static void
713 gg_setup_commands(FinchConv *fconv, gboolean remove_first)
715 GList *commands;
716 char command[256] = "/";
718 if (remove_first) {
719 commands = purple_cmd_list(NULL);
720 for (; commands; commands = g_list_delete_link(commands, commands)) {
721 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
722 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
726 commands = purple_cmd_list(fconv->active_conv);
727 for (; commands; commands = g_list_delete_link(commands, commands)) {
728 g_strlcpy(command + 1, commands->data, sizeof(command) - 1);
729 gnt_entry_add_suggest(GNT_ENTRY(fconv->entry), command);
733 static void
734 cmd_added_cb(const char *cmd, PurpleCmdPriority prior, PurpleCmdFlag flags,
735 FinchConv *fconv)
737 gg_setup_commands(fconv, TRUE);
740 static void
741 cmd_removed_cb(const char *cmd, FinchConv *fconv)
743 char command[256] = "/";
744 g_strlcpy(command + 1, cmd, sizeof(command) - 1);
745 gnt_entry_remove_suggest(GNT_ENTRY(fconv->entry), command);
746 gg_setup_commands(fconv, TRUE);
749 static void
750 finch_create_conversation(PurpleConversation *conv)
752 FinchConv *ggc = FINCH_GET_DATA(conv);
753 char *title;
754 PurpleConversationType type;
755 PurpleConversation *cc;
756 PurpleAccount *account;
757 PurpleBlistNode *convnode = NULL;
759 if (ggc) {
760 gnt_window_present(ggc->window);
761 return;
764 account = purple_conversation_get_account(conv);
765 cc = find_conv_with_contact(account, purple_conversation_get_name(conv));
766 if (cc && FINCH_GET_DATA(cc))
767 ggc = FINCH_GET_DATA(cc);
768 else
769 ggc = g_new0(FinchConv, 1);
771 /* Each conversation with the same person will have the same logging setting */
772 if (ggc->list) {
773 purple_conversation_set_logging(conv,
774 purple_conversation_is_logging(ggc->list->data));
777 ggc->list = g_list_prepend(ggc->list, conv);
778 ggc->active_conv = conv;
779 FINCH_SET_DATA(conv, ggc);
781 if (cc && FINCH_GET_DATA(cc) && cc != conv) {
782 finch_conversation_set_active(conv);
783 return;
786 type = purple_conversation_get_type(conv);
787 title = get_conversation_title(conv, account);
789 ggc->window = gnt_vwindow_new(FALSE);
790 gnt_box_set_title(GNT_BOX(ggc->window), title);
791 gnt_box_set_toplevel(GNT_BOX(ggc->window), TRUE);
792 gnt_box_set_pad(GNT_BOX(ggc->window), 0);
794 switch (purple_conversation_get_type(conv)) {
795 case PURPLE_CONV_TYPE_UNKNOWN:
796 gnt_widget_set_name(ggc->window, "conversation-window-unknown" );
797 break;
798 case PURPLE_CONV_TYPE_IM:
799 gnt_widget_set_name(ggc->window, "conversation-window-im" );
800 break;
801 case PURPLE_CONV_TYPE_CHAT:
802 gnt_widget_set_name(ggc->window, "conversation-window-chat" );
803 break;
804 case PURPLE_CONV_TYPE_MISC:
805 gnt_widget_set_name(ggc->window, "conversation-window-misc" );
806 break;
807 case PURPLE_CONV_TYPE_ANY:
808 gnt_widget_set_name(ggc->window, "conversation-window-any" );
809 break;
812 ggc->tv = gnt_text_view_new();
813 gnt_widget_set_name(ggc->tv, "conversation-window-textview");
814 gnt_widget_set_size(ggc->tv, purple_prefs_get_int(PREF_ROOT "/size/width"),
815 purple_prefs_get_int(PREF_ROOT "/size/height"));
817 if (type == PURPLE_CONV_TYPE_CHAT) {
818 GntWidget *hbox, *tree;
819 FinchConvChat *fc = ggc->u.chat = g_new0(FinchConvChat, 1);
820 hbox = gnt_hbox_new(FALSE);
821 gnt_box_set_pad(GNT_BOX(hbox), 0);
822 tree = fc->userlist = gnt_tree_new_with_columns(2);
823 gnt_tree_set_col_width(GNT_TREE(tree), 0, 1); /* The flag column */
824 gnt_tree_set_compare_func(GNT_TREE(tree), (GCompareFunc)g_utf8_collate);
825 gnt_tree_set_hash_fns(GNT_TREE(tree), g_str_hash, g_str_equal, g_free);
826 gnt_tree_set_search_column(GNT_TREE(tree), 1);
827 GNT_WIDGET_SET_FLAGS(tree, GNT_WIDGET_NO_BORDER);
828 gnt_box_add_widget(GNT_BOX(hbox), ggc->tv);
829 gnt_box_add_widget(GNT_BOX(hbox), tree);
830 gnt_box_add_widget(GNT_BOX(ggc->window), hbox);
831 g_signal_connect(G_OBJECT(tree), "activate", G_CALLBACK(create_conv_from_userlist), ggc);
832 gnt_widget_set_visible(tree, purple_prefs_get_bool(PREF_USERLIST));
833 } else {
834 ggc->u.im = g_new0(FinchConvIm, 1);
835 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->tv);
838 ggc->info = gnt_vbox_new(FALSE);
839 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->info);
841 ggc->entry = gnt_entry_new(NULL);
842 gnt_box_add_widget(GNT_BOX(ggc->window), ggc->entry);
843 gnt_widget_set_name(ggc->entry, "conversation-window-entry");
844 gnt_entry_set_history_length(GNT_ENTRY(ggc->entry), -1);
845 gnt_entry_set_word_suggest(GNT_ENTRY(ggc->entry), TRUE);
846 gnt_entry_set_always_suggest(GNT_ENTRY(ggc->entry), FALSE);
848 gnt_text_view_attach_scroll_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
849 gnt_text_view_attach_pager_widget(GNT_TEXT_VIEW(ggc->tv), ggc->entry);
851 g_signal_connect_after(G_OBJECT(ggc->entry), "activate", G_CALLBACK(entry_key_pressed), ggc);
852 g_signal_connect(G_OBJECT(ggc->entry), "completion", G_CALLBACK(completion_cb), NULL);
853 g_signal_connect(G_OBJECT(ggc->window), "destroy", G_CALLBACK(closing_window), ggc);
855 gnt_widget_set_position(ggc->window, purple_prefs_get_int(PREF_ROOT "/position/x"),
856 purple_prefs_get_int(PREF_ROOT "/position/y"));
857 gnt_widget_show(ggc->window);
859 g_signal_connect(G_OBJECT(ggc->tv), "size_changed", G_CALLBACK(size_changed_cb), NULL);
860 g_signal_connect(G_OBJECT(ggc->window), "position_set", G_CALLBACK(save_position_cb), NULL);
862 if (type == PURPLE_CONV_TYPE_IM) {
863 g_signal_connect(G_OBJECT(ggc->entry), "text_changed", G_CALLBACK(send_typing_notification), ggc);
866 convnode = get_conversation_blist_node(conv);
867 if ((convnode && purple_blist_node_get_bool(convnode, "gnt-mute-sound")) ||
868 !finch_sound_is_enabled())
869 ggc->flags |= FINCH_CONV_NO_SOUND;
871 gg_create_menu(ggc);
872 gg_setup_commands(ggc, FALSE);
874 purple_signal_connect(purple_cmds_get_handle(), "cmd-added", ggc,
875 G_CALLBACK(cmd_added_cb), ggc);
876 purple_signal_connect(purple_cmds_get_handle(), "cmd-removed", ggc,
877 G_CALLBACK(cmd_removed_cb), ggc);
879 g_free(title);
880 gnt_box_give_focus_to_child(GNT_BOX(ggc->window), ggc->entry);
881 g_signal_connect(G_OBJECT(ggc->window), "gained-focus", G_CALLBACK(gained_focus_cb), ggc);
884 static void
885 finch_destroy_conversation(PurpleConversation *conv)
887 /* do stuff here */
888 FinchConv *ggc = FINCH_GET_DATA(conv);
889 ggc->list = g_list_remove(ggc->list, conv);
890 if (ggc->list && conv == ggc->active_conv) {
891 ggc->active_conv = ggc->list->data;
892 gg_setup_commands(ggc, TRUE);
895 if (ggc->list == NULL) {
896 g_free(ggc->u.chat);
897 purple_signals_disconnect_by_handle(ggc);
898 if (ggc->window)
899 gnt_widget_destroy(ggc->window);
900 g_free(ggc);
904 static void
905 finch_write_common(PurpleConversation *conv, const char *who, const char *message,
906 PurpleMessageFlags flags, time_t mtime)
908 FinchConv *ggconv = FINCH_GET_DATA(conv);
909 char *strip, *newline;
910 GntTextFormatFlags fl = 0;
911 int pos;
913 g_return_if_fail(ggconv != NULL);
915 if ((flags & PURPLE_MESSAGE_SYSTEM) && !(flags & PURPLE_MESSAGE_NOTIFY)) {
916 flags &= ~(PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV);
919 if (ggconv->active_conv != conv) {
920 if (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV))
921 finch_conversation_set_active(conv);
922 else
923 return;
926 pos = gnt_text_view_get_lines_below(GNT_TEXT_VIEW(ggconv->tv));
928 gnt_text_view_tag_change(GNT_TEXT_VIEW(ggconv->tv), "typing", NULL, TRUE);
929 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), "\n", GNT_TEXT_FLAG_NORMAL);
931 /* Unnecessary to print the timestamp for delayed message */
932 if (purple_prefs_get_bool("/finch/conversations/timestamps")) {
933 if (!mtime)
934 time(&mtime);
935 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
936 purple_utf8_strftime("(%H:%M:%S)", localtime(&mtime)), gnt_color_pair(color_timestamp));
939 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), " ", GNT_TEXT_FLAG_NORMAL);
941 if (flags & PURPLE_MESSAGE_AUTO_RESP)
942 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
943 _("<AUTO-REPLY> "), GNT_TEXT_FLAG_BOLD);
945 if (who && *who && (flags & (PURPLE_MESSAGE_SEND | PURPLE_MESSAGE_RECV)) &&
946 !(flags & PURPLE_MESSAGE_NOTIFY))
948 char * name = NULL;
949 GntTextFormatFlags msgflags = GNT_TEXT_FLAG_NORMAL;
950 gboolean me = FALSE;
952 if (purple_message_meify((char*)message, -1)) {
953 name = g_strdup_printf("*** %s", who);
954 if (!(flags & PURPLE_MESSAGE_SEND) &&
955 (flags & PURPLE_MESSAGE_NICK))
956 msgflags = gnt_color_pair(color_message_highlight);
957 else
958 msgflags = gnt_color_pair(color_message_action);
959 me = TRUE;
960 } else {
961 name = g_strdup_printf("%s", who);
962 if (flags & PURPLE_MESSAGE_SEND)
963 msgflags = gnt_color_pair(color_message_send);
964 else if (flags & PURPLE_MESSAGE_NICK)
965 msgflags = gnt_color_pair(color_message_highlight);
966 else
967 msgflags = gnt_color_pair(color_message_receive);
969 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
970 name, msgflags);
971 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv), me ? " " : ": ", GNT_TEXT_FLAG_NORMAL);
972 g_free(name);
973 } else
974 fl = GNT_TEXT_FLAG_DIM;
976 if (flags & PURPLE_MESSAGE_ERROR)
977 fl |= GNT_TEXT_FLAG_BOLD;
979 /* XXX: Remove this workaround when textview can parse messages. */
980 newline = purple_strdup_withhtml(message);
981 strip = purple_markup_strip_html(newline);
982 gnt_text_view_append_text_with_flags(GNT_TEXT_VIEW(ggconv->tv),
983 strip, fl);
985 g_free(newline);
986 g_free(strip);
988 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM &&
989 purple_conv_im_get_typing_state(PURPLE_CONV_IM(conv)) == PURPLE_TYPING) {
990 strip = g_strdup_printf(_("\n%s is typing..."), purple_conversation_get_title(conv));
991 gnt_text_view_append_text_with_tag(GNT_TEXT_VIEW(ggconv->tv),
992 strip, GNT_TEXT_FLAG_DIM, "typing");
993 g_free(strip);
996 if (pos <= 1)
997 gnt_text_view_scroll(GNT_TEXT_VIEW(ggconv->tv), 0);
999 if (flags & (PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_NICK | PURPLE_MESSAGE_ERROR))
1000 gnt_widget_set_urgent(ggconv->tv);
1001 if (flags & PURPLE_MESSAGE_RECV && !gnt_widget_has_focus(ggconv->window)) {
1002 int count = GPOINTER_TO_INT(purple_conversation_get_data(conv, "unseen-count"));
1003 purple_conversation_set_data(conv, "unseen-count", GINT_TO_POINTER(count + 1));
1004 purple_conversation_update(conv, PURPLE_CONV_UPDATE_UNSEEN);
1008 static void
1009 finch_write_chat(PurpleConversation *conv, const char *who, const char *message,
1010 PurpleMessageFlags flags, time_t mtime)
1012 purple_conversation_write(conv, who, message, flags, mtime);
1015 static void
1016 finch_write_im(PurpleConversation *conv, const char *who, const char *message,
1017 PurpleMessageFlags flags, time_t mtime)
1019 PurpleAccount *account = purple_conversation_get_account(conv);
1020 if (flags & PURPLE_MESSAGE_SEND)
1022 who = purple_connection_get_display_name(purple_account_get_connection(account));
1023 if (!who)
1024 who = purple_account_get_alias(account);
1025 if (!who)
1026 who = purple_account_get_username(account);
1028 else if (flags & PURPLE_MESSAGE_RECV)
1030 PurpleBuddy *buddy;
1031 who = purple_conversation_get_name(conv);
1032 buddy = purple_find_buddy(account, who);
1033 if (buddy)
1034 who = purple_buddy_get_contact_alias(buddy);
1037 purple_conversation_write(conv, who, message, flags, mtime);
1040 static void
1041 finch_write_conv(PurpleConversation *conv, const char *who, const char *alias,
1042 const char *message, PurpleMessageFlags flags, time_t mtime)
1044 const char *name;
1045 if (alias && *alias)
1046 name = alias;
1047 else if (who && *who)
1048 name = who;
1049 else
1050 name = NULL;
1052 finch_write_common(conv, name, message, flags, mtime);
1055 static const char *
1056 chat_flag_text(PurpleConvChatBuddyFlags flags)
1058 if (flags & PURPLE_CBFLAGS_FOUNDER)
1059 return "~";
1060 if (flags & PURPLE_CBFLAGS_OP)
1061 return "@";
1062 if (flags & PURPLE_CBFLAGS_HALFOP)
1063 return "%";
1064 if (flags & PURPLE_CBFLAGS_VOICE)
1065 return "+";
1066 return " ";
1069 static void
1070 finch_chat_add_users(PurpleConversation *conv, GList *users, gboolean new_arrivals)
1072 FinchConv *ggc = FINCH_GET_DATA(conv);
1073 GntEntry *entry = GNT_ENTRY(ggc->entry);
1075 if (!new_arrivals)
1077 /* Print the list of users in the room */
1078 GString *string = g_string_new(NULL);
1079 GList *iter;
1080 int count = g_list_length(users);
1082 g_string_printf(string,
1083 ngettext("List of %d user:\n", "List of %d users:\n", count), count);
1084 for (iter = users; iter; iter = iter->next)
1086 PurpleConvChatBuddy *cbuddy = iter->data;
1087 char *str;
1089 if ((str = cbuddy->alias) == NULL)
1090 str = cbuddy->name;
1091 g_string_append_printf(string, "[ %s ]", str);
1094 purple_conversation_write(conv, NULL, string->str,
1095 PURPLE_MESSAGE_SYSTEM, time(NULL));
1096 g_string_free(string, TRUE);
1099 for (; users; users = users->next)
1101 PurpleConvChatBuddy *cbuddy = users->data;
1102 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1103 gnt_entry_add_suggest(entry, cbuddy->name);
1104 gnt_entry_add_suggest(entry, cbuddy->alias);
1105 gnt_tree_add_row_after(tree, g_strdup(cbuddy->name),
1106 gnt_tree_create_row(tree, chat_flag_text(cbuddy->flags), cbuddy->alias), NULL, NULL);
1110 static void
1111 finch_chat_rename_user(PurpleConversation *conv, const char *old, const char *new_n, const char *new_a)
1113 /* Update the name for string completion */
1114 FinchConv *ggc = FINCH_GET_DATA(conv);
1115 GntEntry *entry = GNT_ENTRY(ggc->entry);
1116 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1117 PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), new_n);
1119 gnt_entry_remove_suggest(entry, old);
1120 gnt_tree_remove(tree, (gpointer)old);
1122 gnt_entry_add_suggest(entry, new_n);
1123 gnt_entry_add_suggest(entry, new_a);
1124 gnt_tree_add_row_after(tree, g_strdup(new_n),
1125 gnt_tree_create_row(tree, chat_flag_text(cb->flags), new_a), NULL, NULL);
1128 static void
1129 finch_chat_remove_users(PurpleConversation *conv, GList *list)
1131 /* Remove the name from string completion */
1132 FinchConv *ggc = FINCH_GET_DATA(conv);
1133 GntEntry *entry = GNT_ENTRY(ggc->entry);
1134 for (; list; list = list->next) {
1135 GntTree *tree = GNT_TREE(ggc->u.chat->userlist);
1136 gnt_entry_remove_suggest(entry, list->data);
1137 gnt_tree_remove(tree, list->data);
1141 static void
1142 finch_chat_update_user(PurpleConversation *conv, const char *user)
1144 PurpleConvChatBuddy *cb = purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), user);
1145 FinchConv *ggc = FINCH_GET_DATA(conv);
1146 gnt_tree_change_text(GNT_TREE(ggc->u.chat->userlist), (gpointer)user, 0, chat_flag_text(cb->flags));
1149 static void
1150 finch_conv_present(PurpleConversation *conv)
1152 FinchConv *fc = FINCH_CONV(conv);
1153 if (fc && fc->window)
1154 gnt_window_present(fc->window);
1157 static gboolean
1158 finch_conv_has_focus(PurpleConversation *conv)
1160 FinchConv *fc = FINCH_CONV(conv);
1161 if (fc && fc->window)
1162 return gnt_widget_has_focus(fc->window);
1163 return FALSE;
1166 static PurpleConversationUiOps conv_ui_ops =
1168 finch_create_conversation,
1169 finch_destroy_conversation,
1170 finch_write_chat,
1171 finch_write_im,
1172 finch_write_conv,
1173 finch_chat_add_users,
1174 finch_chat_rename_user,
1175 finch_chat_remove_users,
1176 finch_chat_update_user,
1177 finch_conv_present, /* present */
1178 finch_conv_has_focus, /* has_focus */
1179 NULL, /* custom_smiley_add */
1180 NULL, /* custom_smiley_write */
1181 NULL, /* custom_smiley_close */
1182 NULL, /* send_confirm */
1183 NULL,
1184 NULL,
1185 NULL,
1186 NULL
1189 PurpleConversationUiOps *finch_conv_get_ui_ops()
1191 return &conv_ui_ops;
1194 /* Xerox */
1195 static PurpleCmdRet
1196 say_command_cb(PurpleConversation *conv,
1197 const char *cmd, char **args, char **error, void *data)
1199 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1200 purple_conv_im_send(PURPLE_CONV_IM(conv), args[0]);
1201 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1202 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), args[0]);
1204 return PURPLE_CMD_RET_OK;
1207 /* Xerox */
1208 static PurpleCmdRet
1209 me_command_cb(PurpleConversation *conv,
1210 const char *cmd, char **args, char **error, void *data)
1212 char *tmp;
1214 tmp = g_strdup_printf("/me %s", args[0]);
1216 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1217 purple_conv_im_send(PURPLE_CONV_IM(conv), tmp);
1218 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1219 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), tmp);
1221 g_free(tmp);
1222 return PURPLE_CMD_RET_OK;
1225 /* Xerox */
1226 static PurpleCmdRet
1227 debug_command_cb(PurpleConversation *conv,
1228 const char *cmd, char **args, char **error, void *data)
1230 char *tmp, *markup;
1232 if (!g_ascii_strcasecmp(args[0], "version")) {
1233 tmp = g_strdup_printf("Using Finch v%s with libpurple v%s.",
1234 DISPLAY_VERSION, purple_core_get_version());
1235 } else if (!g_ascii_strcasecmp(args[0], "plugins")) {
1236 /* Show all the loaded plugins, including the protocol plugins and plugin loaders.
1237 * This is intentional, since third party prpls are often sources of bugs, and some
1238 * plugin loaders (e.g. mono) can also be buggy.
1240 GString *str = g_string_new("Loaded Plugins: ");
1241 const GList *plugins = purple_plugins_get_loaded();
1242 if (plugins) {
1243 for (; plugins; plugins = plugins->next) {
1244 str = g_string_append(str, purple_plugin_get_name(plugins->data));
1245 if (plugins->next)
1246 str = g_string_append(str, ", ");
1248 } else {
1249 str = g_string_append(str, "(none)");
1252 tmp = g_string_free(str, FALSE);
1253 } else {
1254 purple_conversation_write(conv, NULL, _("Supported debug options are: plugins version"),
1255 PURPLE_MESSAGE_NO_LOG|PURPLE_MESSAGE_ERROR, time(NULL));
1256 return PURPLE_CMD_RET_OK;
1259 markup = g_markup_escape_text(tmp, -1);
1260 if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_IM)
1261 purple_conv_im_send(PURPLE_CONV_IM(conv), markup);
1262 else if (purple_conversation_get_type(conv) == PURPLE_CONV_TYPE_CHAT)
1263 purple_conv_chat_send(PURPLE_CONV_CHAT(conv), markup);
1265 g_free(tmp);
1266 g_free(markup);
1267 return PURPLE_CMD_RET_OK;
1270 /* Xerox */
1271 static PurpleCmdRet
1272 clear_command_cb(PurpleConversation *conv,
1273 const char *cmd, char **args, char **error, void *data)
1275 purple_conversation_clear_message_history(conv);
1276 return PURPLE_CMD_RET_OK;
1279 /* Xerox */
1280 static PurpleCmdRet
1281 help_command_cb(PurpleConversation *conv,
1282 const char *cmd, char **args, char **error, void *data)
1284 GList *l, *text;
1285 GString *s;
1287 if (args[0] != NULL) {
1288 s = g_string_new("");
1289 text = purple_cmd_help(conv, args[0]);
1291 if (text) {
1292 for (l = text; l; l = l->next)
1293 if (l->next)
1294 g_string_append_printf(s, "%s\n", (char *)l->data);
1295 else
1296 g_string_append_printf(s, "%s", (char *)l->data);
1297 } else {
1298 g_string_append(s, _("No such command (in this context)."));
1300 } else {
1301 s = g_string_new(_("Use \"/help &lt;command&gt;\" for help on a specific command.\n"
1302 "The following commands are available in this context:\n"));
1304 text = purple_cmd_list(conv);
1305 for (l = text; l; l = l->next)
1306 if (l->next)
1307 g_string_append_printf(s, "%s, ", (char *)l->data);
1308 else
1309 g_string_append_printf(s, "%s.", (char *)l->data);
1310 g_list_free(text);
1313 purple_conversation_write(conv, NULL, s->str, PURPLE_MESSAGE_NO_LOG, time(NULL));
1314 g_string_free(s, TRUE);
1316 return PURPLE_CMD_RET_OK;
1319 static PurpleCmdRet
1320 cmd_show_window(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1322 void (*callback)(void) = data;
1323 callback();
1324 return PURPLE_CMD_RET_OK;
1327 static PurpleCmdRet
1328 cmd_message_color(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1330 int *msgclass = NULL;
1331 int fg, bg;
1333 if (strcmp(args[0], "receive") == 0)
1334 msgclass = &color_message_receive;
1335 else if (strcmp(args[0], "send") == 0)
1336 msgclass = &color_message_send;
1337 else if (strcmp(args[0], "highlight") == 0)
1338 msgclass = &color_message_highlight;
1339 else if (strcmp(args[0], "action") == 0)
1340 msgclass = &color_message_action;
1341 else if (strcmp(args[0], "timestamp") == 0)
1342 msgclass = &color_timestamp;
1343 else {
1344 if (error)
1345 *error = g_strdup_printf(_("%s is not a valid message class. See '/help msgcolor' for valid message classes."), args[0]);
1346 return PURPLE_CMD_RET_FAILED;
1349 fg = gnt_colors_get_color(args[1]);
1350 if (fg == -EINVAL) {
1351 if (error)
1352 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[1]);
1353 return PURPLE_CMD_RET_FAILED;
1356 bg = gnt_colors_get_color(args[2]);
1357 if (bg == -EINVAL) {
1358 if (error)
1359 *error = g_strdup_printf(_("%s is not a valid color. See '/help msgcolor' for valid colors."), args[2]);
1360 return PURPLE_CMD_RET_FAILED;
1363 init_pair(*msgclass, fg, bg);
1365 return PURPLE_CMD_RET_OK;
1368 static PurpleCmdRet
1369 users_command_cb(PurpleConversation *conv, const char *cmd, char **args, char **error, gpointer data)
1371 FinchConv *fc = FINCH_GET_DATA(conv);
1372 FinchConvChat *ch;
1373 if (!fc)
1374 return PURPLE_CMD_RET_FAILED;
1376 ch = fc->u.chat;
1377 gnt_widget_set_visible(ch->userlist,
1378 (GNT_WIDGET_IS_FLAG_SET(ch->userlist, GNT_WIDGET_INVISIBLE)));
1379 gnt_box_readjust(GNT_BOX(fc->window));
1380 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);
1381 purple_prefs_set_bool(PREF_USERLIST, !(GNT_WIDGET_IS_FLAG_SET(ch->userlist, GNT_WIDGET_INVISIBLE)));
1382 return PURPLE_CMD_RET_OK;
1385 void finch_conversation_init()
1387 color_message_send = gnt_style_get_color(NULL, "color-message-sent");
1388 if (!color_message_send)
1389 color_message_send = gnt_color_add_pair(COLOR_CYAN, -1);
1390 color_message_receive = gnt_style_get_color(NULL, "color-message-received");
1391 if (!color_message_receive)
1392 color_message_receive = gnt_color_add_pair(COLOR_RED, -1);
1393 color_message_highlight = gnt_style_get_color(NULL, "color-message-highlight");
1394 if (!color_message_highlight)
1395 color_message_highlight = gnt_color_add_pair(COLOR_GREEN, -1);
1396 color_timestamp = gnt_style_get_color(NULL, "color-timestamp");
1397 if (!color_timestamp)
1398 color_timestamp = gnt_color_add_pair(COLOR_BLUE, -1);
1399 color_message_action = gnt_style_get_color(NULL, "color-message-action");
1400 if (!color_message_action)
1401 color_message_action = gnt_color_add_pair(COLOR_YELLOW, -1);
1402 purple_prefs_add_none(PREF_ROOT);
1403 purple_prefs_add_none(PREF_ROOT "/size");
1404 purple_prefs_add_int(PREF_ROOT "/size/width", 70);
1405 purple_prefs_add_int(PREF_ROOT "/size/height", 20);
1406 purple_prefs_add_none(PREF_ROOT "/position");
1407 purple_prefs_add_int(PREF_ROOT "/position/x", 0);
1408 purple_prefs_add_int(PREF_ROOT "/position/y", 0);
1409 purple_prefs_add_none(PREF_CHAT);
1410 purple_prefs_add_bool(PREF_USERLIST, FALSE);
1412 /* Xerox the commands */
1413 purple_cmd_register("say", "S", PURPLE_CMD_P_DEFAULT,
1414 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1415 say_command_cb, _("say &lt;message&gt;: Send a message normally as if you weren't using a command."), NULL);
1416 purple_cmd_register("me", "S", PURPLE_CMD_P_DEFAULT,
1417 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1418 me_command_cb, _("me &lt;action&gt;: Send an IRC style action to a buddy or chat."), NULL);
1419 purple_cmd_register("debug", "w", PURPLE_CMD_P_DEFAULT,
1420 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1421 debug_command_cb, _("debug &lt;option&gt;: Send various debug information to the current conversation."), NULL);
1422 purple_cmd_register("clear", "", PURPLE_CMD_P_DEFAULT,
1423 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1424 clear_command_cb, _("clear: Clears the conversation scrollback."), NULL);
1425 purple_cmd_register("help", "w", PURPLE_CMD_P_DEFAULT,
1426 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1427 help_command_cb, _("help &lt;command&gt;: Help on a specific command."), NULL);
1428 purple_cmd_register("users", "", PURPLE_CMD_P_DEFAULT,
1429 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS, NULL,
1430 users_command_cb, _("users: Show the list of users in the chat."), NULL);
1432 /* Now some commands to bring up some other windows */
1433 purple_cmd_register("plugins", "", PURPLE_CMD_P_DEFAULT,
1434 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1435 cmd_show_window, _("plugins: Show the plugins window."), finch_plugins_show_all);
1436 purple_cmd_register("buddylist", "", PURPLE_CMD_P_DEFAULT,
1437 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1438 cmd_show_window, _("buddylist: Show the buddylist."), finch_blist_show);
1439 purple_cmd_register("accounts", "", PURPLE_CMD_P_DEFAULT,
1440 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1441 cmd_show_window, _("accounts: Show the accounts window."), finch_accounts_show_all);
1442 purple_cmd_register("debugwin", "", PURPLE_CMD_P_DEFAULT,
1443 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1444 cmd_show_window, _("debugwin: Show the debug window."), finch_debug_window_show);
1445 purple_cmd_register("prefs", "", PURPLE_CMD_P_DEFAULT,
1446 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1447 cmd_show_window, _("prefs: Show the preference window."), finch_prefs_show_all);
1448 purple_cmd_register("status", "", PURPLE_CMD_P_DEFAULT,
1449 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1450 cmd_show_window, _("statuses: Show the savedstatuses window."), finch_savedstatus_show_all);
1452 /* Allow customizing the message colors using a command during run-time */
1453 purple_cmd_register("msgcolor", "www", PURPLE_CMD_P_DEFAULT,
1454 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1455 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1456 "Set the color for different classes of messages in the conversation window.<br>"
1457 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1458 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1459 "EXAMPLE:<br> msgcolor send cyan default"),
1460 NULL);
1461 purple_cmd_register("msgcolour", "www", PURPLE_CMD_P_DEFAULT,
1462 PURPLE_CMD_FLAG_CHAT | PURPLE_CMD_FLAG_IM, NULL,
1463 cmd_message_color, _("msgcolor &lt;class&gt; &lt;foreground&gt; &lt;background&gt;: "
1464 "Set the color for different classes of messages in the conversation window.<br>"
1465 " &lt;class&gt;: receive, send, highlight, action, timestamp<br>"
1466 " &lt;foreground/background&gt;: black, red, green, blue, white, gray, darkgray, magenta, cyan, default<br><br>"
1467 "EXAMPLE:<br> msgcolor send cyan default"),
1468 NULL);
1470 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing", finch_conv_get_handle(),
1471 PURPLE_CALLBACK(update_buddy_typing), NULL);
1472 purple_signal_connect(purple_conversations_get_handle(), "buddy-typing-stopped", finch_conv_get_handle(),
1473 PURPLE_CALLBACK(update_buddy_typing), NULL);
1474 purple_signal_connect(purple_conversations_get_handle(), "chat-left", finch_conv_get_handle(),
1475 PURPLE_CALLBACK(chat_left_cb), NULL);
1476 purple_signal_connect(purple_conversations_get_handle(), "cleared-message-history", finch_conv_get_handle(),
1477 PURPLE_CALLBACK(cleared_message_history_cb), NULL);
1478 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-on", finch_conv_get_handle(),
1479 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1480 purple_signal_connect(purple_blist_get_handle(), "buddy-signed-off", finch_conv_get_handle(),
1481 PURPLE_CALLBACK(buddy_signed_on_off), NULL);
1482 purple_signal_connect(purple_connections_get_handle(), "signed-on", finch_conv_get_handle(),
1483 PURPLE_CALLBACK(account_signed_on_off), NULL);
1484 purple_signal_connect(purple_connections_get_handle(), "signed-off", finch_conv_get_handle(),
1485 PURPLE_CALLBACK(account_signed_on_off), NULL);
1486 purple_signal_connect(purple_connections_get_handle(), "signing-off", finch_conv_get_handle(),
1487 PURPLE_CALLBACK(account_signing_off), NULL);
1490 void finch_conversation_uninit()
1492 purple_signals_disconnect_by_handle(finch_conv_get_handle());
1495 void finch_conversation_set_active(PurpleConversation *conv)
1497 FinchConv *ggconv = FINCH_GET_DATA(conv);
1498 PurpleAccount *account;
1499 char *title;
1501 g_return_if_fail(ggconv);
1502 g_return_if_fail(g_list_find(ggconv->list, conv));
1503 if (ggconv->active_conv == conv)
1504 return;
1506 ggconv->active_conv = conv;
1507 gg_setup_commands(ggconv, TRUE);
1508 account = purple_conversation_get_account(conv);
1509 title = get_conversation_title(conv, account);
1510 gnt_screen_rename_widget(ggconv->window, title);
1511 g_free(title);
1514 void finch_conversation_set_info_widget(PurpleConversation *conv, GntWidget *widget)
1516 FinchConv *fc = FINCH_GET_DATA(conv);
1517 int height, width;
1519 gnt_box_remove_all(GNT_BOX(fc->info));
1521 if (widget) {
1522 gnt_box_add_widget(GNT_BOX(fc->info), widget);
1523 gnt_box_readjust(GNT_BOX(fc->info));
1526 gnt_widget_get_size(fc->window, &width, &height);
1527 gnt_box_readjust(GNT_BOX(fc->window));
1528 gnt_screen_resize_widget(fc->window, width, height);
1529 gnt_box_give_focus_to_child(GNT_BOX(fc->window), fc->entry);