diff options
-rw-r--r-- | src/command/cmd_defs.c | 13 | ||||
-rw-r--r-- | src/command/cmd_funcs.c | 27 | ||||
-rw-r--r-- | src/command/cmd_funcs.h | 1 | ||||
-rw-r--r-- | src/xmpp/connection.c | 13 | ||||
-rw-r--r-- | src/xmpp/iq.c | 30 | ||||
-rw-r--r-- | src/xmpp/stanza.c | 37 | ||||
-rw-r--r-- | src/xmpp/stanza.h | 6 | ||||
-rw-r--r-- | src/xmpp/xmpp.h | 2 | ||||
-rw-r--r-- | tests/unittests/xmpp/stub_xmpp.c | 10 |
9 files changed, 138 insertions, 1 deletions
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index 49c9bf95..3493b214 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2564,6 +2564,19 @@ static struct cmd_t command_defs[] = { CMD_NOEXAMPLES }, + { "/changepassword", + parse_args, 0, 0, NULL, + CMD_NOSUBFUNCS + CMD_MAINFUNC(cmd_change_password) + CMD_NOTAGS + CMD_SYN( + "/changepassword") + CMD_DESC( + "Change password of logged in account") + CMD_NOARGS + CMD_NOEXAMPLES + }, + // NEXT-COMMAND (search helper) }; diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 60b7010b..77422dc6 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -9298,3 +9298,30 @@ cmd_mam(ProfWin* window, const char* const command, gchar** args) return TRUE; } + +gboolean +cmd_change_password(ProfWin* window, const char* const command, gchar** args) +{ + jabber_conn_status_t conn_status = connection_get_status(); + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + return TRUE; + } + + char* user = connection_get_user(); + char* passwd = ui_ask_password(false); + char* confirm_passwd = ui_ask_password(true); + + if (g_strcmp0(passwd, confirm_passwd) == 0) { + iq_register_change_password(user, passwd); + } else { + cons_show("Aborted! The new password and the confirmed password do not match."); + } + + free(user); + free(passwd); + free(confirm_passwd); + + return TRUE; +} diff --git a/src/command/cmd_funcs.h b/src/command/cmd_funcs.h index bcd634da..5a192efb 100644 --- a/src/command/cmd_funcs.h +++ b/src/command/cmd_funcs.h @@ -159,6 +159,7 @@ gboolean cmd_charset(ProfWin* window, const char* const command, gchar** args); gboolean cmd_console(ProfWin* window, const char* const command, gchar** args); gboolean cmd_command_list(ProfWin* window, const char* const command, gchar** args); gboolean cmd_command_exec(ProfWin* window, const char* const command, gchar** args); +gboolean cmd_change_password(ProfWin* window, const char* const command, gchar** args); gboolean cmd_plugins(ProfWin* window, const char* const command, gchar** args); gboolean cmd_plugins_sourcepath(ProfWin* window, const char* const command, gchar** args); diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index d34f9a4f..c188498c 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -451,6 +451,19 @@ connection_get_barejid(void) return result; } +char* +connection_get_user(void) +{ + const char* jid = connection_get_fulljid(); + char* result; + result = strdup(jid); + + char* split = strchr(result, '@'); + *split = '\0'; + + return result; +} + void connection_features_received(const char* const jid) { diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 5012d1a5..8556d63d 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -149,6 +149,7 @@ static int _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userda static int _command_list_result_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _command_exec_response_handler(xmpp_stanza_t* const stanza, void* const userdata); static int _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata); +static int _register_change_password_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata); static void _iq_free_room_data(ProfRoomInfoData* roominfo); static void _iq_free_affiliation_set(ProfPrivilegeSet* affiliation_set); @@ -2616,3 +2617,32 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata) return 0; } + +void +iq_register_change_password(const char* const user, const char* const password) +{ + xmpp_ctx_t* const ctx = connection_get_ctx(); + xmpp_stanza_t* iq = stanza_change_password(ctx, user, password); + + const char* id = xmpp_stanza_get_id(iq); + iq_id_handler_add(id, _register_change_password_result_id_handler, NULL, NULL); + + iq_send_stanza(iq); + xmpp_stanza_release(iq); +} + +static int +_register_change_password_result_id_handler(xmpp_stanza_t* const stanza, void* const userdata) +{ + const char* type = xmpp_stanza_get_type(stanza); + if (g_strcmp0(type, "error") == 0) { + char* error_message = stanza_get_error_message(stanza); + cons_show_error("Server error: %s", error_message); + log_debug("Password change error: %s", error_message); + free(error_message); + } else { + cons_show("Password successfully changed."); + log_debug("Password successfully changed."); + } + return 0; +} diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 2b39a023..f13d784b 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2739,3 +2739,40 @@ stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const s return iq; } + +xmpp_stanza_t* +stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* const password) +{ + char* id = connection_create_stanza_id(); + xmpp_stanza_t* iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id); + free(id); + + xmpp_stanza_t* change_password = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(change_password, STANZA_NAME_QUERY); + xmpp_stanza_set_ns(change_password, STANZA_NS_REGISTER); + + xmpp_stanza_t* username_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(username_st, STANZA_NAME_USERNAME); + xmpp_stanza_t* username_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(username_text, user); + xmpp_stanza_add_child(username_st, username_text); + xmpp_stanza_release(username_text); + + xmpp_stanza_t* password_st = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(password_st, STANZA_NAME_PASSWORD); + xmpp_stanza_t* password_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(password_text, password); + xmpp_stanza_add_child(password_st, password_text); + xmpp_stanza_release(password_text); + + xmpp_stanza_add_child(change_password, username_st); + xmpp_stanza_release(username_st); + + xmpp_stanza_add_child(change_password, password_st); + xmpp_stanza_release(password_st); + + xmpp_stanza_add_child(iq, change_password); + xmpp_stanza_release(change_password); + + return iq; +} diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index a4ac6b33..0634f1ad 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -116,6 +116,7 @@ #define STANZA_NAME_FIN "fin" #define STANZA_NAME_LAST "last" #define STANZA_NAME_AFTER "after" +#define STANZA_NAME_USERNAME "username" // error conditions #define STANZA_NAME_BAD_REQUEST "bad-request" @@ -224,6 +225,7 @@ #define STANZA_NS_MAM2 "urn:xmpp:mam:2" #define STANZA_NS_EXT_GAJIM_BOOKMARKS "xmpp:gajim.org/bookmarks" #define STANZA_NS_RSM "http://jabber.org/protocol/rsm" +#define STANZA_NS_REGISTER "jabber:iq:register" #define STANZA_DATAFORM_SOFTWARE "urn:xmpp:dataforms:softwareinfo" @@ -383,6 +385,8 @@ void stanza_free_caps(XMPPCaps* caps); xmpp_stanza_t* stanza_create_avatar_retrieve_data_request(xmpp_ctx_t* ctx, const char* stanza_id, const char* const item_id, const char* const jid); -xmpp_stanza_t* stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const startdate, const char *const lastid); +xmpp_stanza_t* stanza_create_mam_iq(xmpp_ctx_t* ctx, const char* const jid, const char* const startdate, const char* const lastid); + +xmpp_stanza_t* stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* const password); #endif diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index cc93c51a..d4d32836 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -186,6 +186,7 @@ char* connection_get_presence_msg(void); void connection_set_presence_msg(const char* const message); const char* connection_get_fulljid(void); char* connection_get_barejid(void); +char* connection_get_user(void); char* connection_create_uuid(void); void connection_free_uuid(char* uuid); #ifdef HAVE_LIBMESODE @@ -256,6 +257,7 @@ void iq_http_upload_request(HTTPUpload* upload); void iq_command_list(const char* const target); void iq_command_exec(const char* const target, const char* const command); void iq_mam_request(ProfChatWin* win); +void iq_register_change_password(const char* const user, const char* const password); EntityCapabilities* caps_lookup(const char* const jid); void caps_close(void); diff --git a/tests/unittests/xmpp/stub_xmpp.c b/tests/unittests/xmpp/stub_xmpp.c index e1b6fdd8..0a0f3fc6 100644 --- a/tests/unittests/xmpp/stub_xmpp.c +++ b/tests/unittests/xmpp/stub_xmpp.c @@ -62,6 +62,12 @@ connection_get_barejid(void) return mock_ptr_type(char*); } +char* +connection_get_user(void) +{ + return mock_ptr_type(char*); +} + const char* connection_get_domain(void) { @@ -393,6 +399,10 @@ void iq_command_exec(const char* const target, const char* const command) { } +void +iq_register_change_password(const char* const user, const char* const password) +{ +} // caps functions void |