about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDebXWoody <stefan@debxwoody.de>2021-10-16 08:43:55 +0200
committerMichael Vetter <jubalh@iodoru.org>2021-12-06 13:32:54 +0100
commit23e886ed5eb7fd8d53239aaf5d6266a08b01156a (patch)
tree3231d9c28cf303c6cbd5710baa464d8ed9dd86a1
parent9a9122c148a8462eb612c7a28ab0f2e3f2f0464e (diff)
downloadprofani-tty-23e886ed5eb7fd8d53239aaf5d6266a08b01156a.tar.gz
Add xep-0107: User Mood support
Implementation of XEP 0107 - User Mood
-rw-r--r--src/command/cmd_ac.c114
-rw-r--r--src/command/cmd_defs.c13
-rw-r--r--src/command/cmd_funcs.c15
-rw-r--r--src/command/cmd_funcs.h1
-rw-r--r--src/xmpp/iq.c52
-rw-r--r--src/xmpp/message.c9
-rw-r--r--src/xmpp/session.c35
-rw-r--r--src/xmpp/xmpp.h2
8 files changed, 240 insertions, 1 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c
index ee0f717e..475d02ff 100644
--- a/src/command/cmd_ac.c
+++ b/src/command/cmd_ac.c
@@ -129,6 +129,7 @@ static char* _url_autocomplete(ProfWin* window, const char* const input, gboolea
 static char* _executable_autocomplete(ProfWin* window, const char* const input, gboolean previous);
 static char* _lastactivity_autocomplete(ProfWin* window, const char* const input, gboolean previous);
 static char* _intype_autocomplete(ProfWin* window, const char* const input, gboolean previous);
+static char* _mood_autocomplete(ProfWin* window, const char* const input, gboolean previous);
 
 static char* _script_autocomplete_func(const char* const prefix, gboolean previous, void* context);
 
@@ -269,6 +270,8 @@ static Autocomplete avatar_ac;
 static Autocomplete url_ac;
 static Autocomplete executable_ac;
 static Autocomplete intype_ac;
+static Autocomplete mood_ac;
+static Autocomplete mood_type_ac;
 
 /*!
  * \brief Initialization of auto completion for commands.
@@ -1057,6 +1060,90 @@ cmd_ac_init(void)
     intype_ac = autocomplete_new();
     autocomplete_add(intype_ac, "console");
     autocomplete_add(intype_ac, "titlebar");
+
+    mood_ac = autocomplete_new();
+    autocomplete_add(mood_ac, "set");
+    mood_type_ac = autocomplete_new();
+    autocomplete_add(mood_type_ac, "afraid");
+    autocomplete_add(mood_type_ac, "amazed");
+    autocomplete_add(mood_type_ac, "angry");
+    autocomplete_add(mood_type_ac, "amorous");
+    autocomplete_add(mood_type_ac, "annoyed");
+    autocomplete_add(mood_type_ac, "anxious");
+    autocomplete_add(mood_type_ac, "aroused");
+    autocomplete_add(mood_type_ac, "ashamed");
+    autocomplete_add(mood_type_ac, "bored");
+    autocomplete_add(mood_type_ac, "brave");
+    autocomplete_add(mood_type_ac, "calm");
+    autocomplete_add(mood_type_ac, "cautious");
+    autocomplete_add(mood_type_ac, "cold");
+    autocomplete_add(mood_type_ac, "confident");
+    autocomplete_add(mood_type_ac, "confused");
+    autocomplete_add(mood_type_ac, "contemplative");
+    autocomplete_add(mood_type_ac, "contented");
+    autocomplete_add(mood_type_ac, "cranky");
+    autocomplete_add(mood_type_ac, "crazy");
+    autocomplete_add(mood_type_ac, "creative");
+    autocomplete_add(mood_type_ac, "curious");
+    autocomplete_add(mood_type_ac, "dejected");
+    autocomplete_add(mood_type_ac, "depressed");
+    autocomplete_add(mood_type_ac, "disappointed");
+    autocomplete_add(mood_type_ac, "disgusted");
+    autocomplete_add(mood_type_ac, "dismayed");
+    autocomplete_add(mood_type_ac, "distracted");
+    autocomplete_add(mood_type_ac, "embarrassed");
+    autocomplete_add(mood_type_ac, "envious");
+    autocomplete_add(mood_type_ac, "excited");
+    autocomplete_add(mood_type_ac, "flirtatious");
+    autocomplete_add(mood_type_ac, "frustrated");
+    autocomplete_add(mood_type_ac, "grumpy");
+    autocomplete_add(mood_type_ac, "guilty");
+    autocomplete_add(mood_type_ac, "happy");
+    autocomplete_add(mood_type_ac, "hopeful");
+    autocomplete_add(mood_type_ac, "hot");
+    autocomplete_add(mood_type_ac, "humbled");
+    autocomplete_add(mood_type_ac, "humiliated");
+    autocomplete_add(mood_type_ac, "hungry");
+    autocomplete_add(mood_type_ac, "hurt");
+    autocomplete_add(mood_type_ac, "impressed");
+    autocomplete_add(mood_type_ac, "in_awe");
+    autocomplete_add(mood_type_ac, "in_love");
+    autocomplete_add(mood_type_ac, "indignant");
+    autocomplete_add(mood_type_ac, "interested");
+    autocomplete_add(mood_type_ac, "intoxicated");
+    autocomplete_add(mood_type_ac, "invincible");
+    autocomplete_add(mood_type_ac, "jealous");
+    autocomplete_add(mood_type_ac, "lonely");
+    autocomplete_add(mood_type_ac, "lucky");
+    autocomplete_add(mood_type_ac, "mean");
+    autocomplete_add(mood_type_ac, "moody");
+    autocomplete_add(mood_type_ac, "nervous");
+    autocomplete_add(mood_type_ac, "neutral");
+    autocomplete_add(mood_type_ac, "offended");
+    autocomplete_add(mood_type_ac, "outraged");
+    autocomplete_add(mood_type_ac, "playful");
+    autocomplete_add(mood_type_ac, "proud");
+    autocomplete_add(mood_type_ac, "relaxed");
+    autocomplete_add(mood_type_ac, "relieved");
+    autocomplete_add(mood_type_ac, "remorseful");
+    autocomplete_add(mood_type_ac, "restless");
+    autocomplete_add(mood_type_ac, "sad");
+    autocomplete_add(mood_type_ac, "sarcastic");
+    autocomplete_add(mood_type_ac, "serious");
+    autocomplete_add(mood_type_ac, "shocked");
+    autocomplete_add(mood_type_ac, "shy");
+    autocomplete_add(mood_type_ac, "sick");
+    autocomplete_add(mood_type_ac, "sleepy");
+    autocomplete_add(mood_type_ac, "spontaneous");
+    autocomplete_add(mood_type_ac, "stressed");
+    autocomplete_add(mood_type_ac, "strong");
+    autocomplete_add(mood_type_ac, "surprised");
+    autocomplete_add(mood_type_ac, "thankful");
+    autocomplete_add(mood_type_ac, "thirsty");
+    autocomplete_add(mood_type_ac, "tired");
+    autocomplete_add(mood_type_ac, "undefined");
+    autocomplete_add(mood_type_ac, "weak");
+    autocomplete_add(mood_type_ac, "worried");
 }
 
 void
@@ -1372,6 +1459,7 @@ cmd_ac_reset(ProfWin* window)
     autocomplete_reset(url_ac);
     autocomplete_reset(executable_ac);
     autocomplete_reset(intype_ac);
+    autocomplete_reset(mood_ac);
 
     autocomplete_reset(script_ac);
     if (script_show_ac) {
@@ -1801,6 +1889,7 @@ _cmd_ac_complete_params(ProfWin* window, const char* const input, gboolean previ
     g_hash_table_insert(ac_funcs, "/executable", _executable_autocomplete);
     g_hash_table_insert(ac_funcs, "/lastactivity", _lastactivity_autocomplete);
     g_hash_table_insert(ac_funcs, "/intype", _intype_autocomplete);
+    g_hash_table_insert(ac_funcs, "/mood", _mood_autocomplete);
 
     int len = strlen(input);
     char parsed[len + 1];
@@ -4147,3 +4236,28 @@ _intype_autocomplete(ProfWin* window, const char* const input, gboolean previous
     result = autocomplete_param_with_ac(input, "/intype", intype_ac, FALSE, previous);
     return result;
 }
+
+static char*
+_mood_autocomplete(ProfWin* window, const char* const input, gboolean previous)
+{
+    char* result = NULL;
+
+    result = autocomplete_param_with_ac(input, "/mood", status_ac, TRUE, previous);
+    if (result) {
+        return result;
+    }
+
+    //jabber_conn_status_t conn_status = connection_get_status();
+    //if (conn_status == JABBER_CONNECTED) {
+    result = autocomplete_param_with_ac(input, "/mood set", mood_type_ac, FALSE, previous);
+    if (result) {
+        return result;
+    }
+
+    //    result = autocomplete_param_with_func(input, "/mood get", roster_barejid_autocomplete, previous, NULL);
+    //        if (result) {
+    //            return result;
+    //        }
+    //}
+    return result;
+}
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index 7bc4a1ff..fc4c8dca 100644
--- a/src/command/cmd_defs.c
+++ b/src/command/cmd_defs.c
@@ -2679,6 +2679,19 @@ static struct cmd_t command_defs[] = {
               "/register someuser my.xmppserv.er port 5443 tls force")
     },
 
+    { "/mood",
+      parse_args, 2, 3, NULL,
+      CMD_NOSUBFUNCS
+      CMD_MAINFUNC(cmd_mood)
+      CMD_TAGS(
+              CMD_TAG_CHAT)
+      CMD_SYN(
+              "/mood set <mood> [\"Text\"]")
+      CMD_DESC(
+              "Set your mood")
+      CMD_NOARGS
+      CMD_NOEXAMPLES
+    },
     // NEXT-COMMAND (search helper)
 };
 
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 74bbc5e0..5e6b3cc2 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -9645,3 +9645,18 @@ cmd_register(ProfWin* window, const char* const command, gchar** args)
     log_info("we are leaving the registration process");
     return TRUE;
 }
+
+gboolean
+cmd_mood(ProfWin* window, const char* const command, gchar** args)
+{
+
+    if (g_strcmp0(args[0], "set") == 0) {
+        cons_show("Your mood: %s", args[1]);
+        if (args[2]) {
+            publish_user_mood(args[1], args[2]);
+        } else {
+            publish_user_mood(args[1], args[1]);
+        }
+    }
+    return TRUE;
+}
diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h
index 5e2a7876..f4cbe0bf 100644
--- a/src/command/cmd_funcs.h
+++ b/src/command/cmd_funcs.h
@@ -249,5 +249,6 @@ gboolean cmd_editor(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_correct_editor(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_silence(ProfWin* window, const char* const command, gchar** args);
 gboolean cmd_register(ProfWin* window, const char* const command, gchar** args);
+gboolean cmd_mood(ProfWin* window, const char* const command, gchar** args);
 
 #endif
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index ebc052fc..4d140013 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -2798,3 +2798,55 @@ iq_muc_register_nick(const char* const roomjid)
     xmpp_stanza_release(iq);
     xmpp_stanza_release(query);
 }
+
+void
+publish_user_mood(const char* const mood, const char* const text)
+{
+    xmpp_ctx_t* const ctx = connection_get_ctx();
+    char* id = connection_create_stanza_id();
+
+    xmpp_stanza_t* iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id);
+
+    xmpp_stanza_t* pubsub = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(pubsub, "pubsub");
+    xmpp_stanza_set_ns(pubsub, "http://jabber.org/protocol/pubsub");
+    xmpp_stanza_add_child(iq, pubsub);
+
+    xmpp_stanza_t* publish = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(publish, "publish");
+    xmpp_stanza_set_attribute(publish, "node", "http://jabber.org/protocol/mood");
+    xmpp_stanza_add_child(pubsub, publish);
+
+    xmpp_stanza_t* item = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(item, "item");
+    xmpp_stanza_set_attribute(item, "id", "current");
+    xmpp_stanza_add_child(publish, item);
+
+    xmpp_stanza_t* mood_t = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(mood_t, "mood");
+    xmpp_stanza_set_ns(mood_t, "http://jabber.org/protocol/mood");
+    xmpp_stanza_add_child(item, mood_t);
+
+    xmpp_stanza_t* x = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(x, mood);
+    xmpp_stanza_add_child(mood_t, x);
+
+    xmpp_stanza_t* text_t = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(text_t, "text");
+    xmpp_stanza_add_child(mood_t, text_t);
+
+    xmpp_stanza_t* t = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_text(t, text);
+    xmpp_stanza_add_child(text_t, t);
+
+    iq_send_stanza(iq);
+
+    xmpp_stanza_release(iq);
+    xmpp_stanza_release(pubsub);
+    xmpp_stanza_release(publish);
+    xmpp_stanza_release(item);
+    xmpp_stanza_release(mood_t);
+    xmpp_stanza_release(x);
+    xmpp_stanza_release(text_t);
+    xmpp_stanza_release(t);
+}
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 1a964846..17ef979b 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -161,8 +161,13 @@ _message_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* con
     } else if (type && g_strcmp0(type, STANZA_TYPE_GROUPCHAT) == 0) {
         // XEP-0045: Multi-User Chat
         _handle_groupchat(stanza);
+
     } else if (type && g_strcmp0(type, STANZA_TYPE_HEADLINE) == 0) {
-        _handle_headline(stanza);
+        xmpp_stanza_t* event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT);
+        if (event) {
+            _handle_pubsub(stanza, event);
+            return 1;
+        }
     } else if (type == NULL || g_strcmp0(type, STANZA_TYPE_CHAT) == 0 || g_strcmp0(type, STANZA_TYPE_NORMAL) == 0) {
         // type: chat, normal (==NULL)
 
@@ -247,6 +252,8 @@ _message_handler(xmpp_conn_t* const conn, xmpp_stanza_t* const stanza, void* con
         if (msg_stanza) {
             _handle_chat(msg_stanza, FALSE, is_carbon, NULL, NULL);
         }
+    } else if (type && g_strcmp0(type, STANZA_TYPE_HEADLINE) == 0) {
+        _handle_headline(stanza);
     } else {
         // none of the allowed types
         char* text;
diff --git a/src/xmpp/session.c b/src/xmpp/session.c
index 046d4fd2..a58ab1f7 100644
--- a/src/xmpp/session.c
+++ b/src/xmpp/session.c
@@ -286,6 +286,38 @@ session_get_account_name(void)
     return saved_account.name;
 }
 
+static int _receive_mood(xmpp_stanza_t* const stanza, void* const userdata);
+
+static int
+_receive_mood(xmpp_stanza_t* const stanza, void* const userdata)
+{
+    const char* from = xmpp_stanza_get_from(stanza);
+    xmpp_stanza_t* event = xmpp_stanza_get_child_by_name_and_ns(stanza, "event", "http://jabber.org/protocol/pubsub#event");
+    if (event) {
+        xmpp_stanza_t* items = xmpp_stanza_get_child_by_name(event, "items");
+        if (items) {
+            xmpp_stanza_t* item = xmpp_stanza_get_child_by_name(items, "item");
+            if (item) {
+                xmpp_stanza_t* mood = xmpp_stanza_get_child_by_name_and_ns(item, "mood", "http://jabber.org/protocol/mood");
+                if (mood) {
+                    xmpp_stanza_t* c = xmpp_stanza_get_children(mood);
+                    if (c) {
+                        const char* m = xmpp_stanza_get_name(c);
+                        xmpp_stanza_t* t = xmpp_stanza_get_child_by_name(mood, "text");
+                        if (t) {
+                            const char* text = xmpp_stanza_get_text(t);
+                            cons_show("Mood from %s %s (%s)", from, m, text);
+                        } else {
+                            cons_show("Mood from %s %s", from, m);
+                        }
+                    }
+                }
+            }
+        }
+    }
+    return TRUE;
+}
+
 void
 session_login_success(gboolean secured)
 {
@@ -330,6 +362,9 @@ session_login_success(gboolean secured)
         g_timer_destroy(reconnect_timer);
         reconnect_timer = NULL;
     }
+
+    message_pubsub_event_handler_add("http://jabber.org/protocol/mood", _receive_mood, NULL, NULL);
+    caps_add_feature("http://jabber.org/protocol/mood+notify");
 }
 
 void
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index 16293d73..8a2d2eb2 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -307,4 +307,6 @@ FormField* form_get_field_by_tag(DataForm* form, const char* const tag);
 Autocomplete form_get_value_ac(DataForm* form, const char* const tag);
 void form_reset_autocompleters(DataForm* form);
 
+void publish_user_mood(const char* const mood, const char* const text);
+
 #endif