Пример #1
0
func main() {
	quit := make(chan bool, 1)
	config := irc.Config{
		Host: "chat.freenode.net",

		Nick:     "goirc",
		User:     "******",
		RealName: "goirc",

		Init: func(hr irc.HandlerRegistry) {
			fmt.Println("init")
			hr.AddHandler(irc.CONNECTED, h_LoggedIn)
			hr.AddHandler(irc.DISCONNECTED, func(*irc.Conn, irc.Line) {
				fmt.Println("disconnected")
				quit <- true
			})
			hr.AddHandler("PRIVMSG", h_PRIVMSG)
			hr.AddHandler(irc.ACTION, h_ACTION)
		},
	}

	fmt.Println("Connecting")
	if _, err := irc.Connect(config); err != nil {
		fmt.Println("error:", err)
		quit <- true
	}

	<-quit
	fmt.Println("Goodbye")
}
Пример #2
0
func (b *Bot) Run() (err error) {
	log.Println("connecting...")
	if b.Conn, err = irc.Connect(b.IrcConfig); err != nil {
		close(b.quit)
		return err
	}

	for _, m := range b.Mods {
		if err = m.Init(b, b.Conn); err != nil {
			return
		}
	}

	<-b.quit
	log.Println("goodbye.")

	return
}
Пример #3
0
func main() {
	config := checkConfig()

	if config.Server == "" {
		fmt.Fprintln(os.Stderr, "error: No valid server found in config.yaml")
		os.Exit(1)
	} else if config.Nick == "" || config.User == "" || config.RealName == "" {
		fmt.Fprintln(os.Stderr, "error: No valid user data found in config.yaml")
		os.Exit(1)
	} else if config.Plugins != nil && len(config.Plugins) == 0 {
		fmt.Fprintln(os.Stderr, "warning: You have no plugins enabled. This bot will do nothing.")
	}

	signals := make(chan os.Signal, 1)
	signal.Notify(signals, os.Interrupt)
	interrupt := make(chan struct{}, 1)
	go func() {
		for {
			sig := <-signals
			if sig == os.Interrupt {
				interrupt <- struct{}{}
			}
		}
	}()

	if err := plugin.InvokeInit(config.Plugins, config.PluginConfig); err != nil {
		fmt.Println("error in plugin init:", err)
		plugin.InvokeTeardown()
		return
	}

	autojoin := config.AutoJoin

	discon := make(chan struct{}, 1)
	var stdin *Stdin
	for {
		config := irc.Config{
			Host: config.Server,
			Port: config.Port,
			SSL:  config.UseSSL,

			Nick:     config.Nick,
			User:     config.User,
			RealName: config.RealName,
			Password: config.ServerPass,

			Init: func(reg irc.HandlerRegistry) {
				fmt.Println("Bot started")
				reg.AddHandler(irc.CONNECTED, func(conn *irc.Conn, line irc.Line) {
					fmt.Println("Connected")
					if len(autojoin) > 0 {
						conn.Join(autojoin, nil)
					} else {
						fmt.Println("No channels configured to autojoin")
					}
				})

				reg.AddHandler(irc.DISCONNECTED, func(conn *irc.Conn, line irc.Line) {
					discon <- struct{}{}
				})

				reg.AddHandler("PRIVMSG", func(conn *irc.Conn, line irc.Line) {
					dst := line.Args[0]

					if dst == conn.Me().Nick {
						fmt.Println(line.Raw)
					}
				})

				reg.AddHandler("NOTICE", func(conn *irc.Conn, line irc.Line) {
					dst := line.Args[0]

					if dst == conn.Me().Nick {
						fmt.Println(line.Raw)
					}
				})

				reg.AddHandler("JOIN", func(conn *irc.Conn, line irc.Line) {
					if line.SrcIsMe() {
						fmt.Printf("! Channel %s joined\n", line.Args[0])
					}
				})

				reg.AddHandler("PART", func(conn *irc.Conn, line irc.Line) {
					if line.SrcIsMe() {
						fmt.Printf("! Channel %s left\n", line.Args[0])
					}
				})

				reg.AddHandler(irc.CTCP, func(conn *irc.Conn, line irc.Line) {
					fmt.Printf("Received CTCP[%s] from %s [%s]: %s\n", line.Args[0], line.Src.Nick, line.Src.Ident(), append(line.Args[1:len(line.Args)], "")[0])
					if line.Args[0] == "VERSION" {
						plugin.Conn(conn).CTCPReply(line.Src.Nick, "VERSION", "voidbot powered by github.com/kballard/goirc")
					} else {
						conn.DefaultCTCPHandler(line)
					}
				})

				plugin.InvokeNewConnection(reg)
			},
		}

		fmt.Println("Connecting...")
		conn, err := irc.Connect(config)
		if err != nil {
			fmt.Println("error:", err)
			plugin.InvokeTeardown()
			return
		}

		if stdin != nil {
			stdin.ReplaceConn(conn)
		} else {
			stdin = NewStdin(conn)
			go stdin.Run(interrupt)
		}

		dcsent := false
	loop:
		for {
			select {
			case <-interrupt:
				if dcsent {
					break loop
				}
				dcsent = true
				fmt.Println("Quitting...")
				if !conn.Quit("Quitting...") {
					break loop
				}
			case <-discon:
				break loop
			}
		}

		plugin.InvokeDisconnected()

		if dcsent {
			break
		}
	}

	plugin.InvokeTeardown()

	fmt.Println("Goodbye")
}