Просмотр исходного кода

help: Generate index of help topics

tags/v0.8.0
Daniel Oaks 7 лет назад
Родитель
Сommit
51425b7764
4 измененных файлов: 109 добавлений и 12 удалений
  1. 4
    0
      irc/commands.go
  2. 101
    11
      irc/help.go
  3. 1
    1
      irc/nickserv.go
  4. 3
    0
      irc/server.go

+ 4
- 0
irc/commands.go Просмотреть файл

@@ -95,6 +95,10 @@ var Commands = map[string]Command{
95 95
 		handler:   helpHandler,
96 96
 		minParams: 0,
97 97
 	},
98
+	"HELPOP": {
99
+		handler:   helpHandler,
100
+		minParams: 0,
101
+	},
98 102
 	"INVITE": {
99 103
 		handler:   inviteHandler,
100 104
 		minParams: 2,

+ 101
- 11
irc/help.go Просмотреть файл

@@ -4,15 +4,28 @@
4 4
 package irc
5 5
 
6 6
 import (
7
+	"fmt"
8
+	"sort"
7 9
 	"strings"
8 10
 
9 11
 	"github.com/DanielOaks/girc-go/ircmsg"
10 12
 )
11 13
 
14
+// HelpEntryType represents the different sorts of help entries that can exist.
15
+type HelpEntryType int
16
+
17
+const (
18
+	CommandHelpEntry     HelpEntryType = 0
19
+	InformationHelpEntry HelpEntryType = 1
20
+	ISupportHelpEntry    HelpEntryType = 2
21
+)
22
+
12 23
 // HelpEntry represents an entry in the Help map.
13 24
 type HelpEntry struct {
14
-	oper bool
15
-	text string
25
+	oper      bool
26
+	text      string
27
+	helpType  HelpEntryType
28
+	duplicate bool
16 29
 }
17 30
 
18 31
 // used for duplicates
@@ -128,7 +141,12 @@ ON <server> specifies that the ban is to be set on that specific server.
128 141
 	"help": {
129 142
 		text: `HELP <argument>
130 143
 
131
-Get an explanation of <argument>.`,
144
+Get an explanation of <argument>, or "index" for a list of help topics.`,
145
+	},
146
+	"helpop": {
147
+		text: `HELPOP <argument>
148
+
149
+Get an explanation of <argument>, or "index" for a list of help topics.`,
132 150
 	},
133 151
 	"invite": {
134 152
 		text: `INVITE <nickname> <channel>
@@ -411,19 +429,26 @@ Returns historical information on the last user with the given nickname.`,
411 429
 
412 430
 	// Informational
413 431
 	"modes": {
414
-		text: cmodeHelpText + "\n\n" + umodeHelpText,
432
+		text:     cmodeHelpText + "\n\n" + umodeHelpText,
433
+		helpType: InformationHelpEntry,
415 434
 	},
416 435
 	"cmode": {
417
-		text: cmodeHelpText,
436
+		text:     cmodeHelpText,
437
+		helpType: InformationHelpEntry,
418 438
 	},
419 439
 	"cmodes": {
420
-		text: cmodeHelpText,
440
+		text:      cmodeHelpText,
441
+		helpType:  InformationHelpEntry,
442
+		duplicate: true,
421 443
 	},
422 444
 	"umode": {
423
-		text: umodeHelpText,
445
+		text:     umodeHelpText,
446
+		helpType: InformationHelpEntry,
424 447
 	},
425 448
 	"umodes": {
426
-		text: umodeHelpText,
449
+		text:      umodeHelpText,
450
+		helpType:  InformationHelpEntry,
451
+		duplicate: true,
427 452
 	},
428 453
 
429 454
 	// RPL_ISUPPORT
@@ -433,6 +458,7 @@ Returns historical information on the last user with the given nickname.`,
433 458
 Oragono supports an experimental unicode casemapping designed for extended
434 459
 Unicode support. This casemapping is based off RFC 7613 and the draft rfc7613
435 460
 casemapping spec here: http://oragono.io/specs.html`,
461
+		helpType: ISupportHelpEntry,
436 462
 	},
437 463
 	"prefix": {
438 464
 		text: `RPL_ISUPPORT PREFIX
@@ -444,9 +470,63 @@ Oragono supports the following channel membership prefixes:
444 470
   +o (@)  |  Operator channel mode.
445 471
   +h (%)  |  Halfop channel mode.
446 472
   +v (+)  |  Voice channel mode.`,
473
+		helpType: ISupportHelpEntry,
447 474
 	},
448 475
 }
