Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More protocol implementations #2

Merged
merged 1 commit into from
Nov 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions eventHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type handlers struct {
pmHandler func(PrivateMessage, *Session)
broadcastHandler func(Broadcast, *Session)
pingHandler func(Ping, *Session)
subOnlyHandler func(SubOnly, *Session)
}

// AddMessageHandler adds a function that will be called every time a message is received
Expand Down Expand Up @@ -68,3 +69,8 @@ func (s *Session) AddBroadcastHandler(fn func(Broadcast, *Session)) {
func (s *Session) AddPingHandler(fn func(Ping, *Session)) {
s.handlers.pingHandler = fn
}

// AddSubOnlyHandler adds a function that will will be called every time a subonly message is received
func (s *Session) AddSubOnlyHandler(fn func(SubOnly, *Session)) {
s.handlers.subOnlyHandler = fn
}
16 changes: 16 additions & 0 deletions messageStructs.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ type (
Ping struct {
Timestamp int64 `json:"timestamp"`
}

// SubOnly represents a subonly message from the server
// if active, only subscribers and some other special user classes are allowed to send messages,
// until another SubOnly message is received with active set to false
SubOnly struct {
Sender User
Timestamp time.Time
Active bool
}

subOnly struct {
Data string `json:"data"`
Timestamp int64 `json:"timestamp"`
Nick string `json:"nick"`
Features []string `json:"features"`
}
)

// HasFeature returns true if user has given feature
Expand Down
28 changes: 27 additions & 1 deletion parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func parseErrorMessage(s string) string {
return strings.Replace(s, `"`, "", -1)
}

func parsePrivateMessage(s string) (PrivateMessage, error) {
func parsePrivateMessage(s string, sess *Session) (PrivateMessage, error) {
var pm privateMessage

err := json.Unmarshal([]byte(s), &pm)
Expand All @@ -125,6 +125,11 @@ func parsePrivateMessage(s string) (PrivateMessage, error) {
Timestamp: unixToTime(pm.Timestamp),
}

u, found := sess.GetUser(privateMessage.User.Nick)
if found {
privateMessage.User = u
}

return privateMessage, nil
}

Expand All @@ -144,6 +149,27 @@ func parseBroadcast(s string) (Broadcast, error) {
return broadcast, nil
}

func parseSubOnly(s string) (SubOnly, error) {
var so subOnly

err := json.Unmarshal([]byte(s), &so)
if err != nil {
return SubOnly{}, err
}

subonly := SubOnly{
Sender: User{
Nick: so.Nick,
Features: so.Features,
},
Timestamp: unixToTime(so.Timestamp),
// the backend specifies values "on" and "off" ONLY.
Active: so.Data == "on",
}

return subonly, nil
}

func parsePing(s string) (Ping, error) {
var p Ping

Expand Down
87 changes: 45 additions & 42 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ type privateMessageOut struct {
Data string `json:"data"`
}

type banOut struct {
Nick string `json:"nick"`
Reason string `json:"reason,omitempty"`
Duration time.Duration `json:"duration,omitempty"`
Banip bool `json:"banip,omitempty"`
type muteOut struct {
Data string `json:"data"`
Duration int64 `data:"duration,omitempty"`
}

type subOnly struct {
SubOnly bool `json:"subonly"`
type banOut struct {
Nick string `json:"nick"`
Reason string `json:"reason,omitempty"`
Duration int64 `json:"duration,omitempty"`
Banip bool `json:"banip,omitempty"`
}

type pingOut struct {
Expand Down Expand Up @@ -139,7 +140,7 @@ func (s *Session) listen(ws *websocket.Conn, listening <-chan bool) {
}

mType := mslice[0]
mContent := strings.Join(mslice[1:], " ")
mContent := mslice[1]

switch mType {

Expand Down Expand Up @@ -179,65 +180,52 @@ func (s *Session) listen(ws *websocket.Conn, listening <-chan bool) {
s.handlers.unbanHandler(ban, s)

case "SUBONLY":
//TODO
case "BROADCAST":
if s.handlers.broadcastHandler == nil {
so, err := parseSubOnly(mContent)
if s.handlers.subOnlyHandler == nil || err != nil {
continue
}
s.handlers.subOnlyHandler(so, s)

case "BROADCAST":
b, err := parseBroadcast(mContent)
if err != nil {
if s.handlers.broadcastHandler == nil || err != nil {
continue
}

s.handlers.broadcastHandler(b, s)

case "PRIVMSG":
if s.handlers.pmHandler == nil {
pm, err := parsePrivateMessage(mContent, s)
if s.handlers.pmHandler == nil || err != nil {
continue
}

pm, err := parsePrivateMessage(mContent)
if err != nil {
continue
}

u, found := s.GetUser(pm.User.Nick)
if found {
pm.User = u
}

s.handlers.pmHandler(pm, s)

case "PRIVMSGSENT":
//TODO confirms the successful sending of a PM(?)
//TODO confirms sending of a PM was successful

case "PING":
case "PONG":
if s.handlers.pingHandler == nil {
continue
}

p, err := parsePing(mContent)
if err != nil {
if s.handlers.pingHandler == nil || err != nil {
continue
}

s.handlers.pingHandler(p, s)

case "ERR":
if s.handlers.errHandler == nil {
continue
}

errMessage := parseErrorMessage(mContent)
s.handlers.errHandler(errMessage, s)

case "NAMES":
n, err := parseNames(mContent)
if err != nil {
continue
}

s.state.users = n.Users
s.state.connections = n.Connections

case "JOIN":
ra, err := parseRoomAction(mContent)
if err != nil {
Expand All @@ -249,6 +237,7 @@ func (s *Session) listen(ws *websocket.Conn, listening <-chan bool) {
if s.handlers.joinHandler != nil {
s.handlers.joinHandler(ra, s)
}

case "QUIT":
ra, err := parseRoomAction(mContent)
if err != nil {
Expand All @@ -260,8 +249,10 @@ func (s *Session) listen(ws *websocket.Conn, listening <-chan bool) {
if s.handlers.quitHandler != nil {
s.handlers.quitHandler(ra, s)
}

case "REFRESH":
//TODO voluntary reconnect when server determines state should be refreshed
// TODO voluntary reconnect when server determines state should be refreshed
// happens e.g. when user-flair is modified in the database
}

select {
Expand Down Expand Up @@ -329,8 +320,12 @@ func (s *Session) SendMessage(message string) error {
}

// SendMute mutes the user with the given nick.
func (s *Session) SendMute(nick string) error {
m := messageOut{Data: nick}
// If duration is <= 0, the server uses its built-in default duration
func (s *Session) SendMute(nick string, duration time.Duration) error {
m := muteOut{Data: nick}
if duration > 0 {
m.Duration = int64(duration)
}
return s.send(m, "MUTE")
}

Expand All @@ -341,12 +336,16 @@ func (s *Session) SendUnmute(nick string) error {
}

// SendBan bans the user with the given nick.
// Bans require a ban reason to be specified. TODO: ban duration is optional
// Bans require a ban reason to be specified.
// If duration is <= 0, the server uses its built-in default duration
func (s *Session) SendBan(nick string, reason string, duration time.Duration, banip bool) error {
b := banOut{
Nick: nick,
Reason: reason,
Duration: duration,
Nick: nick,
Reason: reason,
Banip: banip,
}
if duration > 0 {
b.Duration = int64(duration)
}
return s.send(b, "BAN")
}
Expand Down Expand Up @@ -377,7 +376,11 @@ func (s *Session) SendPrivateMessage(nick string, message string) error {
// SendSubOnly modifies the chat subonly mode.
// During subonly mode, only subscribers and some other special user classes are allowed to send messages.
func (s *Session) SendSubOnly(subonly bool) error {
so := subOnly{SubOnly: subonly}
data := "off"
if subonly {
data = "on"
}
so := messageOut{Data: data}
return s.send(so, "SUBONLY")
}

Expand Down