<commit_before>package main

import (
	"encoding/binary"
	"flag"
	"fmt"
	"io"
	"os"
	"reflect"
	"regexp"
	"strings"

	"github.com/akavel/rsrc/binutil"
	"github.com/akavel/rsrc/coff"
	"github.com/akavel/rsrc/ico"
)

const (
	RT_ICON       = coff.RT_ICON
	RT_GROUP_ICON = coff.RT_GROUP_ICON
	RT_MANIFEST   = coff.RT_MANIFEST
)

// on storing icons, see: http://blogs.msdn.com/b/oldnewthing/archive/2012/07/20/10331787.aspx
type GRPICONDIR struct {
	ico.ICONDIR
	Entries []GRPICONDIRENTRY
}

func (group GRPICONDIR) Size() int64 {
	return int64(binary.Size(group.ICONDIR) + len(group.Entries)*binary.Size(group.Entries[0]))
}

type GRPICONDIRENTRY struct {
	ico.IconDirEntryCommon
	Id uint16
}

func main() {
	//TODO: allow in options advanced specification of multiple resources, as a tree (json?)
	//FIXME: verify that data file size doesn't exceed uint32 max value
	var fnamein, fnameico, fnamedata, fnameout string
	flags := flag.NewFlagSet("", flag.ContinueOnError)
	flags.StringVar(&fnamein, "manifest", "", "path to a Windows manifest file to embed")
	flags.StringVar(&fnameico, "ico", "", "path to .ico file to embed")
	flags.StringVar(&fnamedata, "data", "", "path to raw data file to embed")
	flags.StringVar(&fnameout, "o", "rsrc.syso", "name of output COFF (.res or .syso) file")
	_ = flags.Parse(os.Args[1:])
	if fnamein == "" && (fnamedata == "" || fnameout == "") {
		fmt.Fprintf(os.Stderr, "USAGE: %s -manifest FILE.exe.manifest [-ico FILE.ico] [-o FILE.syso]\n"+
			"       %s -data FILE.dat -o FILE.syso > FILE.c\n"+
			"Generates a .syso file with specified resources embedded in .rsrc section,\n"+
			"aimed for consumption by Go linker when building Win32 excecutables.\n"+
			"OPTIONS:\n",
			os.Args[0], os.Args[0])
		flags.PrintDefaults()
		os.Exit(1)
	}

	var err error
	switch {
	case fnamein != "":
		err = run(fnamein, fnameico, fnameout)
	case fnamedata != "":
		err = rundata(fnamedata, fnameout)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func rundata(fnamedata, fnameout string) error {
	if !strings.HasSuffix(fnameout, ".syso") {
		return fmt.Errorf("Output file name '%s' must end with '.syso'", fnameout)
	}
	symname := strings.TrimSuffix(fnameout, ".syso")
	ok, err := regexp.MatchString(`^[a-z0-9_]+$`, symname)
	if err != nil {
		return fmt.Errorf("Internal error: %s", err)
	}
	if !ok {
		return fmt.Errorf("Output file name '%s' must be composed of only lowercase letters (a-z), digits (0-9) and underscore (_)", fnameout)
	}

	dat, err := binutil.SizedOpen(fnamedata)
	if err != nil {
		return fmt.Errorf("Error opening data file '%s': %s", fnamedata, err)
	}
	defer dat.Close()

	coff := coff.NewRDATA()
	coff.AddData("_brsrc_"+symname, dat)
	coff.AddData("_ersrc_"+symname, io.NewSectionReader(strings.NewReader("\000\000"), 0, 2)) // TODO: why? copied from as-generated
	coff.Freeze()
	err = write(coff, fnameout)
	if err != nil {
		return err
	}

	//FIXME: output a .c file
	fmt.Println(strings.Replace(`#include "runtime.h"
extern byte _brsrc_NAME[], _ersrc_NAME;

/* func get_NAME() []byte */
void ·get_NAME(Slice a) {
  a.array = _brsrc_NAME;
  a.len = a.cap = &_ersrc_NAME - _brsrc_NAME;
  FLUSH(&a);
}`, "NAME", symname, -1))

	return nil
}

func run(fnamein, fnameico, fnameout string) error {
	manifest, err := binutil.SizedOpen(fnamein)
	if err != nil {
		return fmt.Errorf("Error opening manifest file '%s': %s", fnamein, err)
	}
	defer manifest.Close()

	var icons []ico.ICONDIRENTRY
	var iconsf *os.File
	if fnameico != "" {
		iconsf, err = os.Open(fnameico)
		if err != nil {
			return err
		}
		defer iconsf.Close()
		icons, err = ico.DecodeHeaders(iconsf)
		if err != nil {
			return err
		}
	}

	newid := make(chan uint16)
	go func() {
		for i := uint16(1); ; i++ {
			newid <- i
		}
	}()

	coff := coff.NewRSRC()

	coff.AddResource(RT_MANIFEST, <-newid, manifest)

	if len(icons) > 0 {
		// RT_ICONs
		group := GRPICONDIR{ICONDIR: ico.ICONDIR{
			Reserved: 0, // magic num.
			Type:     1, // magic num.
			Count:    uint16(len(icons)),
		}}
		for _, icon := range icons {
			id := <-newid
			r := io.NewSectionReader(iconsf, int64(icon.ImageOffset), int64(icon.BytesInRes))

			coff.AddResource(RT_ICON, id, r)
			group.Entries = append(group.Entries, GRPICONDIRENTRY{icon.IconDirEntryCommon, id})
		}

		// RT_GROUP_ICON
		coff.AddResource(RT_GROUP_ICON, <-newid, group)
	}

	coff.Freeze()

	return write(coff, fnameout)
}

func write(coff *coff.Coff, fnameout string) error {
	out, err := os.Create(fnameout)
	if err != nil {
		return err
	}
	defer out.Close()
	w := binutil.Writer{W: out}

	// write the resulting file to disk
	binutil.Walk(coff, func(v reflect.Value, path string) error {
		if binutil.Plain(v.Kind()) {
			w.WriteLE(v.Interface())
			return nil
		}
		vv, ok := v.Interface().(binutil.SizedReader)
		if ok {
			w.WriteFromSized(vv)
			return binutil.WALK_SKIP
		}
		return nil
	})

	if w.Err != nil {
		return fmt.Errorf("Error writing output file: %s", w.Err)
	}

	return nil
}
<commit_msg>update the embedded usage instructions<commit_after>package main

import (
	"encoding/binary"
	"flag"
	"fmt"
	"io"
	"os"
	"reflect"
	"regexp"
	"strings"

	"github.com/akavel/rsrc/binutil"
	"github.com/akavel/rsrc/coff"
	"github.com/akavel/rsrc/ico"
)

const (
	RT_ICON       = coff.RT_ICON
	RT_GROUP_ICON = coff.RT_GROUP_ICON
	RT_MANIFEST   = coff.RT_MANIFEST
)

// on storing icons, see: http://blogs.msdn.com/b/oldnewthing/archive/2012/07/20/10331787.aspx
type GRPICONDIR struct {
	ico.ICONDIR
	Entries []GRPICONDIRENTRY
}

func (group GRPICONDIR) Size() int64 {
	return int64(binary.Size(group.ICONDIR) + len(group.Entries)*binary.Size(group.Entries[0]))
}

type GRPICONDIRENTRY struct {
	ico.IconDirEntryCommon
	Id uint16
}

var usage = `USAGE:

%s -manifest FILE.exe.manifest [-ico FILE.ico] [-o FILE.syso]
  Generates a .syso file with specified resources embedded in .rsrc section,
  aimed for consumption by Go linker when building Win32 excecutables.

%s -data FILE.dat -o FILE.syso > FILE.c
  Generates a .syso file with specified opaque binary blob embedded,
  together with related .c file making it possible to access from Go code.
  Theoretically cross-platform, but reportedly cannot compile together with cgo.

The generated *.syso and *.c files will be automatically recognized by 'go build'
command and linked into an executable/library, as long as there are any *.go files
in the same directory.

OPTIONS:
`

func main() {
	//TODO: allow in options advanced specification of multiple resources, as a tree (json?)
	//FIXME: verify that data file size doesn't exceed uint32 max value
	var fnamein, fnameico, fnamedata, fnameout string
	flags := flag.NewFlagSet("", flag.ContinueOnError)
	flags.StringVar(&fnamein, "manifest", "", "path to a Windows manifest file to embed")
	flags.StringVar(&fnameico, "ico", "", "path to .ico file to embed")
	flags.StringVar(&fnamedata, "data", "", "path to raw data file to embed")
	flags.StringVar(&fnameout, "o", "rsrc.syso", "name of output COFF (.res or .syso) file")
	_ = flags.Parse(os.Args[1:])
	if fnamein == "" && fnamedata == "" {
		fmt.Fprintf(os.Stderr, usage, os.Args[0], os.Args[0])
		flags.PrintDefaults()
		os.Exit(1)
	}

	var err error
	switch {
	case fnamein != "":
		err = run(fnamein, fnameico, fnameout)
	case fnamedata != "":
		err = rundata(fnamedata, fnameout)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}

func rundata(fnamedata, fnameout string) error {
	if !strings.HasSuffix(fnameout, ".syso") {
		return fmt.Errorf("Output file name '%s' must end with '.syso'", fnameout)
	}
	symname := strings.TrimSuffix(fnameout, ".syso")
	ok, err := regexp.MatchString(`^[a-z0-9_]+$`, symname)
	if err != nil {
		return fmt.Errorf("Internal error: %s", err)
	}
	if !ok {
		return fmt.Errorf("Output file name '%s' must be composed of only lowercase letters (a-z), digits (0-9) and underscore (_)", fnameout)
	}

	dat, err := binutil.SizedOpen(fnamedata)
	if err != nil {
		return fmt.Errorf("Error opening data file '%s': %s", fnamedata, err)
	}
	defer dat.Close()

	coff := coff.NewRDATA()
	coff.AddData("_brsrc_"+symname, dat)
	coff.AddData("_ersrc_"+symname, io.NewSectionReader(strings.NewReader("\000\000"), 0, 2)) // TODO: why? copied from as-generated
	coff.Freeze()
	err = write(coff, fnameout)
	if err != nil {
		return err
	}

	//FIXME: output a .c file
	fmt.Println(strings.Replace(`#include "runtime.h"
extern byte _brsrc_NAME[], _ersrc_NAME;

/* func get_NAME() []byte */
void ·get_NAME(Slice a) {
  a.array = _brsrc_NAME;
  a.len = a.cap = &_ersrc_NAME - _brsrc_NAME;
  FLUSH(&a);
}`, "NAME", symname, -1))

	return nil
}

func run(fnamein, fnameico, fnameout string) error {
	manifest, err := binutil.SizedOpen(fnamein)
	if err != nil {
		return fmt.Errorf("Error opening manifest file '%s': %s", fnamein, err)
	}
	defer manifest.Close()

	var icons []ico.ICONDIRENTRY
	var iconsf *os.File
	if fnameico != "" {
		iconsf, err = os.Open(fnameico)
		if err != nil {
			return err
		}
		defer iconsf.Close()
		icons, err = ico.DecodeHeaders(iconsf)
		if err != nil {
			return err
		}
	}

	newid := make(chan uint16)
	go func() {
		for i := uint16(1); ; i++ {
			newid <- i
		}
	}()

	coff := coff.NewRSRC()

	coff.AddResource(RT_MANIFEST, <-newid, manifest)

	if len(icons) > 0 {
		// RT_ICONs
		group := GRPICONDIR{ICONDIR: ico.ICONDIR{
			Reserved: 0, // magic num.
			Type:     1, // magic num.
			Count:    uint16(len(icons)),
		}}
		for _, icon := range icons {
			id := <-newid
			r := io.NewSectionReader(iconsf, int64(icon.ImageOffset), int64(icon.BytesInRes))

			coff.AddResource(RT_ICON, id, r)
			group.Entries = append(group.Entries, GRPICONDIRENTRY{icon.IconDirEntryCommon, id})
		}

		// RT_GROUP_ICON
		coff.AddResource(RT_GROUP_ICON, <-newid, group)
	}

	coff.Freeze()

	return write(coff, fnameout)
}

func write(coff *coff.Coff, fnameout string) error {
	out, err := os.Create(fnameout)
	if err != nil {
		return err
	}
	defer out.Close()
	w := binutil.Writer{W: out}

	// write the resulting file to disk
	binutil.Walk(coff, func(v reflect.Value, path string) error {
		if binutil.Plain(v.Kind()) {
			w.WriteLE(v.Interface())
			return nil
		}
		vv, ok := v.Interface().(binutil.SizedReader)
		if ok {
			w.WriteFromSized(vv)
			return binutil.WALK_SKIP
		}
		return nil
	})

	if w.Err != nil {
		return fmt.Errorf("Error writing output file: %s", w.Err)
	}

	return nil
}
<|endoftext|>


<commit_before>// Copyright 2014 Sam Whited. All rights reserved.
// Use of this source code is governed by the MIT
// license which can be found in the LICENSE file.

package sass

// #cgo LDFLAGS: -lsass
/*
#include <sass_interface.h>
#include <stdlib.h>
void set_source(char* source_string, struct sass_context* ctx) {
	ctx->source_string = source_string;
}
void set_options(struct sass_options options, struct sass_context* ctx) {
	ctx->options = options;
}
struct sass_options create_options(int output_style, int source_comments, char* image_path, char* include_paths) {
	struct sass_options options;
	options.output_style = output_style;
	options.source_comments = source_comments;
	options.image_path = image_path;
	options.include_paths = include_paths;

	return options;
}
char* get_output(struct sass_context* ctx) {
	return ctx->output_string;
}
*/
import "C"
import "unsafe"

const (
	STYLE_NESTED = iota
	STYLE_EXPANDED
	STYLE_COMPACT
	STYLE_COMPRESSED
)

const (
	SOURCE_COMMENTS_NONE = iota
	SOURCE_COMMENTS_DEFAULT
	SOURCE_COMMENTS_MAP
)

type options struct {
	output_style    int
	source_comments int
	include_paths   string
	image_path      string
}

// Returns a new options struct with the defaults initialized
func NewOptions() options {
	return options{
		output_style:    STYLE_NESTED,
		source_comments: SOURCE_COMMENTS_NONE,
		include_paths:   "",
		image_path:      "images",
	}
}

// Compile the given sass string.
func Compile(source string, opts options) (string, error) {
	var (
		ctx *C.struct_sass_context
		ret *C.char
	)

	ctx = C.sass_new_context()
	defer C.sass_free_context(ctx)
	defer C.free(unsafe.Pointer(ret))

	ctx.setOptions(opts)
	ctx.setSource(source)
	_, err := C.sass_compile(ctx)
	ret = C.get_output(ctx)
	out := C.GoString(ret)

	return out, err
}

// Sets the source for the given context.
func (ctx *_Ctype_struct_sass_context) setSource(source string) error {
	source_string := C.CString(source)
	_, err := C.set_source(source_string, ctx)
	return err
}

// Sets the options for the given context
func (ctx *_Ctype_struct_sass_context) setOptions(opts options) error {
	var (
		coptions C.struct_sass_options
		cim      = C.CString(opts.image_path)
		cin      = C.CString(opts.include_paths)
		cos      = C.int(opts.output_style)
		csc      = C.int(opts.source_comments)
	)

	coptions, err := C.create_options(cos, csc, cim, cin)
	if err != nil {
		return err
	}
	_, err = C.set_options(coptions, ctx)

	return err
}
<commit_msg>Add ability to compile a file<commit_after>// Copyright 2014 Sam Whited. All rights reserved.
// Use of this source code is governed by the MIT
// license which can be found in the LICENSE file.

package sass

// #cgo LDFLAGS: -lsass
/*
#include <sass_interface.h>
#include <stdlib.h>
void set_source(char* source_string, struct sass_context* ctx) {
	ctx->source_string = source_string;
}
void set_file_path(char* input_path, struct sass_file_context* ctx) {
	ctx->input_path = input_path;
}
void set_options(struct sass_options options, struct sass_context* ctx) {
	ctx->options = options;
}
void set_file_options(struct sass_options options, struct sass_file_context* ctx) {
	ctx->options = options;
}
struct sass_options create_options(int output_style, int source_comments, char* image_path, char* include_paths) {
	struct sass_options options;
	options.output_style = output_style;
	options.source_comments = source_comments;
	options.image_path = image_path;
	options.include_paths = include_paths;

	return options;
}
char* get_output(struct sass_context* ctx) {
	return ctx->output_string;
}
char* get_file_output(struct sass_file_context* ctx) {
	return ctx->output_string;
}
*/
import "C"
import "unsafe"

const (
	STYLE_NESTED = iota
	STYLE_EXPANDED
	STYLE_COMPACT
	STYLE_COMPRESSED
)

const (
	SOURCE_COMMENTS_NONE = iota
	SOURCE_COMMENTS_DEFAULT
	SOURCE_COMMENTS_MAP
)

type options struct {
	output_style    int
	source_comments int
	include_paths   string
	image_path      string
}

// Returns a new options struct with the defaults initialized
func NewOptions() options {
	return options{
		output_style:    STYLE_NESTED,
		source_comments: SOURCE_COMMENTS_NONE,
		include_paths:   "",
		image_path:      "images",
	}
}

// Compile the given sass string.
func Compile(source string, opts options) (string, error) {
	var (
		ctx *C.struct_sass_context
		ret *C.char
	)

	ctx = C.sass_new_context()
	defer C.sass_free_context(ctx)
	defer C.free(unsafe.Pointer(ret))

	ctx.setOptions(opts)
	ctx.setSource(source)
	_, err := C.sass_compile(ctx)
	ret = C.get_output(ctx)
	out := C.GoString(ret)

	return out, err
}

// Compile the given file
func CompileFile(path string, opts options) (string, error) {
	var (
		ctx *C.struct_sass_file_context
		ret *C.char
	)

	ctx = C.sass_new_file_context()
	defer C.sass_free_file_context(ctx)
	defer C.free(unsafe.Pointer(ret))

	ctx.setOptions(opts)
	ctx.setPath(path)
	_, err := C.sass_compile_file(ctx)
	ret = C.get_file_output(ctx)
	out := C.GoString(ret)

	return out, err
}

// Sets the source for the given context.
func (ctx *_Ctype_struct_sass_context) setSource(source string) error {
	csource := C.CString(source)
	_, err := C.set_source(csource, ctx)
	return err
}

// Sets the source for the given file context.
func (ctx *_Ctype_struct_sass_file_context) setPath(path string) error {
	cpath := C.CString(path)
	_, err := C.set_file_path(cpath, ctx)
	return err
}

// Sets the options for the given context
func (ctx *_Ctype_struct_sass_context) setOptions(opts options) error {
	var (
		coptions C.struct_sass_options
		cim      = C.CString(opts.image_path)
		cin      = C.CString(opts.include_paths)
		cos      = C.int(opts.output_style)
		csc      = C.int(opts.source_comments)
	)

	coptions, err := C.create_options(cos, csc, cim, cin)
	if err != nil {
		return err
	}
	_, err = C.set_options(coptions, ctx)

	return err
}

// Sets the options for the given file context
func (ctx *_Ctype_struct_sass_file_context) setOptions(opts options) error {
	var (
		coptions C.struct_sass_options
		cim      = C.CString(opts.image_path)
		cin      = C.CString(opts.include_paths)
		cos      = C.int(opts.output_style)
		csc      = C.int(opts.source_comments)
	)

	coptions, err := C.create_options(cos, csc, cim, cin)
	if err != nil {
		return err
	}
	_, err = C.set_file_options(coptions, ctx)

	return err
}
<|endoftext|>


<commit_before>package main

import (
	"database/sql"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

func (f *MetaFile) saveFile() {

	response, e := http.Get(f.bildurl())
	if e != nil {
		log.Fatal(e)
	}

	defer response.Body.Close()
	file, err := os.Create(f.buildpath())
	if err != nil {
		log.Fatal(err)
	}

	_, err = io.Copy(file, response.Body)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()

	log.Println(f.bildurl(), f.buildthumb(), " downloaded!")
	return

}

func createDay(day string) int64 {
	// return 11
	stmt, err := DB.Prepare("INSERT INTO days(day) VALUES(?)")
	if err != nil {
		log.Fatal(err)
	}
	res, err := stmt.Exec(day)
	if err != nil {
		log.Fatal(err)
	}
	lastID, err := res.LastInsertId()
	if err != nil {
		log.Fatal(err)
	}
	return lastID
}

func newMeta(md5 string, name int, day_id int64) {
	// SAVE MD5
	stmt, err := DB.Prepare("INSERT INTO meta_webms(md5) VALUES(?)")
	if err != nil {
		log.Fatal(err)
	}
	res, err := stmt.Exec(md5)
	if err != nil {
		log.Fatal(err)
	}
	lastID, err := res.LastInsertId()
	if err != nil {
		log.Fatal(err)
	}
	log.Println(lastID)
	// SAVE FILE META
	fstmt, err := DB.Prepare("INSERT INTO files(name,day_id,meta_id) VALUES(?,?,?)")
	if err != nil {
		log.Fatal(err)
	}
	_, err = fstmt.Exec(name, day_id, lastID)
	if err != nil {
		log.Fatal(err)
	}
}

func (f *MetaFile) saveMeta() {
	var dayID int64
	t := time.Now().Format("2006-01-02")

	err := DB.QueryRow("select id from days where day=?", t).Scan(&dayID)
	switch {
	case err == sql.ErrNoRows:
		dayID = createDay(t)
		log.Println(t)
		newMeta(f.Hash, f.Name, dayID)
	case err != nil:
		log.Fatal(err)
	default:
		newMeta(f.Hash, f.Name, dayID)
	}
}

func (f *MetaFile) buildpath() string {
	t := time.Now().Format("2006-01-02")
	createFolder()
	path, err := filepath.Abs("files/" + t + "/" + strconv.Itoa(f.Name) + ".webm")
	if err != nil {
		log.Fatalln(err)
	}
	return path
}

func createFolder() {
	t := time.Now().Format("2006-01-02")
	path, err := filepath.Abs("files/" + t + "/")
	if err != nil {
		log.Fatal(err)
	}
	if _, err = os.Stat(path); os.IsNotExist(err) {
		os.Mkdir(path, 0755)
		log.Println(path, "created!")
	}
}
<commit_msg>download thumb and files<commit_after>package main

import (
	"database/sql"
	"io"
	"log"
	"net/http"
	"os"
	"path/filepath"
	"strconv"
	"time"
)

func (f *MetaFile) saveFile() {

	response, e := http.Get(f.bildurl())
	if e != nil {
		log.Fatal(e)
	}

	defer response.Body.Close()
	file, err := os.Create(f.buildpath())
	if err != nil {
		log.Fatal(err)
	}

	_, err = io.Copy(file, response.Body)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()

	// TODO download thumb fixme please
	response, e = http.Get(f.buildthumb())
	if e != nil {
		log.Fatal(e)
	}

	defer response.Body.Close()
	file, err = os.Create(f.buildpathThumb())
	if err != nil {
		log.Fatal(err)
	}

	_, err = io.Copy(file, response.Body)
	if err != nil {
		log.Fatal(err)
	}
	file.Close()

	log.Println(f.bildurl(), " downloaded!")
	return

}

func createDay(day string) int64 {
	// return 11
	stmt, err := DB.Prepare("INSERT INTO days(day) VALUES(?)")
	if err != nil {
		log.Fatal(err)
	}
	res, err := stmt.Exec(day)
	if err != nil {
		log.Fatal(err)
	}
	lastID, err := res.LastInsertId()
	if err != nil {
		log.Fatal(err)
	}
	return lastID
}

func newMeta(md5 string, name int, day_id int64) {
	// SAVE MD5
	stmt, err := DB.Prepare("INSERT INTO meta_webms(md5) VALUES(?)")
	if err != nil {
		log.Fatal(err)
	}
	res, err := stmt.Exec(md5)
	if err != nil {
		log.Fatal(err)
	}
	lastID, err := res.LastInsertId()
	if err != nil {
		log.Fatal(err)
	}
	log.Println(lastID)
	// SAVE FILE META
	fstmt, err := DB.Prepare("INSERT INTO files(name,day_id,meta_id) VALUES(?,?,?)")
	if err != nil {
		log.Fatal(err)
	}
	_, err = fstmt.Exec(name, day_id, lastID)
	if err != nil {
		log.Fatal(err)
	}
}

func (f *MetaFile) saveMeta() {
	var dayID int64
	t := time.Now().Format("2006-01-02")

	err := DB.QueryRow("select id from days where day=?", t).Scan(&dayID)
	switch {
	case err == sql.ErrNoRows:
		dayID = createDay(t)
		log.Println(t)
		newMeta(f.Hash, f.Name, dayID)
	case err != nil:
		log.Fatal(err)
	default:
		newMeta(f.Hash, f.Name, dayID)
	}
}

func (f *MetaFile) buildpath() string {
	t := time.Now().Format("2006-01-02")
	relativePath := "files/"
	createFolder(relativePath)
	path, err := filepath.Abs("files/" + t + "/" + strconv.Itoa(f.Name) + ".webm")
	if err != nil {
		log.Fatalln(err)
	}
	return path
}

func (f *MetaFile) buildpathThumb() string {
	t := time.Now().Format("2006-01-02")
	relativePath := "thumb/"
	createFolder(relativePath)
	path, err := filepath.Abs(relativePath + t + "/" + strconv.Itoa(f.Name) + ".jpg")
	if err != nil {
		log.Fatalln(err)
	}
	return path
}

func createFolder(relativePath string) {
	t := time.Now().Format("2006-01-02")
	path, err := filepath.Abs(relativePath + t + "/")
	if err != nil {
		log.Fatal(err)
	}
	if _, err = os.Stat(path); os.IsNotExist(err) {
		os.Mkdir(path, 0755)
		log.Println(path, "created!")
	}
}
<|endoftext|>


<commit_before>package main

import "fmt"
import "os"
import "log"
import "flag"
import md5 "crypto/md5"
import hex "encoding/hex"
import "path"

// SRSLY?
func min(x, y int64) int64 {
    if x < y {
        return x
    }
    return y
}

type Chunk struct {
	path     string
	info     os.FileInfo
	offset   int64
	md5sum   string
	data     []byte
}

func walkDirectory(root string) <-chan Chunk {
	out := make(chan Chunk)
	var queue []string
	queue = append(queue, root)

	go func() {
		for len(queue) > 0 {
			d := queue[0]
			queue = queue[1:]

			f,err := os.Open(d)
			if err != nil {
				log.Print("Unable to open: %s\n", d)
				continue
			}

			for {
				infos,err := f.Readdir(100)
				if len(infos) == 0 {
					break;
				}
				if err != nil {
					log.Fatal("Readdir failed")
				}

				for _,stat := range infos {
					full_path := path.Join(d, stat.Name())
					if stat.IsDir() {
						queue = append(queue, full_path)
					} else {
						var o int64
						o = 0
						for o < stat.Size() {
							size := min(1 << 20, stat.Size() - o)
							c := Chunk{path: full_path, info: stat, offset: o, md5sum: "empty", data: make([]byte, size)}
							out <- c
							o += (1 << 20)
						}
					}
				}
			}

		}
		close(out)
	}()

	return out
}

func hashFiles(chunks <-chan Chunk) <-chan Chunk {
	out := make(chan Chunk)

	go func() {
		for c := range chunks {
			f,err := os.Open(c.path)
			if err != nil {
				log.Printf("Couldn't open %s. Skipping it.", c.path)
				continue
			}
			n,err := f.ReadAt(c.data, c.offset)
			if err != nil {
				log.Fatal("Non EOF error on ", f.Name())
			} else if  n != len(c.data) {
				log.Fatal("Short read: %d v %d (on %s)\n", n, len(c.data), c.path)
			}
			csum := md5.Sum(c.data[:])
			c.md5sum = hex.EncodeToString(csum[:])
			out <- c
		}
		close(out)
	}()
	return out
}

func main() {
	root := flag.String("directory", "", "Directory to scan")
	flag.Parse()

	chunks := walkDirectory(*root)
	for c := range hashFiles(chunks) {
		fmt.Printf("%s (%d): %s\n", c.path, c.offset, c.md5sum)
	}
}
<commit_msg>Write out JSON. TODO: FileInfo<commit_after>package main

import "fmt"
import "encoding/json"
import "os"
import "log"
import "flag"
import md5 "crypto/md5"
import hex "encoding/hex"
import "path"
import "bufio"

// SRSLY?
func min(x, y int64) int64 {
    if x < y {
        return x
    }
    return y
}

type Chunk struct {
	Path     string
	Info     os.FileInfo
	Offset   int64
	Md5sum   string
	data     []byte
}

func walkDirectory(root string) <-chan Chunk {
	out := make(chan Chunk)
	var queue []string
	queue = append(queue, root)

	go func() {
		for len(queue) > 0 {
			d := queue[0]
			queue = queue[1:]

			f,err := os.Open(d)
			if err != nil {
				log.Print("Unable to open: %s\n", d)
				continue
			}

			for {
				infos,err := f.Readdir(100)
				if len(infos) == 0 {
					break;
				}
				if err != nil {
					log.Fatal("Readdir failed")
				}

				for _,stat := range infos {
					full_path := path.Join(d, stat.Name())
					if stat.IsDir() {
						queue = append(queue, full_path)
					} else {
						var o int64
						o = 0
						for o < stat.Size() {
							size := min(1 << 20, stat.Size() - o)
							c := Chunk{Path: full_path, Info: stat, Offset: o, Md5sum: "empty", data: make([]byte, size)}
							out <- c
							o += (1 << 20)
						}
					}
				}
			}

		}
		close(out)
	}()

	return out
}

func hashFiles(chunks <-chan Chunk) <-chan Chunk {
	out := make(chan Chunk)

	go func() {
		for c := range chunks {
			f,err := os.Open(c.Path)
			if err != nil {
				log.Printf("Couldn't open %s. Skipping it.", c.Path)
				continue
			}
			n,err := f.ReadAt(c.data, c.Offset)
			if err != nil {
				log.Fatal("Non EOF error on ", f.Name())
			} else if  n != len(c.data) {
				log.Fatal("Short read: %d v %d (on %s)\n", n, len(c.data), c.Path)
			}
			csum := md5.Sum(c.data[:])
			c.Md5sum = hex.EncodeToString(csum[:])
			out <- c
		}
		close(out)
	}()
	return out
}

func writeJSON(chunks <-chan Chunk, filename string) {
	f,err := os.Create(filename)
	if err != nil {
		log.Fatal("Failed to open ", filename);
	}
	w := bufio.NewWriter(f)
	enc := json.NewEncoder(w)
	for c := range chunks {
		fmt.Printf("%s (%d): %s\n", c.Path, c.Offset, c.Md5sum)
		err := enc.Encode(c)
		if (err != nil) {
			log.Fatal("Failed to encode")
		}

	}
	w.Flush()
}

func main() {
	root := flag.String("directory", "", "Directory to scan")
	flag.Parse()

	chunks := walkDirectory(*root)
	writeJSON(hashFiles(chunks), "/dev/stdout")
}
<|endoftext|>


<commit_before>package sers

import (
	"fmt"
	"io"
)

const (
	N = 0 // no parity
	E = 1 // even parity
	O = 2 // odd parity
)

const (
	NO_HANDSHAKE     = 0
	RTSCTS_HANDSHAKE = 1
)

type SerialPort interface {
	io.Reader
	io.Writer
	io.Closer
	SetMode(baudrate, databits, parity, stopbits, handshake int) error
	SetReadParams(minread int, timeout float64) error
}

type StringError string

func (se StringError) Error() string {
	return string(se)
}

type ParameterError struct {
	Parameter string
	Reason    string
}

func (pe *ParameterError) Error() string {
	return fmt.Sprintf("error in parameter '%s': %s")
}

type Error struct {
	Operation       string
	UnderlyingError error
}

func (e *Error) Error() string {
	return fmt.Sprintf("%s: %v", e.Operation, e.UnderlyingError)
}
<commit_msg>add 'import "C"' to sers.go. this makes it compile on linux arm.<commit_after>package sers

import "C"

import (
	"fmt"
	"io"
)

const (
	N = 0 // no parity
	E = 1 // even parity
	O = 2 // odd parity
)

const (
	NO_HANDSHAKE     = 0
	RTSCTS_HANDSHAKE = 1
)

type SerialPort interface {
	io.Reader
	io.Writer
	io.Closer
	SetMode(baudrate, databits, parity, stopbits, handshake int) error
	SetReadParams(minread int, timeout float64) error
}

type StringError string

func (se StringError) Error() string {
	return string(se)
}

type ParameterError struct {
	Parameter string
	Reason    string
}

func (pe *ParameterError) Error() string {
	return fmt.Sprintf("error in parameter '%s': %s")
}

type Error struct {
	Operation       string
	UnderlyingError error
}

func (e *Error) Error() string {
	return fmt.Sprintf("%s: %v", e.Operation, e.UnderlyingError)
}
<|endoftext|>


<commit_before>package main

import (
	"crypto/rand"
	"fmt"
	"github.com/gorilla/sessions"
	"io"
	"net/http"
	"encoding/binary"
)

var authKey = []byte("somesecretauth")
var store sessions.Store
var pool *Pool

type Pool struct {
	clients []*Client
	in      chan *Client
	out     chan *Room
}

type Client struct {
	id		int64
	retChan chan *Room
}

type Room struct {
	id 		int64
	client1	*Client
	client2 *Client
}

func (p *Pool) Pair() {
	for {
		fmt.Println("pairing")
		c1 := <- p.in
		fmt.Println("middlePairing")
		c2 := <- p.in
		fmt.Println("donePairing")

		b := make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return
		}
		crId, _ := binary.Varint(b)

		room := &Room{crId, c1, c2}
		c1.retChan <- room
		c2.retChan <- room
	}
}

func newPool() *Pool {
	pool := &Pool{
		clients:	make([]*Client, 0),
		in:			make(chan *Client),
		out:		make(chan *Room),
	}

	go pool.Pair()

	return pool
}

func main() {
	store = sessions.NewCookieStore(authKey)

	pool = newPool()

	http.HandleFunc("/chatroom/join", joinChatRoom)
	http.HandleFunc("/chatroom/leave", leaveChatRoom)
	http.ListenAndServe(":8080", nil)
}

func joinChatRoom(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "session")
	userid := session.Values["userid"]

	var uid int64
	var b []byte

	if userid == nil {
		b = make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			fmt.Println(err)
			return
		}
		session.Values["userid"] = b
		session.Save(r, w)
	} else {
		b = []byte(userid.([]uint8))
	}
	uid, _ = binary.Varint(b)

	retChan := make(chan *Room)
	client := &Client {uid, retChan}
	pool.in <- client
	fmt.Println("client sent")

	chatroom := <- retChan
	fmt.Println("chatroom received")

	fmt.Fprint(w, "Joined chatroom ", chatroom.id)
}

func leaveChatRoom(w http.ResponseWriter, r *http.Request) {
	session, _ := store.Get(r, "session")
	fmt.Println(session.Values["userid"])
	fmt.Fprint(w, session.Values["userid"])
}
<commit_msg>simple chat<commit_after>package main

import (
	"crypto/rand"
	"fmt"
	"github.com/gorilla/sessions"
	"io"
	"net/http"
	"encoding/binary"
)

var authKey = []byte("somesecretauth")
var store sessions.Store
var pool *Pool
var clients map[int64]*Client

type Pool struct {
	in      chan *Client
	out     chan *Room
}

type Client struct {
	id		int64
	in 		chan string
	out		chan string
	retChan chan *Room
}

type Room struct {
	id 		int64
	client1	*Client
	client2 *Client
}

func (p *Pool) Pair() {
	for {
		c1, c2 := <- p.in, <- p.in

		b := make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return
		}
		crId, _ := binary.Varint(b)

		room := &Room{crId, c1, c2}

		c1.in, c2.in = c2.out, c1.out

		c1.retChan <- room
		c2.retChan <- room
	}
}

