commit 65718d3

Alexey Yerin  ·  2021-12-06 20:09:08 +0000 UTC
parent 75155fa
Implement /kick and /[un]ban commands
3 files changed,  +93, -0
+76, -0
 1@@ -142,6 +142,30 @@ func init() {
 2 			Desc:      "invite someone to a channel",
 3 			Handle:    commandDoInvite,
 4 		},
 5+		"KICK": {
 6+			AllowHome: false,
 7+			MinArgs:   1,
 8+			MaxArgs:   2,
 9+			Usage:     "<nick> [channel]",
10+			Desc:      "eject someone from the channel",
11+			Handle:    commandDoKick,
12+		},
13+		"BAN": {
14+			AllowHome: false,
15+			MinArgs:   1,
16+			MaxArgs:   2,
17+			Usage:     "<nick> [channel]",
18+			Desc:      "ban someone from entering the channel",
19+			Handle:    commandDoBan,
20+		},
21+		"UNBAN": {
22+			AllowHome: false,
23+			MinArgs:   1,
24+			MaxArgs:   2,
25+			Usage:     "<nick> [channel]",
26+			Desc:      "remove effect of a ban from the user",
27+			Handle:    commandDoUnban,
28+		},
29 	}
30 }
31 
32@@ -506,6 +530,58 @@ func commandDoInvite(app *App, args []string) (err error) {
33 	return nil
34 }
35 
36+func commandDoKick(app *App, args []string) (err error) {
37+	nick := args[0]
38+	netID, channel := app.win.CurrentBuffer()
39+	s := app.sessions[netID]
40+	if s == nil {
41+		return errOffline
42+	}
43+	if len(args) >= 2 {
44+		channel = args[1]
45+	} else if channel == "" {
46+		return fmt.Errorf("either send this command from a channel, or specify the channel")
47+	}
48+	comment := ""
49+	if len(args) == 3 {
50+		comment = args[2]
51+	}
52+	s.Kick(nick, channel, comment)
53+	return nil
54+}
55+
56+func commandDoBan(app *App, args []string) (err error) {
57+	nick := args[0]
58+	netID, channel := app.win.CurrentBuffer()
59+	s := app.sessions[netID]
60+	if s == nil {
61+		return errOffline
62+	}
63+	if len(args) == 2 {
64+		channel = args[1]
65+	} else if channel == "" {
66+		return fmt.Errorf("either send this command from a channel, or specify the channel")
67+	}
68+	s.ChangeMode(channel, "+b", []string{nick})
69+	return nil
70+}
71+
72+func commandDoUnban(app *App, args []string) (err error) {
73+	nick := args[0]
74+	netID, channel := app.win.CurrentBuffer()
75+	s := app.sessions[netID]
76+	if s == nil {
77+		return errOffline
78+	}
79+	if len(args) == 2 {
80+		channel = args[1]
81+	} else if channel == "" {
82+		return fmt.Errorf("either send this command from a channel, or specify the channel")
83+	}
84+	s.ChangeMode(channel, "-b", []string{nick})
85+	return nil
86+}
87+
88 // implemented from https://golang.org/src/strings/strings.go?s=8055:8085#L310
89 func fieldsN(s string, n int) []string {
90 	s = strings.TrimSpace(s)
+9, -0
 1@@ -165,6 +165,15 @@ _name_ is matched case-insensitively.  It can be one of the following:
 2 *INVITE* <nick> [channel]
 3 	Invite _nick_ to _channel_ (the current channel if not given).
 4 
 5+*KICK* <nick> [channel]
 6+	Eject _nick_ from _channel_ (the current channel if not given).
 7+
 8+*BAN* <nick> [channel]
 9+	Ban _nick_ from entering _channel_ (the current channel if not given).
10+
11+*UNBAN* <nick> [channel]
12+	Allow _nick_ to enter _channel_ again (the current channel if not given).
13+
14 # SEE ALSO
15 
16 *senpai*(5)
+8, -0
 1@@ -494,6 +494,14 @@ func (s *Session) Invite(nick, channel string) {
 2 	s.out <- NewMessage("INVITE", nick, channel)
 3 }
 4 
 5+func (s *Session) Kick(nick, channel, comment string) {
 6+	if comment == "" {
 7+		s.out <- NewMessage("KICK", channel, nick)
 8+	} else {
 9+		s.out <- NewMessage("KICK", channel, nick, comment)
10+	}
11+}
12+
13 func (s *Session) HandleMessage(msg Message) (Event, error) {
14 	if s.registered {
15 		return s.handleRegistered(msg)