2017-03-11 13:01:40 +01:00
// Copyright (c) 2017 Daniel Oaks <daniel@danieloaks.net>
// released under the MIT license
package irc
import (
"fmt"
"strings"
2017-06-15 18:14:19 +02:00
"github.com/goshuirc/irc-go/ircfmt"
2017-06-14 20:00:53 +02:00
"github.com/oragono/oragono/irc/sno"
2017-03-11 13:01:40 +01:00
)
func ( server * Server ) chanservReceiveNotice ( client * Client , message string ) {
// do nothing
}
// ChanServNotice sends the client a notice from ChanServ.
func ( client * Client ) ChanServNotice ( text string ) {
client . Send ( nil , fmt . Sprintf ( "ChanServ!services@%s" , client . server . name ) , "NOTICE" , client . nick , text )
}
func ( server * Server ) chanservReceivePrivmsg ( client * Client , message string ) {
var params [ ] string
for _ , p := range strings . Split ( message , " " ) {
if len ( p ) > 0 {
params = append ( params , p )
}
}
if len ( params ) < 1 {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "You need to run a command" ) )
2017-03-11 13:01:40 +01:00
//TODO(dan): dump CS help here
return
}
command := strings . ToLower ( params [ 0 ] )
server . logger . Debug ( "chanserv" , fmt . Sprintf ( "Client %s ran command %s" , client . nick , command ) )
if command == "register" {
if len ( params ) < 2 {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "Syntax: REGISTER <channel>" ) )
2017-03-11 13:01:40 +01:00
return
}
2017-03-24 03:52:38 +01:00
if ! server . channelRegistrationEnabled {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "Channel registration is not enabled" ) )
2017-03-24 03:52:38 +01:00
return
}
2017-03-11 13:01:40 +01:00
channelName := params [ 1 ]
channelKey , err := CasefoldChannel ( channelName )
if err != nil {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "Channel name is not valid" ) )
2017-03-11 13:01:40 +01:00
return
}
channelInfo := server . channels . Get ( channelKey )
2017-11-09 04:19:50 +01:00
if channelInfo == nil || ! channelInfo . ClientIsAtLeast ( client , ChannelOperator ) {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "You must be an oper on the channel to register it" ) )
2017-03-11 13:01:40 +01:00
return
}
2017-11-09 04:19:50 +01:00
if client . account == & NoAccount {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "You must be logged in to register a channel" ) )
2017-03-11 13:01:40 +01:00
return
}
2017-11-09 04:19:50 +01:00
// this provides the synchronization that allows exactly one registration of the channel:
err = channelInfo . SetRegistered ( client . AccountName ( ) )
if err != nil {
client . ChanServNotice ( err . Error ( ) )
return
}
2017-03-11 13:01:40 +01:00
2017-11-09 04:19:50 +01:00
// registration was successful: make the database reflect it
go server . channelRegistry . StoreChannel ( channelInfo , true )
2017-03-11 13:01:40 +01:00
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( fmt . Sprintf ( client . t ( "Channel %s successfully registered" ) , channelName ) )
2017-03-11 13:01:40 +01:00
2017-11-09 04:19:50 +01:00
server . logger . Info ( "chanserv" , fmt . Sprintf ( "Client %s registered channel %s" , client . nick , channelName ) )
server . snomasks . Send ( sno . LocalChannels , fmt . Sprintf ( ircfmt . Unescape ( "Channel registered $c[grey][$r%s$c[grey]] by $c[grey][$r%s$c[grey]]" ) , channelName , client . nickMaskString ) )
// give them founder privs
change := channelInfo . applyModeMemberNoMutex ( client , ChannelFounder , Add , client . NickCasefolded ( ) )
if change != nil {
//TODO(dan): we should change the name of String and make it return a slice here
//TODO(dan): unify this code with code in modes.go
args := append ( [ ] string { channelName } , strings . Split ( change . String ( ) , " " ) ... )
for _ , member := range channelInfo . Members ( ) {
member . Send ( nil , fmt . Sprintf ( "ChanServ!services@%s" , client . server . name ) , "MODE" , args ... )
}
}
2017-03-11 13:01:40 +01:00
} else {
2018-01-22 12:26:01 +01:00
client . ChanServNotice ( client . t ( "Sorry, I don't know that command" ) )
2017-03-11 13:01:40 +01:00
}
}