func newPool() *Pool {
	pool := &Pool{
		in:			make(chan *Client),
		out:		make(chan *Room),
	}

	go pool.Pair()

	return pool
}

func UIDFromSession(w http.ResponseWriter, r *http.Request) (int64, error) {
	session, _ := store.Get(r, "session")
	userid := session.Values["userid"]

	var uid int64
	var b []byte

	if userid == nil {
		b = make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return 0, err
		}
		session.Values["userid"] = b
		session.Save(r, w)
	} else {
		b = []byte(userid.([]uint8))
	}
	uid, _ = binary.Varint(b)
	return uid, nil
}

func main() {
	store = sessions.NewCookieStore(authKey)

	pool = newPool()
	clients = make(map[int64]*Client)

	http.HandleFunc("/message/check", checkMessage)
	http.HandleFunc("/message/send", sendMessage)
	http.HandleFunc("/chatroom/join", joinChatRoom)
	http.HandleFunc("/chatroom/leave", leaveChatRoom)
	http.ListenAndServe(":8080", nil)
}

func joinChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	retChan := make(chan *Room)
	client := &Client {
		id:		uid,
		in:		nil,
		out: 	make(chan string),
		retChan: retChan,
	}
	clients[uid] = client
	pool.in <- client

	chatroom := <- retChan

	fmt.Fprint(w, "{\"status\":\"success\",\"crid\":", chatroom.id, "}")
}

func leaveChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, _ := UIDFromSession(w, r)
	fmt.Fprint(w, uid)
}

func sendMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	message := "some string"

	client := clients[uid]

	fmt.Println("sending")
	client.out <- message
	fmt.Println("sent")

	fmt.Fprint(w, "{\"status\":\"success\"}")
}

func checkMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	message := <- clients[uid].in

	fmt.Fprint(w, message)
}

func handleError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}
<|endoftext|>


<commit_before>package main

import (
	_ "time"
	"crypto/rand"
	"encoding/binary"
	"fmt"
	"github.com/gorilla/sessions"
	"io"
	"net/http"
	"encoding/json"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"runtime"
)

const MESSAGE_QUEUE_SIZE = 10

var authKey = []byte("somesecretauth")
var store sessions.Store
var pool *Pool
var clients map[int64]*Client

var db *sql.DB
// var tv syscall.Timeval

type Pool struct {
	in  chan *Client
	out chan *Room
}

type Client struct {
	id      int64
	in      chan string
	out     chan string
	retChan chan *Room
}

type Room struct {
	id      int64
	client1 *Client
	client2 *Client
}

func (p *Pool) Pair() {
	for {
		c1, c2 := <-p.in, <-p.in

		fmt.Println("match found")

		b := make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return
		}
		crId, _ := binary.Varint(b)

		room := &Room{crId, c1, c2}

		c1.in, c2.in = c2.out, c1.out

		c1.retChan <- room
		c2.retChan <- room
	}
}

func newPool() *Pool {
	pool := &Pool{
		in:  make(chan *Client),
		out: make(chan *Room),
	}

	go pool.Pair()

	return pool
}

func UIDFromSession(w http.ResponseWriter, r *http.Request) (int64, error) {
	session, _ := store.Get(r, "session")
	userid := session.Values["userid"]

	var uid int64
	var b []byte

	if userid == nil {
		b = make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return 0, err
		}
		session.Values["userid"] = b
		session.Save(r, w)
	} else {
		b = []byte(userid.([]uint8))
	}
	uid, _ = binary.Varint(b)
	return uid, nil
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	db, _ = sql.Open("mysql", "root:@/suitup")
	defer db.Close()

	store = sessions.NewCookieStore(authKey)

	pool = newPool()
	clients = make(map[int64]*Client)

	http.HandleFunc("/", mainHandle)

	http.HandleFunc("/login", login)

	http.HandleFunc("/message/check", checkMessage)
	http.HandleFunc("/message/send", sendMessage)

	http.HandleFunc("/chatroom/join", joinChatRoom)
	http.HandleFunc("/chatroom/leave", leaveChatRoom)
	http.ListenAndServe(":8080", nil)
}

type IdQuery struct {
    Id            int64      `json:"id"`

}

func mainHandle(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "hey")
}

func joinChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

  	fmt.Println("uid: ", uid)
	retChan := make(chan *Room)
	client := &Client{
		id:      uid,
		in:      nil,
		out:     make(chan string, MESSAGE_QUEUE_SIZE),
		retChan: retChan,
	}
	clients[uid] = client
	pool.in <- client

	fmt.Println("added ", uid, " to queue")
	chatroom := <- retChan

	fmt.Fprint(w, "{\"status\":\"success\",\"crid\":", chatroom.id, "}")
}

func leaveChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, _ := UIDFromSession(w, r)
	fmt.Fprint(w, uid)
}

func sendMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	message := "some string"

	client := clients[uid]
	if client != nil {
		client.out <- message
		fmt.Fprint(w, "{\"status\":\"success\"}")
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func checkMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	client := clients[uid]
	if client != nil {
		fmt.Println("waiting")
		message := <- clients[uid].in
		fmt.Println("received")
		fmt.Fprint(w, message)
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("login")
	inputToken := r.FormValue("access_token")
	if len(inputToken) != 0 {
		uid := GetMe(inputToken)

		fmt.Println("querying for: ", uid)
		// row := db.QueryRow("SELECT id FROM users")
		row := db.QueryRow("SELECT id FROM users WHERE facebook_id=?", string(uid))
		fmt.Println("returned")
		iq := new(IdQuery)
		err := row.Scan(&iq.Id)

		if err == nil {
			fmt.Fprint(w, "{\"status\":\"success\",\"uid\":", iq.Id, "}")
		} else {
			// regStmt, err := db.Prepare("INSERT INTO users (facebook_id, username, email, level, points) VALUES(?, ?, ?, ?, ?);")
			// handleError(err)
			// regStmt.Run(uid, "", "", 0, 0)
			fmt.Fprint(w, "{\"status\":\"success\"}")
		}
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func readHttpBody(response *http.Response) string {

	fmt.Println("Reading body")

	bodyBuffer := make([]byte, 1000)
	var str string

	count, err := response.Body.Read(bodyBuffer)

	for ; count > 0; count, err = response.Body.Read(bodyBuffer) {

		if err != nil {

		}

		str += string(bodyBuffer[:count])
	}

	return str

}

func getUncachedResponse(uri string) (*http.Response, error) {
	fmt.Println("Uncached response GET")
	request, err := http.NewRequest("GET", uri, nil)

	if err == nil {
		request.Header.Add("Cache-Control", "no-cache")

		client := new(http.Client)

		return client.Do(request)
	}

	if (err != nil) {
	}
	return nil, err

}

func GetMe(token string) string {
	fmt.Println("Getting me")
	response, err := getUncachedResponse("https://graph.facebook.com/me?access_token="+token)

	if err == nil {

		var jsonBlob interface{}

		responseBody := readHttpBody(response)

		fmt.Println("responseboyd", responseBody)

		if responseBody != "" {
			err = json.Unmarshal([]byte(responseBody), &jsonBlob)

			if err == nil {
				jsonObj := jsonBlob.(map[string]interface{})
				return jsonObj["id"].(string)
			}
		}
		return err.Error()
	}

	return err.Error()
}

func handleError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}
<commit_msg>basic login<commit_after>package main

import (
	_ "time"
	"crypto/rand"
	"encoding/binary"
	"fmt"
	"github.com/gorilla/sessions"
	"io"
	"net/http"
	"encoding/json"
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"runtime"
)

const MESSAGE_QUEUE_SIZE = 10

var authKey = []byte("somesecretauth")
var store sessions.Store
var pool *Pool
var clients map[int64]*Client

var db *sql.DB
// var tv syscall.Timeval

type Pool struct {
	in  chan *Client
	out chan *Room
}

type Client struct {
	id      int64
	in      chan string
	out     chan string
	retChan chan *Room
}

type Room struct {
	id      int64
	client1 *Client
	client2 *Client
}

func (p *Pool) Pair() {
	for {
		c1, c2 := <-p.in, <-p.in

		fmt.Println("match found")

		b := make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return
		}
		crId, _ := binary.Varint(b)

		room := &Room{crId, c1, c2}

		c1.in, c2.in = c2.out, c1.out

		c1.retChan <- room
		c2.retChan <- room
	}
}

func newPool() *Pool {
	pool := &Pool{
		in:  make(chan *Client),
		out: make(chan *Room),
	}

	go pool.Pair()

	return pool
}

func UIDFromSession(w http.ResponseWriter, r *http.Request) (int64, error) {
	session, _ := store.Get(r, "session")
	userid := session.Values["userid"]

	var uid int64
	var b []byte

	if userid == nil {
		b = make([]byte, 8)
		n, err := io.ReadFull(rand.Reader, b)
		if err != nil || n != 8 {
			return 0, err
		}
		session.Values["userid"] = b
		session.Save(r, w)
	} else {
		b = []byte(userid.([]uint8))
	}
	uid, _ = binary.Varint(b)
	return uid, nil
}

func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	db, _ = sql.Open("mysql", "root:@/suitup")
	defer db.Close()

	store = sessions.NewCookieStore(authKey)

	pool = newPool()
	clients = make(map[int64]*Client)

	http.HandleFunc("/", mainHandle)

	http.HandleFunc("/login", login)

	http.HandleFunc("/message/check", checkMessage)
	http.HandleFunc("/message/send", sendMessage)

	http.HandleFunc("/chatroom/join", joinChatRoom)
	http.HandleFunc("/chatroom/leave", leaveChatRoom)
	http.ListenAndServe(":8080", nil)
}

type IdQuery struct {
    Id            int64      `json:"id"`

}

func mainHandle(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, "hey")
}

func joinChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

  	fmt.Println("uid: ", uid)
	retChan := make(chan *Room)
	client := &Client{
		id:      uid,
		in:      nil,
		out:     make(chan string, MESSAGE_QUEUE_SIZE),
		retChan: retChan,
	}
	clients[uid] = client
	pool.in <- client

	fmt.Println("added ", uid, " to queue")
	chatroom := <- retChan

	fmt.Fprint(w, "{\"status\":\"success\",\"crid\":", chatroom.id, "}")
}

func leaveChatRoom(w http.ResponseWriter, r *http.Request) {
	uid, _ := UIDFromSession(w, r)
	fmt.Fprint(w, uid)
}

func sendMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	message := "some string"

	client := clients[uid]
	if client != nil {
		client.out <- message
		fmt.Fprint(w, "{\"status\":\"success\"}")
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func checkMessage(w http.ResponseWriter, r *http.Request) {
	uid, err := UIDFromSession(w, r)
	handleError(err)

	client := clients[uid]
	if client != nil {
		fmt.Println("waiting")
		message := <- clients[uid].in
		fmt.Println("received")
		fmt.Fprint(w, message)
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func login(w http.ResponseWriter, r *http.Request) {
	fmt.Println("login")
	inputToken := r.FormValue("access_token")
	if len(inputToken) != 0 {
		uid := GetMe(inputToken)

		fmt.Println("querying for: ", uid)
		// row := db.QueryRow("SELECT id FROM users")
		row := db.QueryRow("SELECT id FROM users WHERE facebook_id=?", string(uid))
		fmt.Println("returned")
		iq := new(IdQuery)
		err := row.Scan(&iq.Id)

		if err == nil {
			fmt.Fprint(w, "{\"status\":\"success\",\"uid\":", iq.Id, "}")
		} else {
			_, err = db.Exec("insert into users (facebook_id, username, email, level, points) values (?, ?, ?, 0, 0)", uid, "", "")
			fmt.Println("err: ", err)
			if err == nil {
				row = db.QueryRow("SELECT id FROM users WHERE facebook_id=?", string(uid))
				err = row.Scan(&iq.Id)
				if err == nil {
					fmt.Fprint(w, "{\"status\":\"success\"}")
				} else {
					fmt.Fprint(w, "{\"status\":\"failure\"}")
				}
			} else {
				fmt.Fprint(w, "{\"status\":\"failure\"}")
			}
		}
	} else {
		fmt.Fprint(w, "{\"status\":\"failure\"}")
	}
}

func readHttpBody(response *http.Response) string {

	fmt.Println("Reading body")

	bodyBuffer := make([]byte, 1000)
	var str string

	count, err := response.Body.Read(bodyBuffer)

	for ; count > 0; count, err = response.Body.Read(bodyBuffer) {

		if err != nil {

		}

		str += string(bodyBuffer[:count])
	}

	return str

}

func getUncachedResponse(uri string) (*http.Response, error) {
	fmt.Println("Uncached response GET")
	request, err := http.NewRequest("GET", uri, nil)

	if err == nil {
		request.Header.Add("Cache-Control", "no-cache")

		client := new(http.Client)

		return client.Do(request)
	}

	if (err != nil) {
	}
	return nil, err

}

func GetMe(token string) string {
	fmt.Println("Getting me")
	response, err := getUncachedResponse("https://graph.facebook.com/me?access_token="+token)

	if err == nil {

		var jsonBlob interface{}

		responseBody := readHttpBody(response)

		fmt.Println("responseboyd", responseBody)

		if responseBody != "" {
			err = json.Unmarshal([]byte(responseBody), &jsonBlob)

			if err == nil {
				jsonObj := jsonBlob.(map[string]interface{})
				return jsonObj["id"].(string)
			}
		}
		return err.Error()
	}

	return err.Error()
}

func handleError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}
<|endoftext|>


<commit_before>package units

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

// HumanSize returns a human-readable approximation of a size
// using SI standard (eg. "44kB", "17MB")
func HumanSize(size int64) string {
	i := 0
	var sizef float64
	sizef = float64(size)
	units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
	for sizef >= 1000.0 {
		sizef = sizef / 1000.0
		i++
	}
	return fmt.Sprintf("%.4g %s", sizef, units[i])
}

// FromHumanSize returns an integer from a human-readable specification of a size
// using SI standard (eg. "44kB", "17MB")
func FromHumanSize(size string) (int64, error) {
	re, err := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
	if err != nil {
		return -1, fmt.Errorf("%s does not specify not a size", size)
	}

	matches := re.FindStringSubmatch(size)

	if len(matches) != 3 {
		return -1, fmt.Errorf("Invalid size: '%s'", size)
	}

	theSize, err := strconv.ParseInt(matches[1], 10, 0)
	if err != nil {
		return -1, err
	}

	unit := strings.ToLower(matches[2])

	if unit == "k" {
		theSize *= 1000
	} else if unit == "m" {
		theSize *= 1000 * 1000
	} else if unit == "g" {
		theSize *= 1000 * 1000 * 1000
	} else if unit == "t" {
		theSize *= 1000 * 1000 * 1000 * 1000
	} else if unit == "p" {
		theSize *= 1000 * 1000 * 1000 * 1000 * 1000
	}

	return theSize, nil
}

// Parses a human-readable string representing an amount of RAM
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
// returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (int64, error) {
	re, err := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
	if err != nil {
		return -1, err
	}

	matches := re.FindStringSubmatch(size)

	if len(matches) != 3 {
		return -1, fmt.Errorf("Invalid size: '%s'", size)
	}

	memLimit, err := strconv.ParseInt(matches[1], 10, 0)
	if err != nil {
		return -1, err
	}

	unit := strings.ToLower(matches[2])

	if unit == "k" {
		memLimit *= 1024
	} else if unit == "m" {
		memLimit *= 1024 * 1024
	} else if unit == "g" {
		memLimit *= 1024 * 1024 * 1024
	} else if unit == "t" {
		memLimit *= 1024 * 1024 * 1024 * 1024
	}

	return memLimit, nil
}
<commit_msg>pkg/units: Using 'case' instead of trickled ifs<commit_after>package units

import (
	"fmt"
	"regexp"
	"strconv"
	"strings"
)

// HumanSize returns a human-readable approximation of a size
// using SI standard (eg. "44kB", "17MB")
func HumanSize(size int64) string {
	i := 0
	var sizef float64
	sizef = float64(size)
	units := []string{"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
	for sizef >= 1000.0 {
		sizef = sizef / 1000.0
		i++
	}
	return fmt.Sprintf("%.4g %s", sizef, units[i])
}

// FromHumanSize returns an integer from a human-readable specification of a size
// using SI standard (eg. "44kB", "17MB")
func FromHumanSize(size string) (int64, error) {
	re, err := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
	if err != nil {
		return -1, fmt.Errorf("%s does not specify not a size", size)
	}

	matches := re.FindStringSubmatch(size)

	if len(matches) != 3 {
		return -1, fmt.Errorf("Invalid size: '%s'", size)
	}

	theSize, err := strconv.ParseInt(matches[1], 10, 0)
	if err != nil {
		return -1, err
	}

	unit := strings.ToLower(matches[2])

	switch unit {
	case "k":
		theSize *= 1000
	case "m":
		theSize *= 1000 * 1000
	case "g":
		theSize *= 1000 * 1000 * 1000
	case "t":
		theSize *= 1000 * 1000 * 1000 * 1000
	case "p":
		theSize *= 1000 * 1000 * 1000 * 1000 * 1000
	}

	return theSize, nil
}

// Parses a human-readable string representing an amount of RAM
// in bytes, kibibytes, mebibytes, gibibytes, or tebibytes and
// returns the number of bytes, or -1 if the string is unparseable.
// Units are case-insensitive, and the 'b' suffix is optional.
func RAMInBytes(size string) (int64, error) {
	re, err := regexp.Compile("^(\\d+)([kKmMgGtT])?[bB]?$")
	if err != nil {
		return -1, err
	}

	matches := re.FindStringSubmatch(size)

	if len(matches) != 3 {
		return -1, fmt.Errorf("Invalid size: '%s'", size)
	}

	memLimit, err := strconv.ParseInt(matches[1], 10, 0)
	if err != nil {
		return -1, err
	}

	unit := strings.ToLower(matches[2])

	switch unit {
	case "k":
		memLimit *= 1024
	case "m":
		memLimit *= 1024 * 1024
	case "g":
		memLimit *= 1024 * 1024 * 1024
	case "t":
		memLimit *= 1024 * 1024 * 1024 * 1024
	}

	return memLimit, nil
}
<|endoftext|>


<commit_before>package slog

import (
	"log"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/stretchr/pat/stop"
)

const nestedLogSep = "»"

// Level represents the level of logging.
type Level uint8

var levelStrs = map[Level]string{
	LevelInvalid: "invalid",
	LevelNothing: "none",
	LevelErr:     "error",
	LevelWarn:    "warning",
	LevelInfo:    "info",
	LevelDebug:   "debug",
}

// String gets the string representation of
// the Level.
func (l Level) String() string {
	if s, ok := levelStrs[l]; ok {
		return s
	}
	return levelStrs[LevelInvalid]
}

// ParseLevel gets the Level from the specified
// String.
func ParseLevel(s string) Level {
	s = strings.ToLower(s)
	for l, str := range levelStrs {
		if strings.HasPrefix(str, s) {
			return l
		}
	}
	return LevelInvalid
}

const (
	// LevelInvalid represents an invalid Level.
	// Should never be used, use LevelNothing instead.
	LevelInvalid Level = iota

	// LevelNothing represents no logging.
	LevelNothing

	// LevelErr represents error level logging.
	LevelErr
	// LevelWarn represents warning level logging.
	LevelWarn
	// LevelInfo represents information level logging.
	LevelInfo
	// LevelDebug represents debug level logging.
	LevelDebug

	// LevelEverything logs everything.
	LevelEverything // must always be last value
)

// Log represents a single log item.
type Log struct {
	Level  Level
	When   time.Time
	Data   []interface{}
	Source []string
}

// Reporter represents types capable of doing something
// with logs.
type Reporter interface {
	Log(*Log)
}

// ReporterFunc is a function type capable of acting as
// a reporter.
type ReporterFunc func(*Log)

// Log calls the ReporterFunc.
func (f ReporterFunc) Log(l *Log) {
	f(l)
}

type reporters []Reporter

func (rs reporters) Log(l *Log) {
	for _, r := range rs {
		r.Log(l)
	}
}

// Reporters makes a Reporter that reports to multiple
// reporters in order.
func Reporters(rs ...Reporter) Reporter {
	return reporters(rs)
}

var _ Reporter = (Reporters)(nil)

// RootLogger represents a the root Logger that has
// more capabilities than a normal Logger.
// Normally, caller code would require the Logger interface only.
type RootLogger interface {
	stop.Stopper
	Logger
	// SetReporter sets the Reporter for this logger and
	// child loggers to use.
	SetReporter(r Reporter)
	// SetReporterFunc sets the specified ReporterFunc as
	// the Reporter.
	SetReporterFunc(f ReporterFunc)
	// SetLevel sets the level of this and all children loggers.
	SetLevel(level Level)
}

// Logger represents types capable of logging at
// different levels.
type Logger interface {
	// Info gets whether the logger is logging information or not,
	// and also makes such logs.
	Info(a ...interface{}) bool
	// Warn gets whether the logger is logging warnings or not,
	// and also makes such logs.
	Warn(a ...interface{}) bool
	// Err gets whether the logger is logging errors or not,
	// and also makes such logs.
	Err(a ...interface{}) bool
	// Debug gets whether the logger is logging errors or not,
	// and also makes such logs.
	Debug(a ...interface{}) bool
	// New creates a new child logger, with this as the parent.
	New(source string) Logger
	// SetSource sets the source of this logger.
	SetSource(source string)
}

type logger struct {
	m        sync.Mutex
	level    Level
	r        Reporter
	c        chan *Log
	src      []string
	stopChan chan stop.Signal
	root     *logger
}

var _ Logger = (*logger)(nil)

// New creates a new RootLogger, which is capable of acting
// like a Logger, used for logging.
// RootLogger is also a stop.Stopper and can have the
// Reporter specified, where children Logger types cannot.
// By default, the returned Logger will log to the slog.Stdout
// reporter, but this can be changed with SetReporter.
func New(source string, level Level) RootLogger {
	l := &logger{
		level: level,
		src:   []string{source},
		r:     Stdout,
	}
	l.root = l // use this one as the root one
	l.Start()
	return l
}

// New makes a new child logger with the specified source.
func (l *logger) New(source string) Logger {
	return &logger{
		level: l.level,
		src:   append(l.src, source),
		root:  l.root,
	}
}

func (l *logger) SetLevel(level Level) {
	l.root.m.Lock()
	l.root.level = level
	l.root.m.Unlock()
}

func (l *logger) SetSource(source string) {
	l.m.Lock()
	l.src[len(l.src)-1] = source
	l.m.Unlock()
}

func (l *logger) SetReporter(r Reporter) {
	l.root.Stop(stop.NoWait)
	<-l.root.StopChan()
	l.root.r = r
	l.root.Start()
}

func (l *logger) SetReporterFunc(f ReporterFunc) {
	l.SetReporter(f)
}

func (l *logger) Start() {
	l.root.c = make(chan *Log)
	l.root.stopChan = stop.Make()
	go func() {
		for item := range l.root.c {
			l.root.r.Log(item)
		}
	}()
}

func (l *logger) Debug(a ...interface{}) bool {
	if l.skip(LevelDebug) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelDebug}
	return true
}

func (l *logger) Info(a ...interface{}) bool {
	if l.skip(LevelInfo) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelInfo}
	return true
}

