1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Basic chat room handling of presence

This commit is contained in:
James Booth 2012-11-08 00:05:32 +00:00
parent c4c5668779
commit 181669a8cb
7 changed files with 111 additions and 12 deletions

View File

@ -645,7 +645,7 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata)
} }
static int static int
_room_presence_handler(const char * const jid) _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
{ {
char *room = NULL; char *room = NULL;
char *nick = NULL; char *nick = NULL;
@ -662,9 +662,36 @@ _room_presence_handler(const char * const jid)
if (strcmp(room_get_nick_for_room(room), nick) == 0) { if (strcmp(room_get_nick_for_room(room), nick) == 0) {
room_set_roster_received(room); room_set_roster_received(room);
prof_handle_room_roster_complete(room); prof_handle_room_roster_complete(room);
// handle presence from room members
} else { } else {
// roster not yet complete, just add to roster
if (!room_get_roster_received(room)) { if (!room_get_roster_received(room)) {
room_add_to_roster(room, nick); room_add_to_roster(room, nick);
// deal with presence information
} else {
char *type = xmpp_stanza_get_attribute(stanza, "type");
char *show_str, *status_str;
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, "status");
if (status != NULL) {
status_str = xmpp_stanza_get_text(status);
} else {
status_str = NULL;
}
if ((type != NULL) && (strcmp(type, "unavailable") == 0)) {
prof_handle_room_member_offline(room, nick, "offline", status_str);
} else {
xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, "show");
if (show != NULL) {
show_str = xmpp_stanza_get_text(show);
} else {
show_str = "online";
prof_handle_room_member_online(room, nick, show_str, status_str);
}
}
} }
} }
@ -684,7 +711,7 @@ _presence_handler(xmpp_conn_t * const conn,
// handle chat room presence // handle chat room presence
if (room_is_active(from)) { if (room_is_active(from)) {
return _room_presence_handler(from); return _room_presence_handler(from, stanza);
// handle regular presence // handle regular presence
} else { } else {

View File

@ -30,6 +30,7 @@
#include "chat_log.h" #include "chat_log.h"
#include "command.h" #include "command.h"
#include "common.h" #include "common.h"
#include "contact.h"
#include "contact_list.h" #include "contact_list.h"
#include "history.h" #include "history.h"
#include "log.h" #include "log.h"
@ -177,6 +178,7 @@ prof_handle_room_history(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message) GTimeVal tv_stamp, const char * const message)
{ {
win_show_room_history(room_jid, nick, tv_stamp, message); win_show_room_history(room_jid, nick, tv_stamp, message);
win_page_off();
} }
void void
@ -184,12 +186,32 @@ prof_handle_room_message(const char * const room_jid, const char * const nick,
const char * const message) const char * const message)
{ {
win_show_room_message(room_jid, nick, message); win_show_room_message(room_jid, nick, message);
win_page_off();
} }
void void
prof_handle_room_roster_complete(const char * const room) prof_handle_room_roster_complete(const char * const room)
{ {
win_show_room_roster(room); win_show_room_roster(room);
win_page_off();
}
void
prof_handle_room_member_online(const char * const room, const char * const nick,
const char * const show, const char * const status)
{
room_add_to_roster(room, nick);
win_show_room_member_online(room, nick);
win_page_off();
}
void
prof_handle_room_member_offline(const char * const room, const char * const nick,
const char * const show, const char * const status)
{
room_remove_from_roster(room, nick);
win_show_room_member_offline(room, nick);
win_page_off();
} }
void void

View File

@ -40,5 +40,9 @@ void prof_handle_room_history(const char * const room_jid,
void prof_handle_room_message(const char * const room_jid, const char * const nick, void prof_handle_room_message(const char * const room_jid, const char * const nick,
const char * const message); const char * const message);
void prof_handle_room_roster_complete(const char * const room); void prof_handle_room_roster_complete(const char * const room);
void prof_handle_room_member_online(const char * const room,
const char * const nick, const char * const show, const char * const status);
void prof_handle_room_member_offline(const char * const room,
const char * const nick, const char * const show, const char * const status);
#endif #endif

View File

@ -25,10 +25,12 @@
#include <glib.h> #include <glib.h>
#include <contact.h>
typedef struct _muc_room_t { typedef struct _muc_room_t {
char *jid; char *jid;
char *nick; char *nick;
GSList *roster; GHashTable *roster;
gboolean roster_received; gboolean roster_received;
} muc_room; } muc_room;
@ -47,7 +49,8 @@ room_join(const char * const jid, const char * const nick)
muc_room *new_room = malloc(sizeof(muc_room)); muc_room *new_room = malloc(sizeof(muc_room));
new_room->jid = strdup(jid); new_room->jid = strdup(jid);
new_room->nick = strdup(nick); new_room->nick = strdup(nick);
new_room->roster = NULL; new_room->roster = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
(GDestroyNotify)p_contact_free);
new_room->roster_received = FALSE; new_room->roster_received = FALSE;
g_hash_table_insert(rooms, strdup(jid), new_room); g_hash_table_insert(rooms, strdup(jid), new_room);
@ -117,17 +120,28 @@ room_add_to_roster(const char * const jid, const char * const nick)
muc_room *room = g_hash_table_lookup(rooms, jid); muc_room *room = g_hash_table_lookup(rooms, jid);
if (room != NULL) { if (room != NULL) {
room->roster = g_slist_append(room->roster, strdup(nick)); PContact contact = p_contact_new(nick, NULL, "online", NULL, NULL);
g_hash_table_replace(room->roster, strdup(nick), contact);
} }
} }
GSList * void
room_remove_from_roster(const char * const jid, const char * const nick)
{
muc_room *room = g_hash_table_lookup(rooms, jid);
if (room != NULL) {
g_hash_table_remove(room->roster, nick);
}
}
GList *
room_get_roster(const char * const jid) room_get_roster(const char * const jid)
{ {
muc_room *room = g_hash_table_lookup(rooms, jid); muc_room *room = g_hash_table_lookup(rooms, jid);
if (room != NULL) { if (room != NULL) {
return room->roster; return g_hash_table_get_keys(room->roster);
} else { } else {
return NULL; return NULL;
} }
@ -168,7 +182,7 @@ _room_free(muc_room *room)
room->nick = NULL; room->nick = NULL;
} }
if (room->roster != NULL) { if (room->roster != NULL) {
g_slist_free_full(room->roster, g_free); g_hash_table_remove_all(room->roster);
room->roster = NULL; room->roster = NULL;
} }
g_free(room); g_free(room);

