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

	if flag.NArg() < 1 {
		ansi.Fprintf(os.Stderr, ansi.RedBold, "usage: %s [options] <server address>\n", os.Args[0])
		os.Exit(2)
	}

	addr := flag.Arg(0)
	username := *usernameP
	password := *passwordP
	debug := *debugP

	var debugWriter io.Writer
	var client *mcclient.Client
	var err error

	if debug {
		debugWriter = DebugWriter{}
	}

	if password == "" {
		if username == "" {
			username = "******"
		}

		ansi.Printf(ansi.Green, "Authenticating offline as %s...\n", username)
		client = mcclient.LoginOffline(username, debugWriter)

	} else {
		ansi.Printf(ansi.Green, "Authenticating as %s...\n", username)
		client, err = mcclient.Login(username, password, debugWriter)
		die(err)
	}

	client.StoreWorld = true
	client.HandleMessage = func(msg string) {
		matches := WhisperRegexp.FindStringSubmatch(msg)
		if matches != nil {
			ansi.Printf(ansi.YellowBold, "Message from %s: %s\n", matches[1], matches[2])
			client.Chat(fmt.Sprintf("/tell %s %s", matches[1], matches[2]))
		}

		//fmt.Printf("# %s\n", mcclient.ANSIEscapes(msg))
		//fmt.Printf("# %s\n", msg)
	}

	go func() {
		/*
			for err := range client.ErrChan {
				ansi.Printf(ansi.RedBold, "Error: %s\n", err.Error())
			}
		*/

		die(<-client.ErrChan)
	}()

	ansi.Printf(ansi.Green, "Connecting to %s...\n", addr)
	die(client.Join(addr))

	ansi.Printf(ansi.Green, "Connected!\n")

	go bot(client)

	kickMessage := client.Run()
	ansi.Printf(ansi.Green, "Disconnected: %s\n", kickMessage)
}
Beispiel #2
0
func main() {
	/*
		defer func() {
			v := recover()
			if v != nil {
				err, ok := v.(error)
				if ok {
					fmt.Printf("Error: %s\n", err.Error())
				}
			}
		}()
	*/

	if len(os.Args) < 2 {
		fmt.Printf("Not enough arguments\n\nusage: %s <server address>\n\nThis program expects the MC_USER and MC_PASSWD environment variables to be set. Otherwise, the user is logged in with an offline account.\n", os.Args[0])
		os.Exit(2)
	}

	fmt.Printf("*** Welcome to mcchat!\n")

	addr := os.Args[1]
	username := os.Getenv("MC_USER")
	password := os.Getenv("MC_PASSWD")

	var debugWriter io.Writer
	debugWriter = os.Stdout

	fmt.Printf("*** Logging in...\n")

	var err error
	var client *mcclient.Client

	if password == "" {
		if username == "" {
			username = "******"
		}

		client = mcclient.LoginOffline(username, debugWriter)

	} else {
		client, err = mcclient.Login(username, password, debugWriter)
		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
			os.Exit(1)
		}
	}

	go func() {
		err := <-client.ErrChan
		if err != nil {
			fmt.Printf("Error: %s\n", err.Error())
			client.Leave()
			client.Logout()
		}
	}()

	client.HandleMessage = func(msg string) {
		fmt.Printf("\r%s\n>", mcclient.ANSIEscapes(msg))
	}

	fmt.Printf("*** Connecting to %s...\n", addr)

	err = client.Join(addr)
	if err != nil {
		fmt.Printf("Error: %s\n", err.Error())
		client.Logout()
		os.Exit(1)
	}

	fmt.Printf("*** Connected!\n*** Type & press enter to send messages!\n*** Press Ctrl+D to exit\n\n")

	go func() {
		stdinReader := bufio.NewReader(os.Stdin)

		fmt.Printf(">")

		for {
			msg, err := stdinReader.ReadString('\n')
			if err != nil {
				if err != io.EOF {
					fmt.Printf("Error: %s\n", err.Error())
				}

				client.Leave()
				client.Logout()
				return
			}

			fmt.Printf("\x1b[1T>")
			client.Chat(msg[:len(msg)-1])
		}
	}()

	kickmsg := client.Run()
	if kickmsg != "" {
		fmt.Printf("\nKicked: %s\n", kickmsg)
	}
}