Exemplo n.º 1
0
func TestMapCounter(t *testing.T) {
	colours := NewMap("bike-shed-colours")

	colours.Add("red", 1)
	colours.Add("red", 2)
	colours.Add("blue", 4)
	if x := colours.m["red"].(*Int).i; x != 3 {
		t.Errorf("colours.m[\"red\"] = %v, want 3", x)
	}
	if x := colours.m["blue"].(*Int).i; x != 4 {
		t.Errorf("colours.m[\"blue\"] = %v, want 4", x)
	}

	// colours.String() should be '{"red":3, "blue":4}',
	// though the order of red and blue could vary.
	s := colours.String()
	j, err := json.Decode(s)
	if err != nil {
		t.Errorf("colours.String() isn't valid JSON: %v", err)
	}
	m, ok := j.(map[string]interface{})
	if !ok {
		t.Error("colours.String() didn't produce a map.")
	}
	red := m["red"]
	x, ok := red.(float64)
	if !ok {
		t.Error("red.Kind() is not a number.")
	}
	if x != 3 {
		t.Error("red = %v, want 3", x)
	}
}
Exemplo n.º 2
0
func ReadConfig(filename string) {
	contents, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Exitf("Impossible to read %s", filename, err)
	}
	data, err := json.Decode(string(contents))
	if err != nil {
		log.Exitf("Can't parse %s as JSON", filename, err)
	}
	config = map[string]string{}
	for key, value := range data.(map[string]interface{}) {
		config[key] = value.(string)
	}
}
Exemplo n.º 3
0
func convo(chfromus chan []string, chfromomegle chan []string, die chan bool, name string) {
	resp, err := httpv2.Post("http://omegle.com/start?rcs=1", "application/x-www-form-urlencoded", nil, nil)

	if err == nil {
		data, _ := ioutil.ReadAll(resp.Body)
		id, _ := json.Decode(string(data))
		headers := map[string]string{"Content-Length": "9"}
		requestChan := make(chan Req)

		resp, err = httpv2.Post("http://omegle.com/events", "application/x-www-form-urlencoded", headers, &StringReader{data: "id=" + id.(string)})
		if err == nil {

			msgbody := "id=" + id.(string) + "&msg=" + httpv2.URLEscape(flag.Arg(0))
			sendHeaders := map[string]string{"Content-Length": strconv.Itoa(len(msgbody))}
			fmt.Printf("->")
			httpv2.Post("http://omegle.com/send", "application/x-www-form-urlencoded", sendHeaders, &StringReader{data: msgbody})

			go func() {
				for {
					resp, err2 := httpv2.Post("http://omegle.com/events", "application/x-www-form-urlencoded", headers, &StringReader{data: "id=" + id.(string)})
					response := Req{Resp: resp, Err: err2}
					requestChan <- response
				}
			}()
			for {

				select {
				case r := <-requestChan:
					if r.Err == nil {
						data, _ = ioutil.ReadAll(r.Resp.Body)
						//                      fmt.Printf("%s   ", string(data))
						js, err := json.Decode(string(data))
						if err != nil {
							fmt.Printf("JSON Decoding error, %s\n", err)
							break
						}
						if string(data) == "[]" {
							continue
						}
						events := js.([]interface{})

						for i := 0; i < len(events); i++ {
							e := events[i].([]interface{})
							switch {
							case e[0].(string) == "typing":
								lol := make([]string, 1)
								lol[0] = "typing"
								fmt.Printf("...\n")
								chfromomegle <- lol
							case e[0].(string) == "gotMessage":
								fmt.Printf("%s: %s\n", name, e[1].(string))
								lol := make([]string, 2)
								lol[0] = "msg"
								lol[1] = e[1].(string)
								chfromomegle <- lol
							case e[0].(string) == "stoppedTyping":
								lol := make([]string, 1)
								lol[0] = "stopped"
								chfromomegle <- lol
							case e[0].(string) == "strangerDisconnected":
								lol := make([]string, 1)
								lol[0] = "disconnect"
								chfromomegle <- lol
								fmt.Printf("%s disconnected\n", name)
								die <- true
								return

							}
						}

					} else {
						break
					}

				case msg := <-chfromus:
					if msg[0] == "typing" {
						msgbody := "id=" + id.(string)
						sendHeaders := map[string]string{"Content-Length": strconv.Itoa(len(msgbody))}
						httpv2.Post("http://omegle.com/typing", "application/x-www-form-urlencoded", sendHeaders, &StringReader{data: msgbody})
					} else if msg[0] == "msg" {
						msgbody := "id=" + id.(string) + "&msg=" + httpv2.URLEscape(msg[1])
						sendHeaders := map[string]string{"Content-Length": strconv.Itoa(len(msgbody))}
						fmt.Printf("->")
						httpv2.Post("http://omegle.com/send", "application/x-www-form-urlencoded", sendHeaders, &StringReader{data: msgbody})
					} else if msg[0] == "stopped" {
						msgbody := "id=" + id.(string)
						sendHeaders := map[string]string{"Content-Length": strconv.Itoa(len(msgbody))}
						httpv2.Post("http://omegle.com/stoppedtyping", "application/x-www-form-urlencoded", sendHeaders, &StringReader{data: msgbody})
					} else if msg[0] == "disconnect" {
						msgbody := "id=" + id.(string)
						sendHeaders := map[string]string{"Content-Length": strconv.Itoa(len(msgbody))}
						httpv2.Post("http://omegle.com/disconnect", "application/x-www-form-urlencoded", sendHeaders, &StringReader{data: msgbody})
						die <- true
						return
					}
				}
			}
		} else {
			fmt.Printf("Somethings terribly wrong here %s\n ", err)
		}
	} else {
		fmt.Printf("Error: %s\n", err)
	}
}