Initial commit
This commit is contained in:
		
							
								
								
									
										54
									
								
								.gitea/workflows/docker-publish.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								.gitea/workflows/docker-publish.yml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
			
		||||
on:
 | 
			
		||||
  push:
 | 
			
		||||
    branches:
 | 
			
		||||
      - main
 | 
			
		||||
  release:
 | 
			
		||||
    types:
 | 
			
		||||
      - published
 | 
			
		||||
 | 
			
		||||
jobs:
 | 
			
		||||
  build-deploy:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    env: 
 | 
			
		||||
      DOCKER_LATEST: nightly
 | 
			
		||||
      DOCKER_ORG: beisel.org
 | 
			
		||||
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout Code
 | 
			
		||||
        uses: actions/checkout@v4
 | 
			
		||||
 | 
			
		||||
      - name: Setup Docker
 | 
			
		||||
        run: curl -fsSL https://get.docker.com | sh
 | 
			
		||||
 | 
			
		||||
      - name: Setup Docker BuildX
 | 
			
		||||
        uses: docker/setup-buildx-action@v2
 | 
			
		||||
        with:
 | 
			
		||||
          config-inline: |
 | 
			
		||||
            [registry."https://git.beisel.it"]
 | 
			
		||||
 | 
			
		||||
      - name: Login to Registry
 | 
			
		||||
        uses: docker/login-action@v2
 | 
			
		||||
        with:
 | 
			
		||||
          registry: git.beisel.it # replace it with your local IP
 | 
			
		||||
          username: ${{ gitea.actor }}
 | 
			
		||||
          password: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
 | 
			
		||||
 | 
			
		||||
      - name: Get Meta
 | 
			
		||||
        id: meta
 | 
			
		||||
        run: |
 | 
			
		||||
          echo REPO_NAME=$(echo ${GITHUB_REPOSITORY} | awk -F"/" '{print $2}') >> $GITHUB_OUTPUT
 | 
			
		||||
          echo REPO_VERSION=$(git describe --tags --always | sed 's/^v//') >> $GITHUB_OUTPUT   
 | 
			
		||||
 | 
			
		||||
      - name: Build and push
 | 
			
		||||
        uses: docker/build-push-action@v4
 | 
			
		||||
        with:
 | 
			
		||||
          context: .
 | 
			
		||||
          file: ./Dockerfile
 | 
			
		||||
          platforms: |
 | 
			
		||||
            linux/amd64
 | 
			
		||||
            linux/arm64                        
 | 
			
		||||
          push: true
 | 
			
		||||
          tags: | # replace it with your local IP and tags
 | 
			
		||||
            git.beisel.it/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ steps.meta.outputs.REPO_VERSION }}
 | 
			
		||||
            git.beisel.it/${{ env.DOCKER_ORG }}/${{ steps.meta.outputs.REPO_NAME }}:${{ env.DOCKER_LATEST }}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										22
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								Dockerfile
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,22 @@
 | 
			
		||||
# Start from a Go base image
 | 
			
		||||
FROM golang:latest as builder
 | 
			
		||||
 | 
			
		||||
# Set the working directory
 | 
			
		||||
WORKDIR /app
 | 
			
		||||
 | 
			
		||||
# Copy the source code into the container
 | 
			
		||||
COPY . .
 | 
			
		||||
 | 
			
		||||
# Build the application
 | 
			
		||||
RUN go build -o mybot .
 | 
			
		||||
 | 
			
		||||
# Use a small base image
 | 
			
		||||
FROM alpine:latest
 | 
			
		||||
 | 
			
		||||
WORKDIR /root/
 | 
			
		||||
 | 
			
		||||
# Copy the binary from the builder stage
 | 
			
		||||
COPY --from=builder /app/mybot .
 | 
			
		||||
 | 
			
		||||
# Command to run the executable
 | 
			
		||||
CMD ["./mybot"]
 | 
			
		||||
							
								
								
									
										17
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								go.mod
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
module git.beisel.it/florian/gitea-register-account-bot
 | 
			
		||||
 | 
			
		||||