func (l *logger) Warn(a ...interface{}) bool {
	if l.skip(LevelWarn) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelWarn}
	return true
}

func (l *logger) Err(a ...interface{}) bool {
	if l.skip(LevelErr) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelErr}
	return true
}

func (l *logger) skip(level Level) bool {
	l.root.m.Lock()
	s := l.level < level
	l.root.m.Unlock()
	return s
}

func (l *logger) Stop(time.Duration) {
	close(l.root.c)
	close(l.root.stopChan)
}

func (l *logger) StopChan() <-chan stop.Signal {
	return l.root.stopChan
}

type logReporter struct {
	logger *log.Logger
	fatal  bool
}

// NewLogReporter gets a Reporter that writes to the specified
// log.Logger.
// If fatal is true, errors will call Fatalln on the logger, otherwise
// they will always call Println.
func NewLogReporter(logger *log.Logger, fatal bool) Reporter {
	return &logReporter{logger: logger}
}

func (l *logReporter) Log(log *Log) {
	args := []interface{}{strings.Join(log.Source, nestedLogSep) + ":"}
	for _, d := range log.Data {
		args = append(args, d)
	}

	if l.fatal && log.Level == LevelErr {
		l.logger.Fatalln(args...)
	} else {
		l.logger.Println(args...)
	}

}

// Stdout represents a reporter that writes to os.Stdout.
// Errors will also call os.Exit.
var Stdout = NewLogReporter(log.New(os.Stdout, "", log.LstdFlags), true)

type nilLogger struct{}

// NilLogger represents a zero memory Logger that always
// returns false on the methods.
var NilLogger nilLogger

var _ Logger = (*nilLogger)(nil) // ensure nilLogger is a valid Logger

func (_ nilLogger) Debug(a ...interface{}) bool { return false }
func (_ nilLogger) Info(a ...interface{}) bool  { return false }
func (_ nilLogger) Warn(a ...interface{}) bool  { return false }
func (_ nilLogger) Err(a ...interface{}) bool   { return false }
func (_ nilLogger) New(string) Logger           { return NilLogger }
func (_ nilLogger) SetSource(string)            {}
<commit_msg>made NilLogger implement RootLogger<commit_after>package slog

import (
	"log"
	"os"
	"strings"
	"sync"
	"time"

	"github.com/stretchr/pat/stop"
)

const nestedLogSep = "»"

// Level represents the level of logging.
type Level uint8

var levelStrs = map[Level]string{
	LevelInvalid: "(invalid)",
	LevelNothing: "none",
	LevelErr:     "error",
	LevelWarn:    "warning",
	LevelInfo:    "info",
	LevelDebug:   "debug",
}

// String gets the string representation of
// the Level.
func (l Level) String() string {
	if s, ok := levelStrs[l]; ok {
		return s
	}
	return levelStrs[LevelInvalid]
}

// ParseLevel gets the Level from the specified
// String.
func ParseLevel(s string) Level {
	s = strings.ToLower(s)
	for l, str := range levelStrs {
		log.Println(str, s)
		if strings.HasPrefix(str, s) {
			return l
		}
	}
	return LevelInvalid
}

const (
	// LevelInvalid represents an invalid Level.
	// Should never be used, use LevelNothing instead.
	LevelInvalid Level = iota

	// LevelNothing represents no logging.
	LevelNothing

	// LevelErr represents error level logging.
	LevelErr
	// LevelWarn represents warning level logging.
	LevelWarn
	// LevelInfo represents information level logging.
	LevelInfo
	// LevelDebug represents debug level logging.
	LevelDebug

	// LevelEverything logs everything.
	LevelEverything // must always be last value
)

// Log represents a single log item.
type Log struct {
	Level  Level
	When   time.Time
	Data   []interface{}
	Source []string
}

// Reporter represents types capable of doing something
// with logs.
type Reporter interface {
	Log(*Log)
}

// ReporterFunc is a function type capable of acting as
// a reporter.
type ReporterFunc func(*Log)

// Log calls the ReporterFunc.
func (f ReporterFunc) Log(l *Log) {
	f(l)
}

type reporters []Reporter

func (rs reporters) Log(l *Log) {
	for _, r := range rs {
		r.Log(l)
	}
}

// Reporters makes a Reporter that reports to multiple
// reporters in order.
func Reporters(rs ...Reporter) Reporter {
	return reporters(rs)
}

var _ Reporter = (Reporters)(nil)

// RootLogger represents a the root Logger that has
// more capabilities than a normal Logger.
// Normally, caller code would require the Logger interface only.
type RootLogger interface {
	stop.Stopper
	Logger
	// SetReporter sets the Reporter for this logger and
	// child loggers to use.
	SetReporter(r Reporter)
	// SetReporterFunc sets the specified ReporterFunc as
	// the Reporter.
	SetReporterFunc(f ReporterFunc)
	// SetLevel sets the level of this and all children loggers.
	SetLevel(level Level)
}

// Logger represents types capable of logging at
// different levels.
type Logger interface {
	// Info gets whether the logger is logging information or not,
	// and also makes such logs.
	Info(a ...interface{}) bool
	// Warn gets whether the logger is logging warnings or not,
	// and also makes such logs.
	Warn(a ...interface{}) bool
	// Err gets whether the logger is logging errors or not,
	// and also makes such logs.
	Err(a ...interface{}) bool
	// Debug gets whether the logger is logging errors or not,
	// and also makes such logs.
	Debug(a ...interface{}) bool
	// New creates a new child logger, with this as the parent.
	New(source string) Logger
	// SetSource sets the source of this logger.
	SetSource(source string)
}

type logger struct {
	m        sync.Mutex
	level    Level
	r        Reporter
	c        chan *Log
	src      []string
	stopChan chan stop.Signal
	root     *logger
}

var _ Logger = (*logger)(nil)

// New creates a new RootLogger, which is capable of acting
// like a Logger, used for logging.
// RootLogger is also a stop.Stopper and can have the
// Reporter specified, where children Logger types cannot.
// By default, the returned Logger will log to the slog.Stdout
// reporter, but this can be changed with SetReporter.
func New(source string, level Level) RootLogger {
	l := &logger{
		level: level,
		src:   []string{source},
		r:     Stdout,
	}
	l.root = l // use this one as the root one
	l.Start()
	return l
}

// New makes a new child logger with the specified source.
func (l *logger) New(source string) Logger {
	return &logger{
		level: l.level,
		src:   append(l.src, source),
		root:  l.root,
	}
}

func (l *logger) SetLevel(level Level) {
	l.root.m.Lock()
	l.root.level = level
	l.root.m.Unlock()
}

func (l *logger) SetSource(source string) {
	l.m.Lock()
	l.src[len(l.src)-1] = source
	l.m.Unlock()
}

func (l *logger) SetReporter(r Reporter) {
	l.root.Stop(stop.NoWait)
	<-l.root.StopChan()
	l.root.r = r
	l.root.Start()
}

func (l *logger) SetReporterFunc(f ReporterFunc) {
	l.SetReporter(f)
}

func (l *logger) Start() {
	l.root.c = make(chan *Log)
	l.root.stopChan = stop.Make()
	go func() {
		for item := range l.root.c {
			l.root.r.Log(item)
		}
	}()
}

func (l *logger) Debug(a ...interface{}) bool {
	if l.skip(LevelDebug) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelDebug}
	return true
}

func (l *logger) Info(a ...interface{}) bool {
	if l.skip(LevelInfo) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelInfo}
	return true
}

func (l *logger) Warn(a ...interface{}) bool {
	if l.skip(LevelWarn) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelWarn}
	return true
}

func (l *logger) Err(a ...interface{}) bool {
	if l.skip(LevelErr) {
		return false
	}
	if len(a) == 0 {
		return true
	}
	l.root.c <- &Log{When: time.Now(), Data: a, Source: l.src, Level: LevelErr}
	return true
}

func (l *logger) skip(level Level) bool {
	l.root.m.Lock()
	s := l.level < level
	l.root.m.Unlock()
	return s
}

func (l *logger) Stop(time.Duration) {
	close(l.root.c)
	close(l.root.stopChan)
}

func (l *logger) StopChan() <-chan stop.Signal {
	return l.root.stopChan
}

type logReporter struct {
	logger *log.Logger
	fatal  bool
}

// NewLogReporter gets a Reporter that writes to the specified
// log.Logger.
// If fatal is true, errors will call Fatalln on the logger, otherwise
// they will always call Println.
func NewLogReporter(logger *log.Logger, fatal bool) Reporter {
	return &logReporter{logger: logger}
}

func (l *logReporter) Log(log *Log) {
	args := []interface{}{strings.Join(log.Source, nestedLogSep) + ":"}
	for _, d := range log.Data {
		args = append(args, d)
	}

	if l.fatal && log.Level == LevelErr {
		l.logger.Fatalln(args...)
	} else {
		l.logger.Println(args...)
	}

}

