Browse Source

Merge pull request #1463 from slingamn/issue1294_purge.1

Enhancements to CS PURGE
tags/v2.5.0-rc1
Shivaram Lingamneni 3 years ago
parent
commit
91cc2156f0
No account linked to committer's email address
3 changed files with 76 additions and 19 deletions
  1. 12
    0
      irc/channelmanager.go
  2. 57
    19
      irc/chanserv.go
  3. 7
    0
      irc/getters.go

+ 12
- 0
irc/channelmanager.go View File

4
 package irc
4
 package irc
5
 
5
 
6
 import (
6
 import (
7
+	"sort"
7
 	"sync"
8
 	"sync"
8
 
9
 
9
 	"github.com/oragono/oragono/irc/utils"
10
 	"github.com/oragono/oragono/irc/utils"
405
 	}
406
 	}
406
 	return nil
407
 	return nil
407
 }
408
 }
409
+
410
+func (cm *ChannelManager) ListPurged() (result []string) {
411
+	cm.RLock()
412
+	result = make([]string, 0, len(cm.purgedChannels))
413
+	for c := range cm.purgedChannels {
414
+		result = append(result, c)
415
+	}
416
+	cm.RUnlock()
417
+	sort.Strings(result)
418
+	return
419
+}

+ 57
- 19
irc/chanserv.go View File

114
 		},
114
 		},
115
 		"purge": {
115
 		"purge": {
116
 			handler: csPurgeHandler,
116
 			handler: csPurgeHandler,
117
-			help: `Syntax: $bPURGE #channel [reason]$b
117
+			help: `Syntax: $bPURGE <ADD | DEL | LIST> #channel [code] [reason]$b
118
 
118
 
119
-PURGE blacklists a channel from the server, making it impossible to join
119
+PURGE ADD blacklists a channel from the server, making it impossible to join
120
 or otherwise interact with the channel. If the channel currently has members,
120
 or otherwise interact with the channel. If the channel currently has members,
121
 they will be kicked from it. PURGE may also be applied preemptively to
121
 they will be kicked from it. PURGE may also be applied preemptively to
122
-channels that do not currently have members.`,
122
+channels that do not currently have members. A purge can be undone with
123
+PURGE DEL. To list purged channels, use PURGE LIST.`,
123
 			helpShort:         `$bPURGE$b blacklists a channel from the server.`,
124
 			helpShort:         `$bPURGE$b blacklists a channel from the server.`,
124
 			capabs:            []string{"chanreg"},
125
 			capabs:            []string{"chanreg"},
125
 			minParams:         1,
126
 			minParams:         1,
126
-			maxParams:         2,
127
+			maxParams:         3,
127
 			unsplitFinalParam: true,
128
 			unsplitFinalParam: true,
128
 		},
129
 		},
129
-		"unpurge": {
130
-			handler: csUnpurgeHandler,
131
-			help: `Syntax: $bUNPURGE #channel$b
132
-
133
-UNPURGE removes any blacklisting of a channel that was previously
134
-set using PURGE.`,
135
-			helpShort: `$bUNPURGE$b undoes a previous PURGE command.`,
136
-			capabs:    []string{"chanreg"},
137
-			minParams: 1,
138
-		},
139
 		"list": {
130
 		"list": {
140
 			handler: csListHandler,
131
 			handler: csListHandler,
141
 			help: `Syntax: $bLIST [regex]$b
132
 			help: `Syntax: $bLIST [regex]$b
587
 		return // should be impossible because you need oper capabs for this
578
 		return // should be impossible because you need oper capabs for this
588
 	}
579
 	}
589
 
580
 
