1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Added roster command

This commit is contained in:
James Booth 2012-02-09 02:47:25 +00:00
parent bdf5e483c6
commit f5013914ef
5 changed files with 87 additions and 6 deletions

5
app.c
View File

@ -96,6 +96,11 @@ static void main_event_loop(void)
cons_help();
inp_clear();
// /who -> request roster
} else if (strncmp(command, "/who", 4) == 0) {
jabber_roster_request();
inp_clear();
// /close -> close the current chat window, if in chat
} else if (strncmp(command, "/close", 6) == 0) {
if (in_chat()) {

View File

@ -20,6 +20,9 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event
static int _jabber_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata);
static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata);
void jabber_connect(char *user, char *passwd)
{
xmpp_initialize();
@ -45,7 +48,6 @@ void jabber_process_events(void)
xmpp_run_once(_ctx, 10);
}
void jabber_send(char *msg, char *recipient)
{
xmpp_stanza_t *reply, *body, *text;
@ -67,6 +69,25 @@ void jabber_send(char *msg, char *recipient)
xmpp_stanza_release(reply);
}
void jabber_roster_request(void)
{
xmpp_stanza_t *iq, *query;
iq = xmpp_stanza_new(_ctx);
xmpp_stanza_set_name(iq, "iq");
xmpp_stanza_set_type(iq, "get");
xmpp_stanza_set_id(iq, "roster");
query = xmpp_stanza_new(_ctx);
xmpp_stanza_set_name(query, "query");
xmpp_stanza_set_ns(query, XMPP_NS_ROSTER);
xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query);
xmpp_send(_conn, iq);
xmpp_stanza_release(iq);
}
static int _jabber_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
@ -95,6 +116,7 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event
xmpp_stanza_t* pres;
log_msg(CONN, "connected");
xmpp_handler_add(conn, _jabber_message_handler, NULL, "message", NULL, ctx);
xmpp_id_handler_add(conn, _roster_handler, "roster", ctx);
pres = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(pres, "presence");
@ -106,3 +128,37 @@ static void _jabber_conn_handler(xmpp_conn_t * const conn, const xmpp_conn_event
xmpp_stop(ctx);
}
}
static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
void * const userdata)
{
xmpp_stanza_t *query, *item;
char *type, *name;
type = xmpp_stanza_get_type(stanza);
if (strcmp(type, "error") == 0)
log_msg(CONN, "ERROR: query failed");
else {
query = xmpp_stanza_get_child_by_name(stanza, "query");
cons_show("Roster:");
for (item = xmpp_stanza_get_children(query); item;
item = xmpp_stanza_get_next(item)) {
if ((name = xmpp_stanza_get_attribute(item, "name"))) {
char line[200];
sprintf(line, " %s (%s) sub=%s", name,
xmpp_stanza_get_attribute(item, "jid"),
xmpp_stanza_get_attribute(item, "subscription"));
cons_show(line);
} else {
char line[200];
sprintf(line, " %s sub=%s",
xmpp_stanza_get_attribute(item, "jid"),
xmpp_stanza_get_attribute(item, "subscription"));
cons_show(line);
}
}
}
return 1;
}

View File

@ -3,6 +3,7 @@
void jabber_connect(char *user, char *passwd);
void jabber_disconnect(void);
void jabber_roster_request(void);
void jabber_process_events(void);
void jabber_send(char *msg, char *recipient);

View File

@ -142,11 +142,15 @@ void cons_help(void)
char tstmp[80];
get_time(tstmp);
wprintw(wins[0].win, " [%s] Help\n", tstmp);
wprintw(wins[0].win, " [%s] ----\n", tstmp);
wprintw(wins[0].win, " [%s] /help - This help.\n", tstmp);
wprintw(wins[0].win, " [%s] /connect <username@host> - Login to jabber.\n", tstmp);
wprintw(wins[0].win, " [%s] /quit - Quits Profanity.\n", tstmp);
wprintw(wins[0].win, " [%s] Help:\n", tstmp);
wprintw(wins[0].win, " [%s] Commands:\n", tstmp);
wprintw(wins[0].win, " [%s] /help : This help.\n", tstmp);
wprintw(wins[0].win, " [%s] /connect <username@host> : Login to jabber.\n", tstmp);
wprintw(wins[0].win, " [%s] /who : Get roster.\n", tstmp);
wprintw(wins[0].win, " [%s] /close : Close a chat window.\n", tstmp);
wprintw(wins[0].win, " [%s] /quit : Quits Profanity.\n", tstmp);
wprintw(wins[0].win, " [%s] Shortcuts:\n", tstmp);
wprintw(wins[0].win, " [%s] F Keys : Chat windows.\n", tstmp);
// if its the current window, draw it
if (curr_win == 0) {
@ -155,6 +159,20 @@ void cons_help(void)
}
}
void cons_show(char *msg)
{
char tstmp[80];
get_time(tstmp);
wprintw(wins[0].win, " [%s] %s\n", tstmp, msg);
// if its the current window, draw it
if (curr_win == 0) {
touchwin(wins[0].win);
wrefresh(wins[0].win);
}
}
void cons_bad_command(char *cmd)
{
char tstmp[80];

View File

@ -35,4 +35,5 @@ void show_incomming_msg(char *from, char *message);
void show_outgoing_msg(char *from, char *message);
void cons_help(void);
void cons_bad_command(char *cmd);
void cons_show(char *cmd);
#endif