// Stdout represents a reporter that writes to os.Stdout.
// Errors will also call os.Exit.
var Stdout = NewLogReporter(log.New(os.Stdout, "", log.LstdFlags), true)

type nilLogger struct{}

// NilLogger represents a zero memory Logger that always
// returns false on the methods.
var NilLogger nilLogger

var _ RootLogger = (*nilLogger)(nil) // ensure nilLogger is a valid Logger

func (n nilLogger) Debug(a ...interface{}) bool  { return false }
func (n nilLogger) Info(a ...interface{}) bool   { return false }
func (n nilLogger) Warn(a ...interface{}) bool   { return false }
func (n nilLogger) Err(a ...interface{}) bool    { return false }
func (n nilLogger) New(string) Logger            { return NilLogger }
func (n nilLogger) SetSource(string)             {}
func (n nilLogger) SetLevel(Level)               {}
func (n nilLogger) SetReporter(Reporter)         {}
func (n nilLogger) SetReporterFunc(ReporterFunc) {}
func (n nilLogger) Stop(time.Duration)           {}
func (n nilLogger) StopChan() <-chan stop.Signal { return nil }
<|endoftext|>


<commit_before>package sts

import (
	"crypto/tls"
	"fmt"
	"net"
	"net/smtp"
	"strings"
)

// CheckMXViaSMTP returns an error if a secure connection to the MX cannot be established.
func CheckMXViaSMTP(mx *net.MX) error {
	c, err := smtp.Dial(strings.TrimSuffix(mx.Host, ".") + ":25")
	if err != nil {
		return err
	}
	if err = c.StartTLS(&tls.Config{
		InsecureSkipVerify: false,
		ServerName:         mx.Host,
	}); err != nil {
		return err
	}
	if _, ok := c.TLSConnectionState(); !ok {
		return fmt.Errorf("Could not negotiate TLS with %v\n", mx.Host)
	}
	return nil
}
<commit_msg>Specify client hostname in EHLO.<commit_after>package sts

import (
	"crypto/tls"
	"fmt"
	"net"
	"net/smtp"
	"os"
	"strings"
)

func helloHostName() (string, error) {
	// Get the hostname for HELO/EHLO.
	h, err := os.Hostname()
	if err != nil {
		return h, err
	}
	// TODO: Do something more sophisticated here to try to guess the FQDN, I guess.
	return h, err
}

// CheckMXViaSMTP returns an error if a secure connection to the MX cannot be established.
func CheckMXViaSMTP(mx *net.MX) error {
	c, err := smtp.Dial(strings.TrimSuffix(mx.Host, ".") + ":25")
	if err != nil {
		return err
	}
	// Set hostname manually, because some hosts reject 'localhost', which is the default.
	h, err := helloHostName()
	if err != nil {
		return err
	}
	if err := c.Hello(h); err != nil {
		return err
	}
	if err = c.StartTLS(&tls.Config{
		InsecureSkipVerify: false,
		ServerName:         mx.Host,
	}); err != nil {
		return err
	}
	if _, ok := c.TLSConnectionState(); !ok {
		return fmt.Errorf("Could not negotiate TLS with %v\n", mx.Host)
	}
	return c.Quit()
	return nil
}
<|endoftext|>


<commit_before>package sorg

import (
	"os"
	"time"

	log "github.com/Sirupsen/logrus"
)

const (
	// Release is the asset version of the site. Bump when any assets are
	// updated to blow away any browser caches.
	Release = "8"
)

const (
	// ContentDir is the location of the site's content (articles, fragments,
	// assets, etc.).
	ContentDir = "./content"

	// LayoutsDir is the source directory for view layouts.
	LayoutsDir = "./layouts"

	// MainLayout is the site's main layout.
	MainLayout = LayoutsDir + "/main"

	// PagesDir is the source directory for one-off page content.
	PagesDir = "./pages"

	// ViewsDir is the source directory for views.
	ViewsDir = "./views"
)

// A list of all directories that are in the built static site.
var outputDirs = []string{
	".",
	"articles",
	"assets",
	"assets/" + Release,
	"assets/photos",
	"fragments",
	"photos",
	"reading",
	"runs",
	"twitter",
}

// CreateOutputDirs creates a target directory for the static site and all
// other necessary directories for the build if they don't already exist.
func CreateOutputDirs(targetDir string) error {
	start := time.Now()
	defer func() {
		log.Debugf("Created target directories in %v.", time.Now().Sub(start))
	}()

	for _, dir := range outputDirs {
		dir = targetDir + "/" + dir
		err := os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}
	}

	return nil
}

// InitLog initializes logging for singularity programs.
func InitLog(verbose bool) {
	log.SetFormatter(&plainFormatter{})

	if verbose {
		log.SetLevel(log.DebugLevel)
	}
}

// plainFormatter is a logrus formatter that displays text in a much more
// simple fashion that's more suitable as CLI output.
type plainFormatter struct {
}

// Format takes a logrus.Entry and returns bytes that are suitable for log
// output.
func (f *plainFormatter) Format(entry *log.Entry) ([]byte, error) {
	bytes := []byte(entry.Message + "\n")

	if entry.Level == log.DebugLevel {
		bytes = append([]byte("DEBUG: "), bytes...)
	}

	return bytes, nil
}
<commit_msg>Bump release to get new CSS<commit_after>package sorg

import (
	"os"
	"time"

	log "github.com/Sirupsen/logrus"
)

const (
	// Release is the asset version of the site. Bump when any assets are
	// updated to blow away any browser caches.
	Release = "9"
)

const (
	// ContentDir is the location of the site's content (articles, fragments,
	// assets, etc.).
	ContentDir = "./content"

	// LayoutsDir is the source directory for view layouts.
	LayoutsDir = "./layouts"

	// MainLayout is the site's main layout.
	MainLayout = LayoutsDir + "/main"

	// PagesDir is the source directory for one-off page content.
	PagesDir = "./pages"

	// ViewsDir is the source directory for views.
	ViewsDir = "./views"
)

// A list of all directories that are in the built static site.
var outputDirs = []string{
	".",
	"articles",
	"assets",
	"assets/" + Release,
	"assets/photos",
	"fragments",
	"photos",
	"reading",
	"runs",
	"twitter",
}

// CreateOutputDirs creates a target directory for the static site and all
// other necessary directories for the build if they don't already exist.
func CreateOutputDirs(targetDir string) error {
	start := time.Now()
	defer func() {
		log.Debugf("Created target directories in %v.", time.Now().Sub(start))
	}()

	for _, dir := range outputDirs {
		dir = targetDir + "/" + dir
		err := os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}
	}

	return nil
}

// InitLog initializes logging for singularity programs.
func InitLog(verbose bool) {
	log.SetFormatter(&plainFormatter{})

	if verbose {
		log.SetLevel(log.DebugLevel)
	}
}

// plainFormatter is a logrus formatter that displays text in a much more
// simple fashion that's more suitable as CLI output.
type plainFormatter struct {
}

// Format takes a logrus.Entry and returns bytes that are suitable for log
// output.
func (f *plainFormatter) Format(entry *log.Entry) ([]byte, error) {
	bytes := []byte(entry.Message + "\n")

	if entry.Level == log.DebugLevel {
		bytes = append([]byte("DEBUG: "), bytes...)
	}

	return bytes, nil
}
<|endoftext|>


<commit_before>package sorg

import (
	"os"
	"path"
	"time"

	log "github.com/Sirupsen/logrus"
)

const (
	// Release is the asset version of the site. Bump when any assets are
	// updated to blow away any browser caches.
	Release = "19"
)

const (
	// ContentDir is the location of the site's content (articles, fragments,
	// assets, etc.).
	ContentDir = "./content"

	// LayoutsDir is the source directory for view layouts.
	LayoutsDir = "./layouts"

	// MainLayout is the site's main layout.
	MainLayout = LayoutsDir + "/main"

	// PagesDir is the source directory for one-off page content.
	PagesDir = "./pages"

	// ViewsDir is the source directory for views.
	ViewsDir = "./views"
)

// A list of all directories that are in the built static site.
var outputDirs = []string{
	".",
	"articles",
	"assets",
	"assets/" + Release,
	"assets/photos",
	"fragments",
	"photos",
	"reading",
	"runs",
	"twitter",
}

// CreateOutputDirs creates a target directory for the static site and all
// other necessary directories for the build if they don't already exist.
func CreateOutputDirs(targetDir string) error {
	start := time.Now()
	defer func() {
		log.Debugf("Created target directories in %v.", time.Now().Sub(start))
	}()

	for _, dir := range outputDirs {
		dir = path.Join(targetDir, dir)
		err := os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}
	}

	return nil
}

// InitLog initializes logging for singularity programs.
func InitLog(verbose bool) {
	log.SetFormatter(&plainFormatter{})

	if verbose {
		log.SetLevel(log.DebugLevel)
	}
}

// plainFormatter is a logrus formatter that displays text in a much more
// simple fashion that's more suitable as CLI output.
type plainFormatter struct {
}

// Format takes a logrus.Entry and returns bytes that are suitable for log
// output.
func (f *plainFormatter) Format(entry *log.Entry) ([]byte, error) {
	bytes := []byte(entry.Message + "\n")

	if entry.Level == log.DebugLevel {
		bytes = append([]byte("DEBUG: "), bytes...)
	}

	return bytes, nil
}
<commit_msg>Bump assets version<commit_after>package sorg

import (
	"os"
	"path"
	"time"

	log "github.com/Sirupsen/logrus"
)

const (
	// Release is the asset version of the site. Bump when any assets are
	// updated to blow away any browser caches.
	Release = "20"
)

const (
	// ContentDir is the location of the site's content (articles, fragments,
	// assets, etc.).
	ContentDir = "./content"

	// LayoutsDir is the source directory for view layouts.
	LayoutsDir = "./layouts"

	// MainLayout is the site's main layout.
	MainLayout = LayoutsDir + "/main"

	// PagesDir is the source directory for one-off page content.
	PagesDir = "./pages"

	// ViewsDir is the source directory for views.
	ViewsDir = "./views"
)

// A list of all directories that are in the built static site.
var outputDirs = []string{
	".",
	"articles",
	"assets",
	"assets/" + Release,
	"assets/photos",
	"fragments",
	"photos",
	"reading",
	"runs",
	"twitter",
}

// CreateOutputDirs creates a target directory for the static site and all
// other necessary directories for the build if they don't already exist.
func CreateOutputDirs(targetDir string) error {
	start := time.Now()
	defer func() {
		log.Debugf("Created target directories in %v.", time.Now().Sub(start))
	}()

	for _, dir := range outputDirs {
		dir = path.Join(targetDir, dir)
		err := os.MkdirAll(dir, 0755)
		if err != nil {
			return err
		}
	}

	return nil
}

// InitLog initializes logging for singularity programs.
func InitLog(verbose bool) {
	log.SetFormatter(&plainFormatter{})

	if verbose {
		log.SetLevel(log.DebugLevel)
	}
}

// plainFormatter is a logrus formatter that displays text in a much more
// simple fashion that's more suitable as CLI output.
type plainFormatter struct {
}

// Format takes a logrus.Entry and returns bytes that are suitable for log
// output.
func (f *plainFormatter) Format(entry *log.Entry) ([]byte, error) {
	bytes := []byte(entry.Message + "\n")

	if entry.Level == log.DebugLevel {
		bytes = append([]byte("DEBUG: "), bytes...)
	}

	return bytes, nil
}
<|endoftext|>


<commit_before>package opentracing

import (
	"time"

	"golang.org/x/net/context"
)

// Span represents an active, un-finished span in the opentracing system.
//
// Spans are created by the Tracer interface and Span.StartChild.
type Span interface {
	// Creates and starts a child span.
	StartChild(operationName string) Span

	// Adds a tag to the span.
	//
	// Tag values can be of arbitrary types, however the treatment of complex
	// types is dependent on the underlying tracing system implementation.
	// It is expected that most tracing systems will handle primitive types
	// like strings and numbers. If a tracing system cannot understand how
	// to handle a particular value type, it may ignore the tag, but shall
	// not panic.
	//
	// If there is a pre-existing tag set for `key`, it is overwritten.
	SetTag(key string, value interface{}) Span

	// Sets the end timestamp and calls the `Recorder`s RecordSpan()
	// internally.
	//
	// Finish() should be the last call made to any span instance, and to do
	// otherwise leads to undefined behavior.
	Finish()

	// Suitable for serializing over the wire, etc.
	TraceContext() TraceContext

	// Event() is equivalent to
	//
	//   Log(time.Now(), LogData{Event: event})
	//
	LogEvent(event string)

	// EventWithPayload() is equivalent to
	//
	//   Log(time.Now(), LogData{Event: event, Payload: payload0})
	//
	LogEventWithPayload(event string, payload interface{})

	// Log() records `data` to this Span.
	//
	// See LogData for semantic details.
	Log(data LogData)

	// A convenience method. Equivalent to
	//
	//    var goCtx context.Context = ...
	//    var span Span = ...
	//    goCtx := opentracing.GoContextWithSpan(ctx, span)
	//
	//
	// NOTE: We use the term "GoContext" to minimize confusion with
	// TraceContext.
	AddToGoContext(goCtx context.Context) (Span, context.Context)
}