go 1.21.1
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	code.gitea.io/sdk/gitea v0.17.1
 | 
			
		||||
	github.com/bwmarrin/discordgo v0.27.1
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/davidmz/go-pageant v1.0.2 // indirect
 | 
			
		||||
	github.com/go-fed/httpsig v1.1.0 // indirect
 | 
			
		||||
	github.com/gorilla/websocket v1.4.2 // indirect
 | 
			
		||||
	github.com/hashicorp/go-version v1.6.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.17.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.15.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										68
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,68 @@
 | 
			
		||||
code.gitea.io/sdk/gitea v0.17.1 h1:3jCPOG2ojbl8AcfaUCRYLT5MUcBMFwS0OSK2mA5Zok8=
 | 
			
		||||
code.gitea.io/sdk/gitea v0.17.1/go.mod h1:aCnBqhHpoEWA180gMbaCtdX9Pl6BWBAuuP2miadoTNM=
 | 
			
		||||
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
 | 
			
		||||
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/davidmz/go-pageant v1.0.2 h1:bPblRCh5jGU+Uptpz6LgMZGD5hJoOt7otgT454WvHn0=
 | 
			
		||||
github.com/davidmz/go-pageant v1.0.2/go.mod h1:P2EDDnMqIwG5Rrp05dTRITj9z2zpGcD9efWSkTNKLIE=
 | 
			
		||||
github.com/go-fed/httpsig v1.1.0 h1:9M+hb0jkEICD8/cAiNqEB66R87tTINszBRTjwjQzWcI=
 | 
			
		||||
