aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd-user.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd-user.c')
-rw-r--r--src/cmd-user.c195
1 files changed, 195 insertions, 0 deletions
diff --git a/src/cmd-user.c b/src/cmd-user.c
new file mode 100644
index 0000000..cf875c8
--- /dev/null
+++ b/src/cmd-user.c
@@ -0,0 +1,195 @@
+#include "commands.h"
+#include "command.h"
+#include "user.h"
+#include "l2su.h"
+#include "uuid/uuid.h"
+
+#include <stdbool.h>
+
+#define CMD_MSG_USER_ERROR_LOADING "Error loading users."
+#define CMD_MSG_USER_ERROR_SAVING "Error saving users."
+#define CMD_MSG_USER_NO_USER_FOUND "User %s not found."
+
+unsigned cmd_user_add(struct l2_context_node *ctx, char **args)
+{
+ L2_UNUSED(ctx);
+
+ if (*args) {
+ CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
+ }
+
+ int res;
+ if ((res = l2_user_load()) < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_LOADING);
+ }
+
+ struct l2_user *user = NULL;
+ l2_user_session_t *session = NULL;
+
+ session = l2_user_session_new();
+ if (!session) {
+ CMD_FATAL0("Failed to create a session");
+ }
+
+ if ((res = l2_user_session_login(session)) < 0) {
+ CMD_FATAL0("Failed to login");
+ } else if (!res) {
+ CMD_FATAL0("User declined authorization");
+ }
+
+ user = calloc(1, sizeof(struct l2_user));
+ if (!user) {
+ CMD_FATAL0("Failed to create a user");
+ }
+
+ l2_user_init(user);
+
+ user->session = session;
+
+ if (l2_user_update_profile(user) < 0) {
+ CMD_FATAL0("Failed to retrieve your profile");
+ }
+
+ for (struct l2_user *cur = l2_state.users_head; cur; cur = cur->next) {
+ if (!l2_uuid_compare(&cur->profile.uuid, &user->profile.uuid)) {
+ char uuidstr[UUID_STRLEN + 1];
+ l2_uuid_to_string(&cur->profile.uuid, uuidstr);
+ CMD_FATAL("A profile with the UUID %s already exists!", uuidstr);
+ }
+ }
+
+ l2_user_add(user);
+
+ if (l2_user_save() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_SAVING);
+ }
+
+ return CMD_RESULT_SUCCESS;
+}
+
+unsigned cmd_user_remove(struct l2_context_node *ctx, char **args)
+{
+ char *username = NULL;
+
+ if (*args) {
+ CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
+ }
+
+ l2_cmd_collect_args(ctx, 1, "name", &username);
+
+ if (l2_user_load() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_LOADING);
+ }
+
+ struct l2_user *user = l2_user_search(username);
+ if (!user) {
+ CMD_FATAL(CMD_MSG_USER_NO_USER_FOUND, username);
+ }
+
+ l2_user_delete(user);
+ l2_user_free(user);
+
+ user = NULL;
+
+ if (l2_user_save() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_SAVING);
+ }
+
+ return CMD_RESULT_SUCCESS;
+}
+
+unsigned cmd_user_nick(struct l2_context_node *ctx, char **args)
+{
+ char *username = NULL;
+ char *newnick = NULL;
+
+ if (*args) {
+ CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
+ }
+
+ l2_cmd_collect_args(ctx, 2, "name", &username, "#newnick", &newnick);
+
+ if (l2_user_load() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_LOADING);
+ }
+
+ struct l2_user *user = l2_user_search(username);
+ if (!user) {
+ CMD_FATAL(CMD_MSG_USER_NO_USER_FOUND, username);
+ }
+
+ free(user->nickname);
+ user->nickname = newnick ? strdup(newnick) : NULL;
+ if (newnick && !user->nickname) {
+ CMD_FATAL0("Failed to set nickname");
+ }
+
+ if (l2_user_save() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_SAVING);
+ }
+
+ return CMD_RESULT_SUCCESS;
+}
+
+unsigned cmd_user_login(struct l2_context_node *ctx, char **args)
+{
+ char *username = NULL;
+
+ if (*args) {
+ CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
+ }
+
+ l2_cmd_collect_args(ctx, 1, "name", &username);
+
+ if (l2_user_load() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_LOADING);
+ }
+
+ struct l2_user *user = l2_user_search(username);
+ if (!user) {
+ CMD_FATAL(CMD_MSG_USER_NO_USER_FOUND, username);
+ }
+
+ int res;
+ if ((res = l2_user_session_login(user->session)) < 0) {
+ CMD_FATAL0("Error logging in");
+ } else if (!res) {
+ CMD_FATAL0("User declined authorization");
+ }
+
+ if (l2_user_update_profile(user) < 0) {
+ CMD_FATAL0("Failed to update your profile");
+ }
+
+ if (l2_user_save() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_SAVING);
+ }
+
+ return CMD_RESULT_SUCCESS;
+}
+
+unsigned cmd_user_list(struct l2_context_node *ctx, char **args)
+{
+ L2_UNUSED(ctx);
+
+ if (*args) {
+ CMD_FATAL(CMD_MSG_UNKNOWN_ARGUMENT, *args);
+ }
+
+ if (l2_user_load() < 0) {
+ CMD_FATAL0(CMD_MSG_USER_ERROR_LOADING);
+ }
+
+ for (struct l2_user *cur = l2_state.users_head; cur; cur = cur->next) {
+ char uuidstr[UUID_STRLEN + 1];
+ l2_uuid_to_string(&cur->profile.uuid, uuidstr);
+
+ if (cur->nickname) {
+ printf("- %s (a.k.a. %s) - %s\n", cur->profile.name, cur->nickname, uuidstr);
+ } else {
+ printf("- %s - %s\n", cur->profile.name, uuidstr);
+ }
+ }
+
+ return CMD_RESULT_SUCCESS;
+}