581
+	switch strings.ToLower(params[0]) {
582
+	case "add":
583
+		csPurgeAddHandler(service, client, params[1:], oper.Name, rb)
584
+	case "del", "remove":
585
+		csPurgeDelHandler(service, client, params[1:], oper.Name, rb)
586
+	case "list":
587
+		csPurgeListHandler(service, client, rb)
588
+	default:
589
+		service.Notice(rb, client.t("Invalid parameters"))
590
+	}
591
+}
592
+
593
+func csPurgeAddHandler(service *ircService, client *Client, params []string, operName string, rb *ResponseBuffer) {
594
+	if len(params) == 0 {
595
+		service.Notice(rb, client.t("Invalid parameters"))
596
+		return
597
+	}
598
+
590
 	chname := params[0]
599
 	chname := params[0]
600
+	params = params[1:]
601
+	channel := client.server.channels.Get(chname) // possibly nil
602
+	var ctime time.Time
603
+	if channel != nil {
604
+		chname = channel.Name()
605
+		ctime = channel.Ctime()
606
+	}
607
+	code := utils.ConfirmationCode(chname, ctime)
608
+
609
+	if len(params) == 0 || params[0] != code {
610
+		service.Notice(rb, ircfmt.Unescape(client.t("$bWarning: you are about to empty this channel and remove it from the server.$b")))
611
+		service.Notice(rb, fmt.Sprintf(client.t("To confirm, run this command: %s"), fmt.Sprintf("/CS PURGE ADD %s %s", chname, code)))
612
+		return
613
+	}
614
+	params = params[1:]
615
+
591
 	var reason string
616
 	var reason string
592
 	if 1 < len(params) {
617
 	if 1 < len(params) {
593
 		reason = params[1]
618
 		reason = params[1]
594
 	}
619
 	}
620
+
595
 	purgeRecord := ChannelPurgeRecord{
621
 	purgeRecord := ChannelPurgeRecord{
596
-		Oper:     oper.Name,
622
+		Oper:     operName,
597
 		PurgedAt: time.Now().UTC(),
623
 		PurgedAt: time.Now().UTC(),
598
 		Reason:   reason,
624
 		Reason:   reason,
599
 	}
625
 	}
600
-	switch server.channels.Purge(chname, purgeRecord) {
626
+	switch client.server.channels.Purge(chname, purgeRecord) {
601
 	case nil:
627
 	case nil:
602
-		channel := server.channels.Get(chname)
603
 		if channel != nil { // channel need not exist to be purged
628
 		if channel != nil { // channel need not exist to be purged
604
 			for _, target := range channel.Members() {
629
 			for _, target := range channel.Members() {
605
 				channel.Kick(client, target, "Cleared by ChanServ", rb, true)
630
 				channel.Kick(client, target, "Cleared by ChanServ", rb, true)
613
 	}
638
 	}
614
 }
639
 }
615
 
640
 
616
-func csUnpurgeHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
641
+func csPurgeDelHandler(service *ircService, client *Client, params []string, operName string, rb *ResponseBuffer) {
642
+	if len(params) == 0 {
643
+		service.Notice(rb, client.t("Invalid parameters"))
644
+		return
645
+	}
646
+
617
 	chname := params[0]
647
 	chname := params[0]
618
-	switch server.channels.Unpurge(chname) {
648
+	switch client.server.channels.Unpurge(chname) {
619
 	case nil:
649
 	case nil:
620
 		service.Notice(rb, fmt.Sprintf(client.t("Successfully unpurged channel %s from the server"), chname))
650
 		service.Notice(rb, fmt.Sprintf(client.t("Successfully unpurged channel %s from the server"), chname))
621
 	case errNoSuchChannel:
651
 	case errNoSuchChannel:
625
 	}
655
 	}
626
 }
656
 }
627
 
657
 
658
+func csPurgeListHandler(service *ircService, client *Client, rb *ResponseBuffer) {
659
+	l := client.server.channels.ListPurged()
660
+	service.Notice(rb, fmt.Sprintf(client.t("There are %d purged channel(s)."), len(l)))
661
+	for i, c := range l {
662
+		service.Notice(rb, fmt.Sprintf("%d: %s", i+1, c))
663
+	}
664
+}
665
+
628
 func csListHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
666
 func csListHandler(service *ircService, server *Server, client *Client, command string, params []string, rb *ResponseBuffer) {
629
 	if !client.HasRoleCapabs("chanreg") {
667
 	if !client.HasRoleCapabs("chanreg") {
630
 		service.Notice(rb, client.t("Insufficient privileges"))
668
 		service.Notice(rb, client.t("Insufficient privileges"))

+ 7
- 0
irc/getters.go View File

523
 	channel.forward = forward
523
 	channel.forward = forward
524
 	channel.stateMutex.Unlock()
524
 	channel.stateMutex.Unlock()
525
 }
525
 }
526
+
527
+func (channel *Channel) Ctime() (ctime time.Time) {
528
+	channel.stateMutex.RLock()
529
+	ctime = channel.createdTime
530
+	channel.stateMutex.RUnlock()
531
+	return
532
+}

Loading…
Cancel
Save