2014-12-13 05:53:18 +01:00
|
|
|
/*
|
|
|
|
* MumbleDJ
|
|
|
|
* By Matthieu Grieger
|
|
|
|
* commands.go
|
|
|
|
* Copyright (c) 2014 Matthieu Grieger (MIT License)
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-27 09:25:49 +01:00
|
|
|
"errors"
|
2014-12-13 05:53:18 +01:00
|
|
|
"fmt"
|
2014-12-19 01:28:38 +01:00
|
|
|
"github.com/kennygrant/sanitize"
|
2014-12-16 01:40:05 +01:00
|
|
|
"github.com/layeh/gumble/gumble"
|
2014-12-27 09:25:49 +01:00
|
|
|
"os"
|
2014-12-16 01:40:05 +01:00
|
|
|
"regexp"
|
2014-12-19 01:28:38 +01:00
|
|
|
"strconv"
|
2014-12-13 05:53:18 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Called on text message event. Checks the message for a command string, and processes it accordingly if
|
|
|
|
// it contains a command.
|
2014-12-16 01:40:05 +01:00
|
|
|
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-16 01:40:05 +01:00
|
|
|
|
2014-12-13 05:53:18 +01:00
|
|
|
switch com {
|
2014-12-27 19:05:13 +01:00
|
|
|
// Add command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.AddAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminAdd) {
|
|
|
|
if argument == "" {
|
|
|
|
user.Send(NO_ARGUMENT_MSG)
|
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
if songTitle, err := add(username, argument); err == nil {
|
2014-12-20 06:52:58 +01:00
|
|
|
dj.client.Self().Channel().Send(fmt.Sprintf(SONG_ADDED_HTML, username, songTitle), false)
|
2014-12-27 09:25:49 +01:00
|
|
|
if dj.queue.Len() == 1 && !dj.audioStream.IsPlaying() {
|
|
|
|
dj.currentSong = dj.queue.NextSong()
|
|
|
|
if err := dj.currentSong.Download(); err == nil {
|
|
|
|
dj.currentSong.Play()
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 01:28:38 +01:00
|
|
|
} else {
|
|
|
|
user.Send(INVALID_URL_MSG)
|
2014-12-16 01:40:05 +01:00
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Skip command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.SkipAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminSkip) {
|
2014-12-27 09:25:49 +01:00
|
|
|
if err := skip(username, false); err == nil {
|
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
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Forceskip command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.AdminSkipAlias:
|
2014-12-27 09:25:49 +01:00
|
|
|
if dj.HasPermission(username, true) {
|
|
|
|
if err := skip(username, true); err == nil {
|
|
|
|
dj.client.Self().Channel().Send(ADMIN_SONG_SKIP_MSG, false)
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Volume command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.VolumeAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminVolume) {
|
|
|
|
if argument == "" {
|
2014-12-27 18:41:27 +01:00
|
|
|
dj.client.Self().Channel().Send(fmt.Sprintf(CUR_VOLUME_HTML, dj.audioStream.Volume()), false)
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
if err := volume(username, argument); err == nil {
|
|
|
|
dj.client.Self().Channel().Send(fmt.Sprintf(VOLUME_SUCCESS_HTML, username, argument), false)
|
|
|
|
} else {
|
2014-12-27 18:41:27 +01:00
|
|
|
user.Send(fmt.Sprintf(NOT_IN_VOLUME_RANGE_MSG, dj.conf.Volume.LowestVolume, dj.conf.Volume.HighestVolume))
|
2014-12-16 01:40:05 +01:00
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Move command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.MoveAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminMove) {
|
|
|
|
if argument == "" {
|
|
|
|
user.Send(NO_ARGUMENT_MSG)
|
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
if err := move(argument); err == nil {
|
2014-12-16 01:40:05 +01:00
|
|
|
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-16 01:40:05 +01:00
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Reload command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.ReloadAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminReload) {
|
2014-12-15 05:35:44 +01:00
|
|
|
err := loadConfiguration()
|
2014-12-13 05:53:18 +01:00
|
|
|
if err == nil {
|
2014-12-16 01:40:05 +01:00
|
|
|
user.Send(CONFIG_RELOAD_SUCCESS_MSG)
|
|
|
|
} else {
|
|
|
|
panic(err)
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
2014-12-27 19:05:13 +01:00
|
|
|
// Kill command
|
2014-12-16 01:40:05 +01:00
|
|
|
case dj.conf.Aliases.KillAlias:
|
|
|
|
if dj.HasPermission(username, dj.conf.Permissions.AdminKill) {
|
2014-12-27 09:25:49 +01:00
|
|
|
if err := kill(); err == nil {
|
|
|
|
fmt.Println("Kill successful. Goodbye!")
|
|
|
|
os.Exit(0)
|
|
|
|
} else {
|
|
|
|
user.Send(KILL_ERROR_MSG)
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
|
|
|
user.Send(NO_PERMISSION_MSG)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
user.Send(COMMAND_DOESNT_EXIST_MSG)
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Performs add functionality. Checks input URL for YouTube format, and adds
|
|
|
|
// the URL to the queue if the format matches.
|
2014-12-27 09:25:49 +01:00
|
|
|
func add(user, url string) (string, error) {
|
2014-12-16 01:40:05 +01:00
|
|
|
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 {
|
2014-12-27 09:25:49 +01:00
|
|
|
if re, err := regexp.Compile(pattern); err == nil {
|
2014-12-16 01:40:05 +01:00
|
|
|
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)
|
2014-12-27 09:25:49 +01:00
|
|
|
if err := dj.queue.AddSong(newSong); err == nil {
|
|
|
|
return newSong.title, nil
|
2014-12-16 01:40:05 +01:00
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
return "", errors.New("Could not add the Song to the queue.")
|
2014-12-16 01:40:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
return "", errors.New("The URL provided did not match a YouTube URL.")
|
2014-12-16 01:40:05 +01:00
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Performs skip functionality. Adds a skip to the skippers slice for the current song, and then
|
|
|
|
// evaluates if a skip should be performed. Both skip and forceskip are implemented here.
|
2014-12-27 09:25:49 +01:00
|
|
|
func skip(user string, admin bool) error {
|
|
|
|
if err := dj.currentSong.AddSkip(user); err == nil {
|
|
|
|
if dj.currentSong.SkipReached(len(dj.client.Self().Channel().Users())) || admin {
|
|
|
|
if err := dj.audioStream.Stop(); err == nil {
|
|
|
|
dj.OnSongFinished()
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("An error occurred while stopping the current song.")
|
|
|
|
}
|
|
|
|
} else {
|
2014-12-29 11:36:00 +01:00
|
|
|
return nil
|
2014-12-27 09:25:49 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return errors.New("An error occurred while adding a skip to the current song.")
|
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Performs volume functionality. Checks input value against LowestVolume and HighestVolume from
|
|
|
|
// config to determine if the volume should be applied. If in the correct range, the new volume
|
|
|
|
// is applied and is immediately in effect.
|
2014-12-27 09:25:49 +01:00
|
|
|
func volume(user, value string) error {
|
|
|
|
if parsedVolume, err := strconv.ParseFloat(value, 32); err == nil {
|
2014-12-19 01:28:38 +01:00
|
|
|
newVolume := float32(parsedVolume)
|
|
|
|
if newVolume >= dj.conf.Volume.LowestVolume && newVolume <= dj.conf.Volume.HighestVolume {
|
2014-12-27 18:41:27 +01:00
|
|
|
dj.audioStream.SetVolume(newVolume)
|
2014-12-27 09:25:49 +01:00
|
|
|
return nil
|
2014-12-19 01:28:38 +01:00
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
return errors.New("The volume supplied was not in the allowed range.")
|
2014-12-19 01:28:38 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-12-27 09:25:49 +01:00
|
|
|
return errors.New("An error occurred while parsing the volume string.")
|
2014-12-19 01:28:38 +01:00
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Performs move functionality. Determines if the supplied channel is valid and moves the bot
|
|
|
|
// to the channel if it is.
|
2014-12-27 09:25:49 +01:00
|
|
|
func move(channel string) error {
|
|
|
|
if dj.client.Channels().Find(channel) != nil {
|
|
|
|
dj.client.Self().Move(dj.client.Channels().Find(channel))
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("The channel provided does not exist.")
|
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 19:05:13 +01:00
|
|
|
// Performs kill functionality. First cleans the ~/.mumbledj/songs directory to get rid of any
|
|
|
|
// excess m4a files. The bot then safely disconnects from the server.
|
2014-12-27 09:25:49 +01:00
|
|
|
func kill() error {
|
|
|
|
songsDir := fmt.Sprintf("%s/.mumbledj/songs", dj.homeDir)
|
|
|
|
if err := os.RemoveAll(songsDir); err != nil {
|
|
|
|
return errors.New("An error occurred while deleting the audio files.")
|
|
|
|
} else {
|
|
|
|
if err := os.Mkdir(songsDir, 0777); err != nil {
|
|
|
|
return errors.New("An error occurred while recreating the songs directory.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := dj.client.Disconnect(); err == nil {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("An error occurred while disconnecting from the server.")
|
|
|
|
}
|
2014-12-13 05:53:18 +01:00
|
|
|
}
|