1
0
mirror of https://github.com/pinpox/gitea-matrix-bot synced 2024-11-22 19:31:58 +01:00
gitea-matrix-bot/giteabot.go

137 lines
3.2 KiB
Go
Raw Normal View History

2019-04-14 22:38:13 +02:00
package main
import (
"fmt"
2019-04-14 22:51:52 +02:00
"github.com/binaryplease/matrix-bot"
2019-04-14 22:38:13 +02:00
)
2019-04-14 22:51:52 +02:00
//GiteaBot is the main struct to hold the bot
2019-04-14 22:38:13 +02:00
type GiteaBot struct {
2019-04-14 22:51:52 +02:00
*matrixbot.MatrixBot
2019-04-14 22:38:13 +02:00
Subscriptions map[string][]string
}
//SendMessageToRooms sends a message to all roomes that have subscribed to the repo
func (gb *GiteaBot) SendMessageToRooms(repo, message string) {
for _, v := range gb.Subscriptions[repo] {
_, err = gb.Client.SendText(v, message)
if err != nil {
panic(err)
}
}
}
2019-04-14 22:51:52 +02:00
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
//NewGiteaBot creates a new bot form user credentials
2019-04-14 22:38:13 +02:00
func NewGiteaBot(user, pass string) *GiteaBot {
subs := make(map[string][]string)
2019-04-14 22:51:52 +02:00
bot, err := matrixbot.NewMatrixBot(user, pass)
2019-04-14 22:38:13 +02:00
if err != nil {
panic(err)
}
gbot := &GiteaBot{
bot,
subs,
}
2019-04-18 20:38:27 +02:00
bot.RegisterCommand("!sub_user", 0, gbot.handleCommandAddSubRepo)
bot.RegisterCommand("!sub_repo", 0, gbot.handleCommandAddSubUser)
bot.RegisterCommand("!unsub_repo", 0, gbot.handleCommandRemoveSubRepo)
bot.RegisterCommand("!unsub_user", 0, gbot.handleCommandRemoveSubUser)
2019-04-14 22:38:13 +02:00
bot.RegisterCommand("!listsubs", 0, gbot.handleCommandListSubs)
bot.RegisterCommand("!help", 0, gbot.handleCommandHelp)
2019-04-14 22:51:52 +02:00
2019-04-14 22:38:13 +02:00
return gbot
}
func (gb *GiteaBot) handleCommandListSubs(message, room, sender string) {
repos := ""
for k, repo := range gb.Subscriptions {
for _, subscriber := range repo {
if subscriber == room {
repos = repos + "\n -" + k
}
}
}
if repos == "" {
gb.SendToRoom(room, "This room has not subscribed to any repositorys.")
} else {
msg := "This room has is subscribed to the following repositorys:" + repos
gb.SendToRoom(room, msg)
}
}
func (gb *GiteaBot) handleCommandAddSub(message string, room, sender string) {
repo := message[5:]
if !contains(gb.Subscriptions[repo], room) {
gb.Subscriptions[repo] = append(gb.Subscriptions[repo], room)
gb.SendToRoom(room, "Subscribed to: "+repo)
} else {
gb.SendToRoom(room, "This room has already subscribed to: "+repo)
}
}
func (gb *GiteaBot) handleCommandRemoveSub(message, room, sender string) {
repo := message[7:]
fmt.Println(gb.Subscriptions[repo])
if contains(gb.Subscriptions[repo], room) {
var tmp []string
for _, v := range gb.Subscriptions[repo] {
if v != room {
fmt.Println("readding '" + v + "'" + "because it is not equal to '" + room + "'")
tmp = append(tmp, v)
} else {
gb.SendToRoom(room, "Un-subscribed from: "+repo)
}
}
gb.Subscriptions[repo] = tmp
} else {
gb.SendToRoom(room, "This room has not subscribed to: "+repo)
}
}
func (gb *GiteaBot) handleCommandHelp(message, room, sender string) {
helpMsg := `
I'm your friendly Gitea Bot!
You can invite me to any matrix room to get updates on subscribed gitea repositorys.
The following commands are avaitible:
2019-04-18 20:38:27 +02:00
!sub_repo user/repo Subscribe to a repository
!sub_user user/repo Subscribe to a user (all repos)
!unsub_repo user/repo Remove subscription to a repository
!unsub_user user/repo Remove subscription to a user
!listsubs List the room's subscriptions
!help Display this message
2019-04-14 22:38:13 +02:00
Some of the commands might require admin powers!
`
gb.SendToRoom(room, helpMsg)
}