1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

initial register command test

This commit is contained in:
swirl 2021-07-01 19:30:30 -04:00
parent 4641537721
commit d9366a26d6
9 changed files with 274 additions and 0 deletions

View File

@ -2642,6 +2642,32 @@ static struct cmd_t command_defs[] = {
CMD_NOEXAMPLES
},
{ "/register",
parse_args, 2, 6, NULL,
CMD_NOSUBFUNCS
CMD_MAINFUNC(cmd_register)
CMD_TAGS(
CMD_TAG_CONNECTION)
CMD_SYN(
"/register <host> <username> [port <port>] [tls force|allow|trust|legacy|disable]")
CMD_DESC(
"Register an account on a server.")
CMD_ARGS(
{ "<host>", "Server to register account on." },
{ "<username>", "Username to register with." },
{ "port <port>", "The port to use if different to the default (5222, or 5223 for SSL)." },
{ "tls force", "Force TLS connection, and fail if one cannot be established. This is the default behavior." },
{ "tls allow", "Use TLS for the connection if it is available." },
{ "tls trust", "Force TLS connection and trust server's certificate." },
{ "tls legacy", "Use legacy TLS for the connection. This forces TLS just after the TCP connection is established. Use when a server doesn't support STARTTLS." },
{ "tls disable", "Disable TLS for the connection." })
CMD_EXAMPLES(
"/register valhalla.edda odin",
"/register vanaheimr.edda freyr port 5678",
"/register 127.0.0.1 me tls disable",
"/register my.xmppserv.er someuser port 5443 tls force")
},
// NEXT-COMMAND (search helper)
};
@ -3012,3 +3038,4 @@ command_mangen(void)
g_free(header);
g_list_free(cmds);
}

View File

@ -9541,3 +9541,68 @@ cmd_silence(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}
gboolean
cmd_register(ProfWin* window, const char* const command, gchar** args)
{
gchar* opt_keys[] = { "port", "tls", NULL };
gboolean parsed;
GHashTable* options = parse_options(&args[2], opt_keys, &parsed);
if (!parsed) {
cons_bad_cmd_usage(command);
cons_show("");
options_destroy(options);
return TRUE;
}
char* tls_policy = g_hash_table_lookup(options, "tls");
if (tls_policy && (g_strcmp0(tls_policy, "force") != 0) && (g_strcmp0(tls_policy, "allow") != 0) && (g_strcmp0(tls_policy, "trust") != 0) && (g_strcmp0(tls_policy, "disable") != 0) && (g_strcmp0(tls_policy, "legacy") != 0)) {
cons_bad_cmd_usage(command);
cons_show("");
options_destroy(options);
return TRUE;
}
int port = 0;
if (g_hash_table_contains(options, "port")) {
char* port_str = g_hash_table_lookup(options, "port");
char* err_msg = NULL;
gboolean res = strtoi_range(port_str, &port, 1, 65535, &err_msg);
if (!res) {
cons_show(err_msg);
cons_show("");
free(err_msg);
port = 0;
options_destroy(options);
return TRUE;
}
}
char* host = args[0];
jabber_conn_status_t conn_status = connection_connect_raw(host, port, tls_policy, "default");
if (conn_status == JABBER_DISCONNECTED) {
cons_show_error("Connection attempt to server %s port %d failed.", host, port);
log_info("Connection attempt to server %s port %d failed.", host, port);
return TRUE;
}
char* username = args[1];
char* passwd = ui_ask_password(false);
char* confirm_passwd = ui_ask_password(true);
if (g_strcmp0(passwd, confirm_passwd) == 0) {
iq_register_new_account(username, passwd);
} else {
cons_show("The two passwords do not match.");
}
free(username);
free(passwd);
free(confirm_passwd);
return TRUE;
}

View File

@ -247,5 +247,7 @@ gboolean cmd_executable_editor(ProfWin* window, const char* const command, gchar
gboolean cmd_mam(ProfWin* window, const char* const command, gchar** args);
gboolean cmd_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);
#endif

View File

