<commit_before>package main

import "fmt"

func main() {
	fmt.Printf("jabmud server started\n")
}
<commit_msg>jabmud-server in Go<commit_after>package main

import (
	"github.com/emgee/go-xmpp/src/xmpp"
	"log"
)

func main() {
	log.Printf("jabmud server started\n")
	log.Print("neat now with autocomplete working\n")

	jid, err := xmpp.ParseJID("jabmud.localhost")
	log.Printf("xmpp parsing: %v\n", err)
	stream, err := xmpp.NewStream("localhost:5275", nil)
	log.Printf("created stream: %v\n", err)
	X, err := xmpp.NewComponentXMPP(stream, jid, "secret")
	log.Printf("created component: %v\n", X)

	for i := range X.In {
		switch v := i.(type) {
		case error:
			log.Printf("error: %v\n", v)
		case *xmpp.Message:
			log.Printf("msg: %s says %s\n", v.From, v.Body)
			// for fun, send a response
			X.Out <- xmpp.Message{Body: "hi!", To: v.From, From: v.To}
		default:
			log.Printf("%T: %v\n", v, v)
		}
	}
}
<|endoftext|>


<commit_before>package isard

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/isard-vdi/isard/backend/model"
	"github.com/isard-vdi/isard/backend/pkg/utils"
)

type registerRsp struct {
	Role     string
	Category string
	Group    string
}

func (i *Isard) CheckRegistrationCode(u *model.User, code string) error {
	rsp, err := http.PostForm(i.url("register"), url.Values{"code": {code}})
	if err != nil {
		i.sugar.Errorw("check registration code",
			"err", err,
			"id", u.ID(),
			"code", code,
		)

		return fmt.Errorf("check registration code: %w", err)
	}

	if rsp.StatusCode != 200 {
		if rsp.StatusCode == http.StatusNotFound {
			return errors.New("invalid registration code")
		}

		b, _ := ioutil.ReadAll(rsp.Body)

		err = utils.NewHTTPCodeErr(rsp.StatusCode)
		i.sugar.Errorw("check registration code",
			"err", err,
			"code", rsp.StatusCode,
			"body", string(b),
			"id", u.ID(),
		)

		return fmt.Errorf("check registration code: %w", err)
	}

	props := &registerRsp{}
	if err := json.NewDecoder(rsp.Body).Decode(props); err != nil {
		i.sugar.Errorw("check registration code: decode JSON response",
			"err", err,
			"id", u.ID(),
		)

		return fmt.Errorf("check registration code: decode JSON response: %w", err)
	}

	u.Role = props.Role
	u.Category = props.Category
	u.Group = props.Group

	return nil
}
<commit_msg>Fix backend by not allowing registration codes for other categories<commit_after>package isard

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"

	"github.com/isard-vdi/isard/backend/model"
	"github.com/isard-vdi/isard/backend/pkg/utils"
)

type registerRsp struct {
	Role     string
	Category string
	Group    string
}

func (i *Isard) CheckRegistrationCode(u *model.User, code string) error {
	rsp, err := http.PostForm(i.url("register"), url.Values{"code": {code}})
	if err != nil {
		i.sugar.Errorw("check registration code",
			"err", err,
			"id", u.ID(),
			"code", code,
		)

		return fmt.Errorf("check registration code: %w", err)
	}

	if rsp.StatusCode != 200 {
		if rsp.StatusCode == http.StatusNotFound {
			return errors.New("invalid registration code")
		}

		b, _ := ioutil.ReadAll(rsp.Body)

		err = utils.NewHTTPCodeErr(rsp.StatusCode)
		i.sugar.Errorw("check registration code",
			"err", err,
			"code", rsp.StatusCode,
			"body", string(b),
			"id", u.ID(),
		)

		return fmt.Errorf("check registration code: %w", err)
	}

	props := &registerRsp{}
	if err := json.NewDecoder(rsp.Body).Decode(props); err != nil {
		i.sugar.Errorw("check registration code: decode JSON response",
			"err", err,
			"id", u.ID(),
		)

		return fmt.Errorf("check registration code: decode JSON response: %w", err)
	}

	if u.Category != props.Category {
		return errors.New("check registration code: registration code from other category")
	}

	u.Role = props.Role
	u.Group = props.Group

	return nil
}
<|endoftext|>