// See Span.Log(). Every LogData instance should specify at least one of Event
// and/or Payload.
type LogData struct {
	// The timestamp of the log record; if set to the default value (the unix
	// epoch), implementations should use time.Now() implicitly.
	Timestamp time.Time

	// Event (if non-empty) should be the stable name of some notable moment in
	// the lifetime of a Span. For instance, a Span representing a browser page
	// load might add an Event for each of the Performance.timing moments
	// here: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming
	//
	// While it is not a formal requirement, Event strings will be most useful
	// if they are *not* unique; rather, tracing systems should be able to use
	// them to understand how two similar Spans relate from an internal timing
	// perspective.
	Event string

	// Payload is a free-form potentially structured object which Tracer
	// implementations may retain and record all, none, or part of.
	//
	// If included, `Payload` should be restricted to data derived from the
	// instrumented application; in particular, it should not be used to pass
	// semantic flags to a Log() implementation.
	Payload interface{}
}
<commit_msg>e.g. and minor thing<commit_after>package opentracing

import (
	"time"

	"golang.org/x/net/context"
)

// Span represents an active, un-finished span in the opentracing system.
//
// Spans are created by the Tracer interface and Span.StartChild.
type Span interface {
	// Creates and starts a child span.
	StartChild(operationName string) Span

	// Adds a tag to the span.
	//
	// Tag values can be of arbitrary types, however the treatment of complex
	// types is dependent on the underlying tracing system implementation.
	// It is expected that most tracing systems will handle primitive types
	// like strings and numbers. If a tracing system cannot understand how
	// to handle a particular value type, it may ignore the tag, but shall
	// not panic.
	//
	// If there is a pre-existing tag set for `key`, it is overwritten.
	SetTag(key string, value interface{}) Span

	// Sets the end timestamp and calls the `Recorder`s RecordSpan()
	// internally.
	//
	// Finish() should be the last call made to any span instance, and to do
	// otherwise leads to undefined behavior.
	Finish()

	// Suitable for serializing over the wire, etc.
	TraceContext() TraceContext

	// LogEvent() is equivalent to
	//
	//   Log(time.Now(), LogData{Event: event})
	//
	LogEvent(event string)

	// LogEventWithPayload() is equivalent to
	//
	//   Log(time.Now(), LogData{Event: event, Payload: payload0})
	//
	LogEventWithPayload(event string, payload interface{})

	// Log() records `data` to this Span.
	//
	// See LogData for semantic details.
	Log(data LogData)

	// A convenience method. Equivalent to
	//
	//    var goCtx context.Context = ...
	//    var span Span = ...
	//    goCtx := opentracing.GoContextWithSpan(ctx, span)
	//
	//
	// NOTE: We use the term "GoContext" to minimize confusion with
	// TraceContext.
	AddToGoContext(goCtx context.Context) (Span, context.Context)
}

// See Span.Log(). Every LogData instance should specify at least one of Event
// and/or Payload.
type LogData struct {
	// The timestamp of the log record; if set to the default value (the unix
	// epoch), implementations should use time.Now() implicitly.
	Timestamp time.Time

	// Event (if non-empty) should be the stable name of some notable moment in
	// the lifetime of a Span. For instance, a Span representing a browser page
	// load might add an Event for each of the Performance.timing moments
	// here: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming
	//
	// While it is not a formal requirement, Event strings will be most useful
	// if they are *not* unique; rather, tracing systems should be able to use
	// them to understand how two similar Spans relate from an internal timing
	// perspective.
	Event string

	// Payload is a free-form potentially structured object which Tracer
	// implementations may retain and record all, none, or part of.
	//
	// If included, `Payload` should be restricted to data derived from the
	// instrumented application; in particular, it should not be used to pass
	// semantic flags to a Log() implementation.
	//
	// For example, an RPC system could log the wire contents in both
	// directions, or a SQL library could log the query (with or without
	// parameter bindings); tracing implementations may truncate or otherwise
	// record only a snippet of these payloads (or may strip out PII, etc,
	// etc).
	Payload interface{}
}
<|endoftext|>


<commit_before>package main

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

const version = "pre0.0"
const execName = "spdx-go"

var specVersions = []string{"SPDX-1.2"}

const (
	formatRdf  = "rdf"
	formatTag  = "tag"
	formatAuto = "auto"
)

var (
	flagConvert     = flag.String("c", "-", "Convert input file to the specified format.")
	flagValidate    = flag.Bool("v", false, "Set action to validate.")
	flagFmt         = flag.Bool("p", false, "Set action to format (pretty print).")
	flagOutput      = flag.String("o", "-", "Sets the output file. If not set, output is written to stdout.")
	flagInPlace     = flag.Bool("w", false, "If defined, it overwrites the input file.")
	flagIgnoreCase  = flag.Bool("i", false, "If defined, it ignores the case for properties. (e.g. treat \"packagename\" same as \"PackageName\")")
	flagInputFormat = flag.String("f", "auto", "Defines the format of the input. Valid values: rdf, tag or auto. Default is auto.")
	flagHelp        = flag.Bool("help", false, "Show help message.")
	flagVersion     = flag.Bool("version", false, "Show tool version and supported SPDX spec versions.")
)

var (
	input  = os.Stdin
	output = os.Stdout
)

func xor(a, b bool) bool { return a != b }

func main() {
	flag.Parse()

	if *flagHelp {
		help()
		return
	}

	if *flagVersion {
		printVersion()
		return
	}

	if !xor(*flagConvert != "-", xor(*flagValidate, *flagFmt)) {
		log.Fatal("No or invalid action flag specified. See -help for usage.")
	}

	*flagConvert = strings.ToLower(*flagConvert)
	if !validFormat(*flagConvert, false) {
		log.Fatalf("No or invalid output format (-f) specified (%s). Valid values are '%s' and '%s'.", *flagConvert, formatRdf, formatTag)
	}

	if !validFormat(*flagInputFormat, true) {
		log.Fatalf("Invalid input format (-f). Valid values are '%s', '%s' and '%s'.", formatRdf, formatTag, formatAuto)
	}

	if *flagInPlace && *flagOutput != "-" {
		log.Fatal("Cannot have both -w and -o set. See -help for usage.")
	}

	if flag.NArg() >= 1 {
		input, err := os.Open(flag.Arg(0))
		defer input.Close()
		if err != nil {
			log.Fatal(err.Error())
		}
	}

	if *flagOutput != "-" {
		output, err := os.Create(*flagOutput)
		defer output.Close()
		if err != nil {
			log.Fatal(err.Error())
		}
	} else if *flagInPlace {
		if input == os.Stdin {
			log.Fatal("Cannot use -w flag when input is stdin. Please specify an input file. See -help for usage.")
		}

		output, err := ioutil.TempFile("", "spdx-go_")
		if err != nil {
			log.Fatal(err.Error())
		}

		defer func() {
			output.Close()
			input.Close()
			CopyFile(output.Name(), input.Name())
			if err := os.Remove(output.Name()); err != nil {
				log.Fatal(err.Error())
			}
		}()
	}

	// auto-detect format
	if *flagInputFormat == formatAuto {
		format := detectFormat()
		flagInputFormat = &format
	}

	if *flagConvert != "-" {
		convert()
	} else if *flagValidate {
		validate()
	} else if *flagFmt {
		format()
	}
}

func validFormat(val string, allowAuto bool) bool {
	return val == formatRdf || val == formatTag || (val == formatAuto && allowAuto)
}

// Tries to guess the format of the input file.
// Current method:
// 1. If input file extension is .tag or .rdf, the format is Tag or RDF, respectively.
// 2. If the file starts with <?xml, <rdf, <!-- or @import, the format is RDF, otherwise Tag
func detectFormat() string {
	dot := strings.LastIndex(input.Name(), ".")
	if dot < 0 || dot+1 == len(input.Name()) {
		log.Fatal("Cannot auto-detect input format. Please specify format using the -f flag.")
	}

	// check extension (if .tag or .rdf)
	format := strings.ToLower(input.Name()[dot+1:])
	if validFormat(format, false) {
		return format
	}

	// Needs improvement but not a priority.
	// Only detects XML RDF or files starting @import (turtle format) as RDF
	scanner := bufio.NewScanner(input)
	scanner.Split(bufio.ScanWords)
	if scanner.Scan() {
		word := strings.ToLower(scanner.Text())
		if strings.HasPrefix(word, "<?xml") || strings.HasPrefix(word, "<rdf") || strings.HasPrefix(word, "<!--") || strings.HasPrefix(word, "@import") {
			return formatRdf
		}
		return formatTag
	}

	log.Fatal("Cannot auto-detect input format from file extension. Please use -f flag.")
	return ""
}

func convert() {

}

func validate() {
}

func format() {
}

func help() {
	printVersion()

	fmt.Printf("\nUsage: spdx-go [<flags>] [<input file>]\n")
	fmt.Println("Stdin is used as input if <input-file> is not specified.")

	fmt.Println("Exactly ONE of the -conv, -fmt or -valid flags MUST be specified.\n")

	flag.PrintDefaults()
}

func printVersion() {
	fmt.Printf("spdx-go version %s.\nSupporting SPDX specifications %s.\n", version, strings.Join(specVersions, ", "))
}
<commit_msg>Initial convertion implementation<commit_after>package main

import (
	"github.com/vladvelici/spdx-go/spdx"
	"github.com/vladvelici/spdx-go/tag"
)

import (
	"bufio"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"
)

const version = "pre0.0"
const execName = "spdx-go"

var specVersions = []string{"SPDX-1.2"}

const (
	formatRdf  = "rdf"
	formatTag  = "tag"
	formatAuto = "auto"
)

var (
	flagConvert     = flag.String("c", "-", "Convert input file to the specified format.")
	flagValidate    = flag.Bool("v", false, "Set action to validate.")
	flagFmt         = flag.Bool("p", false, "Set action to format (pretty print).")
	flagOutput      = flag.String("o", "-", "Sets the output file. If not set, output is written to stdout.")
	flagInPlace     = flag.Bool("w", false, "If defined, it overwrites the input file.")
	flagIgnoreCase  = flag.Bool("i", false, "If defined, it ignores the case for properties. (e.g. treat \"packagename\" same as \"PackageName\")")
	flagInputFormat = flag.String("f", "auto", "Defines the format of the input. Valid values: rdf, tag or auto. Default is auto.")
	flagHelp        = flag.Bool("help", false, "Show help message.")
	flagVersion     = flag.Bool("version", false, "Show tool version and supported SPDX spec versions.")
)

var (
	input  = os.Stdin
	output = os.Stdout
)

func xor(a, b bool) bool { return a != b }

func main() {
	flag.Parse()

	if *flagHelp {
		help()
		return
	}

	if *flagVersion {
		printVersion()
		return
	}

	if !xor(*flagConvert != "-", xor(*flagValidate, *flagFmt)) {
		log.Fatal("No or invalid action flag specified. See -help for usage.")
	}

	*flagConvert = strings.ToLower(*flagConvert)
	if !validFormat(*flagConvert, false) {
		log.Fatalf("No or invalid output format (-f) specified (%s). Valid values are '%s' and '%s'.", *flagConvert, formatRdf, formatTag)
	}

	if !validFormat(*flagInputFormat, true) {
		log.Fatalf("Invalid input format (-f). Valid values are '%s', '%s' and '%s'.", formatRdf, formatTag, formatAuto)
	}

	if *flagInPlace && *flagOutput != "-" {
		log.Fatal("Cannot have both -w and -o set. See -help for usage.")
	}

	if flag.NArg() >= 1 {
		var err error
		input, err = os.Open(flag.Arg(0))
		defer input.Close()
		if err != nil {
			log.Fatal(err.Error())
		}
	}

	if *flagOutput != "-" {
		var err error
		output, err = os.Create(*flagOutput)
		defer output.Close()
		if err != nil {
			log.Fatal(err.Error())
		}
	} else if *flagInPlace {
		if input == os.Stdin {
			log.Fatal("Cannot use -w flag when input is stdin. Please specify an input file. See -help for usage.")
		}
		var err error
		output, err = ioutil.TempFile("", "spdx-go_")
		if err != nil {
			log.Fatal(err.Error())
		}

		defer func() {
			output.Close()
			input.Close()
			CopyFile(output.Name(), input.Name())
			if err := os.Remove(output.Name()); err != nil {
				log.Fatal(err.Error())
			}
		}()
	}

	// auto-detect format
	if *flagInputFormat == formatAuto {
		format := detectFormat()
		flagInputFormat = &format
	}

	if *flagConvert != "-" {
		convert()
	} else if *flagValidate {
		validate()
	} else if *flagFmt {
		format()
	}
}

func validFormat(val string, allowAuto bool) bool {
	return val == formatRdf || val == formatTag || (val == formatAuto && allowAuto)
}

// Tries to guess the format of the input file. Does not work on stdin.
// Current method:
// 1. If input file extension is .tag or .rdf, the format is Tag or RDF, respectively.
// 2. If the file starts with <?xml, <rdf, <!-- or @import, the format is RDF, otherwise Tag
func detectFormat() string {
	if input == os.Stdin {
		log.Fatal("Cannot auto-detect format from stdin.")
	}

	if dot := strings.LastIndex(input.Name(), "."); dot+1 < len(input.Name()) {
		// check extension (if .tag or .rdf)
		format := strings.ToLower(input.Name()[dot+1:])
		if validFormat(format, false) {
			return format
		}
	}

	// Needs improvement but not a priority.
	// Only detects XML RDF or files starting with @import (turtle format) as RDF
	defer func() {
		input.Close()
		var err error
		input, err = os.Open(input.Name())
		if err != nil {
			log.Fatal(err.Error())
		}
	}()
	scanner := bufio.NewScanner(input)
	scanner.Split(bufio.ScanWords)
	if scanner.Scan() {
		word := strings.ToLower(scanner.Text())
		if strings.HasPrefix(word, "<?xml") || strings.HasPrefix(word, "<rdf") || strings.HasPrefix(word, "<!--") || strings.HasPrefix(word, "@import") {
			return formatRdf
		}
		return formatTag
	}

	log.Fatal("Cannot auto-detect input format from file extension. Please use -f flag.")
	return ""
}