@ -245,6 +245,111 @@ connection_connect(const char* const jid, const char* const passwd, const char*
return conn.conn_status;
}
jabber_conn_status_t
connection_connect_raw(const char* const altdomain, int port,
const char* const tls_policy, const char* const auth_policy)
{
long flags;
Jid* jidp = jid_create(altdomain);
if (jidp == NULL) {
log_error("Malformed JID not able to connect: %s", altdomain);
conn.conn_status = JABBER_DISCONNECTED;
return conn.conn_status;
}
_compute_identifier(jidp->barejid);
jid_destroy(jidp);
if (conn.xmpp_log) {
free(conn.xmpp_log);
}
conn.xmpp_log = _xmpp_get_file_logger();
if (conn.xmpp_conn) {
xmpp_conn_release(conn.xmpp_conn);
}
if (conn.xmpp_ctx) {
xmpp_ctx_free(conn.xmpp_ctx);
}
conn.xmpp_ctx = xmpp_ctx_new(NULL, conn.xmpp_log);
if (conn.xmpp_ctx == NULL) {
log_warning("Failed to get libstrophe ctx during connect");
return JABBER_DISCONNECTED;
}
conn.xmpp_conn = xmpp_conn_new(conn.xmpp_ctx);
if (conn.xmpp_conn == NULL) {
log_warning("Failed to get libstrophe conn during connect");
return JABBER_DISCONNECTED;
}
xmpp_conn_set_jid(conn.xmpp_conn, altdomain);
flags = xmpp_conn_get_flags(conn.xmpp_conn);
if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) {
flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
} else if (g_strcmp0(tls_policy, "trust") == 0) {
flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
flags |= XMPP_CONN_FLAG_TRUST_TLS;
} else if (g_strcmp0(tls_policy, "disable") == 0) {
flags |= XMPP_CONN_FLAG_DISABLE_TLS;
} else if (g_strcmp0(tls_policy, "legacy") == 0) {
flags |= XMPP_CONN_FLAG_LEGACY_SSL;
}
if (auth_policy && (g_strcmp0(auth_policy, "legacy") == 0)) {
flags |= XMPP_CONN_FLAG_LEGACY_AUTH;
}
xmpp_conn_set_flags(conn.xmpp_conn, flags);
/* Print debug logs that can help when users share the logs */
if (flags != 0) {
log_debug("Connecting with flags (0x%lx):", flags);
#define LOG_FLAG_IF_SET(name) \
if (flags & name) { \
log_debug(" " #name); \
}
LOG_FLAG_IF_SET(XMPP_CONN_FLAG_MANDATORY_TLS);
LOG_FLAG_IF_SET(XMPP_CONN_FLAG_TRUST_TLS);
LOG_FLAG_IF_SET(XMPP_CONN_FLAG_DISABLE_TLS);
LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_SSL);
LOG_FLAG_IF_SET(XMPP_CONN_FLAG_LEGACY_AUTH);
#undef LOG_FLAG_IF_SET
}
#ifdef HAVE_LIBMESODE
char* cert_path = prefs_get_tls_certpath();
if (cert_path) {
xmpp_conn_tlscert_path(conn.xmpp_conn, cert_path);
free(cert_path);
}
int connect_status = xmpp_connect_raw(
conn.xmpp_conn,
altdomain,
port,
_connection_certfail_cb,
_connection_handler,
conn.xmpp_ctx);
#else
int connect_status = xmpp_connect_raw(
conn.xmpp_conn,
altdomain,
port,
_connection_handler,
conn.xmpp_ctx);
#endif
if (connect_status == 0) {
conn.conn_status = JABBER_CONNECTING;
} else {
conn.conn_status = JABBER_DISCONNECTED;
}
return conn.conn_status;
}
void
connection_disconnect(void)
{
@ -755,3 +860,4 @@ connection_get_profanity_identifier(void)
{
return prof_identifier;
}

View File

@ -46,6 +46,8 @@ void connection_check_events(void);
jabber_conn_status_t connection_connect(const char* const fulljid, const char* const passwd, const char* const altdomain, int port,
const char* const tls_policy, const char* const auth_policy);
jabber_conn_status_t connection_connect_raw(const char* const altdomain, int port,
const char* const tls_policy, const char* const auth_policy);
void connection_disconnect(void);
void connection_set_disconnected(void);
@ -68,3 +70,4 @@ void connection_remove_available_resource(const char* const resource);
char* connection_create_stanza_id(void);
#endif

View File

@ -150,6 +150,7 @@ static int _command_list_result_handler(xmpp_stanza_t* const stanza, void* const
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 int _register_new_account_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);
@ -2654,6 +2655,36 @@ _mam_rsm_id_handler(xmpp_stanza_t* const stanza, void* const userdata)
return 0;
}
void
iq_register_new_account(const char* const user, const char* const password)
{
//char* id = connection_create_stanza_id();
xmpp_ctx_t* const ctx = connection_get_ctx();
xmpp_stanza_t* iq = stanza_register_new_account(ctx, user, password);
const char* id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _register_new_account_result_id_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
static int
_register_new_account_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("Registration error: %s", error_message);
free(error_message);
} else {
cons_show("Registration successful.");
log_debug("Registration successful.");
}
return 0;
}
void
iq_register_change_password(const char* const user, const char* const password)
{
@ -2807,3 +2838,4 @@ iq_muc_register_nick(const char* const roomjid)
xmpp_stanza_release(iq);
xmpp_stanza_release(query);
}

View File

@ -2751,6 +2751,40 @@ stanza_change_password(xmpp_ctx_t* ctx, const char* const user, const char* cons
return iq;
}
xmpp_stanza_t*
stanza_register_new_account(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* register_new_account = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(register_new_account, STANZA_NAME_QUERY);
xmpp_stanza_set_ns(register_new_account, 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(register_new_account, username_st);
xmpp_stanza_release(username_st);
xmpp_stanza_add_child(register_new_account, password_st);
xmpp_stanza_release(password_st);
return iq;
}
xmpp_stanza_t*
stanza_request_voice(xmpp_ctx_t* ctx, const char* const room)
{
@ -2889,3 +2923,4 @@ stanza_get_service_contact_addresses(xmpp_ctx_t* ctx, xmpp_stanza_t* stanza)
return addresses;
}

View File

@ -408,8 +408,10 @@ 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_change_password(xmpp_ctx_t* ctx, const char* const user, const char* const password);
xmpp_stanza_t* stanza_register_new_account(xmpp_ctx_t* ctx, const char* const user, const char* const password);
xmpp_stanza_t* stanza_request_voice(xmpp_ctx_t* ctx, const char* const room);
xmpp_stanza_t* stanza_create_approve_voice(xmpp_ctx_t* ctx, const char* const id, const char* const jid, const char* const node, DataForm* form);
xmpp_stanza_t* stanza_create_muc_register_nick(xmpp_ctx_t* ctx, const char* const id, const char* const jid, const char* const node, DataForm* form);
#endif

View File

@ -266,6 +266,7 @@ 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);
void iq_register_new_account(const char* const user, const char* const password);
void iq_muc_register_nick(const char* const roomjid);
EntityCapabilities* caps_lookup(const char* const jid);
@ -313,3 +314,4 @@ Autocomplete form_get_value_ac(DataForm* form, const char* const tag);
void form_reset_autocompleters(DataForm* form);
#endif