add cmdline argument to start in a specific channel
[barnard.git] / client.go
1 package main
2
3 import (
4         "fmt"
5         "net"
6         "os"
7
8         "layeh.com/gumble/gumble"
9         "layeh.com/gumble/gumbleopenal"
10         "layeh.com/gumble/gumbleutil"
11 )
12
13 func (b *Barnard) start() {
14         b.Config.Attach(gumbleutil.AutoBitrate)
15         b.Config.Attach(b)
16
17         var err error
18         _, err = gumble.DialWithDialer(new(net.Dialer), b.Address, b.Config, &b.TLSConfig)
19         if err != nil {
20                 fmt.Fprintf(os.Stderr, "%s\n", err)
21                 os.Exit(1)
22         }
23
24         // Audio
25         if os.Getenv("ALSOFT_LOGLEVEL") == "" {
26                 os.Setenv("ALSOFT_LOGLEVEL", "0")
27         }
28         if stream, err := gumbleopenal.New(b.Client); err != nil {
29                 fmt.Fprintf(os.Stderr, "%s\n", err)
30                 os.Exit(1)
31         } else {
32                 b.Stream = stream
33         }
34 }
35
36 func (b *Barnard) OnConnect(e *gumble.ConnectEvent) {
37         b.Client = e.Client
38
39         b.Ui.SetActive(uiViewInput)
40         b.UiTree.Rebuild()
41         b.Ui.Refresh()
42
43         b.UpdateInputStatus(fmt.Sprintf("To: %s", e.Client.Self.Channel.Name))
44         b.AddOutputLine(fmt.Sprintf("Connected to %s", b.Client.Conn.RemoteAddr()))
45         if e.WelcomeMessage != nil {
46                 b.AddOutputLine(fmt.Sprintf("Welcome message: %s", esc(*e.WelcomeMessage)))
47         }
48         if (b.DefaultChannel != "") {
49                 b.Client.Self.Move(b.Client.Channels.Find(b.DefaultChannel))
50         }
51
52 }
53
54 func (b *Barnard) OnDisconnect(e *gumble.DisconnectEvent) {
55         var reason string
56         switch e.Type {
57         case gumble.DisconnectError:
58                 reason = "connection error"
59         }
60         if reason == "" {
61                 b.AddOutputLine("Disconnected")
62         } else {
63                 b.AddOutputLine("Disconnected: " + reason)
64         }
65         b.UiTree.Rebuild()
66         b.Ui.Refresh()
67 }
68
69 func (b *Barnard) OnTextMessage(e *gumble.TextMessageEvent) {
70         b.AddOutputMessage(e.Sender, e.Message)
71 }
72
73 func (b *Barnard) OnUserChange(e *gumble.UserChangeEvent) {
74         if e.Type.Has(gumble.UserChangeChannel) && e.User == b.Client.Self {
75                 b.UpdateInputStatus(fmt.Sprintf("To: %s", e.User.Channel.Name))
76         }
77         b.UiTree.Rebuild()
78         b.Ui.Refresh()
79 }
80
81 func (b *Barnard) OnChannelChange(e *gumble.ChannelChangeEvent) {
82         b.UiTree.Rebuild()
83         b.Ui.Refresh()
84 }
85
86 func (b *Barnard) OnPermissionDenied(e *gumble.PermissionDeniedEvent) {
87         var info string
88         switch e.Type {
89         case gumble.PermissionDeniedOther:
90                 info = e.String
91         case gumble.PermissionDeniedPermission:
92                 info = "insufficient permissions"
93         case gumble.PermissionDeniedSuperUser:
94                 info = "cannot modify SuperUser"
95         case gumble.PermissionDeniedInvalidChannelName:
96                 info = "invalid channel name"
97         case gumble.PermissionDeniedTextTooLong:
98                 info = "text too long"
99         case gumble.PermissionDeniedTemporaryChannel:
100                 info = "temporary channel"
101         case gumble.PermissionDeniedMissingCertificate:
102                 info = "missing certificate"
103         case gumble.PermissionDeniedInvalidUserName:
104                 info = "invalid user name"
105         case gumble.PermissionDeniedChannelFull:
106                 info = "channel full"
107         case gumble.PermissionDeniedNestingLimit:
108                 info = "nesting limit"
109         }
110         b.AddOutputLine(fmt.Sprintf("Permission denied: %s", info))
111 }
112
113 func (b *Barnard) OnUserList(e *gumble.UserListEvent) {
114 }
115
116 func (b *Barnard) OnACL(e *gumble.ACLEvent) {
117 }
118
119 func (b *Barnard) OnBanList(e *gumble.BanListEvent) {
120 }
121
122 func (b *Barnard) OnContextActionChange(e *gumble.ContextActionChangeEvent) {
123 }
124
125 func (b *Barnard) OnServerConfig(e *gumble.ServerConfigEvent) {
126 }