-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (93 loc) · 2.63 KB
/
main.go
File metadata and controls
108 lines (93 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"log"
"os"
"os/signal"
"syscall"
"github.com/iotku/mumzic/commands"
"github.com/iotku/mumzic/database"
"github.com/iotku/mumzic/helper"
"github.com/iotku/mumzic/playback"
_ "github.com/mattn/go-sqlite3"
"layeh.com/gumble/gumble"
"layeh.com/gumble/gumbleutil"
)
func main() {
var channelPlayer *playback.Player
var bConfig *database.Config
var hostname, username string
cleanUp := func() {
if bConfig != nil {
bConfig.Channel = channelPlayer.Client.Self.Channel.Name
bConfig.Save()
}
if channelPlayer != nil {
channelPlayer.Playlist.Save(bConfig.Hostname)
}
database.Close(database.ConfigDB)
if database.MediaDB != nil {
database.Close(database.MediaDB)
}
}
// Capture shutdown signal (ctrl+c)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
log.Println("Received shutdown signal")
cleanUp()
os.Exit(0)
}()
gumbleutil.Main(gumbleutil.AutoBitrate, gumbleutil.Listener{
Connect: func(e *gumble.ConnectEvent) {
hostname = getValueFromFlag(flag.Lookup("server"))
username = getValueFromFlag(flag.Lookup("username"))
bConfig = database.NewConfig(hostname)
if e.Client.Channels.Find(bConfig.Channel) != nil {
e.Client.Self.Move(e.Client.Channels.Find(bConfig.Channel))
}
channelPlayer = playback.NewPlayer(e.Client, bConfig)
channelPlayer.Playlist.Load(bConfig.Hostname)
log.Printf("audio player loaded! (%d files)\n", database.GetMaxID())
},
TextMessage: func(e *gumble.TextMessageEvent) {
if e.Sender == nil {
return
}
isPrivate := len(e.TextMessage.Channels) == 0 // If no channels, is private message
logMessage(e, isPrivate)
strippedMessage := helper.StripHTMLTags(e.Message)
if commands.IsCommand(strippedMessage, isPrivate, username, bConfig) {
go func() {
commands.CommandDispatch(channelPlayer, strippedMessage, isPrivate, e.Sender.Name)
}()
}
},
ChannelChange: func(e *gumble.ChannelChangeEvent) {
if bConfig != nil && !e.Channel.IsRoot() {
bConfig.Channel = e.Channel.Name
}
if channelPlayer != nil {
channelPlayer.TargetUsers()
}
},
Disconnect: func(e *gumble.DisconnectEvent) {
log.Println("Disconnecting: ", e.Type)
cleanUp()
},
})
}
func logMessage(e *gumble.TextMessageEvent, isPrivate bool) {
if isPrivate {
log.Printf("DMSG (%s): %s", e.Sender.Name, e.Message)
} else {
log.Printf("CMSG (%s) %s: %s", e.Sender.Channel.Name, e.Sender.Name, e.Message)
}
}
func getValueFromFlag(lookup *flag.Flag) string {
if lookup == nil {
panic("getValueFromFlag: flagNotFound: nil")
}
return lookup.Value.String()
}