Beispiel #1
0
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")
}
Beispiel #2
0
// 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
}
Beispiel #3
0
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")
}