func addFriend(ws *websocket.Conn, req bsimjson.Bsimjson) { _, mname, _, _, _ := bsimdb.FindUserBySession(req.Who) err := bsimdb.AddFriendToGroup(mname, req.ToWho[0], "friends") fmt.Println("add err=", err) req.Msg = "OK" if err != nil { req.Msg = "NO" } s, _ := bsimjson.Bsimjson2json(req) if err := websocket.Message.Send(ws, s); err != nil { fmt.Println("Can't not send to client") } }
func index(w http.ResponseWriter, r *http.Request) { r.ParseForm() cookie, err := r.Cookie("ss") if err != nil { http.Redirect(w, r, "/ii", 307) return } else { fmt.Println("GET ", cookie.Value) id, name, email, password, err := bsimdb.FindUserBySession(cookie.Value) fmt.Println("IIII: ", id, name, email, password, err) if err == nil { fmt.Println("in room") http.ServeFile(w, r, "./html/chat.html") } else { fmt.Println("in signin") http.Redirect(w, r, "/ii", 307) } } }
func pingReply(ws *websocket.Conn, req bsimjson.Bsimjson) { if req.Msg == "start" { var ret bsimjson.Bsimjson _, mname, _, _, _ := bsimdb.FindUserBySession(req.Who) ret.Type = "ping" ret.Who = mname var friends []string friends, _ = bsimdb.FindUsersFriends(mname) ret.ToWho = friends ret.Msg = "friends" s, _ := bsimjson.Bsimjson2json(ret) if err := websocket.Message.Send(ws, s); err != nil { fmt.Println("Can't not send to client") } } else if req.Msg == "msg" || req.Msg == "msgAll" { ret, err := bsimdb.PingMsgReply(req) if err != nil { log.Fatal(err) return } if req.Msg == "msg" && len(ret) > 50 { ret = ret[len(ret)-50:] } for i := 0; i < len(ret); i++ { if ma, _ := regexp.MatchString("(?i:\\<script[\\S\\s]+?\\</script\\>)", ret[i].Msg); ma { ret[i].Msg = `<div class="alert alert-warning" role="alert"> <strong>Sorry.</strong> We modify this massage a little bit for safty.</div>` + html.EscapeString(ret[i].Msg) } ret[i].Msg = string(blackfriday.MarkdownCommon([]byte(ret[i].Msg))) s, _ := bsimjson.Bsimjson2json(ret[i]) if err := websocket.Message.Send(ws, s); err != nil { fmt.Println("Can't not send to client") } } } }