<commit_before>// Copyright 2015 <NAME> (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.

package main

import (
	"fmt"
	"log"
	"net"
	"strings"
	"time"

	"github.com/la5nta/wl2k-go/transport/ax25"
	"github.com/la5nta/wl2k-go/transport/telnet"
)

type incomingConnect struct {
	conn       net.Conn
	remoteCall string
	kind       string
	freq       Frequency
}

func Unlisten(param string) {
	methods := strings.Split(param, ",")
	for _, method := range methods {
		ln, ok := listeners[method]
		if !ok {
			fmt.Printf("No active %s listener, ignoring.\n", method)
		} else if err := ln.Close(); err != nil {
			log.Printf("Unable to close %s listener: %s", method, err)
		}
	}
}

func Listen(listenStr string) {
	cc := make(chan incomingConnect, 2)

	methods := strings.Split(listenStr, ",")
	for _, method := range methods {
		method = strings.TrimSpace(strings.ToLower(method))

		switch method {
		case MethodWinmor:
			if wmTNC == nil {
				initWinmorTNC()
			}
			listenWinmor(cc)
		case MethodArdop:
			if adTNC == nil {
				initArdopTNC()
			}
			listenArdop(cc)
		case MethodTelnet:
			listenTelnet(cc)
		case MethodAX25:
			listenAX25(cc)
		case MethodSerialTNC:
			log.Printf("%s listen not implemented, ignoring.", method)
		default:
			log.Printf("'%s' is not a valid listen method", method)
			return
		}
	}

	log.Printf("Listening for incoming traffic (%s)...", listenStr)
	go func() {
		for {
			connect := <-cc
			eventLog.LogConn("accept", connect.freq, connect.conn, nil)
			log.Printf("Got connect (%s:%s)", connect.kind, connect.remoteCall)

			err := exchange(connect.conn, connect.remoteCall, true)
			if err != nil {
				log.Printf("Exchange failed: %s", err)
			} else {
				log.Println("Disconnected.")
			}
		}
	}()
}

func listenWinmor(incoming chan<- incomingConnect) {
	// RMS Express runs bw at 500Hz except when sending/receiving message. Why?
	// ... Or is it cmdRobust True?
	ln, err := wmTNC.Listen(config.Winmor.InboundBandwidth)
	if err != nil {
		log.Fatal(err)
	}

	listeners[MethodWinmor] = ln
	go func() {
		defer func() {
			delete(listeners, MethodWinmor)
			log.Printf("%s listener closed.", MethodWinmor)
		}()

		for {
			conn, err := ln.Accept()
			if err != nil {
				return
			}

			var freq Frequency
			if rig, ok := rigs[config.Winmor.Rig]; ok {
				f, _ := rig.CurrentVFO().GetFreq()
				freq = Frequency(f)
			}

			incoming <- incomingConnect{
				conn:       conn,
				remoteCall: conn.RemoteAddr().String(),
				kind:       MethodWinmor,
				freq:       freq,
			}
		}
	}()
}

