~taiite/senpai

7a9fe74fdfb3f334b97434df0aa74b2b32e3582e — delthas 15 days ago d525e50 master
/mode: enable getting the channel modes without parameters
2 files changed, 23 insertions(+), 21 deletions(-)

M commands.go
M irc/session.go
M commands.go => commands.go +18 -20
@@ 17,6 17,8 @@ var (
	errOffline = fmt.Errorf("you are disconnected from the server, retry later")
)

const maxArgsInfinite = -1

type command struct {
	AllowHome bool
	MinArgs   int


@@ 77,9 79,8 @@ func init() {
		},
		"MODE": {
			AllowHome: true,
			MinArgs:   1,
			MaxArgs:   5, // <channel> <flags> <limit> <user> <ban mask>
			Usage:     "[<nick/channel>] <flags> [args]",
			MaxArgs:   maxArgsInfinite,
			Usage:     "[<nick/channel>] [<flags>] [args]",
			Desc:      "change channel or user modes",
			Handle:    commandDoMode,
		},


@@ 382,28 383,23 @@ func commandDoNick(app *App, args []string) (err error) {
}

func commandDoMode(app *App, args []string) (err error) {
	hasModePrefix := strings.HasPrefix(args[0], "+") || strings.HasPrefix(args[0], "-")
	hasChanPrefix := strings.HasPrefix(args[0], "#")

	if !hasModePrefix && !hasChanPrefix {
		return errors.New("invalid argument")
	_, target := app.win.CurrentBuffer()
	if len(args) > 0 && !strings.HasPrefix(args[0], "+") && !strings.HasPrefix(args[0], "-") {
		target = args[0]
		args = args[1:]
	}

	if hasModePrefix {
		// if we do eg /MODE +P, automatically insert the current channel: /MODE #<current-chan> +P
		_, channel := app.win.CurrentBuffer()
		args = append([]string{channel}, args...)
	flags := ""
	if len(args) > 0 {
		flags = args[0]
		args = args[1:]
	}

	channel := args[0]
	flags := args[1]
	modeArgs := args[2:]
	modeArgs := args

	s := app.CurrentSession()
	if s == nil {
		return errOffline
	}
	s.ChangeMode(channel, flags, modeArgs)
	s.ChangeMode(target, flags, modeArgs)
	return nil
}



@@ 613,7 609,9 @@ func fieldsN(s string, n int) []string {
	if n == 1 {
		return []string{s}
	}
	n--
	if n == maxArgsInfinite {
		n--
	}
	// Start of the ASCII fast path.
	var a []string
	na := 0


@@ 637,7 635,7 @@ func fieldsN(s string, n int) []string {
			i++
		}
		fieldStart = i
		if n <= na {
		if n != maxArgsInfinite && n <= na {
			a = append(a, s[fieldStart:])
			return a
		}

M irc/session.go => irc/session.go +5 -1
@@ 359,7 359,11 @@ func (s *Session) ChangeNick(nick string) {
}

func (s *Session) ChangeMode(channel, flags string, args []string) {
	args = append([]string{channel, flags}, args...)
	if flags != "" {
		args = append([]string{channel, flags}, args...)
	} else {
		args = append([]string{channel}, args...)
	}
	s.out <- NewMessage("MODE", args...)
}