コード例 #1
0
ファイル: cmd_test.go プロジェクト: ChimeraCoder/gopherbot
func TestAdd(t *testing.T) {
	c := new(Command)
	c.Name = "add"
	c.Params = []Param{
		{Name: "a", Pattern: RegDecimal},
		{Name: "b", Pattern: RegDecimal},
	}
	c.Execute = func(cmd *Command, c *proto.Client, m *proto.Message) {
		c.PrivMsg(m.SenderName, "%f", cmd.Params[0].F64(0)+cmd.Params[1].F64(0))
	}
	Register(c)

	var buf bytes.Buffer
	client := proto.NewClient(func(p []byte) error {
		_, err := buf.Write(p)
		return err
	})

	client.Bind(proto.CmdPrivMsg, func(c *proto.Client, m *proto.Message) {
		if !Parse(Prefix, c, m) {
			t.Fatalf("%s", buf.String())
		}
	})

	client.Read(":[email protected] PRIVMSG bob :?ADD 1 2")
}
コード例 #2
0
ファイル: main.go プロジェクト: ChimeraCoder/gopherbot
// setup initializes the application.
func setup() (*net.Conn, *proto.Client) {
	// parse commandline arguments and create configuration.
	config = parseArgs()

	log.Printf("Connecting to %s...", config.Address)

	// Open connection to server.
	conn, err := net.Dial(config.Address, config.SSLCert, config.SSLKey)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Dial: %v\n", err)
		os.Exit(1)
	}

	log.Println("Connection established.")

	// Create client protocol.
	client := proto.NewClient(func(p []byte) error {
		_, err := conn.Write(p)
		return err
	})

	// Initialize plugins.
	err = plugin.Load(config.Profile, client)
	if err != nil {
		log.Fatal(err)
	}

	// Inform command package of our user whitelist.
	cmd.SetWhitelist(config.Whitelist)

	// Bind protocol handlers and commands.
	bind(client)

	return conn, client
}
コード例 #3
0
ファイル: cmd_test.go プロジェクト: ChimeraCoder/gopherbot
func TestHelp(t *testing.T) {
	c := new(Command)
	c.Name = "help"
	c.Execute = func(cmd *Command, c *proto.Client, m *proto.Message) {}
	Register(c)

	var buf bytes.Buffer
	client := proto.NewClient(func(p []byte) error {
		_, err := buf.Write(p)
		return err
	})

	client.Bind(proto.CmdPrivMsg, func(c *proto.Client, m *proto.Message) {
		if !Parse(Prefix, c, m) {
			t.Fatalf("%s", buf.String())
		}
	})

	client.Read(":[email protected] PRIVMSG bob :?help")
}