Authentication-Results: mail-b.sr.ht; dkim=pass header.d=disroot.org header.i=@disroot.org Received: from knopi.disroot.org (knopi.disroot.org [178.21.23.139]) by mail-b.sr.ht (Postfix) with ESMTPS id D87DA11EEF9 for <~taiite/public-inbox@lists.sr.ht>; Wed, 1 Dec 2021 17:57:11 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by disroot.org (Postfix) with ESMTP id 74F1F7C09C; Wed, 1 Dec 2021 18:57:10 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at disroot.org Received: from knopi.disroot.org ([127.0.0.1]) by localhost (disroot.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Q4FwRrsAvgC1; Wed, 1 Dec 2021 18:57:09 +0100 (CET) From: Alexey Yerin DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=disroot.org; s=mail; t=1638381429; bh=ecMKpQJO5dtdhg375FsIijSEj1j4aAdKYDgvS8eHAIQ=; h=From:To:Cc:Subject:Date; b=CHwQgcrGJ+BrtW7EyjXsem0W2GZwdSHFvI7Uhg3AvJddYet2XqgcSZ0bBIfN1W3wK BnflxV/ey4Y9xZUbhVXzieWUc36g1UjZJu390i9GcLSHG+Gfcr+BogAtyWD8btLFhS 7SPtKVN51odxBSkTsGg83glBnqxqpikxFdC3dKiM0gzIq5m35+UvOX5TDB1dDQi322 xWiCsw4NGazQ+7JFcJV06+o5rk1fesC1XJw1aaOTYmogke6wwKZ6OVc5f1ay3dMrGA nQKpWPGfnzkSGA11UcvDYMl8M+x513YiIDB8lB62HSz8onMywMyWdhgevTeaPz5KtP mXqVEnJyRSXaQ== To: ~taiite/public-inbox@lists.sr.ht Cc: Alexey Yerin Subject: [PATCH senpai] Implement /kick and /[un]ban commands Date: Wed, 1 Dec 2021 20:57:01 +0300 Message-Id: <20211201175701.541-1-yyp@disroot.org> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit --- commands.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ irc/session.go | 8 ++++++ 2 files changed, 84 insertions(+) diff --git a/commands.go b/commands.go index 5dff158..e8e6807 100644 --- a/commands.go +++ b/commands.go @@ -142,6 +142,30 @@ func init() { Desc: "invite someone to a channel", Handle: commandDoInvite, }, + "KICK": { + AllowHome: false, + MinArgs: 1, + MaxArgs: 2, + Usage: " [channel]", + Desc: "remove someone from the channel", + Handle: commandDoKick, + }, + "BAN": { + AllowHome: false, + MinArgs: 1, + MaxArgs: 2, + Usage: " [channel]", + Desc: "ban someone from entering the channel", + Handle: commandDoBan, + }, + "UNBAN": { + AllowHome: false, + MinArgs: 1, + MaxArgs: 2, + Usage: " [channel]", + Desc: "remove effect of a ban from the user", + Handle: commandDoUnban, + }, } } @@ -506,6 +530,58 @@ func commandDoInvite(app *App, args []string) (err error) { return nil } +func commandDoKick(app *App, args []string) (err error) { + nick := args[0] + netID, channel := app.win.CurrentBuffer() + s := app.sessions[netID] + if s == nil { + return errOffline + } + if len(args) >= 2 { + channel = args[1] + } else if channel == "" { + return fmt.Errorf("either send this command from a channel, or specify the channel") + } + comment := "" + if len(args) == 3 { + comment = args[2] + } + s.Kick(nick, channel, comment) + return nil +} + +func commandDoBan(app *App, args []string) (err error) { + nick := args[0] + netID, channel := app.win.CurrentBuffer() + s := app.sessions[netID] + if s == nil { + return errOffline + } + if len(args) == 2 { + channel = args[1] + } else if channel == "" { + return fmt.Errorf("either send this command from a channel, or specify the channel") + } + s.ChangeMode(channel, "+b", []string{nick}) + return nil +} + +func commandDoUnban(app *App, args []string) (err error) { + nick := args[0] + netID, channel := app.win.CurrentBuffer() + s := app.sessions[netID] + if s == nil { + return errOffline + } + if len(args) == 2 { + channel = args[1] + } else if channel == "" { + return fmt.Errorf("either send this command from a channel, or specify the channel") + } + s.ChangeMode(channel, "-b", []string{nick}) + return nil +} + // implemented from https://golang.org/src/strings/strings.go?s=8055:8085#L310 func fieldsN(s string, n int) []string { s = strings.TrimSpace(s) diff --git a/irc/session.go b/irc/session.go index b6d5873..4bbd0c7 100644 --- a/irc/session.go +++ b/irc/session.go @@ -494,6 +494,14 @@ func (s *Session) Invite(nick, channel string) { s.out <- NewMessage("INVITE", nick, channel) } +func (s *Session) Kick(nick, channel, comment string) { + if comment == "" { + s.out <- NewMessage("KICK", channel, nick) + } else { + s.out <- NewMessage("KICK", channel, nick, comment) + } +} + func (s *Session) HandleMessage(msg Message) (Event, error) { if s.registered { return s.handleRegistered(msg) -- 2.34.1