summaryrefslogtreecommitdiffstats
path: root/bridge/mumble/handlers.go
blob: 830aa5c0e079681007e956ce31f77e1c7a548df1 (plain) (blame)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package bmumble

import (
	"strconv"
	"time"

	"layeh.com/gumble/gumble"

	"github.com/42wim/matterbridge/bridge/config"
	"github.com/42wim/matterbridge/bridge/helper"
)

func (b *Bmumble) handleServerConfig(event *gumble.ServerConfigEvent) {
	b.serverConfigUpdate <- *event
}

func (b *Bmumble) handleTextMessage(event *gumble.TextMessageEvent) {
	sender := "unknown"
	if event.TextMessage.Sender != nil {
		sender = event.TextMessage.Sender.Name
	}
	// If the text message is received before receiving a ServerSync
	// and UserState, Client.Self or Self.Channel are nil
	if event.Client.Self == nil || event.Client.Self.Channel == nil {
		b.Log.Warn("Connection bootstrap not finished, discarding text message")
		return
	}
	// Convert Mumble HTML messages to markdown
	parts, err := b.convertHTMLtoMarkdown(event.TextMessage.Message)
	if err != nil {
		b.Log.Error(err)
	}
	now := time.Now().UTC()
	for i, part := range parts {
		// Construct matterbridge message and pass on to the gateway
		rmsg := config.Message{
			Channel:  strconv.FormatUint(uint64(event.Client.Self.Channel.ID), 10),
			Username: sender,
			UserID:   sender + "@" + b.Host,
			Account:  b.Account,
		}
		if part.Image == nil {
			rmsg.Text = part.Text
		} else {
			fileExt := part.FileExtension
			if fileExt == ".jfif" {
				fileExt = ".jpg"
			}
			if fileExt == ".jpe" {
				fileExt = ".jpg"
			}
			fname := b.Account + "_" + strconv.FormatInt(now.UnixNano(), 10) + "_" + strconv.Itoa(i) + fileExt
			rmsg.Extra = make(map[string][]interface{})
			if err = helper.HandleDownloadSize(b.Log, &rmsg, fname, int64(len(part.Image)), b.General); err != nil {
				b.Log.WithError(err).Warn("not including image in message")
				continue
			}
			helper.HandleDownloadData(b.Log, &rmsg, fname, "", "", &part.Image, b.General)
		}
		b.Log.Debugf("Sending message to gateway: %+v", rmsg)
		b.Remote <- rmsg
	}
}

func (b *Bmumble) handleConnect(event *gumble.ConnectEvent) {
	// Set the user's "bio"/comment
	if comment := b.GetString("UserComment"); comment != "" && event.Client.Self != nil {
		event.Client.Self.SetComment(comment)
	}
	// No need to talk or listen
	event.Client.Self.SetSelfDeafened(true)
	// if the Channel variable is set, this is a reconnect -> rejoin channel
	if b.Channel != nil {
		if err := b.doJoin(event.Client, *b.Channel); err != nil {
			b.Log.Error(err)
		}
		b.Remote <- config.Message{
			Username: "system",
			Text:     "rejoin",
			Channel:  "",
			Account:  b.Account,
			Event:    config.EventRejoinChannels,
		}
	}
}

func (b *Bmumble) handleJoinLeave(event *gumble.UserChangeEvent) {
	// Ignore events happening before setup is done
	if b.Channel == nil {
		return
	}
	if b.GetBool("nosendjoinpart") {
		return
	}
	b.Log.Debugf("Received gumble user change event: %+v", event)

	text := ""
	switch {
	case event.Type&gumble.UserChangeKicked > 0:
		text = " was kicked"
	case event.Type&gumble.UserChangeBanned > 0:
		text = " was banned"
	case event.Type&gumble.UserChangeDisconnected > 0:
		if event.User.Channel != nil && event.User.Channel.ID == *b.Channel {
			text = " left"
		}
	case event.Type&gumble.UserChangeConnected > 0:
		if event.User.Channel != nil && event.User.Channel.ID == *b.Channel {
			text = " joined"
		}
	case event.Type&gumble.UserChangeChannel > 0:
		// Treat Mumble channel changes the same as connects/disconnects; as far as matterbridge is concerned, they are identical
		if event.User.Channel != nil && event.User.Channel.ID == *b.Channel {
			text = " joined"
		} else {
			text = " left"
		}
	}

	if text != "" {
		b.Remote <- config.Message{
			Username: "system",
			Text:     event.User.Name + text,
			Channel:  strconv.FormatUint(uint64(*b.Channel), 10),
			Account:  b.Account,
			Event:    config.EventJoinLeave,
		}
	}
}

func (b *Bmumble) handleUserModified(event *gumble.UserChangeEvent) {
	// Ignore events happening before setup is done
	if b.Channel == nil {
		return
	}

	if event.Type&gumble.UserChangeChannel > 0 {
		// Someone attempted to move the user out of the configured channel; attempt to join back
		if err := b.doJoin(event.Client, *b.Channel); err != nil {
			b.Log.Error(err)
		}
	}
}

func (b *Bmumble) handleUserChange(event *gumble.UserChangeEvent) {
	// The UserChangeEvent is used for both the gumble client itself as well as other clients
	if event.User != event.Client.Self {
		// other users
		b.handleJoinLeave(event)
	} else {
		// gumble user
		b.handleUserModified(event)
	}
}

func (b *Bmumble) handleDisconnect(event *gumble.DisconnectEvent) {
	b.connected <- *event
}