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 55965690..77422dc6 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -438,7 +438,7 @@ cmd_connect(ProfWin* window, const char* const command, gchar** args) // no account password setting, prompt } else { - account->password = ui_ask_password(); + account->password = ui_ask_password(false); conn_status = cl_ev_connect_account(account); free(account->password); account->password = NULL; @@ -450,7 +450,7 @@ cmd_connect(ProfWin* window, const char* const command, gchar** args) // connect with JID } else { jid = g_utf8_strdown(user, -1); - char* passwd = ui_ask_password(); + char* passwd = ui_ask_password(false); conn_status = cl_ev_connect_jid(jid, passwd, altdomain, port, tls_policy, auth_policy); free(passwd); } @@ -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/ui/core.c b/src/ui/core.c index 5021ac72..931417c4 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -993,9 +993,13 @@ ui_win_unread(int index) } char* -ui_ask_password(void) +ui_ask_password(gboolean confirm) { - status_bar_set_prompt("Enter password:"); + if (!confirm) { + status_bar_set_prompt("Enter password:"); + } else { + status_bar_set_prompt("Confirm password:"); + } return inp_get_password(); } diff --git a/src/ui/ui.h b/src/ui/ui.h index 8ce27840..391b906c 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -75,7 +75,7 @@ int ui_close_all_wins(void); int ui_close_read_wins(void); void ui_close_win(int index); int ui_win_unread(int index); -char* ui_ask_password(void); +char* ui_ask_password(gboolean confirm); char* ui_get_line(void); char* ui_ask_pgp_passphrase(const char* hint, int prev_fail); void ui_contact_online(char* barejid, Resource* resource, GDateTime* last_activity); 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 17e19297..b866128b 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); @@ -2629,3 +2630,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 45c8f2c4..86bd0d91 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2718,3 +2718,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 a26bd6ff..35f38055 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -120,6 +120,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" @@ -231,6 +232,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" @@ -390,6 +392,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/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c index 61e18ee0..dc36678b 100644 --- a/tests/unittests/ui/stub_ui.c +++ b/tests/unittests/ui/stub_ui.c @@ -211,7 +211,7 @@ ui_win_unread(int index) } char* -ui_ask_password(void) +ui_ask_password(gboolean confirm) { return mock_ptr_type(char*); } 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