Changeset 282 in code for trunk


Ignore:
Timestamp:
Apr 30, 2020, 2:10:39 PM (5 years ago)
Author:
contact
Message:

Introduce permanentUpstreamCaps

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r281 r282  
    1515        "gopkg.in/irc.v3"
    1616)
     17
     18// permanentUpstreamCaps is the static list of upstream capabilities always
     19// requested when supported.
     20var permanentUpstreamCaps = map[string]bool{
     21        "away-notify":      true,
     22        "batch":            true,
     23        "labeled-response": true,
     24        "message-tags":     true,
     25        "server-time":      true,
     26}
    1727
    1828type upstreamChannel struct {
     
    12101220func (uc *upstreamConn) requestCaps() {
    12111221        var requestCaps []string
    1212         for _, c := range []string{"message-tags", "batch", "labeled-response", "server-time", "away-notify"} {
     1222        for c := range permanentUpstreamCaps {
    12131223                if _, ok := uc.supportedCaps[c]; ok && !uc.caps[c] {
    12141224                        requestCaps = append(requestCaps, c)
     
    12201230        }
    12211231
    1222         if len(requestCaps) > 0 {
     1232        if len(requestCaps) == 0 {
     1233                return
     1234        }
     1235
     1236        uc.SendMessage(&irc.Message{
     1237                Command: "CAP",
     1238                Params:  []string{"REQ", strings.Join(requestCaps, " ")},
     1239        })
     1240}
     1241
     1242func (uc *upstreamConn) requestSASL() bool {
     1243        if uc.network.SASL.Mechanism == "" {
     1244                return false
     1245        }
     1246
     1247        v, ok := uc.supportedCaps["sasl"]
     1248        if !ok {
     1249                return false
     1250        }
     1251        if v != "" {
     1252                mechanisms := strings.Split(v, ",")
     1253                found := false
     1254                for _, mech := range mechanisms {
     1255                        if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
     1256                                found = true
     1257                                break
     1258                        }
     1259                }
     1260                if !found {
     1261                        return false
     1262                }
     1263        }
     1264
     1265        return true
     1266}
     1267
     1268func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
     1269        uc.caps[name] = ok
     1270
     1271        switch name {
     1272        case "sasl":
     1273                if !ok {
     1274                        uc.logger.Printf("server refused to acknowledge the SASL capability")
     1275                        return nil
     1276                }
     1277
     1278                auth := &uc.network.SASL
     1279                switch auth.Mechanism {
     1280                case "PLAIN":
     1281                        uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
     1282                        uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
     1283                default:
     1284                        return fmt.Errorf("unsupported SASL mechanism %q", name)
     1285                }
     1286
    12231287                uc.SendMessage(&irc.Message{
    1224                         Command: "CAP",
    1225                         Params:  []string{"REQ", strings.Join(requestCaps, " ")},
    1226                 })
    1227         }
     1288                        Command: "AUTHENTICATE",
     1289                        Params:  []string{auth.Mechanism},
     1290                })
     1291        default:
     1292                if permanentUpstreamCaps[name] {
     1293                        break
     1294                }
     1295                uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
     1296        }
     1297        return nil
    12281298}
    12291299
     
    12911361}
    12921362
    1293 func (uc *upstreamConn) requestSASL() bool {
    1294         if uc.network.SASL.Mechanism == "" {
    1295                 return false
    1296         }
    1297 
    1298         v, ok := uc.supportedCaps["sasl"]
    1299         if !ok {
    1300                 return false
    1301         }
    1302         if v != "" {
    1303                 mechanisms := strings.Split(v, ",")
    1304                 found := false
    1305                 for _, mech := range mechanisms {
    1306                         if strings.EqualFold(mech, uc.network.SASL.Mechanism) {
    1307                                 found = true
    1308                                 break
    1309                         }
    1310                 }
    1311                 if !found {
    1312                         return false
    1313                 }
    1314         }
    1315 
    1316         return true
    1317 }
    1318 
    1319 func (uc *upstreamConn) handleCapAck(name string, ok bool) error {
    1320         uc.caps[name] = ok
    1321 
    1322         switch name {
    1323         case "sasl":
    1324                 if !ok {
    1325                         uc.logger.Printf("server refused to acknowledge the SASL capability")
    1326                         return nil
    1327                 }
    1328 
    1329                 auth := &uc.network.SASL
    1330                 switch auth.Mechanism {
    1331                 case "PLAIN":
    1332                         uc.logger.Printf("starting SASL PLAIN authentication with username %q", auth.Plain.Username)
    1333                         uc.saslClient = sasl.NewPlainClient("", auth.Plain.Username, auth.Plain.Password)
    1334                 default:
    1335                         return fmt.Errorf("unsupported SASL mechanism %q", name)
    1336                 }
    1337 
    1338                 uc.SendMessage(&irc.Message{
    1339                         Command: "AUTHENTICATE",
    1340                         Params:  []string{auth.Mechanism},
    1341                 })
    1342         case "message-tags", "labeled-response", "away-notify", "batch", "server-time":
    1343                 // Nothing to do
    1344         default:
    1345                 uc.logger.Printf("received CAP ACK/NAK for a cap we don't support: %v", name)
    1346         }
    1347         return nil
    1348 }
    1349 
    13501363func (uc *upstreamConn) readMessages(ch chan<- event) error {
    13511364        for {
Note: See TracChangeset for help on using the changeset viewer.