commit 0dc0542

Hubert Hirtz  ·  2021-10-23 16:50:56 +0000 UTC
parent 5ab6aa2
Don't panic when a command is sent when offline

nit: also remove naked returns
1 files changed,  +69, -22
+69, -22
  1@@ -11,6 +11,10 @@ import (
  2 	"github.com/gdamore/tcell/v2"
  3 )
  4 
  5+var (
  6+	errOffline = fmt.Errorf("you are disconnected from the server, retry later")
  7+)
  8+
  9 type command struct {
 10 	AllowHome bool
 11 	MinArgs   int
 12@@ -136,7 +140,11 @@ func noCommand(app *App, buffer, content string) error {
 13 	// You can't send messages to home buffer, and it might get
 14 	// delivered to a user "home" without a bouncer, which will be bad.
 15 	if buffer == Home {
 16-		return fmt.Errorf("Can't send message to home")
 17+		return fmt.Errorf("can't send message to home")
 18+	}
 19+
 20+	if app.s == nil {
 21+		return errOffline
 22 	}
 23 
 24 	app.s.PrivMsg(buffer, content)
 25@@ -233,19 +241,27 @@ func commandDoHelp(app *App, args []string) (err error) {
 26 			})
 27 		}
 28 	}
 29-	return
 30+	return nil
 31 }
 32 
 33 func commandDoJoin(app *App, args []string) (err error) {
 34+	if app.s == nil {
 35+		return errOffline
 36+	}
 37+
 38 	key := ""
 39 	if len(args) == 2 {
 40 		key = args[1]
 41 	}
 42 	app.s.Join(args[0], key)
 43-	return
 44+	return nil
 45 }
 46 
 47 func commandDoMe(app *App, args []string) (err error) {
 48+	if app.s == nil {
 49+		return errOffline
 50+	}
 51+
 52 	buffer := app.win.CurrentBuffer()
 53 	if buffer == Home {
 54 		buffer = app.lastQuery
 55@@ -263,10 +279,14 @@ func commandDoMe(app *App, args []string) (err error) {
 56 		})
 57 		app.win.AddLine(buffer, ui.NotifyNone, line)
 58 	}
 59-	return
 60+	return nil
 61 }
 62 
 63 func commandDoMsg(app *App, args []string) (err error) {
 64+	if app.s == nil {
 65+		return errOffline
 66+	}
 67+
 68 	target := args[0]
 69 	content := args[1]
 70 	app.s.PrivMsg(target, content)
 71@@ -281,10 +301,14 @@ func commandDoMsg(app *App, args []string) (err error) {
 72 		})
 73 		app.win.AddLine(buffer, ui.NotifyNone, line)
 74 	}
 75-	return
 76+	return nil
 77 }
 78 
 79 func commandDoNames(app *App, args []string) (err error) {
 80+	if app.s == nil {
 81+		return errOffline
 82+	}
 83+
 84 	buffer := app.win.CurrentBuffer()
 85 	var sb ui.StyledStringBuilder
 86 	sb.SetStyle(tcell.StyleDefault.Foreground(tcell.ColorGrey))
 87@@ -306,28 +330,40 @@ func commandDoNames(app *App, args []string) (err error) {
 88 		HeadColor: tcell.ColorGray,
 89 		Body:      body,
 90 	})
 91-	return
 92+	return nil
 93 }
 94 
 95 func commandDoNick(app *App, args []string) (err error) {
 96+	if app.s == nil {
 97+		return errOffline
 98+	}
 99+
100 	nick := args[0]
101 	if i := strings.IndexAny(nick, " :@!*?"); i >= 0 {
102 		return fmt.Errorf("illegal char %q in nickname", nick[i])
103 	}
104 	app.s.ChangeNick(nick)
105-	return
106+	return nil
107 }
108 
109 func commandDoMode(app *App, args []string) (err error) {
110+	if app.s == nil {
111+		return errOffline
112+	}
113+
114 	channel := args[0]
115 	flags := args[1]
116 	modeArgs := args[2:]
117 
118 	app.s.ChangeMode(channel, flags, modeArgs)
119-	return
120+	return nil
121 }
122 
123 func commandDoPart(app *App, args []string) (err error) {
124+	if app.s == nil {
125+		return errOffline
126+	}
127+
128 	channel := app.win.CurrentBuffer()
129 	reason := ""
130 	if 0 < len(args) {
131@@ -340,13 +376,11 @@ func commandDoPart(app *App, args []string) (err error) {
132 			reason = args[0]
133 		}
134 	}
135-
136-	if channel != Home {
137-		app.s.Part(channel, reason)
138-	} else {
139-		err = fmt.Errorf("cannot part home!")
140+	if channel == Home {
141+		return fmt.Errorf("cannot part home")
142 	}
143-	return
144+	app.s.Part(channel, reason)
145+	return nil
146 }
147 
148 func commandDoQuit(app *App, args []string) (err error) {
149@@ -358,15 +392,23 @@ func commandDoQuit(app *App, args []string) (err error) {
150 		app.s.Quit(reason)
151 	}
152 	app.win.Exit()
153-	return
154+	return nil
155 }
156 
157 func commandDoQuote(app *App, args []string) (err error) {
158+	if app.s == nil {
159+		return errOffline
160+	}
161+
162 	app.s.SendRaw(args[0])
163-	return
164+	return nil
165 }
166 
167 func commandDoR(app *App, args []string) (err error) {
168+	if app.s == nil {
169+		return errOffline
170+	}
171+
172 	app.s.PrivMsg(app.lastQuery, args[0])
173 	if !app.s.HasCapability("echo-message") {
174 		buffer, line, _ := app.formatMessage(irc.MessageEvent{
175@@ -379,19 +421,27 @@ func commandDoR(app *App, args []string) (err error) {
176 		})
177 		app.win.AddLine(buffer, ui.NotifyNone, line)
178 	}
179-	return
180+	return nil
181 }
182 
183 func commandDoTopic(app *App, args []string) (err error) {
184+	if app.s == nil {
185+		return errOffline
186+	}
187+
188 	if len(args) == 0 {
189 		app.printTopic(app.win.CurrentBuffer())
190 	} else {
191 		app.s.ChangeTopic(app.win.CurrentBuffer(), args[0])
192 	}
193-	return
194+	return nil
195 }
196 
197 func commandDoInvite(app *App, args []string) (err error) {
198+	if app.s == nil {
199+		return errOffline
200+	}
201+
202 	nick := args[0]
203 	channel := app.win.CurrentBuffer()
204 	if len(args) == 2 {
205@@ -462,10 +512,7 @@ func parseCommand(s string) (command, args string, isCommand bool) {
206 		i = len(s)
207 	}
208 
209-	isCommand = true
210-	command = strings.ToUpper(s[1:i])
211-	args = strings.TrimLeft(s[i:], " ")
212-	return
213+	return strings.ToUpper(s[1:i]), strings.TrimLeft(s[i:], " "), true
214 }
215 
216 func (app *App) handleInput(buffer, content string) error {