func listenArdop(incoming chan<- incomingConnect) {
	// RMS Express runs bw at 500Hz except when sending/receiving message. Why?
	// ... Or is it cmdRobust True?
	ln, err := adTNC.Listen()
	if err != nil {
		log.Fatal(err)
	}

	listeners[MethodArdop] = ln
	go func() {
		defer func() {
			delete(listeners, MethodArdop)
			log.Printf("%s listener closed.", MethodArdop)
		}()

		for {
			conn, err := ln.Accept()
			if err != nil {
				return
			}

			var freq Frequency
			if rig, ok := rigs[config.Ardop.Rig]; ok {
				f, _ := rig.CurrentVFO().GetFreq()
				freq = Frequency(f)
			}

			incoming <- incomingConnect{<commit_msg>cmd/wl2k: Remove obsolete comment<commit_after>// Copyright 2015 <NAME> (LA5NTA). All rights reserved.
// Use of this source code is governed by the MIT-license that can be
// found in the LICENSE file.

package main

import (
	"fmt"
	"log"
	"net"
	"strings"
	"time"

	"github.com/la5nta/wl2k-go/transport/ax25"
	"github.com/la5nta/wl2k-go/transport/telnet"
)

type incomingConnect struct {
	conn       net.Conn
	remoteCall string
	kind       string
	freq       Frequency
}

func Unlisten(param string) {
	methods := strings.Split(param, ",")
	for _, method := range methods {
		ln, ok := listeners[method]
		if !ok {
			fmt.Printf("No active %s listener, ignoring.\n", method)
		} else if err := ln.Close(); err != nil {
			log.Printf("Unable to close %s listener: %s", method, err)
		}
	}
}

func Listen(listenStr string) {
	cc := make(chan incomingConnect, 2)

	methods := strings.Split(listenStr, ",")
	for _, method := range methods {
		method = strings.TrimSpace(strings.ToLower(method))

		switch method {
		case MethodWinmor:
			if wmTNC == nil {
				initWinmorTNC()
			}
			listenWinmor(cc)
		case MethodArdop:
			if adTNC == nil {
				initArdopTNC()
			}
			listenArdop(cc)
		case MethodTelnet:
			listenTelnet(cc)
		case MethodAX25:
			listenAX25(cc)
		case MethodSerialTNC:
			log.Printf("%s listen not implemented, ignoring.", method)
		default:
			log.Printf("'%s' is not a valid listen method", method)
			return
		}
	}

	log.Printf("Listening for incoming traffic (%s)...", listenStr)
	go func() {
		for {
			connect := <-cc
			eventLog.LogConn("accept", connect.freq, connect.conn, nil)
			log.Printf("Got connect (%s:%s)", connect.kind, connect.remoteCall)

			err := exchange(connect.conn, connect.remoteCall, true)
			if err != nil {
				log.Printf("Exchange failed: %s", err)
			} else {
				log.Println("Disconnected.")
			}
		}
	}()
}

func listenWinmor(incoming chan<- incomingConnect) {
	// RMS Express runs bw at 500Hz except when sending/receiving message. Why?
	// ... Or is it cmdRobust True?
	ln, err := wmTNC.Listen(config.Winmor.InboundBandwidth)
	if err != nil {
		log.Fatal(err)
	}

	listeners[MethodWinmor] = ln
	go func() {
		defer func() {
			delete(listeners, MethodWinmor)
			log.Printf("%s listener closed.", MethodWinmor)
		}()

		for {
			conn, err := ln.Accept()
			if err != nil {
				return
			}

			var freq Frequency
			if rig, ok := rigs[config.Winmor.Rig]; ok {
				f, _ := rig.CurrentVFO().GetFreq()
				freq = Frequency(f)
			}

			incoming <- incomingConnect{
				conn:       conn,
				remoteCall: conn.RemoteAddr().String(),
				kind:       MethodWinmor,
				freq:       freq,
			}
		}
	}()
}

func listenArdop(incoming chan<- incomingConnect) {
	ln, err := adTNC.Listen()
	if err != nil {
		log.Fatal(err)
	}

	listeners[MethodArdop] = ln
	go func() {
		defer func() {
			delete(listeners, MethodArdop)
			log.Printf("%s listener closed.", MethodArdop)
		}()

		for {
			conn, err := ln.Accept()
			if err != nil {
				return
			}

			var freq Frequency
			if rig, ok := rigs[config.Ardop.Rig]; ok {
				f, _ := rig.CurrentVFO().GetFreq()
				freq = Frequency(f)
			}

			incoming <- incomingConnect{
{'fraction_non_alphanumeric': 0.14051266020631448, 'fraction_numerical': 0.006564551422319475, 'mean_word_length': 3.0219987429289756, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5947205', 'n_tokens_mistral': 2642, 'n_tokens_neox': 2261, 'n_words': 584}