This repository has been archived on 2019-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mumbledj/commands.go

178 lines
4.1 KiB
Go
Raw Normal View History

2014-12-13 05:53:18 +01:00
/*
* MumbleDJ
* By Matthieu Grieger
* commands.go
* Copyright (c) 2014 Matthieu Grieger (MIT License)
*/
package main
import (
"fmt"
2014-12-19 01:28:38 +01:00
"github.com/kennygrant/sanitize"
"github.com/layeh/gumble/gumble"
"regexp"
2014-12-19 01:28:38 +01:00
"strconv"
2014-12-13 05:53:18 +01:00
"strings"
)
func parseCommand(user *gumble.User, username, command string) {
2014-12-13 05:53:18 +01:00
var com, argument string
if strings.Contains(command, " ") {
2014-12-19 01:28:38 +01:00
sanitizedCommand := sanitize.HTML(command)
parsedCommand := strings.Split(sanitizedCommand, " ")
2014-12-13 05:53:18 +01:00
com, argument = parsedCommand[0], parsedCommand[1]
} else {
com = command
argument = ""
}
2014-12-13 05:53:18 +01:00
switch com {
case dj.conf.Aliases.AddAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminAdd) {
if argument == "" {
user.Send(NO_ARGUMENT_MSG)
} else {
2014-12-20 06:52:58 +01:00
success, songTitle := add(username, argument)
if success {
2014-12-20 06:52:58 +01:00
dj.client.Self().Channel().Send(fmt.Sprintf(SONG_ADDED_HTML, username, songTitle), false)
2014-12-19 01:28:38 +01:00
} else {
user.Send(INVALID_URL_MSG)
}
2014-12-13 05:53:18 +01:00
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.SkipAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) {
2014-12-13 05:53:18 +01:00
success := skip(username, false)
if success {
fmt.Println("Skip successful!")
2014-12-19 01:28:38 +01:00
dj.client.Self().Channel().Send(fmt.Sprintf(SKIP_ADDED_HTML, username), false)
2014-12-13 05:53:18 +01:00
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.AdminSkipAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) {
2014-12-13 05:53:18 +01:00
success := skip(username, true)
if success {
fmt.Println("Forceskip successful!")
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.VolumeAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminVolume) {
if argument == "" {
dj.client.Self().Channel().Send(fmt.Sprintf(CUR_VOLUME_HTML, dj.conf.Volume.DefaultVolume), false)
} else {
success := volume(username, argument)
if success {
2014-12-19 01:28:38 +01:00
fmt.Println("Volume change successful!")
}
2014-12-13 05:53:18 +01:00
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.MoveAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminMove) {
if argument == "" {
user.Send(NO_ARGUMENT_MSG)
} else {
success := move(username, argument)
if success {
fmt.Printf("%s has been moved to %s.", dj.client.Self().Name(), argument)
2014-12-19 01:28:38 +01:00
} else {
user.Send(CHANNEL_DOES_NOT_EXIST_MSG)
}
2014-12-13 05:53:18 +01:00
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.ReloadAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminReload) {
err := loadConfiguration()
2014-12-13 05:53:18 +01:00
if err == nil {
user.Send(CONFIG_RELOAD_SUCCESS_MSG)
} else {
panic(err)
2014-12-13 05:53:18 +01:00
}
} else {
user.Send(NO_PERMISSION_MSG)
}
case dj.conf.Aliases.KillAlias:
if dj.HasPermission(username, dj.conf.Permissions.AdminKill) {
2014-12-13 05:53:18 +01:00
success := kill(username)
if success {
fmt.Println("Kill successful!")
}
} else {
user.Send(NO_PERMISSION_MSG)
}
default:
user.Send(COMMAND_DOESNT_EXIST_MSG)
2014-12-13 05:53:18 +01:00
}
}
2014-12-20 06:52:58 +01:00
func add(user, url string) (bool, string) {
youtubePatterns := []string{
`https?:\/\/www\.youtube\.com\/watch\?v=([\w-]+)`,
`https?:\/\/youtube\.com\/watch\?v=([\w-]+)`,
`https?:\/\/youtu.be\/([\w-]+)`,
`https?:\/\/youtube.com\/v\/([\w-]+)`,
`https?:\/\/www.youtube.com\/v\/([\w-]+)`,
}
matchFound := false
for _, pattern := range youtubePatterns {
re, err := regexp.Compile(pattern)
if err == nil {
if re.MatchString(url) {
matchFound = true
break
}
}
}
if matchFound {
2014-12-19 01:28:38 +01:00
urlMatch := strings.Split(url, "=")
shortUrl := urlMatch[1]
2014-12-20 06:52:58 +01:00
newSong := NewSong(user, shortUrl)
if dj.queue.AddSong(newSong) {
return true, newSong.title
} else {
2014-12-20 06:52:58 +01:00
return false, ""
}
} else {
2014-12-20 06:52:58 +01:00
return false, ""
}
2014-12-13 05:53:18 +01:00
}
func skip(user string, admin bool) bool {
return true
}
func volume(user, value string) bool {
2014-12-19 01:28:38 +01:00
parsedVolume, err := strconv.ParseFloat(value, 32)
if err == nil {
newVolume := float32(parsedVolume)
if newVolume >= dj.conf.Volume.LowestVolume && newVolume <= dj.conf.Volume.HighestVolume {
dj.conf.Volume.DefaultVolume = newVolume
return true
} else {
return false
}
} else {
return false
}
2014-12-13 05:53:18 +01:00
}
func move(user, channel string) bool {
return true
}
func kill(user string) bool {
return true
}