Changeset 636 in code


Ignore:
Timestamp:
Oct 13, 2021, 8:58:34 AM (4 years ago)
Author:
contact
Message:

Add bouncer MOTD

Closes: https://todo.sr.ht/~emersion/soju/137

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/cmd/soju/main.go

    r620 r636  
    66        "flag"
    77        "fmt"
     8        "io/ioutil"
    89        "log"
    910        "net"
     
    3738}
    3839
     40func loadMOTD(srv *soju.Server, filename string) error {
     41        if filename == "" {
     42                return nil
     43        }
     44
     45        b, err := ioutil.ReadFile(filename)
     46        if err != nil {
     47                return err
     48        }
     49        srv.SetMOTD(strings.TrimSpace(string(b)))
     50        return nil
     51}
     52
    3953func main() {
    4054        var listen []string
     
    92106        srv.Debug = debug
    93107
     108        if err := loadMOTD(srv, cfg.MOTDPath); err != nil {
     109                log.Fatalf("failed to load MOTD: %v", err)
     110        }
     111
    94112        for _, listen := range cfg.Listen {
    95113                listenURI := listen
     
    225243                switch sig {
    226244                case syscall.SIGHUP:
     245                        log.Print("reloading TLS certificate and MOTD")
    227246                        if cfg.TLS != nil {
    228                                 log.Print("reloading TLS certificate")
    229247                                cert, err := tls.LoadX509KeyPair(cfg.TLS.CertPath, cfg.TLS.KeyPath)
    230248                                if err != nil {
     
    233251                                }
    234252                                tlsCert.Store(&cert)
     253                        }
     254                        if err := loadMOTD(srv, cfg.MOTDPath); err != nil {
     255                                log.Printf("failed to reload MOTD: %v", err)
    235256                        }
    236257                case syscall.SIGINT, syscall.SIGTERM:
  • trunk/config/config.go

    r612 r636  
    4141        Hostname string
    4242        TLS      *TLS
     43        MOTDPath string
    4344
    4445        SQLDriver string
     
    129130                                return nil, fmt.Errorf("directive %q: %v", d.Name, err)
    130131                        }
     132                case "motd":
     133                        if err := d.ParseParams(&srv.MOTDPath); err != nil {
     134                                return nil, err
     135                        }
    131136                default:
    132137                        return nil, fmt.Errorf("unknown directive %q", d.Name)
  • trunk/doc/soju.1.scd

    r632 r636  
    4545be done by adding a "@<client>" suffix to the username.
    4646
    47 soju will reload the TLS certificate and key when it receives the HUP signal.
     47soju will reload the TLS certificate/key and the MOTD file when it receives the
     48HUP signal.
    4849
    4950Administrators can broadcast a message to all bouncer users via _/notice
     
    143144        Maximum number of networks per user. By default, there is no limit.
    144145
     146*motd* <path>
     147        Path to the MOTD file. The bouncer MOTD is sent to clients which aren't
     148        bound to a specific network. By default, no MOTD is sent.
     149
    145150# IRC SERVICE
    146151
  • trunk/downstream.go

    r629 r636  
    11201120                dc.SendMessage(msg)
    11211121        }
    1122         motdHint := "No MOTD"
    11231122        if uc := dc.upstream(); uc != nil {
    1124                 motdHint = "Use /motd to read the message of the day"
    11251123                dc.SendMessage(&irc.Message{
    11261124                        Prefix:  dc.srv.prefix(),
     
    11291127                })
    11301128        }
    1131         dc.SendMessage(&irc.Message{
    1132                 Prefix:  dc.srv.prefix(),
    1133                 Command: irc.ERR_NOMOTD,
    1134                 Params:  []string{dc.nick, motdHint},
    1135         })
     1129
     1130        if motd := dc.user.srv.MOTD(); motd != "" && dc.network == nil {
     1131                for _, msg := range generateMOTD(dc.srv.prefix(), dc.nick, motd) {
     1132                        dc.SendMessage(msg)
     1133                }
     1134        } else {
     1135                motdHint := "No MOTD"
     1136                if dc.network != nil {
     1137                        motdHint = "Use /motd to read the message of the day"
     1138                }
     1139                dc.SendMessage(&irc.Message{
     1140                        Prefix:  dc.srv.prefix(),
     1141                        Command: irc.ERR_NOMOTD,
     1142                        Params:  []string{dc.nick, motdHint},
     1143                })
     1144        }
    11361145
    11371146        dc.updateNick()
  • trunk/irc.go

    r516 r636  
    380380}
    381381
     382func generateMOTD(prefix *irc.Prefix, nick string, motd string) []*irc.Message {
     383        var msgs []*irc.Message
     384        msgs = append(msgs, &irc.Message{
     385                Prefix:  prefix,
     386                Command: irc.RPL_MOTDSTART,
     387                Params:  []string{nick, fmt.Sprintf("- Message of the Day -")},
     388        })
     389
     390        for _, l := range strings.Split(motd, "\n") {
     391                msgs = append(msgs, &irc.Message{
     392                        Prefix:  prefix,
     393                        Command: irc.RPL_MOTD,
     394                        Params:  []string{nick, l},
     395                })
     396        }
     397
     398        msgs = append(msgs, &irc.Message{
     399                Prefix:  prefix,
     400                Command: irc.RPL_ENDOFMOTD,
     401                Params:  []string{nick, "End of /MOTD command."},
     402        })
     403
     404        return msgs
     405}
     406
    382407type joinSorter struct {
    383408        channels []string
  • trunk/server.go

    r612 r636  
    6464        listeners map[net.Listener]struct{}
    6565        users     map[string]*user
     66
     67        motd atomic.Value // string
    6668}
    6769
    6870func NewServer(db Database) *Server {
    69         return &Server{
     71        srv := &Server{
    7072                Logger:          log.New(log.Writer(), "", log.LstdFlags),
    7173                HistoryLimit:    1000,
     
    7577                users:           make(map[string]*user),
    7678        }
     79        srv.motd.Store("")
     80        return srv
    7781}
    7882
     
    269273        return &stats
    270274}
     275
     276func (s *Server) SetMOTD(motd string) {
     277        s.motd.Store(motd)
     278}
     279
     280func (s *Server) MOTD() string {
     281        return s.motd.Load().(string)
     282}
Note: See TracChangeset for help on using the changeset viewer.