Exemplo n.º 1
0
Arquivo: room.go Projeto: beoran/woe
// Load a room from a sitef file.
func LoadRoom(dirname string, id string) (room *Room, err error) {

	path := SavePathFor(dirname, "room", id)

	records, err := sitef.ParseFilename(path)
	if err != nil {
		return nil, err
	}

	if len(records) < 1 {
		return nil, errors.New("No room found!")
	}

	record := records[0]
	monolog.Info("Loading Room record: %s %v", path, record)

	room = new(Room)
	room.Entity.LoadSitef(*record)
	/*
	   account.Name            = record.Get("name")
	   account.Hash            = record.Get("hash")
	   account.Algo            = record.Get("algo")
	   account.Email           = record.Get("email")
	   account.Points          = record.GetIntDefault("points", 0)
	   account.Privilege       = Privilege(record.GetIntDefault("privilege",
	                               int(PRIVILEGE_NORMAL)))

	   var nchars int
	   nchars                  = record.GetIntDefault("characters", 0)
	   _ = nchars
	*/
	monolog.Info("Loaded Room: %s %v", path, room)
	return room, nil
}
Exemplo n.º 2
0
func (me *Client) SetupNegotiate(millis int, command byte, option byte, yes_event telnet.EventType, no_event telnet.EventType) (bool, telnet.Event) {
	me.telnet.TelnetSendNegotiate(command, option)
	tev, timeout, close := me.TryReadEvent(millis)

	if tev == nil || timeout || close {
		monolog.Info("Timeout or close in TryReadEvent")
		return false, nil
	}

	evtype := telnet.EventTypeOf(tev)

	if evtype == no_event {
		monolog.Info("Negative event no_event %v %v %v", tev, evtype, no_event)
		return false, tev
	}

	if evtype != yes_event {
		monolog.Info("Unexpected event yes_event %v %v %v", tev, evtype, yes_event)
		return false, tev
	}

	if tev == nil {
		return false, tev
	}

	return true, tev
}
Exemplo n.º 3
0
Arquivo: woe.go Projeto: beoran/woe
func runSupervisor() (status int) {
	monolog.Setup("woe.log", true, false)
	defer monolog.Close()
	enableDisableLogs()
	monolog.Info("Starting WOE supervisor.")
	for server_restart {
		// wd  , _ := os.Getwd()
		exe := fmt.Sprintf("%s", os.Args[0])
		argp := fmt.Sprintf("-l=%s", *server_tcpip)
		argel := fmt.Sprintf("-el=%s", *enable_logs)
		argdl := fmt.Sprintf("-dl=%s", *disable_logs)
		cmd := exec.Command(exe, "-s=true", argp, argel, argdl)
		monolog.Info("Starting server %s at %s.", exe, *server_tcpip)
		cmd.Stderr = os.Stderr
		cmd.Stdout = os.Stdout
		monolog.Debug("Server command line: %s.", cmd.Args)
		err := cmd.Run()
		monolog.Info("Server at %s shut down.", *server_tcpip)
		if err != nil {
			monolog.Error("Server shut down with error %s!", err)
			server_restart = false
			return 1
		}
	}
	return 0
}
Exemplo n.º 4
0
Arquivo: being.go Projeto: beoran/woe
func (me *Being) Init(kind string, name string, privilege Privilege,
	kin Entitylike, gender Entitylike, job Entitylike) *Being {
	if me == nil {
		return me
	}
	me.Entity.InitKind(kind, name, privilege)

	realkin := EntitylikeToKin(kin)
	realgen := EntitylikeToGender(gender)
	realjob := EntitylikeToJob(job)

	monolog.Info("Init being: Kin: %v", realkin)
	monolog.Info("Init being: Gender: %v", realgen)
	monolog.Info("Init being: Job: %v", realjob)

	me.Kin = realkin
	me.Gender = realgen
	me.Job = realjob

	me.Talents.GrowFrom(BasicTalent)
	me.Talents.GrowFrom(me.Kin.Talents)
	me.Talents.GrowFrom(me.Gender.Talents)
	me.Talents.GrowFrom(me.Job.Talents)

	me.Level = 1
	me.RecalculateVitals()

	return me
}
Exemplo n.º 5
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) onConnect(conn net.Conn) (err error) {
	id, err := me.findFreeID()
	if err != nil {
		monolog.Info("Refusing connection for %s: too many clients. ", conn.RemoteAddr().String())
		conn.Close()
		return nil
	}
	monolog.Info("New client connected from %s, id %d. ", conn.RemoteAddr().String(), id)
	client := NewClient(me, id, conn)
	me.clients[id] = client
	return client.Serve()
}
Exemplo n.º 6
0
Arquivo: sitef.go Projeto: beoran/woe
func (me Record) GetStruct(prefix string, structure interface{}) {
	monolog.Info("GetStruct: structure %v, %v\n", structure,
		reflect.TypeOf(structure))

	st := reflect.TypeOf(structure).Elem()
	vt := reflect.Indirect(reflect.ValueOf(structure))
	monolog.Info("GetStruct: type %v value %v\n", st, vt)

	for i := 0; i < st.NumField(); i++ {
		field := st.Field(i)
		key := prefix + strings.ToLower(field.Name)
		value := reflect.Indirect(vt).Field(i)
		me.GetValue(key, value)
	}
}
Exemplo n.º 7
0
Arquivo: raku.go Projeto: beoran/woe
func (me *Lexer) TryLexing() {
	go me.Start()

	for token := range me.Output {
		monolog.Info("Token %s", token)
	}
}
Exemplo n.º 8
0
// Load an account from a sitef file.
func LoadAccount(dirname string, name string) (account *Account, err error) {

	path := SavePathFor(dirname, "account", name)

	records, err := sitef.ParseFilename(path)
	if err != nil {
		return nil, err
	}

	if len(records) < 1 {
		return nil, errors.New("No record found!")
	}

	record := records[0]
	monolog.Info("Loading Account record: %s %v", path, record)

	account = new(Account)
	account.Name = record.Get("name")
	account.Hash = record.Get("hash")
	account.Algo = record.Get("algo")
	account.Email = record.Get("email")
	account.Points = record.GetIntDefault("points", 0)
	account.Privilege = Privilege(record.GetIntDefault("privilege",
		int(PRIVILEGE_NORMAL)))

	nchars := record.GetIntDefault("characters", 0)
	account.characters = make([]*Character, 0, nchars)
	monolog.Info("Try to load %d characters:\n", nchars)
	for index := 0; index < nchars; index++ {

		chid := record.GetArrayIndex("characters", index)
		monolog.Info("Loading character: %d %s\n", index, chid)

		ch, err := account.LoadCharacter(dirname, chid)
		if err != nil {
			monolog.Error("Could not load character %s: %s", chid, err.Error())
			// return nil, err
		} else {
			account.characters = append(account.characters, ch)
		}
	}

	/* Todo: load characters here... */
	monolog.Info("Loaded Account: %s %v", path, account)
	return account, nil
}
Exemplo n.º 9
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) Close() {
	monolog.Info("Closing server, shutting down tickers.")

	for name, _ := range me.tickers {
		me.RemoveTicker(name)
	}

	monolog.Info("Closing server, shutting down clients.")
	for _, client := range me.clients {
		if client.IsAlive() {
			client.Close()
		}
	}

	me.handleDisconnectedClients()
	monolog.Info("Closed server.")
}
Exemplo n.º 10
0
// Check for MSDP (two way MSSP) support (but don't implement it yet)
func (me *Client) SetupMSDP() telnet.Event {

	ok, tev := me.SetupNegotiate(1000, t.TELNET_WILL, t.TELNET_TELOPT_MSDP, t.TELNET_DO_EVENT, t.TELNET_DONT_EVENT)
	if !ok {
		return tev
	}
	monolog.Info("Client %d accepts MSDP", me.id)
	me.info.msdp = true
	return nil
}
Exemplo n.º 11
0
// Negotiate COMPRESS2 support
func (me *Client) SetupCompress2() telnet.Event {
	ok, tev := me.SetupNegotiate(1000, t.TELNET_WILL, t.TELNET_TELOPT_COMPRESS2, t.TELNET_DO_EVENT, t.TELNET_DONT_EVENT)
	if !ok {
		return tev
	}

	me.telnet.TelnetBeginCompress2()
	monolog.Info("Client #{@id} started COMPRESS2 compression")
	me.info.compress2 = true
	return tev
}
Exemplo n.º 12
0
Arquivo: world.go Projeto: beoran/woe
// Load a world from a sitef file.
func LoadWorld(dirname string, name string) (world *World, err error) {

	path := SavePathFor(dirname, "world", name)

	records, err := sitef.ParseFilename(path)
	if err != nil {
		return nil, err
	}

	if len(records) < 1 {
		return nil, errors.New("No record found!")
	}

	record := records[0]
	monolog.Info("Loading World record: %s %v", path, record)

	world = NewWorld(record.Get("name"), record.Get("motd"), dirname)
	monolog.Info("Loaded World: %s %v", path, world)
	return world, nil
}
Exemplo n.º 13
0
// Negotiate BINARY support, for sending UTF-8. This is largely a courtesy to the cliennt,
// since woe will send UTF-8 anyway.
func (me *Client) SetupBinary() telnet.Event {
	ok, tev := me.SetupNegotiate(1000, t.TELNET_WILL, t.TELNET_TELOPT_BINARY, t.TELNET_DO_EVENT, t.TELNET_DONT_EVENT)
	if !ok {
		me.info.binary = false
		return tev
	}

	monolog.Info("Client #{@id} accepts BINARY")
	me.info.binary = true
	return tev
}
Exemplo n.º 14
0
// negotiate suppress go ahead
func (me *Client) SetupSupressGA() telnet.Event {
	ok, tev := me.SetupNegotiate(1000, t.TELNET_WILL, t.TELNET_TELOPT_SGA, t.TELNET_DO_EVENT, t.TELNET_DONT_EVENT)
	if !ok {
		me.info.binary = false
		return tev
	}

	monolog.Info("Client #{@id} will suppress GA")
	me.info.sga = true
	return tev

}
Exemplo n.º 15
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) handleDisconnectedClients() {
	for me.alive {
		time.Sleep(1)
		for id, client := range me.clients {
			if !client.IsAlive() {
				monolog.Info("Client %d has disconnected.", client.id)
				client.Close()
				delete(me.clients, id)
			}
		}
	}
}
Exemplo n.º 16
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) SetupWorld() (err error) {
	me.World, err = world.LoadWorld(me.DataPath(), "WOE")

	if err != nil {
		monolog.Error("Could not load world WOE: %s", err)
		return err
	}

	if me.World == nil {
		monolog.Info("Creating new default world...")
		me.World = world.NewWorld("WOE", DEFAULT_MOTD, me.DataPath())
		err := me.World.Save(me.DataPath())
		if err != nil {
			monolog.Error("Could not save world: %v", err)
			return err
		} else {
			monolog.Info("Saved default world.")
		}
	}
	return nil
}
Exemplo n.º 17
0
Arquivo: item.go Projeto: beoran/woe
// Load an item from a sitef file.
func LoadItem(dirname string, id string) (item *Item, err error) {

	path := SavePathFor(dirname, "item", id)

	records, err := sitef.ParseFilename(path)
	if err != nil {
		return nil, err
	}

	if len(records) < 1 {
		return nil, errors.New("No item found!")
	}

	record := records[0]
	monolog.Info("Loading Item record: %s %v", path, record)

	item = new(Item)
	item.Entity.LoadSitef(*record)
	item.Quality = record.GetIntDefault("quality", 0)
	item.Price = record.GetIntDefault("price", -1)
	item.Level = record.GetIntDefault("level", -1)
	item.Kind = ItemKind(record.Get("kind"))
	item.Damage = DamageKind(record.Get("damage"))
	item.Equip = EquipWhere(record.Get("equip"))
	item.Upgrade = record.Get("upgrade")
	item.Degrade = record.Get("degrade")
	item.Teaches = record.Get("teaches")
	item.Craft = record.Get("craft")

	ningredients := record.GetIntDefault("ingredients", 0)

	for i := 0; i < ningredients; i++ {
		ingr := record.GetArrayIndex("ingredients", i)
		item.Ingredients = append(item.Ingredients, ingr)
	}

	monolog.Info("Loaded Item: %s %v", path, item)
	return item, nil
}
Exemplo n.º 18
0
// Deletes the character itself from disk
func (me *Character) Delete(dirname string) bool {
	path := SavePathFor(dirname, "character", me.ID)

	if err := os.Remove(path); err != nil {
		monolog.Warning("Could not delete character: %v %s: %s",
			me, path, err.Error())
		return false
	}

	me.Account = nil
	monolog.Info("Character deleted: %s", me.ID)
	return true
}
Exemplo n.º 19
0
Arquivo: woe.go Projeto: beoran/woe
func runServer() (status int) {
	monolog.Setup("woe.log", true, false)
	defer monolog.Close()
	enableDisableLogs()
	monolog.Info("Starting WOE server...")
	monolog.Info("Server will run at %s.", *server_tcpip)
	woe, err := server.NewServer(*server_tcpip)
	if err != nil {
		monolog.Error("Could not initialize server!")
		monolog.Error(err.Error())
		panic(err)
	}
	monolog.Info("Server at %s init ok.", *server_tcpip)
	defer woe.Close()
	status, err = woe.Serve()
	if err != nil {
		monolog.Error("Error while running WOE server!")
		monolog.Error(err.Error())
		panic(err)
	}
	monolog.Info("Server shut down without error indication.", *server_tcpip)
	return status
}
Exemplo n.º 20
0
Arquivo: server.go Projeto: beoran/woe
func NewServer(address string) (server *Server, err error) {
	listener, err := net.Listen("tcp", address)
	if err != nil {
		return nil, err
	}

	monolog.Info("Server listening on %s.", address)

	clients := make(map[int]*Client)
	tickers := make(map[string]*Ticker)

	server = &Server{address, listener, clients, tickers, true, nil, STATUS_RESTART}
	err = server.SetupWorld()
	if err != nil {
		monolog.Error("Could not set up or load world!")
		return nil, err
	}

	monolog.Info("Server world set up.")
	server.AddDefaultTickers()
	monolog.Info("Tickers set up.")

	return server, err
}
Exemplo n.º 21
0
// Negotiate NAWS (window size) support
func (me *Client) SetupNAWS() telnet.Event {
	ok, tev := me.SetupNegotiate(1000, t.TELNET_DO, t.TELNET_TELOPT_NAWS, t.TELNET_WILL_EVENT, t.TELNET_WONT_EVENT)
	if !ok {
		return tev
	}

	tev2, _, _ := me.TryReadEvent(1000)
	if (tev2 == nil) || (!telnet.IsEventType(tev2, t.TELNET_NAWS_EVENT)) {
		return tev2
	}

	nawsevent, ok := tev2.(*telnet.NAWSEvent)
	if ok {
		me.info.w = nawsevent.W
		me.info.h = nawsevent.H
		monolog.Info("Client %d window size #{%d}x#{%d}", me.id, me.info.w, me.info.h)
		me.info.naws = true
	}
	return nil
}
Exemplo n.º 22
0
Arquivo: dialog.go Projeto: beoran/woe
func (me *Client) NewAccountDialog(login string) bool {
	for me.account == nil {
		me.Printf("\nWelcome, %s! Creating new account...\n", login)
		pass1 := me.AskPassword()

		if pass1 == nil {
			return false
		}

		pass2 := me.AskRepeatPassword()

		if pass1 == nil {
			return false
		}

		if string(pass1) != string(pass2) {
			me.Printf("\nPasswords do not match! Please try again!\n")
			continue
		}

		email := me.AskEmail()
		if email == nil {
			return false
		}

		me.account = world.NewAccount(login, string(pass1), string(email), 7)
		err := me.account.Save(me.server.DataPath())

		if err != nil {
			monolog.Error("Could not save account %s: %v", login, err)
			me.Printf("\nFailed to save your account!\nPlease contact a WOE administrator!\n")
			return false
		}

		monolog.Info("Created new account %s", login)
		me.Printf("\nSaved your account.\n")
		return true
	}
	return false
}
Exemplo n.º 23
0
// Load a character from a sitef file. Does no account checking, but returns the account name.
func LoadCharacter(dirname string, id string) (character *Character, aname string, err error) {

	path := SavePathFor(dirname, "character", id)

	records, err := sitef.ParseFilename(path)
	if err != nil {
		return nil, "", err
	}

	if len(records) < 1 {
		return nil, "", fmt.Errorf("No sitef record found for %s!", id)
	}

	record := records[0]
	monolog.Info("Loading Character record: %s %v", path, record)

	character = new(Character)
	aname = record.Get("accountname")
	character.Being.LoadSitef(*record)

	return character, aname, nil
}
Exemplo n.º 24
0
func (me *Client) ReadTelnetSetup() {
	/* First send a NOP to prod the client into activity. This shouldn't be neccesary,
	but some clients, in particular Tinyfugue waits for server activity before it sends it's own
	telnet setup messages. If the NOP is not sent, those setup messages will then end up messing up the
	telnet setup the WOE server is trying to do. Arguably this should perhaps be an AYT, but a NOP will
	hopefully do the least damage.
	*/
	msg := []byte{telnet.TELNET_IAC, telnet.TELNET_NOP}
	me.telnet.SendRaw(msg)

	for {
		// wait for incoming events.
		event, _, _ := me.TryReadEvent(500)
		if event == nil {
			monolog.Info("Client %d no telnet setup received", me.id)
			return
		}

		monolog.Info("Client %d telnet setup received: %v", me.id, event)

		switch event := event.(type) {
		case *telnet.DataEvent:
			monolog.Info("Unexpected DATA event %T : %d.", event, len(event.Data))
			// should do something with the data, maybe?
			return
		case *telnet.NAWSEvent:
			monolog.Info("Received Telnet NAWS event %T.", event)
			me.HandleNAWSEvent(event)

		case *telnet.WillEvent:
			monolog.Info("Received Telnet WILL event %T.", event)
			// XXX do something with it!

		default:
			monolog.Info("Unexpected event in setup phase %T : %v. Ignored for now.", event, event)
		}
	}
}
Exemplo n.º 25
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) Shutdown() {
	monolog.Info("Server is going to shut down.")
	me.alive = false
	me.exitstatus = STATUS_SHUTDOWN
}
Exemplo n.º 26
0
Arquivo: action.go Projeto: beoran/woe
func AddAction(name string, privilege world.Privilege, handler ActionHandler) {
	monolog.Info("Adding new action %s with privilege %d", name, privilege)
	action := Action{name, privilege, handler}
	ActionMap[name] = action
}
Exemplo n.º 27
0
Arquivo: server.go Projeto: beoran/woe
func (me *Server) Restart() {
	monolog.Info("Server is going to restart.")
	me.alive = false
	me.exitstatus = STATUS_RESTART
}
Exemplo n.º 28
0
// Negotiate MTTS/TTYPE (TERMINAL TYPE) support
func (me *Client) SetupTType() telnet.Event {
	me.info.terminals = nil
	ok, tev := me.SetupNegotiate(1000, t.TELNET_DO, t.TELNET_TELOPT_TTYPE, t.TELNET_WILL_EVENT, t.TELNET_WONT_EVENT)
	if !ok {
		return tev
	}

	var last string = "none"
	var now string = ""

	for last != now {
		last = now
		me.telnet.TelnetTTypeSend()
		var tev2 telnet.Event = nil
		// Some clients (like KildClient, but not TinTin or telnet),
		// insist on spamming useless NUL characters
		// here... So we have to retry a few times to get a ttype_is
		// throwing away any undesirable junk in between.
	GET_TTYPE:
		for index := 0; index < 3; index++ {
			tev2, _, _ = me.TryReadEvent(1000)
			if tev2 != nil {
				etyp := telnet.EventTypeOf(tev2)
				monolog.Info("Waiting for TTYPE: %T %v %d", tev2, tev2, etyp)
				if telnet.IsEventType(tev2, t.TELNET_TTYPE_EVENT) {
					monolog.Info("TTYPE received: %T %v %d", tev2, tev2, etyp)
					break GET_TTYPE
				}
			} else { // and some clients don't respond, even
				monolog.Info("Waiting for TTYPE: %T", tev2)
			}
		}

		if tev2 == nil || !telnet.IsEventType(tev2, t.TELNET_TTYPE_EVENT) {
			etyp := telnet.EventTypeOf(tev2)
			monolog.Warning("Received no TTYPE: %T %v %d", tev2, tev2, etyp)
			return tev2
		}

		ttypeevent := tev2.(*telnet.TTypeEvent)
		now = ttypeevent.Name
		if !me.HasTerminal(now) {
			me.info.terminals = append(me.info.terminals, now)
		}
		me.info.terminal = now
	}

	monolog.Info("Client %d supports terminals %v", me.id, me.info.terminals)
	monolog.Info("Client %d active terminal %v", me.id, me.info.terminal)

	//  MTTS support
	for i := range me.info.terminals {
		term := me.info.terminals[i]
		monolog.Info("Checking MTTS support: %s", term)
		if strings.HasPrefix(term, "MTTS ") {
			// it's an mtts terminal
			strnum := strings.TrimPrefix(term, "MTTS ")
			num, err := strconv.Atoi(strnum)
			if err == nil {
				me.info.mtts = num
				monolog.Info("Client %d supports mtts %d", me.id, me.info.mtts)
			} else {
				monolog.Warning("Client %d could not parse mtts %s %v", me.id, strnum, err)
			}
		}
	}
	me.info.ttype = true
	return nil
}