449 476
 
477
+// HelpIndex contains the list of all help topics for regular users.
478
+var HelpIndex = "list of all help topics for regular users"
479
+
480
+// HelpIndexOpers contains the list of all help topics for opers.
481
+var HelpIndexOpers = "list of all help topics for opers"
482
+
483
+// GenerateHelpIndex is used to generate HelpIndex.
484
+func GenerateHelpIndex(forOpers bool) string {
485
+	newHelpIndex := `= Help Topics =
486
+
487
+Commands:
488
+%s
489
+
490
+RPL_ISUPPORT Tokens:
491
+%s
492
+
493
+Information:
494
+%s`
495
+
496
+	// generate them
497
+	var commands, isupport, information []string
498
+
499
+	var line string
500
+	for name, info := range Help {
501
+		if info.duplicate {
502
+			continue
503
+		}
504
+		if info.oper && !forOpers {
505
+			continue
506
+		}
507
+
508
+		line = fmt.Sprintf("   %s", name)
509
+
510
+		if info.helpType == CommandHelpEntry {
511
+			commands = append(commands, line)
512
+		} else if info.helpType == ISupportHelpEntry {
513
+			isupport = append(isupport, line)
514
+		} else if info.helpType == InformationHelpEntry {
515
+			information = append(information, line)
516
+		}
517
+	}
518
+
519
+	// sort the lines
520
+	sort.Strings(commands)
521
+	sort.Strings(isupport)
522
+	sort.Strings(information)
523
+
524
+	// sub them in
525
+	newHelpIndex = fmt.Sprintf(newHelpIndex, strings.Join(commands, "\n"), strings.Join(isupport, "\n"), strings.Join(information, "\n"))
526
+
527
+	return newHelpIndex
528
+}
529
+
450 530
 // sendHelp sends the client help of the given string.
451 531
 func (client *Client) sendHelp(name string, text string) {
452 532
 	splitName := strings.Split(name, " ")
@@ -462,7 +542,7 @@ func (client *Client) sendHelp(name string, text string) {
462 542
 		}
463 543
 	}
464 544
 	args := splitName
465
-	args = append(args, "End of /HELP")
545
+	args = append(args, "End of /HELPOP")
466 546
 	client.Send(nil, client.server.name, RPL_ENDOFHELP, args...)
467 547
 }
468 548
 
@@ -471,9 +551,19 @@ func helpHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
471 551
 	argument := strings.ToLower(strings.TrimSpace(strings.Join(msg.Params, " ")))
472 552
 
473 553
 	if len(argument) < 1 {
474
-		client.sendHelp("HELP", `HELP <argument>
554
+		client.sendHelp("HELPOP", `HELPOP <argument>
475 555
 
476
-Get an explanation of <argument>.`)
556
+Get an explanation of <argument>, or "index" for a list of help topics.`)
557
+		return false
558
+	}
559
+
560
+	// handle index
561
+	if argument == "index" {
562
+		if client.flags[Operator] {
563
+			client.sendHelp("HELP", HelpIndexOpers)
564
+		} else {
565
+			client.sendHelp("HELP", HelpIndex)
566
+		}
477 567
 		return false
478 568
 	}
479 569
 

+ 1
- 1
irc/nickserv.go Просмотреть файл

@@ -20,5 +20,5 @@ func (server *Server) nickservReceiveNotice(client *Client, message string) {
20 20
 }
21 21
 
22 22
 func (server *Server) nickservReceivePrivmsg(client *Client, message string) {
23
-	client.Notice("NickServ is not yet implemented, sorry! To register an account, check /HELP REG")
23
+	client.Notice("NickServ is not yet implemented, sorry! To register an account, check /HELPOP REG")
24 24
 }

+ 3
- 0
irc/server.go Просмотреть файл

@@ -156,6 +156,9 @@ func NewServer(configFilename string, config *Config, logger *logger.Manager) (*
156 156
 			return nil, fmt.Errorf("Help entry does not exist for command %s", name)
157 157
 		}
158 158
 	}
159
+	// generate help indexes
160
+	HelpIndex = GenerateHelpIndex(false)
161
+	HelpIndexOpers = GenerateHelpIndex(true)
159 162
 
160 163
 	if config.Accounts.AuthenticationEnabled {
161 164
 		SupportedCapabilities[SASL] = true

Загрузка…
Отмена
Сохранить