mirror of
https://github.com/profanity-im/profanity.git
synced 2025-02-02 15:08:15 -05:00
Added /duck command to perform searches using duckduckgo chat bot
This commit is contained in:
parent
8c5866ff52
commit
90ac69beb3
@ -138,6 +138,7 @@ static gboolean _cmd_wins(gchar **args, struct cmd_help_t help);
|
||||
static gboolean _cmd_nick(gchar **args, struct cmd_help_t help);
|
||||
static gboolean _cmd_theme(gchar **args, struct cmd_help_t help);
|
||||
static gboolean _cmd_status(gchar **args, struct cmd_help_t help);
|
||||
static gboolean _cmd_duck(gchar **args, struct cmd_help_t help);
|
||||
|
||||
/*
|
||||
* The commands are broken down into three groups:
|
||||
@ -454,6 +455,17 @@ static struct cmd_t main_commands[] =
|
||||
"Example : /tiny http://www.google.com",
|
||||
NULL } } },
|
||||
|
||||
{ "/duck",
|
||||
_cmd_duck, parse_args_with_freetext, 1, 1,
|
||||
{ "/duck query", "Perform search using DuckDuckGo chatbot.",
|
||||
{ "/duck query",
|
||||
"-----------",
|
||||
"Send a search query to the DuckDuckGo chatbot.",
|
||||
"Your chat service must be federated, i.e. allow message to be sent/received outside of its domain.",
|
||||
"",
|
||||
"Example : /duck dennis ritchie",
|
||||
NULL } } },
|
||||
|
||||
{ "/who",
|
||||
_cmd_who, parse_args, 0, 1,
|
||||
{ "/who [status]", "Show contacts/room participants with chosen status.",
|
||||
@ -1094,6 +1106,17 @@ cmd_execute_default(const char * const inp)
|
||||
case WIN_CONSOLE:
|
||||
cons_show("Unknown command: %s", inp);
|
||||
break;
|
||||
|
||||
case WIN_DUCK:
|
||||
if (status != JABBER_CONNECTED) {
|
||||
ui_current_print_line("You are not currently connected.");
|
||||
} else {
|
||||
message_send_duck(inp);
|
||||
ui_duck(inp);
|
||||
free(recipient);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -1920,6 +1943,45 @@ _cmd_msg(gchar **args, struct cmd_help_t help)
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_cmd_duck(gchar **args, struct cmd_help_t help)
|
||||
{
|
||||
char *query = args[0];
|
||||
|
||||
jabber_conn_status_t conn_status = jabber_get_connection_status();
|
||||
|
||||
if (conn_status != JABBER_CONNECTED) {
|
||||
cons_show("You are not currently connected.");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (ui_windows_full()) {
|
||||
cons_show_error("Windows all used, close a window and try again.");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// if no duck win open, create it and send a help command
|
||||
if (!ui_duck_exists()) {
|
||||
ui_create_duck_win();
|
||||
|
||||
if (query != NULL) {
|
||||
message_send_duck(query);
|
||||
ui_duck(query);
|
||||
}
|
||||
|
||||
// window exists, send query
|
||||
} else {
|
||||
ui_open_duck_win();
|
||||
|
||||
if (query != NULL) {
|
||||
message_send_duck(query);
|
||||
ui_duck(query);
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
_cmd_status(gchar **args, struct cmd_help_t help)
|
||||
{
|
||||
|
@ -140,6 +140,13 @@ prof_handle_delayed_message(char *from, char *message, GTimeVal tv_stamp,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
prof_handle_duck_result(const char * const result)
|
||||
{
|
||||
ui_duck_result(result);
|
||||
ui_current_page_off();
|
||||
}
|
||||
|
||||
void
|
||||
prof_handle_error_message(const char *from, const char *err_msg)
|
||||
{
|
||||
|
@ -78,5 +78,7 @@ void prof_handle_room_list(GSList *rooms, const char *conference_node);
|
||||
void prof_handle_disco_items(GSList *items, const char *jid);
|
||||
void prof_handle_disco_info(const char *from, GSList *identities,
|
||||
GSList *features);
|
||||
void prof_handle_duck_help(const char * const result);
|
||||
void prof_handle_duck_result(const char * const result);
|
||||
|
||||
#endif
|
||||
|
@ -265,7 +265,7 @@ cons_show_wins(void)
|
||||
switch (window->type)
|
||||
{
|
||||
case WIN_CHAT:
|
||||
wprintw(console->win, "%d: chat %s", i + 1, window->from);
|
||||
wprintw(console->win, "%d: Chat %s", i + 1, window->from);
|
||||
PContact contact = contact_list_get_contact(window->from);
|
||||
|
||||
if (contact != NULL) {
|
||||
@ -282,7 +282,7 @@ cons_show_wins(void)
|
||||
break;
|
||||
|
||||
case WIN_PRIVATE:
|
||||
wprintw(console->win, "%d: private %s", i + 1, window->from);
|
||||
wprintw(console->win, "%d: Private %s", i + 1, window->from);
|
||||
|
||||
if (window->unread > 0) {
|
||||
wprintw(console->win, ", %d unread", window->unread);
|
||||
@ -291,7 +291,7 @@ cons_show_wins(void)
|
||||
break;
|
||||
|
||||
case WIN_MUC:
|
||||
wprintw(console->win, "%d: room %s", i + 1, window->from);
|
||||
wprintw(console->win, "%d: Room %s", i + 1, window->from);
|
||||
|
||||
if (window->unread > 0) {
|
||||
wprintw(console->win, ", %d unread", window->unread);
|
||||
@ -299,6 +299,11 @@ cons_show_wins(void)
|
||||
|
||||
break;
|
||||
|
||||
case WIN_DUCK:
|
||||
wprintw(console->win, "%d: DuckDuckGo search", i + 1);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
@ -205,6 +205,20 @@ ui_windows_full(void)
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ui_duck_exists(void)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i < NUM_WINS; i++) {
|
||||
if (windows[i] != NULL) {
|
||||
if (windows[i]->type == WIN_DUCK)
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
void
|
||||
ui_contact_typing(const char * const from)
|
||||
{
|
||||
@ -712,6 +726,54 @@ ui_new_chat_win(const char * const to)
|
||||
ui_switch_win(win_index);
|
||||
}
|
||||
|
||||
void
|
||||
ui_create_duck_win(void)
|
||||
{
|
||||
int win_index = _new_prof_win("DuckDuckGo search", WIN_DUCK);
|
||||
ui_switch_win(win_index);
|
||||
win_print_time(windows[win_index], '-');
|
||||
wprintw(windows[win_index]->win, "Type ':help' to find out more.\n");
|
||||
}
|
||||
|
||||
void
|
||||
ui_open_duck_win(void)
|
||||
{
|
||||
int win_index = _find_prof_win_index("DuckDuckGo search");
|
||||
if (win_index != NUM_WINS) {
|
||||
ui_switch_win(win_index);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ui_duck(const char * const query)
|
||||
{
|
||||
int win_index = _find_prof_win_index("DuckDuckGo search");
|
||||
if (win_index != NUM_WINS) {
|
||||
win_print_time(windows[win_index], '-');
|
||||
wprintw(windows[win_index]->win, "\n");
|
||||
win_print_time(windows[win_index], '-');
|
||||
wattron(windows[win_index]->win, COLOUR_ME);
|
||||
wprintw(windows[win_index]->win, "Query : ");
|
||||
wattroff(windows[win_index]->win, COLOUR_ME);
|
||||
wprintw(windows[win_index]->win, query);
|
||||
wprintw(windows[win_index]->win, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ui_duck_result(const char * const result)
|
||||
{
|
||||
int win_index = _find_prof_win_index("DuckDuckGo search");
|
||||
if (win_index != NUM_WINS) {
|
||||
win_print_time(windows[win_index], '-');
|
||||
wattron(windows[win_index]->win, COLOUR_THEM);
|
||||
wprintw(windows[win_index]->win, "Result : ");
|
||||
wattroff(windows[win_index]->win, COLOUR_THEM);
|
||||
wprintw(windows[win_index]->win, result);
|
||||
wprintw(windows[win_index]->win, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ui_outgoing_msg(const char * const from, const char * const to,
|
||||
const char * const message)
|
||||
|
@ -110,6 +110,12 @@ void ui_status_room(const char * const contact);
|
||||
void ui_status(void);
|
||||
void ui_status_private(void);
|
||||
|
||||
void ui_create_duck_win(void);
|
||||
void ui_open_duck_win(void);
|
||||
void ui_duck(const char * const query);
|
||||
void ui_duck_result(const char * const result);
|
||||
gboolean ui_duck_exists(void);
|
||||
|
||||
// create windows
|
||||
void create_title_bar(void);
|
||||
void create_status_bar(void);
|
||||
|
@ -32,7 +32,8 @@ typedef enum {
|
||||
WIN_CONSOLE,
|
||||
WIN_CHAT,
|
||||
WIN_MUC,
|
||||
WIN_PRIVATE
|
||||
WIN_PRIVATE,
|
||||
WIN_DUCK
|
||||
} win_type_t;
|
||||
|
||||
typedef struct prof_win_t {
|
||||
|
@ -71,10 +71,10 @@ message_send(const char * const msg, const char * const recipient)
|
||||
if (prefs_get_boolean(PREF_STATES) && chat_session_get_recipient_supports(recipient)) {
|
||||
chat_session_set_active(recipient);
|
||||
message = stanza_create_message(ctx, recipient, STANZA_TYPE_CHAT,
|
||||
msg, STANZA_NAME_ACTIVE);
|
||||
msg, STANZA_NAME_ACTIVE, NULL);
|
||||
} else {
|
||||
message = stanza_create_message(ctx, recipient, STANZA_TYPE_CHAT,
|
||||
msg, NULL);
|
||||
msg, NULL, NULL);
|
||||
}
|
||||
|
||||
xmpp_send(conn, message);
|
||||
@ -87,7 +87,19 @@ message_send_groupchat(const char * const msg, const char * const recipient)
|
||||
xmpp_conn_t * const conn = connection_get_conn();
|
||||
xmpp_ctx_t * const ctx = connection_get_ctx();
|
||||
xmpp_stanza_t *message = stanza_create_message(ctx, recipient,
|
||||
STANZA_TYPE_GROUPCHAT, msg, NULL);
|
||||
STANZA_TYPE_GROUPCHAT, msg, NULL, NULL);
|
||||
|
||||
xmpp_send(conn, message);
|
||||
xmpp_stanza_release(message);
|
||||
}
|
||||
|
||||
void
|
||||
message_send_duck(const char * const query)
|
||||
{
|
||||
xmpp_conn_t * const conn = connection_get_conn();
|
||||
xmpp_ctx_t * const ctx = connection_get_ctx();
|
||||
xmpp_stanza_t *message = stanza_create_message(ctx, "im@ddg.gg",
|
||||
STANZA_TYPE_CHAT, query, NULL, NULL);
|
||||
|
||||
xmpp_send(conn, message);
|
||||
xmpp_stanza_release(message);
|
||||
@ -293,8 +305,19 @@ _chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
||||
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||
Jid *jid = jid_create(from);
|
||||
|
||||
// handle ddg searches
|
||||
if (strcmp(jid->barejid, "im@ddg.gg") == 0) {
|
||||
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY);
|
||||
if (body != NULL) {
|
||||
char *message = xmpp_stanza_get_text(body);
|
||||
prof_handle_duck_result(message);
|
||||
}
|
||||
|
||||
jid_destroy(jid);
|
||||
return 1;
|
||||
|
||||
// private message from chat room use full jid (room/nick)
|
||||
if (muc_room_is_active(jid)) {
|
||||
} else if (muc_room_is_active(jid)) {
|
||||
// determine if the notifications happened whilst offline
|
||||
GTimeVal tv_stamp;
|
||||
gboolean delayed = stanza_get_delay(stanza, &tv_stamp);
|
||||
|
@ -55,7 +55,7 @@ stanza_create_chat_state(xmpp_ctx_t *ctx, const char * const recipient,
|
||||
xmpp_stanza_t *
|
||||
stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient,
|
||||
const char * const type, const char * const message,
|
||||
const char * const state)
|
||||
const char * const state, const char * const id)
|
||||
{
|
||||
char *encoded_xml = encode_xml(message);
|
||||
|
||||
@ -65,6 +65,9 @@ stanza_create_message(xmpp_ctx_t *ctx, const char * const recipient,
|
||||
xmpp_stanza_set_name(msg, STANZA_NAME_MESSAGE);
|
||||
xmpp_stanza_set_type(msg, type);
|
||||
xmpp_stanza_set_attribute(msg, STANZA_ATTR_TO, recipient);
|
||||
if (id != NULL) {
|
||||
xmpp_stanza_set_id(msg, id);
|
||||
}
|
||||
|
||||
body = xmpp_stanza_new(ctx);
|
||||
xmpp_stanza_set_name(body, STANZA_NAME_BODY);
|
||||
|
@ -116,7 +116,7 @@ xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
|
||||
|
||||
xmpp_stanza_t* stanza_create_message(xmpp_ctx_t *ctx,
|
||||
const char * const recipient, const char * const type,
|
||||
const char * const message, const char * const state);
|
||||
const char * const message, const char * const state, const char * const id);
|
||||
|
||||
xmpp_stanza_t* stanza_create_room_join_presence(xmpp_ctx_t * const ctx,
|
||||
const char * const full_room_jid);
|
||||
|
@ -99,6 +99,7 @@ void message_send_paused(const char * const recipient);
|
||||
void message_send_gone(const char * const recipient);
|
||||
void message_send_invite(const char * const room, const char * const contact,
|
||||
const char * const reason);
|
||||
void message_send_duck(const char * const query);
|
||||
|
||||
// presence functions
|
||||
void presence_subscription(const char * const jid, const jabber_subscr_t action);
|
||||
|
Loading…
x
Reference in New Issue
Block a user