View File

@ -29,6 +29,7 @@ char * room_get_nick_for_room(const char * const jid);
gboolean room_parse_room_jid(const char * const room_jid, char **room, gboolean room_parse_room_jid(const char * const room_jid, char **room,
char **nick); char **nick);
void room_add_to_roster(const char * const jid, const char * const nick); void room_add_to_roster(const char * const jid, const char * const nick);
GSList * room_get_roster(const char * const jid); GList * room_get_roster(const char * const jid);
void room_set_roster_received(const char * const jid); void room_set_roster_received(const char * const jid);
gboolean room_get_roster_received(const char * const jid); gboolean room_get_roster_received(const char * const jid);
void room_remove_from_roster(const char * const jid, const char * const nick);

View File

@ -110,6 +110,8 @@ void win_show_room_history(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message); GTimeVal tv_stamp, const char * const message);
void win_show_room_message(const char * const room_jid, const char * const nick, void win_show_room_message(const char * const room_jid, const char * const nick,
const char * const message); const char * const message);
void win_show_room_member_offline(const char * const room, const char * const nick);
void win_show_room_member_online(const char * const room, const char * const nick);
// console window actions // console window actions
void cons_about(void); void cons_about(void);

View File

@ -500,8 +500,7 @@ win_show_room_roster(const char * const room)
int win_index = _find_prof_win_index(room); int win_index = _find_prof_win_index(room);
WINDOW *win = _wins[win_index].win; WINDOW *win = _wins[win_index].win;
GSList *roster = room_get_roster(room); GList *roster = room_get_roster(room);
if (roster != NULL) { if (roster != NULL) {
wprintw(win, "Room occupants:\n"); wprintw(win, "Room occupants:\n");
@ -514,7 +513,7 @@ win_show_room_roster(const char * const room)
if (roster->next != NULL) { if (roster->next != NULL) {
wprintw(win, ", "); wprintw(win, ", ");
} }
roster = g_slist_next(roster); roster = g_list_next(roster);
} }
wprintw(win, "\n"); wprintw(win, "\n");
@ -524,6 +523,36 @@ win_show_room_roster(const char * const room)
dirty = TRUE; dirty = TRUE;
} }
void
win_show_room_member_offline(const char * const room, const char * const nick)
{
int win_index = _find_prof_win_index(room);
WINDOW *win = _wins[win_index].win;
_win_show_time(win);
wattron(win, COLOUR_OFFLINE);
wprintw(win, "-- %s has left the room.\n", nick);
wattroff(win, COLOUR_OFFLINE);
if (win_index == _curr_prof_win)
dirty = TRUE;
}
void
win_show_room_member_online(const char * const room, const char * const nick)
{
int win_index = _find_prof_win_index(room);
WINDOW *win = _wins[win_index].win;
_win_show_time(win);
wattron(win, COLOUR_ONLINE);
wprintw(win, "++ %s has joined the room.\n", nick);
wattroff(win, COLOUR_ONLINE);
if (win_index == _curr_prof_win)
dirty = TRUE;
}
void void
win_show_room_history(const char * const room_jid, const char * const nick, win_show_room_history(const char * const room_jid, const char * const nick,
GTimeVal tv_stamp, const char * const message) GTimeVal tv_stamp, const char * const message)