func convert() {
	var doc *spdx.Document
	var err error

	if *flagInputFormat == formatTag {
		doc, err = tag.Parse(input)
	} else {
		// doc, err = rdf.Parse(input)
	}

	if err != nil {
		log.Fatal(err)
	}

	if *flagConvert == formatTag {
		tag.Write(output, doc)
	} else {
		// rdf write
	}
}

func validate() {
}

func format() {
}

func help() {
	printVersion()

	fmt.Printf("\nUsage: spdx-go [<flags>] [<input file>]\n")
	fmt.Println("Stdin is used as input if <input-file> is not specified.")

	fmt.Println("Exactly ONE of the -conv, -fmt or -valid flags MUST be specified.\n")

	flag.PrintDefaults()
}

func printVersion() {
	fmt.Printf("spdx-go version %s.\nSupporting SPDX specifications %s.\n", version, strings.Join(specVersions, ", "))
}
<|endoftext|>


<commit_before>package main

import (
  "fmt"
  "github.com/gographics/imagick/imagick"
  "github.com/majestrate/srndv2/src/srnd"
  "os"
  "log"
)

func main() {
  var daemon srnd.NNTPDaemon
  if len(os.Args) > 1 {
    action := os.Args[1]
    if action == "setup" {
      log.Println("Setting up SRNd base...")
      daemon.Setup()
      log.Println("Setup Done")
    } else if action == "run" {
      log.Println("Starting up SRNd...")
      if daemon.Init() {
        imagick.Initialize()
        daemon.Run()
        imagick.Terminate()
      } else {
        log.Println("Failed to initialize")
      }
    } else {
      log.Println("Invalid action:",action)
    }
  } else {
    fmt.Fprintf(os.Stdout, "Usage: %s [setup|run]\n", os.Args[0])
  }
}
<commit_msg>tag version 0.1<commit_after>package main

import (
  "fmt"
  "github.com/gographics/imagick/imagick"
  "github.com/majestrate/srndv2/src/srnd"
  "os"
  "log"
)

func main() {
  var daemon srnd.NNTPDaemon
  if len(os.Args) > 1 {
    fmt.Fprintf(os.Stdout,"Starting up %s\n\n\n", srnd.Version())
    action := os.Args[1]
    if action == "setup" {

      log.Println("Setting up SRNd base...")
      daemon.Setup()
      log.Println("Setup Done")
    } else if action == "run" {
      log.Println("Starting up SRNd...")
      if daemon.Init() {
        imagick.Initialize()
        daemon.Run()
        imagick.Terminate()
      } else {
        log.Println("Failed to initialize")
      }
    } else {
      log.Println("Invalid action:",action)
    }
  } else {
    fmt.Fprintf(os.Stdout, "Usage: %s [setup|run]\n", os.Args[0])
  }
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"io"
	"log"

	gossh "golang.org/x/crypto/ssh"
	"ireul.com/bastion/models"
	"ireul.com/bastion/sandbox"
	"ireul.com/bastion/utils"
	"ireul.com/cli"
	"ireul.com/ssh"
)

// sshdCommand 用来提供 authorized_keys 中的自定义 command
var sshdCommand = cli.Command{
	Name:   "sshd",
	Usage:  "start sshd server",
	Action: execSSHDCommand,
}

func execSSHDCommand(c *cli.Context) (err error) {
	// setup log
	log.SetPrefix("[bastion-sshd] ")

	// decode config
	var cfg *utils.Config
	if cfg, err = utils.ParseConfigFile(c.GlobalString("config")); err != nil {
		log.Fatalln(err)
		return
	}

	// create models.DB
	var db *models.DB
	if db, err = models.NewDB(cfg); err != nil {
		log.Fatalln(err)
		return
	}
	db.AutoMigrate()

	// create sandbox.Manager
	smc := sandbox.ManagerOptions{
		Image:   cfg.Sandbox.Image,
		DataDir: cfg.Sandbox.DataDir,
	}
	var sm sandbox.Manager
	if sm, err = sandbox.NewManager(smc); err != nil {
		log.Fatalln(err)
		return
	}

	// handle
	ssh.Handle(func(s ssh.Session) {
		// extract User
		u := s.Context().Value("User").(models.User)
		// ensure command
		cmd := s.Command()
		if len(cmd) == 0 {
			cmd = []string{"/bin/bash"}
		}
		// get sandbox
		snb, err := sm.GetOrCreate(u)
		if err != nil {
			io.WriteString(s, fmt.Sprintf("Internal Error: %s\n", err.Error()))
			s.Exit(1)
			return
		}
		// attach sandbox
		pty, sshwinch, isPty := s.Pty()

		snbwinch := make(chan sandbox.Window, 1)

		// create opts
		opts := sandbox.ExecAttachOptions{
			Command:    cmd,
			Reader:     s,
			Writer:     s,
			IsPty:      isPty,
			WindowChan: snbwinch,
			Term:       pty.Term,
		}

		// convert channel sshwinch -> snbwinch
		go func() {
			for {
				s, live := <-sshwinch
				if live {
					snbwinch <- sandbox.Window{Height: uint(s.Height), Width: uint(s.Width)}
				} else {
					close(snbwinch)
					break
				}
			}
		}()

		err = sm.ExecAttach(snb, opts)

		if err != nil {
			log.Printf("ERROR: Sandbox ExecAttach Failed: %s\n", err.Error())
		}
	})

	// options
	options := []ssh.Option{
		// set host_key
		ssh.HostKeyFile(cfg.SSHD.HostKeyFile),
		// auth public_key
		ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
			// get fingerprint
			fp := gossh.FingerprintSHA256(key)
			// find SSHKey
			k := models.SSHKey{}
			db.Where("fingerprint = ?", fp).First(&k)
			if db.NewRecord(k) {
				log.Printf("ERROR: Invalid Key, FP=%s", fp)
				return false
			}
			// find User
			u := models.User{}
			db.First(&u, k.UserID)
			if db.NewRecord(u) || u.IsBlocked {
				log.Printf("ERROR: User Not Found / Blocked, UserID=%d, FP=%s\n", k.UserID, fp)
				return false
			}
			// set User.ID
			log.Printf("Signed In, UserID=%d, FP=%s\n", k.UserID, fp)
			ctx.SetValue("User", u)
			return true
		}),
	}

	log.Printf("Listening at %s:%d\n", cfg.SSHD.Host, cfg.SSHD.Port)
	log.Fatal(ssh.ListenAndServe(fmt.Sprintf("%s:%d", cfg.SSHD.Host, cfg.SSHD.Port), nil, options...))
	return nil
}
<commit_msg>refine code<commit_after>package main

import (
	"fmt"
	"io"
	"log"

	gossh "golang.org/x/crypto/ssh"
	"ireul.com/bastion/models"
	"ireul.com/bastion/sandbox"
	"ireul.com/bastion/utils"
	"ireul.com/cli"
	"ireul.com/ssh"
)

// sshdCommand 用来提供 authorized_keys 中的自定义 command
var sshdCommand = cli.Command{
	Name:   "sshd",
	Usage:  "start sshd server",
	Action: execSSHDCommand,
}

func execSSHDCommand(c *cli.Context) (err error) {
	// setup log
	log.SetPrefix("[bastion-sshd] ")

	// decode config
	var cfg *utils.Config
	if cfg, err = utils.ParseConfigFile(c.GlobalString("config")); err != nil {
		log.Fatalln(err)
		return
	}

	// create models.DB
	var db *models.DB
	if db, err = models.NewDB(cfg); err != nil {
		log.Fatalln(err)
		return
	}
	db.AutoMigrate()

	// create sandbox.Manager
	smc := sandbox.ManagerOptions{
		Image:   cfg.Sandbox.Image,
		DataDir: cfg.Sandbox.DataDir,
	}
	var sm sandbox.Manager
	if sm, err = sandbox.NewManager(smc); err != nil {
		log.Fatalln(err)
		return
	}

	// handle
	ssh.Handle(func(s ssh.Session) {
		// extract User
		u := s.Context().Value("User").(models.User)
		// ensure command
		cmd := s.Command()
		if len(cmd) == 0 {
			cmd = []string{"/bin/bash"}
		}
		// get sandbox
		snb, err := sm.GetOrCreate(u)
		if err != nil {
			io.WriteString(s, fmt.Sprintf("Internal Error: %s\n", err.Error()))
			s.Exit(1)
			return
		}
		// attach sandbox
		pty, sshwinch, isPty := s.Pty()

		// create opts
		opts := sandbox.ExecAttachOptions{
			Command: cmd,
			Reader:  s,
			Writer:  s,
			IsPty:   isPty,
			Term:    pty.Term,
		}

		if isPty {
			snbwinch := make(chan sandbox.Window, 1)
			opts.WindowChan = snbwinch

			// convert channel sshwinch -> snbwinch
			go func() {
				for {
					s, live := <-sshwinch
					if live {
						snbwinch <- sandbox.Window{Height: uint(s.Height), Width: uint(s.Width)}
					} else {
						close(snbwinch)
						break
					}
				}
			}()
		}

		err = sm.ExecAttach(snb, opts)

		if err != nil {
			log.Printf("ERROR: Sandbox ExecAttach Failed: %s\n", err.Error())
		}
	})

	// options
	options := []ssh.Option{
		// set host_key
		ssh.HostKeyFile(cfg.SSHD.HostKeyFile),
		// auth public_key
		ssh.PublicKeyAuth(func(ctx ssh.Context, key ssh.PublicKey) bool {
			// get fingerprint
			fp := gossh.FingerprintSHA256(key)
			// find SSHKey
			k := models.SSHKey{}
			db.Where("fingerprint = ?", fp).First(&k)
			if db.NewRecord(k) {
				log.Printf("ERROR: Invalid Key, FP=%s", fp)
				return false
			}
			// find User
			u := models.User{}
			db.First(&u, k.UserID)
			if db.NewRecord(u) || u.IsBlocked {
				log.Printf("ERROR: User Not Found / Blocked, UserID=%d, FP=%s\n", k.UserID, fp)
				return false
			}
			// set User.ID
			log.Printf("Signed In, UserID=%d, FP=%s\n", k.UserID, fp)
			ctx.SetValue("User", u)
			return true
		}),
	}

	log.Printf("Listening at %s:%d\n", cfg.SSHD.Host, cfg.SSHD.Port)
	log.Fatal(ssh.ListenAndServe(fmt.Sprintf("%s:%d", cfg.SSHD.Host, cfg.SSHD.Port), nil, options...))
	return nil
}
<|endoftext|>


<commit_before>package suid

import (
	"fmt"
	"sync"
	"time"
)

var maxSeq = 1<<10 - 1 //  63 bit(total) - 41 bit(ms) - 12 bit(appId) = 10

type suid struct {
	appId     int64
	seq       int
	currentMs int64
	sync.Mutex
}

func NewSUID(appId int) *suid {
	if appId >= 2048 {
		panic("App Id cannot be more than 4096")
	}

	return &suid{
		appId: int64(appId) << 12,
		seq:   0,
	}
}

func (s *suid) Generate() (int64, error) {
	var id, ms int64
	ms = time.Now().UnixNano() / 1e6
	// ms goes to head
	id = ms << 22 // 63 bit - 41 bit(ms)
	// set appId into middle
	id |= s.appId
	// generate sequence
	seq, err := s.nextSeq(ms)
	if err != nil {
		return int64(0), err
	}

	// generated sequence goes to the end
	id |= seq

	return id, nil
}

func (s *suid) nextSeq(ms int64) (int64, error) {
	s.Lock()
	if s.currentMs > ms {
		return int64(0), fmt.Errorf("Time goes backward in this machine")
	}

	if s.currentMs < ms {
		s.currentMs = ms
		s.seq = -1
	}

	s.seq++
	if s.seq > maxSeq {
		return int64(0), fmt.Errorf("You created more than %d ids in one milisecond", maxSeq)
	}

	s.Unlock()
	return int64(s.seq), nil
}
<commit_msg>Suid: parameterize shifters<commit_after>package suid

import (
	"fmt"
	"sync"
	"time"
)

const (
	appIdBitCount = 12
	seqBitCount   = 10
	MaxAppId      = 1<<appIdBitCount - 1 //  63 bit(total) - 41 bit(ms) - 12 bit(appId) = 10
	MaxSeq        = 1<<seqBitCount - 1   //  63 bit(total) - 41 bit(ms) - 12 bit(appId) = 10
)

type suid struct {
	appId     int64
	seq       int
	currentMs int64
	sync.Mutex
}

func NewSUID(appId int) *suid {
	if appId > MaxAppId {
		panic("App Id cannot be more than 4096")
	}

	return &suid{
		appId: int64(appId) << appIdBitCount,
		seq:   0,
	}
}

func (s *suid) Generate() (int64, error) {
	var id, ms int64
	ms = time.Now().UnixNano() / 1e6
	// ms goes to head
	id = ms << 22 // 63 bit - 41 bit(ms)
	// set appId into middle
	id |= s.appId
	// generate sequence
	seq, err := s.nextSeq(ms)
	if err != nil {
		return int64(0), err
	}

	// generated sequence goes to the end
	id |= seq

	return id, nil
}

func (s *suid) nextSeq(ms int64) (int64, error) {
	s.Lock()
	if s.currentMs > ms {
		return int64(0), fmt.Errorf("Time goes backward in this machine")
	}

	if s.currentMs < ms {
		s.currentMs = ms
		s.seq = -1
	}

	s.seq++
	if s.seq > MaxSeq {
		return int64(0), fmt.Errorf("You created more than %d ids in one milisecond", MaxSeq)
	}

	s.Unlock()
	return int64(s.seq), nil
}
<|endoftext|>