// ReadSettings reads settings file and updates settings.Global. func ReadSettings(configPath string) (err error) { // Parse config file. file := ini.New() err = file.Load(configPath) if err != nil { return errutil.Err(err) } config, settingExist := file.Sections[sectionSettings] mail, mailExist := file.Sections[sectionMail] if settingExist { err = parseSettings(config) if err != nil { return errutil.Err(err) } } if mailExist { err = parseMail(mail) if err != nil { return errutil.Err(err) } } return nil }
// Load loads configuration data from the given ini file. func (c *Config) Load(file string) (err error) { ini := ini.New() err = ini.Load(file) if err != nil { return } s := ini.Section("net") c.Address = fmt.Sprintf("%s:%d", s.S("host", ""), s.U32("port", 0)) c.SSLKey = s.S("x509-key", "") c.SSLCert = s.S("x509-cert", "") chans := s.List("channels") c.Channels = make([]*irc.Channel, len(chans)) // Parse channel definitions. A single channel comes as a string like: // // <name>,<key>,<chanservpassword> // // The name is the only required value. for i, line := range chans { elements := strings.Split(line, ",") for k := range elements { elements[k] = strings.TrimSpace(elements[k]) } if len(elements) == 0 || len(elements[0]) == 0 { continue } var ch irc.Channel ch.Name = elements[0] if len(elements) > 1 { ch.Key = elements[1] } if len(elements) > 2 { ch.ChanservPassword = elements[2] } c.Channels[i] = &ch } s = ini.Section("account") c.Nickname = s.S("nickname", "") c.ServerPassword = s.S("server-password", "") c.OperPassword = s.S("oper-password", "") c.NickservPassword = s.S("nickserv-password", "") c.QuitMessage = s.S("quit-message", "") c.CommandPrefix = ini.Section("bot").S("command-prefix", "?") c.Whitelist = ini.Section("whitelist").List("user") return }
// LoadConfig reads the ini configuration file for the given plugin. // Returns nil if the file does not exist. func (p *Base) LoadConfig() *ini.File { ini := ini.New() err := ini.Load(configPath(p.profile, p.name)) if err != nil { return nil } return ini }
func LoadConfig() (*Config, error) { file := flag.String("config", "/etc/powerrest.conf", "Configuration file path") config := &Config{} confFile := ini.New() flag.Parse() // parse command line flags err := confFile.Load(*file) if err != nil { return config, err } sect := confFile.Section("") config.DbType = sect.S("db-type", "mysql") config.DbConn = sect.S("db-connection", "") config.ListenAddr = sect.S("listen-address", ":8080") return config, nil }
// Init initializes the plugin. it loads configuration data and binds // commands and protocol handlers. func Init(profile string, c *proto.Client) { log.Println("Initializing: url") ini := ini.New() err := ini.Load(plugin.ConfigPath(profile, "url")) if err != nil { log.Fatal(err) } s := ini.Section("exclude") list := s.List("url") exclude = make([]*regexp.Regexp, len(list)) for i := range list { exclude[i], err = regexp.Compile(list[i]) if err != nil { log.Fatalf("- Invalid pattern: %s", list[i]) } } c.Bind(proto.CmdPrivMsg, parseURL) }
// ReadPages reads pages file and returns a slice of pages. func ReadPages(pagesPath string) (pages []*page.Page, err error) { // Parse pages file. file := ini.New() err = file.Load(pagesPath) if err != nil { return nil, errutil.Err(err) } // Loop through the INI sections ([section]) and parse page settings. for name, section := range file.Sections { // Skip global scope INI values since they are empty. if len(name) == 0 { continue } if settings.Verbose { fmt.Println("[o] Watching:", name) } for fieldName := range section { if _, found := siteFields[fieldName]; !found { return nil, errutil.NewNoPosf(errFieldNotExist, fieldName) } } var p page.Page var pageSettings settings.Page // Make INI section ([http://example.org]) into url.URL. p.ReqUrl, err = url.Parse(name) if err != nil { return nil, errutil.Err(err) } // Set CSS selector. pageSettings.Selection = section.S(fieldSelection, "") // Set regular expression string. pageSettings.Regexp = section.S(fieldRegexp, "") // Set "negexp" (negative regular expression) string which removes all // that matches it. pageSettings.Negexp = section.S(fieldNegexp, "") // Set threshold value. pageSettings.Threshold = section.F64(fieldThreshold, 0) // Set interval time. intervalStr := section.S(fieldInterval, settings.Global.Interval.String()) // Parse string to duration. pageSettings.Interval, err = time.ParseDuration(intervalStr) if err != nil { return nil, errutil.Err(err) } // Set individual mail address. pageSettings.RecvMail = section.S(fieldRecvMail, settings.Global.RecvMail) if pageSettings.RecvMail != "" && !strings.Contains(pageSettings.RecvMail, "@") { return nil, errutil.NewNoPosf(errInvalidMailAddress, pageSettings.RecvMail) } // Set individual header. headers := section.List(fieldHeader) m := make(map[string]string) for _, header := range headers { if strings.Contains(header, ":") { keyVal := strings.SplitN(header, ":", 2) m[strings.TrimSpace(keyVal[0])] = strings.TrimSpace(keyVal[1]) } else { return nil, errutil.NewNoPosf(errInvalidHeader, header) } } pageSettings.Header = m // Set strip functions to use. pageSettings.StripFuncs = section.List(fieldStrip) if pageSettings.StripFuncs == nil { if _, found := section[fieldStrip]; found { return nil, errutil.NewNoPosf(errInvalidListDeclaration) } } for _, stripFunc := range pageSettings.StripFuncs { if _, found := stripFunctions[stripFunc]; !found { return nil, errutil.NewNoPosf(errInvalidStripFunction, stripFunc) } } p.Settings = pageSettings pages = append(pages, &p) } if pages == nil { return nil, errutil.NewNoPosf("no pages in %s", settings.PagesPath) } return pages, nil }