You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.go 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // Copyright (c) 2014-2015 Edmund Huber
  3. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  4. // released under the MIT license
  5. package irc
  6. import (
  7. "crypto/tls"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "io/ioutil"
  12. "log"
  13. "path/filepath"
  14. "strings"
  15. "time"
  16. "code.cloudfoundry.org/bytefmt"
  17. "github.com/oragono/oragono/irc/connection_limits"
  18. "github.com/oragono/oragono/irc/custime"
  19. "github.com/oragono/oragono/irc/languages"
  20. "github.com/oragono/oragono/irc/logger"
  21. "github.com/oragono/oragono/irc/passwd"
  22. "github.com/oragono/oragono/irc/utils"
  23. "gopkg.in/yaml.v2"
  24. )
  25. // PassConfig holds the connection password.
  26. type PassConfig struct {
  27. Password string
  28. }
  29. // TLSListenConfig defines configuration options for listening on TLS.
  30. type TLSListenConfig struct {
  31. Cert string
  32. Key string
  33. }
  34. // Config returns the TLS contiguration assicated with this TLSListenConfig.
  35. func (conf *TLSListenConfig) Config() (*tls.Config, error) {
  36. cert, err := tls.LoadX509KeyPair(conf.Cert, conf.Key)
  37. if err != nil {
  38. return nil, ErrInvalidCertKeyPair
  39. }
  40. return &tls.Config{
  41. Certificates: []tls.Certificate{cert},
  42. }, err
  43. }
  44. // PasswordBytes returns the bytes represented by the password hash.
  45. func (conf *PassConfig) PasswordBytes() []byte {
  46. bytes, err := passwd.DecodePasswordHash(conf.Password)
  47. if err != nil {
  48. log.Fatal("decode password error: ", err)
  49. }
  50. return bytes
  51. }
  52. type AccountConfig struct {
  53. Registration AccountRegistrationConfig
  54. AuthenticationEnabled bool `yaml:"authentication-enabled"`
  55. SkipServerPassword bool `yaml:"skip-server-password"`
  56. NickReservation NickReservationConfig `yaml:"nick-reservation"`
  57. }
  58. // AccountRegistrationConfig controls account registration.
  59. type AccountRegistrationConfig struct {
  60. Enabled bool
  61. EnabledCallbacks []string `yaml:"enabled-callbacks"`
  62. EnabledCredentialTypes []string `yaml:"-"`
  63. VerifyTimeout time.Duration `yaml:"verify-timeout"`
  64. Callbacks struct {
  65. Mailto struct {
  66. Server string
  67. Port int
  68. TLS struct {
  69. Enabled bool
  70. InsecureSkipVerify bool `yaml:"insecure_skip_verify"`
  71. ServerName string `yaml:"servername"`
  72. }
  73. Username string
  74. Password string
  75. Sender string
  76. VerifyMessageSubject string `yaml:"verify-message-subject"`
  77. VerifyMessage string `yaml:"verify-message"`
  78. }
  79. }
  80. AllowMultiplePerConnection bool `yaml:"allow-multiple-per-connection"`
  81. }
  82. type DnsblConfig struct {
  83. Enabled bool
  84. Channel string
  85. Lists []DnsblListEntry `yaml:"lists"`
  86. }
  87. type DnsblListEntry struct {
  88. Host string
  89. Types []string
  90. Reply map[string]DnsblListReply
  91. Action string
  92. ActionType DnsblActionType
  93. Reason string
  94. }
  95. type DnsblListReply struct {
  96. Action string
  97. ActionType DnsblActionType
  98. Reason string
  99. }
  100. func (conf *Config) DnsblTypes(action string) (DnsblActionType, error) {
  101. actions := map[string]DnsblActionType{
  102. "require-sasl": DnsblRequireSaslReply,
  103. "allow": DnsblAllowReply,
  104. "block": DnsblBlockReply,
  105. "notify": DnsblNotifyReply,
  106. }
  107. if value, exists := actions[action]; exists {
  108. return value, nil
  109. }
  110. return DnsblUnknownReply, errors.New(fmt.Sprintf("Unknown DNSBL action type: %s", action))
  111. }
  112. type NickReservationMethod int
  113. const (
  114. NickReservationWithTimeout NickReservationMethod = iota
  115. NickReservationStrict
  116. )
  117. func (nr *NickReservationMethod) UnmarshalYAML(unmarshal func(interface{}) error) error {
  118. var orig, raw string
  119. var err error
  120. if err = unmarshal(&orig); err != nil {
  121. return err
  122. }
  123. if raw, err = Casefold(orig); err != nil {
  124. return err
  125. }
  126. if raw == "timeout" {
  127. *nr = NickReservationWithTimeout
  128. } else if raw == "strict" {
  129. *nr = NickReservationStrict
  130. } else {
  131. return errors.New(fmt.Sprintf("invalid nick-reservation.method value: %s", orig))
  132. }
  133. return nil
  134. }
  135. type NickReservationConfig struct {
  136. Enabled bool
  137. AdditionalNickLimit int `yaml:"additional-nick-limit"`
  138. Method NickReservationMethod
  139. RenameTimeout time.Duration `yaml:"rename-timeout"`
  140. RenamePrefix string `yaml:"rename-prefix"`
  141. }
  142. // ChannelRegistrationConfig controls channel registration.
  143. type ChannelRegistrationConfig struct {
  144. Enabled bool
  145. }
  146. // OperClassConfig defines a specific operator class.
  147. type OperClassConfig struct {
  148. Title string
  149. WhoisLine string
  150. Extends string
  151. Capabilities []string
  152. }
  153. // OperConfig defines a specific operator's configuration.
  154. type OperConfig struct {
  155. Class string
  156. Vhost string
  157. WhoisLine string `yaml:"whois-line"`
  158. Password string
  159. Modes string
  160. }
  161. // PasswordBytes returns the bytes represented by the password hash.
  162. func (conf *OperConfig) PasswordBytes() []byte {
  163. bytes, err := passwd.DecodePasswordHash(conf.Password)
  164. if err != nil {
  165. log.Fatal("decode password error: ", err)
  166. }
  167. return bytes
  168. }
  169. // LineLenConfig controls line lengths.
  170. type LineLenConfig struct {
  171. Tags int
  172. Rest int
  173. }
  174. // STSConfig controls the STS configuration/
  175. type STSConfig struct {
  176. Enabled bool
  177. Duration time.Duration `yaml:"duration-real"`
  178. DurationString string `yaml:"duration"`
  179. Port int
  180. Preload bool
  181. }
  182. // Value returns the STS value to advertise in CAP
  183. func (sts *STSConfig) Value() string {
  184. val := fmt.Sprintf("duration=%d", int(sts.Duration.Seconds()))
  185. if sts.Enabled && sts.Port > 0 {
  186. val += fmt.Sprintf(",port=%d", sts.Port)
  187. }
  188. if sts.Enabled && sts.Preload {
  189. val += ",preload"
  190. }
  191. return val
  192. }
  193. // StackImpactConfig is the config used for StackImpact's profiling.
  194. type StackImpactConfig struct {
  195. Enabled bool
  196. AgentKey string `yaml:"agent-key"`
  197. AppName string `yaml:"app-name"`
  198. }
  199. type FakelagConfig struct {
  200. Enabled bool
  201. Window time.Duration
  202. BurstLimit uint `yaml:"burst-limit"`
  203. MessagesPerWindow uint `yaml:"messages-per-window"`
  204. Cooldown time.Duration
  205. }
  206. // Config defines the overall configuration.
  207. type Config struct {
  208. Network struct {
  209. Name string
  210. }
  211. Server struct {
  212. PassConfig
  213. Password string
  214. Name string
  215. Listen []string
  216. TLSListeners map[string]*TLSListenConfig `yaml:"tls-listeners"`
  217. STS STSConfig
  218. CheckIdent bool `yaml:"check-ident"`
  219. MOTD string
  220. MOTDFormatting bool `yaml:"motd-formatting"`
  221. ProxyAllowedFrom []string `yaml:"proxy-allowed-from"`
  222. WebIRC []webircConfig `yaml:"webirc"`
  223. MaxSendQString string `yaml:"max-sendq"`
  224. MaxSendQBytes int
  225. ConnectionLimiter connection_limits.LimiterConfig `yaml:"connection-limits"`
  226. ConnectionThrottler connection_limits.ThrottlerConfig `yaml:"connection-throttling"`
  227. }
  228. Languages struct {
  229. Enabled bool
  230. Path string
  231. Default string
  232. Data map[string]languages.LangData
  233. }
  234. Datastore struct {
  235. Path string
  236. }
  237. Accounts AccountConfig
  238. Channels struct {
  239. DefaultModes *string `yaml:"default-modes"`
  240. Registration ChannelRegistrationConfig
  241. }
  242. OperClasses map[string]*OperClassConfig `yaml:"oper-classes"`
  243. Opers map[string]*OperConfig
  244. Logging []logger.LoggingConfig
  245. Debug struct {
  246. RecoverFromErrors *bool `yaml:"recover-from-errors"`
  247. PprofListener *string `yaml:"pprof-listener"`
  248. StackImpact StackImpactConfig
  249. }
  250. Limits struct {
  251. AwayLen uint `yaml:"awaylen"`
  252. ChanListModes uint `yaml:"chan-list-modes"`
  253. ChannelLen uint `yaml:"channellen"`
  254. KickLen uint `yaml:"kicklen"`
  255. MonitorEntries uint `yaml:"monitor-entries"`
  256. NickLen uint `yaml:"nicklen"`
  257. TopicLen uint `yaml:"topiclen"`
  258. WhowasEntries uint `yaml:"whowas-entries"`
  259. LineLen LineLenConfig `yaml:"linelen"`
  260. }
  261. Dnsbl DnsblConfig
  262. Fakelag FakelagConfig
  263. Filename string
  264. }
  265. // OperClass defines an assembled operator class.
  266. type OperClass struct {
  267. Title string
  268. WhoisLine string `yaml:"whois-line"`
  269. Capabilities map[string]bool // map to make lookups much easier
  270. }
  271. // OperatorClasses returns a map of assembled operator classes from the given config.
  272. func (conf *Config) OperatorClasses() (*map[string]OperClass, error) {
  273. ocs := make(map[string]OperClass)
  274. // loop from no extends to most extended, breaking if we can't add any more
  275. lenOfLastOcs := -1
  276. for {
  277. if lenOfLastOcs == len(ocs) {
  278. return nil, ErrOperClassDependencies
  279. }
  280. lenOfLastOcs = len(ocs)
  281. var anyMissing bool
  282. for name, info := range conf.OperClasses {
  283. _, exists := ocs[name]
  284. _, extendsExists := ocs[info.Extends]
  285. if exists {
  286. // class already exists
  287. continue
  288. } else if len(info.Extends) > 0 && !extendsExists {
  289. // class we extend on doesn't exist
  290. _, exists := conf.OperClasses[info.Extends]
  291. if !exists {
  292. return nil, fmt.Errorf("Operclass [%s] extends [%s], which doesn't exist", name, info.Extends)
  293. }
  294. anyMissing = true
  295. continue
  296. }
  297. // create new operclass
  298. var oc OperClass
  299. oc.Capabilities = make(map[string]bool)
  300. // get inhereted info from other operclasses
  301. if len(info.Extends) > 0 {
  302. einfo, _ := ocs[info.Extends]
  303. for capab := range einfo.Capabilities {
  304. oc.Capabilities[capab] = true
  305. }
  306. }
  307. // add our own info
  308. oc.Title = info.Title
  309. for _, capab := range info.Capabilities {
  310. oc.Capabilities[capab] = true
  311. }
  312. if len(info.WhoisLine) > 0 {
  313. oc.WhoisLine = info.WhoisLine
  314. } else {
  315. oc.WhoisLine = "is a"
  316. if strings.Contains(strings.ToLower(string(oc.Title[0])), "aeiou") {
  317. oc.WhoisLine += "n"
  318. }
  319. oc.WhoisLine += " "
  320. oc.WhoisLine += oc.Title
  321. }
  322. ocs[name] = oc
  323. }
  324. if !anyMissing {
  325. // we've got every operclass!
  326. break
  327. }
  328. }
  329. return &ocs, nil
  330. }
  331. // Oper represents a single assembled operator's config.
  332. type Oper struct {
  333. Class *OperClass
  334. WhoisLine string
  335. Vhost string
  336. Pass []byte
  337. Modes string
  338. }
  339. // Operators returns a map of operator configs from the given OperClass and config.
  340. func (conf *Config) Operators(oc *map[string]OperClass) (map[string]Oper, error) {
  341. operators := make(map[string]Oper)
  342. for name, opConf := range conf.Opers {
  343. var oper Oper
  344. // oper name
  345. name, err := CasefoldName(name)
  346. if err != nil {
  347. return nil, fmt.Errorf("Could not casefold oper name: %s", err.Error())
  348. }
  349. oper.Pass = opConf.PasswordBytes()
  350. oper.Vhost = opConf.Vhost
  351. class, exists := (*oc)[opConf.Class]
  352. if !exists {
  353. return nil, fmt.Errorf("Could not load operator [%s] - they use operclass [%s] which does not exist", name, opConf.Class)
  354. }
  355. oper.Class = &class
  356. if len(opConf.WhoisLine) > 0 {
  357. oper.WhoisLine = opConf.WhoisLine
  358. } else {
  359. oper.WhoisLine = class.WhoisLine
  360. }
  361. oper.Modes = strings.TrimSpace(opConf.Modes)
  362. // successful, attach to list of opers
  363. operators[name] = oper
  364. }
  365. return operators, nil
  366. }
  367. // TLSListeners returns a list of TLS listeners and their configs.
  368. func (conf *Config) TLSListeners() map[string]*tls.Config {
  369. tlsListeners := make(map[string]*tls.Config)
  370. for s, tlsListenersConf := range conf.Server.TLSListeners {
  371. config, err := tlsListenersConf.Config()
  372. if err != nil {
  373. log.Fatal(err)
  374. }
  375. config.ClientAuth = tls.RequestClientCert
  376. tlsListeners[s] = config
  377. }
  378. return tlsListeners
  379. }
  380. // LoadConfig loads the given YAML configuration file.
  381. func LoadConfig(filename string) (config *Config, err error) {
  382. data, err := ioutil.ReadFile(filename)
  383. if err != nil {
  384. return nil, err
  385. }
  386. err = yaml.Unmarshal(data, &config)
  387. if err != nil {
  388. return nil, err
  389. }
  390. config.Filename = filename
  391. // we need this so PasswordBytes returns the correct info
  392. if config.Server.Password != "" {
  393. config.Server.PassConfig.Password = config.Server.Password
  394. }
  395. if config.Network.Name == "" {
  396. return nil, ErrNetworkNameMissing
  397. }
  398. if config.Server.Name == "" {
  399. return nil, ErrServerNameMissing
  400. }
  401. if !utils.IsHostname(config.Server.Name) {
  402. return nil, ErrServerNameNotHostname
  403. }
  404. if config.Datastore.Path == "" {
  405. return nil, ErrDatastorePathMissing
  406. }
  407. if len(config.Server.Listen) == 0 {
  408. return nil, ErrNoListenersDefined
  409. }
  410. if config.Limits.NickLen < 1 || config.Limits.ChannelLen < 2 || config.Limits.AwayLen < 1 || config.Limits.KickLen < 1 || config.Limits.TopicLen < 1 {
  411. return nil, ErrLimitsAreInsane
  412. }
  413. if config.Server.STS.Enabled {
  414. config.Server.STS.Duration, err = custime.ParseDuration(config.Server.STS.DurationString)
  415. if err != nil {
  416. return nil, fmt.Errorf("Could not parse STS duration: %s", err.Error())
  417. }
  418. if config.Server.STS.Port < 0 || config.Server.STS.Port > 65535 {
  419. return nil, fmt.Errorf("STS port is incorrect, should be 0 if disabled: %d", config.Server.STS.Port)
  420. }
  421. }
  422. if config.Server.ConnectionThrottler.Enabled {
  423. config.Server.ConnectionThrottler.Duration, err = time.ParseDuration(config.Server.ConnectionThrottler.DurationString)
  424. if err != nil {
  425. return nil, fmt.Errorf("Could not parse connection-throttle duration: %s", err.Error())
  426. }
  427. config.Server.ConnectionThrottler.BanDuration, err = time.ParseDuration(config.Server.ConnectionThrottler.BanDurationString)
  428. if err != nil {
  429. return nil, fmt.Errorf("Could not parse connection-throttle ban-duration: %s", err.Error())
  430. }
  431. }
  432. // process webirc blocks
  433. var newWebIRC []webircConfig
  434. for _, webirc := range config.Server.WebIRC {
  435. // skip webirc blocks with no hosts (such as the example one)
  436. if len(webirc.Hosts) == 0 {
  437. continue
  438. }
  439. err = webirc.Populate()
  440. if err != nil {
  441. return nil, fmt.Errorf("Could not parse WebIRC config: %s", err.Error())
  442. }
  443. newWebIRC = append(newWebIRC, webirc)
  444. }
  445. config.Server.WebIRC = newWebIRC
  446. for id, list := range config.Dnsbl.Lists {
  447. actionType, err := config.DnsblTypes(list.Action)
  448. if err != nil {
  449. return nil, err
  450. }
  451. reason := list.Reason
  452. var newDnsblListReply = make(map[string]DnsblListReply)
  453. for key, reply := range list.Reply {
  454. if reply.Action == "" {
  455. reply.ActionType = actionType
  456. }
  457. if reply.Reason == "" {
  458. reply.Reason = reason
  459. }
  460. for _, newKey := range strings.Split(key, ",") {
  461. newDnsblListReply[newKey] = reply
  462. }
  463. }
  464. config.Dnsbl.Lists[id].ActionType = actionType
  465. config.Dnsbl.Lists[id].Reply = newDnsblListReply
  466. }
  467. // process limits
  468. if config.Limits.LineLen.Tags < 512 || config.Limits.LineLen.Rest < 512 {
  469. return nil, ErrLineLengthsTooSmall
  470. }
  471. var newLogConfigs []logger.LoggingConfig
  472. for _, logConfig := range config.Logging {
  473. // methods
  474. methods := make(map[string]bool)
  475. for _, method := range strings.Split(logConfig.Method, " ") {
  476. if len(method) > 0 {
  477. methods[strings.ToLower(method)] = true
  478. }
  479. }
  480. if methods["file"] && logConfig.Filename == "" {
  481. return nil, ErrLoggerFilenameMissing
  482. }
  483. logConfig.MethodFile = methods["file"]
  484. logConfig.MethodStdout = methods["stdout"]
  485. logConfig.MethodStderr = methods["stderr"]
  486. // levels
  487. level, exists := logger.LogLevelNames[strings.ToLower(logConfig.LevelString)]
  488. if !exists {
  489. return nil, fmt.Errorf("Could not translate log leve [%s]", logConfig.LevelString)
  490. }
  491. logConfig.Level = level
  492. // types
  493. for _, typeStr := range strings.Split(logConfig.TypeString, " ") {
  494. if len(typeStr) == 0 {
  495. continue
  496. }
  497. if typeStr == "-" {
  498. return nil, ErrLoggerExcludeEmpty
  499. }
  500. if typeStr[0] == '-' {
  501. typeStr = typeStr[1:]
  502. logConfig.ExcludedTypes = append(logConfig.ExcludedTypes, typeStr)
  503. } else {
  504. logConfig.Types = append(logConfig.Types, typeStr)
  505. }
  506. }
  507. if len(logConfig.Types) < 1 {
  508. return nil, ErrLoggerHasNoTypes
  509. }
  510. newLogConfigs = append(newLogConfigs, logConfig)
  511. }
  512. config.Logging = newLogConfigs
  513. // hardcode this for now
  514. config.Accounts.Registration.EnabledCredentialTypes = []string{"passphrase", "certfp"}
  515. for i, name := range config.Accounts.Registration.EnabledCallbacks {
  516. if name == "none" {
  517. // we store "none" as "*" internally
  518. config.Accounts.Registration.EnabledCallbacks[i] = "*"
  519. }
  520. }
  521. maxSendQBytes, err := bytefmt.ToBytes(config.Server.MaxSendQString)
  522. if err != nil {
  523. return nil, fmt.Errorf("Could not parse maximum SendQ size (make sure it only contains whole numbers): %s", err.Error())
  524. }
  525. config.Server.MaxSendQBytes = int(maxSendQBytes)
  526. // get language files
  527. config.Languages.Data = make(map[string]languages.LangData)
  528. if config.Languages.Enabled {
  529. files, err := ioutil.ReadDir(config.Languages.Path)
  530. if err != nil {
  531. return nil, fmt.Errorf("Could not load language files: %s", err.Error())
  532. }
  533. for _, f := range files {
  534. // skip dirs
  535. if f.IsDir() {
  536. continue
  537. }
  538. // only load core .lang.yaml files, and ignore help/irc files
  539. name := f.Name()
  540. lowerName := strings.ToLower(name)
  541. if !strings.HasSuffix(lowerName, ".lang.yaml") {
  542. continue
  543. }
  544. // don't load our example files in practice
  545. if strings.HasPrefix(lowerName, "example") {
  546. continue
  547. }
  548. // load core info file
  549. data, err = ioutil.ReadFile(filepath.Join(config.Languages.Path, name))
  550. if err != nil {
  551. return nil, fmt.Errorf("Could not load language file [%s]: %s", name, err.Error())
  552. }
  553. var langInfo languages.LangData
  554. err = yaml.Unmarshal(data, &langInfo)
  555. if err != nil {
  556. return nil, fmt.Errorf("Could not parse language file [%s]: %s", name, err.Error())
  557. }
  558. langInfo.Translations = make(map[string]string)
  559. // load actual translation files
  560. var tlList map[string]string
  561. // load irc strings file
  562. ircName := strings.TrimSuffix(name, ".lang.yaml") + "-irc.lang.json"
  563. data, err = ioutil.ReadFile(filepath.Join(config.Languages.Path, ircName))
  564. if err != nil {
  565. return nil, fmt.Errorf("Could not load language's irc file [%s]: %s", ircName, err.Error())
  566. }
  567. err = json.Unmarshal(data, &tlList)
  568. if err != nil {
  569. return nil, fmt.Errorf("Could not parse language's irc file [%s]: %s", ircName, err.Error())
  570. }
  571. for key, value := range tlList {
  572. // because of how crowdin works, this is how we skip untranslated lines
  573. if key == value || value == "" {
  574. continue
  575. }
  576. langInfo.Translations[key] = value
  577. }
  578. // load help strings file
  579. helpName := strings.TrimSuffix(name, ".lang.yaml") + "-help.lang.json"
  580. data, err = ioutil.ReadFile(filepath.Join(config.Languages.Path, helpName))
  581. if err != nil {
  582. return nil, fmt.Errorf("Could not load language's help file [%s]: %s", helpName, err.Error())
  583. }
  584. err = json.Unmarshal(data, &tlList)
  585. if err != nil {
  586. return nil, fmt.Errorf("Could not parse language's help file [%s]: %s", helpName, err.Error())
  587. }
  588. for key, value := range tlList {
  589. // because of how crowdin works, this is how we skip untranslated lines
  590. if key == value || value == "" {
  591. continue
  592. }
  593. langInfo.Translations[key] = value
  594. }
  595. // confirm that values are correct
  596. if langInfo.Code == "en" {
  597. return nil, fmt.Errorf("Cannot have language file with code 'en' (this is the default language using strings inside the server code). If you're making an English variant, name it with a more specific code")
  598. }
  599. if langInfo.Code == "" || langInfo.Name == "" || langInfo.Contributors == "" {
  600. return nil, fmt.Errorf("Code, name or contributors is empty in language file [%s]", name)
  601. }
  602. if len(langInfo.Translations) == 0 {
  603. return nil, fmt.Errorf("Language [%s / %s] contains no translations", langInfo.Code, langInfo.Name)
  604. }
  605. // check for duplicate languages
  606. _, exists := config.Languages.Data[strings.ToLower(langInfo.Code)]
  607. if exists {
  608. return nil, fmt.Errorf("Language code [%s] defined twice", langInfo.Code)
  609. }
  610. // and insert into lang info
  611. config.Languages.Data[strings.ToLower(langInfo.Code)] = langInfo
  612. }
  613. // confirm that default language exists
  614. if config.Languages.Default == "" {
  615. config.Languages.Default = "en"
  616. } else {
  617. config.Languages.Default = strings.ToLower(config.Languages.Default)
  618. }
  619. _, exists := config.Languages.Data[config.Languages.Default]
  620. if config.Languages.Default != "en" && !exists {
  621. return nil, fmt.Errorf("Cannot find default language [%s]", config.Languages.Default)
  622. }
  623. }
  624. return config, nil
  625. }