Beispiel #1
0
func main() {
	flag.Usage = usage
	flag.Parse()

	narg := flag.NArg()

	if narg < 2 || narg > 3 {
		usage()
		os.Exit(1)
	}

	db, err := ndb.Open(*ndbfile)

	if err != nil {
		fmt.Fprint(os.Stderr, err)
		os.Exit(1)
	}

	records := db.Search(flag.Arg(0), flag.Arg(1))

	switch narg {
	case 2:
		// print all attributes
		for _, rec := range records {
			for _, tuple := range rec.Tuples {
				fmt.Printf("%s=%s ", tuple.Attr, tuple.Val)
			}
			fmt.Print("\n")
		}

	case 3:
		// only print rattr
		for _, rec := range records {
			for _, tuple := range rec.Tuples {
				if tuple.Attr == flag.Arg(2) {
					fmt.Printf("%s\n", tuple.Val)
				}
			}
		}
	}

}
Beispiel #2
0
func NewBot(conf string) (*Bot, error) {
	var err error

	bot := &Bot{
		Mods: make(map[string]Module),
		quit: make(chan bool, 1),
	}

	bot.LoginFn = func(conn *irc.Conn, line irc.Line) {
		for _, c := range bot.Channels {
			conn.Join([]string{c}, nil)
		}
	}

	bot.PrivmsgFn = func(conn *irc.Conn, l irc.Line) {
		log.Printf("[%s] %s> %s\n", l.Args[0], l.Src, l.Args[1])
		/*
			if l.Args[1] == ".quit" {
				conn.Quit("quit")
				return
			}
		*/

		args := strings.Split(l.Args[1], " ")
		cmd := args[0]

		args = args[1:]

		if cmd == "" {
			return
		}

		if !strings.HasPrefix(cmd, bot.Magic) {
			return
		}

		cmd = strings.TrimPrefix(cmd, bot.Magic)

		hk, ok := bot.hooks[cmd]
		if !ok {
			return
		}

		var sender string

		if l.Args[0][0] == '#' {
			sender = l.Args[0]
		} else {
			sender = l.Src.String()
		}

		if err := hk(bot, sender, cmd, args...); err != nil {
			bot.Conn.Privmsg(sender, err.Error())
		}
	}

	bot.ActionFn = func(conn *irc.Conn, line irc.Line) {
		log.Printf("[%s] %s %s\n", line.Dst, line.Src, line.Args[0])
	}

	bot.hooks = make(map[string]HookFn)

	config, err := ndb.Open(conf)
	if err != nil {
		log.Fatalf("cannot open config file %s: %s", *configfile, err)
	}

	bot.Config = config

	ircconf := config.Search("irc", "")

	if len(ircconf) <= 0 {
		log.Fatalf("missing irc section in config %s", *configfile)
	}

	bot.IrcConfig, err = bot.parseconfig(ircconf)
	if err != nil {
		log.Fatalf("error parsing config: %s", err)
	}

	//log.Printf("bot config: %+v", bot.IrcConfig)
	log.Printf("channels: %+v", bot.Channels)

	log.Printf("modules available: %s", strings.Join(ListModules(), " "))

	var mods []string
	for n, _ := range bot.Mods {
		mods = append(mods, n)
	}

	log.Printf("modules loaded: %s", strings.Join(mods, " "))

	bot.IrcConfig.Init = func(hr irc.HandlerRegistry) {
		log.Println("initializing...")
		hr.AddHandler(irc.CONNECTED, bot.LoginFn)
		hr.AddHandler(irc.DISCONNECTED, func(*irc.Conn, irc.Line) {
			fmt.Println("disconnected")
			bot.quit <- true
		})
		hr.AddHandler("PRIVMSG", bot.PrivmsgFn)
		hr.AddHandler(irc.ACTION, bot.ActionFn)
	}

	return bot, err
}