Example #1
0
// Add channel metadata (channel_info table from SQLite) by reading the SQLite database.
func populateChannelInfoFromDatabase(server *Server, c *Channel, db *sql.DB) error {
	stmt, err := db.Prepare("SELECT value FROM channel_info WHERE server_id=? AND channel_id=? AND key=?")
	if err != nil {
		return err
	}

	// Fetch description
	rows, err := stmt.Query(server.Id, c.Id, ChannelInfoDescription)
	if err != nil {
		return err
	}
	for rows.Next() {
		var description string
		err = rows.Scan(&description)
		if err != nil {
			return err
		}

		if len(description) > 0 {
			key, err := blobstore.Put([]byte(description))
			if err != nil {
				return err
			}
			c.DescriptionBlob = key
		}
	}

	// Fetch position
	rows, err = stmt.Query(server.Id, c.Id, ChannelInfoPosition)
	if err != nil {
		return err
	}
	for rows.Next() {
		var pos int
		if err := rows.Scan(&pos); err != nil {
			return err
		}

		c.Position = pos
	}

	return nil
}
Example #2
0
func populateUsers(server *Server, db *sql.DB) (err error) {
	// Populate the server with regular user data
	stmt, err := db.Prepare("SELECT user_id, name, pw, lastchannel, texture, strftime('%s', last_active) FROM users WHERE server_id=?")
	if err != nil {
		return
	}

	rows, err := stmt.Query(server.Id)
	if err != nil {
		return
	}

	for rows.Next() {
		var (
			UserId       int64
			UserName     string
			SHA1Password string
			LastChannel  int
			Texture      []byte
			LastActive   int64
		)

		err = rows.Scan(&UserId, &UserName, &SHA1Password, &LastChannel, &Texture, &LastActive)
		if err != nil {
			continue
		}

		if UserId == 0 {
			server.cfg.Set("SuperUserPassword", "sha1$$"+SHA1Password)
		}

		user, err := NewUser(uint32(UserId), UserName)
		if err != nil {
			return err
		}

		if len(Texture) > 0 {
			key, err := blobstore.Put(Texture)
			if err != nil {
				return err
			}
			user.TextureBlob = key
		}

		user.LastActive = uint64(LastActive)
		user.LastChannelId = LastChannel

		server.Users[user.Id] = user
	}

	stmt, err = db.Prepare("SELECT key, value FROM user_info WHERE server_id=? AND user_id=?")
	if err != nil {
		return
	}

	// Populate users with any new-style UserInfo records
	for uid, user := range server.Users {
		rows, err = stmt.Query(server.Id, uid)
		if err != nil {
			return err
		}

		for rows.Next() {
			var (
				Key   int
				Value string
			)

			err = rows.Scan(&Key, &Value)
			if err != nil {
				return err
			}

			switch Key {
			case UserInfoEmail:
				user.Email = Value
			case UserInfoComment:
				key, err := blobstore.Put([]byte(Value))
				if err != nil {
					return err
				}
				user.CommentBlob = key
			case UserInfoHash:
				user.CertHash = Value
			case UserInfoLastActive:
				// not a kv-pair (trigger)
			case UserInfoPassword:
				// not a kv-pair
			case UserInfoName:
				// not a kv-pair
			}
		}
	}

	return
}