~emersion/soju-dev

Add ability for a user to delete themselves v1 APPLIED

gildarts: 2
 Add ability for a user to delete themselves
 Add ability for a user to delete themselves

 3 files changed, 65 insertions(+), 15 deletions(-)
v2 pushed with minor edits, thanks!
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/soju-dev/patches/33230/mbox | git am -3
Learn more about email & git

[RFC PATCH] Add ability for a user to delete themselves Export this patch

Also adds confirmation of users deletion
---
 service.go | 40 ++++++++++++++++++++++++++++++++--------
 1 file changed, 32 insertions(+), 8 deletions(-)

diff --git a/service.go b/service.go
index feba0fa..0d5382c 100644
--- a/service.go
+++ b/service.go
@@ -273,10 +273,9 @@ func init() {
					handle: handleUserUpdate,
				},
				"delete": {
					usage:  "<username>",
					usage:  "<username> [<confirmation token>]",
					desc:   "delete a user",
					handle: handleUserDelete,
					admin:  true,
				},
			},
		},
@@ -936,23 +935,48 @@ func handleUserUpdate(ctx context.Context, dc *downstreamConn, params []string)
}

func handleUserDelete(ctx context.Context, dc *downstreamConn, params []string) error {
	if len(params) != 1 {
		return fmt.Errorf("expected exactly one argument")
	if len(params) != 1 && len(params) != 2 {
		return fmt.Errorf("expected one or two arguments")
	}

	username := params[0]
	namehash := sha1.Sum([]byte(username))
	hash := fmt.Sprintf("%x", namehash[0:3])

	self := dc.user.Username == username

	if !dc.user.Admin && !self {
		return fmt.Errorf("only admins may use this not on themselves")
	}

	u := dc.srv.getUser(username)
	if u == nil {
		return fmt.Errorf("unknown username %q", username)
	}

	u.stop()
	if len(params) == 2 {
		token := params[1]

	if err := dc.srv.db.DeleteUser(ctx, u.ID); err != nil {
		return fmt.Errorf("failed to delete user: %v", err)
		if token != hash {
			return fmt.Errorf("provided token doesn't match name")
		}

		if err := dc.srv.db.DeleteUser(ctx, u.ID); err != nil {
			return fmt.Errorf("failed to delete user: %v", err)
		}

		sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))

		if self {
			sendServicePRIVMSG(dc, fmt.Sprintf("goodbye %s", username))
		}

		u.stop()

	} else {
		sendServicePRIVMSG(dc, fmt.Sprintf("to confirm user deletion enter /msg BouncerServ user delete %s %s", username, hash))
	}

	sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))
	return nil
}

-- 
2.36.1.windows.1

[PATCH v2] Add ability for a user to delete themselves Export this patch

Adds user self delete
Adds confirmation of user deletion
---
 doc/soju.1.scd |  4 ++--
 service.go     | 36 +++++++++++++++++++++++++++++++-----
 2 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/doc/soju.1.scd b/doc/soju.1.scd
index 4b7a7de..d6a9fec 100644
--- a/doc/soju.1.scd
+++ b/doc/soju.1.scd
@@ -432,8 +432,8 @@ abbreviated form, for instance *network* can be abbreviated as *net* or just
	- The _-realname_ flag is only valid when updating the current user.
	- The _-admin_ flag is only valid when updating another user.

*user delete* <username>
	Delete a soju user. Only admins can delete accounts.
*user delete* <username> [<confirmation token>]
	Delete a soju user. Only admins can delete accounts other than themselves.

*server status*
	Show some bouncer statistics. Only admins can query this information.
diff --git a/service.go b/service.go
index 689555f..2c6443a 100644
--- a/service.go
+++ b/service.go
@@ -273,10 +273,9 @@ func init() {
					handle: handleUserUpdate,
				},
				"delete": {
					usage:  "<username>",
					usage:  "<username> [<confirmation token>]",
					desc:   "delete a user",
					handle: handleUserDelete,
					admin:  true,
				},
			},
		},
@@ -936,23 +935,50 @@ func handleUserUpdate(ctx context.Context, dc *downstreamConn, params []string)
}

func handleUserDelete(ctx context.Context, dc *downstreamConn, params []string) error {
	if len(params) != 1 {
		return fmt.Errorf("expected exactly one argument")
	if len(params) != 1 && len(params) != 2 {
		return fmt.Errorf("expected one or two arguments")
	}

	username := params[0]
	hashbytes := sha1.Sum([]byte(username))
	hash := fmt.Sprintf("%x", hashbytes[0:3])

	self := dc.user.Username == username

	if !dc.user.Admin && !self {
		return fmt.Errorf("only admins may use this not on themselves")
	}

	u := dc.srv.getUser(username)
	if u == nil {
		return fmt.Errorf("unknown username %q", username)
	}

	if len(params) < 2 {
		sendServicePRIVMSG(dc, fmt.Sprintf("to confirm user deletion enter /msg BouncerServ user delete %s %s", username, hash))
		return nil
	}

	token := params[1]

	if token != hash {
		return fmt.Errorf("provided token doesn't match user")
	}

	if self {
		sendServicePRIVMSG(dc, fmt.Sprintf("goodbye %s, starting to delete record. There will be no further confirmation.", username))
	}

	u.stop()

	if err := dc.srv.db.DeleteUser(ctx, u.ID); err != nil {
		return fmt.Errorf("failed to delete user: %v", err)
	}

	sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))
	if !self {
		sendServicePRIVMSG(dc, fmt.Sprintf("deleted user %q", username))
	}

	return nil
}

-- 
2.36.1.windows.1
v2 pushed with minor edits, thanks!