github.com/go-fed/httpsig v1.1.0/go.mod h1:RCMrTZvN1bJYtofsG4rd5NaO5obxQ5xBkdiS7xsT7bM=
 | 
			
		||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
 | 
			
		||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
 | 
			
		||||
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
 | 
			
		||||
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
			
		||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
			
		||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
 | 
			
		||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
			
		||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
 | 
			
		||||
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
 | 
			
		||||
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
 | 
			
		||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 | 
			
		||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 | 
			
		||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 | 
			
		||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
 | 
			
		||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 | 
			
		||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 | 
			
		||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 | 
			
		||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
 | 
			
		||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
			
		||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
 | 
			
		||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
 | 
			
		||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
 | 
			
		||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
 | 
			
		||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
 | 
			
		||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
 | 
			
		||||
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
 | 
			
		||||
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
 | 
			
		||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 | 
			
		||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 | 
			
		||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 | 
			
		||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
 | 
			
		||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
 | 
			
		||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 | 
			
		||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
 | 
			
		||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
 | 
			
		||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 | 
			
		||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
							
								
								
									
										173
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										173
									
								
								main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,173 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/rand"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"math/big"
 | 
			
		||||
	"os"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/sdk/gitea"
 | 
			
		||||
	"github.com/bwmarrin/discordgo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	discordToken string
 | 
			
		||||
	giteaToken   string
 | 
			
		||||
	giteaURL     string
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	giteaToken = os.Getenv("GITEA_TOKEN")
 | 
			
		||||
	giteaURL = os.Getenv("GITEA_URL")
 | 
			
		||||
	discordToken = os.Getenv("DISCORD_TOKEN")
 | 
			
		||||
 | 
			
		||||
	if giteaToken == "" || giteaURL == "" || discordToken == "" {
 | 
			
		||||
		fmt.Println("Environment variables GITEA_TOKEN, GITEA_URL, and DISCORD_TOKEN must be set.")
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dg, err := discordgo.New("Bot " + discordToken)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("error creating Discord session,", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
 | 
			
		||||
	dg.AddHandler(messageCreate)
 | 
			
		||||
 | 
			
		||||
	err = dg.Open()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println("error opening connection,", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Println("Bot is now running. Press CTRL+C to exit.")
 | 
			
		||||
	select {}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// validateUsername checks if the username complies with Gitea requirements
 | 
			
		||||
func validateUsername(username string) (bool, error) {
 | 
			
		||||
	// Regular expression for valid username
 | 
			
		||||
	var validUsernameRegex = regexp.MustCompile(`^[a-zA-Z0-9]+([_.-]?[a-zA-Z0-9]+)*$`)
 | 
			
		||||
 | 
			
		||||
	// Check length constraints (1 to 39 characters)
 | 
			
		||||
	if len(username) < 1 || len(username) > 39 {
 | 
			
		||||
		return false, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return validUsernameRegex.MatchString(username), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
 | 
			
		||||
	// Ignore messages from the bot itself
 | 
			
		||||
	if m.Author.ID == s.State.User.ID {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check if the command is !register
 | 
			
		||||
	if strings.HasPrefix(m.Content, "!register") {
 | 
			
		||||
		args := strings.Fields(m.Content)
 | 
			
		||||
 | 
			
		||||
		var username, email string
 | 
			
		||||
 | 
			
		||||
		// Loop through arguments to find username and email
 | 
			
		||||
		for _, arg := range args[1:] { // Skip the command itself
 | 
			
		||||
			if strings.HasPrefix(arg, "--username=") {
 | 
			
		||||
				username = strings.TrimPrefix(arg, "--username=")
 | 
			
		||||
			} else if strings.HasPrefix(arg, "--email=") {
 | 
			
		||||
				email = strings.TrimPrefix(arg, "--email=")
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// If no username parameter is provided, use the user's Discord nickname or username
 | 
			
		||||
		if username == "" {
 | 
			
		||||
			member, err := s.GuildMember(m.GuildID, m.Author.ID)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				username = m.Author.Username
 | 
			
		||||
			} else if member.Nick != "" {
 | 
			
		||||
				username = member.Nick
 | 
			
		||||
			} else {
 | 
			
		||||
				username = m.Author.Username
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Remove trailing period from username if present
 | 
			
		||||
		username = strings.TrimRight(username, ".")
 | 
			
		||||
 | 
			
		||||
		// If no email parameter is provided, use the default email format
 | 
			
		||||
		if email == "" {
 | 
			
		||||
			email = fmt.Sprintf("%s@git.beisel.it", username)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Validate the username
 | 
			
		||||
		isValid, err := validateUsername(username)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Invalid username. Username must consist of alphanumeric characters, dashes, underscores, and dots. It cannot begin or end with non-alphanumeric characters, and consecutive non-alphanumeric characters are forbidden.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		if !isValid {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Invalid username. Username must consist of alphanumeric characters, dashes, underscores, and dots. It cannot begin or end with non-alphanumeric characters, and consecutive non-alphanumeric characters are forbidden.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fmt.Println("Creating user:", username)
 | 
			
		||||
 | 
			
		||||
		// Auto-generate a password
 | 
			
		||||
		password, err := generatePassword(12) // You can choose the length of the password
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Failed to generate a secure password.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Create user in Gitea
 | 
			
		||||
		err = createUserInGitea(username, email, password)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Failed to create user in Gitea.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Send DM with login details and instructions
 | 
			
		||||
		dmChannel, err := s.UserChannelCreate(m.Author.ID)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Failed to send DM.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		dmMessage := fmt.Sprintf("You can now log in to %s using:\n\nUsername: %s\nPassword: %s\n\nYour E-Mail has temporarily been set to \"%s\". Please make sure to set this to your correct e-mail address and update your password immediately.", giteaURL, username, password, email)
 | 
			
		||||
		_, err = s.ChannelMessageSend(dmChannel.ID, dmMessage)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			s.ChannelMessageSend(m.ChannelID, "Failed to send login details via DM.")
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// generatePassword creates a secure, random password of a given length
 | 
			
		||||
func generatePassword(length int) (string, error) {
 | 
			
		||||
	const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
 | 
			
		||||
	var password strings.Builder
 | 
			
		||||
	for i := 0; i < length; i++ {
 | 
			
		||||
		charIndex, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return "", err
 | 
			
		||||
		}
 | 
			
		||||
		password.WriteByte(charset[charIndex.Int64()])
 | 
			
		||||
	}
 | 
			
		||||
	return password.String(), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func createUserInGitea(username, email, password string) error {
 | 
			
		||||
	client, _ := gitea.NewClient(giteaURL, gitea.SetToken(giteaToken))
 | 
			
		||||
 | 
			
		||||
	_, _, err := client.AdminCreateUser(gitea.CreateUserOption{
 | 
			
		||||
		LoginName: username,
 | 
			
		||||
		Username:  username,
 | 
			
		||||
		Email:     email,
 | 
			
		||||
		Password:  password,
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user