diff options
Diffstat (limited to 'main.go')
-rw-r--r-- | main.go | 253 |
1 files changed, 225 insertions, 28 deletions
@@ -37,9 +37,7 @@ var activitystreams = "application/ld+json; profile=\"https://www.w3.org/ns/acti func main() { - if _, err := os.Stat("./public"); os.IsNotExist(err) { - os.Mkdir("./public", 0755) - } + CreatedNeededDirectories() InitCache() @@ -48,7 +46,7 @@ func main() { defer db.Close() RunDatabaseSchema(db) - + go MakeCaptchas(db, 100) *Key = CreateClientKey() @@ -188,6 +186,7 @@ func main() { page, _ := strconv.Atoi(postNum) collection, valid := WantToServePage(db, actor.Name, page) + if valid { OutboxGet(w, r, db, collection) } @@ -471,9 +470,14 @@ func main() { FollowingBoards = GetActorFollowingDB(db, Domain) - Boards = GetBoardCollection(db) + Boards = GetBoardCollection(db) + + var redirect string + if(actor.Name != "main") { + redirect = "/" + actor.Name + } - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, "/" + *Key + "/" + redirect, http.StatusSeeOther) } else if manage && actor.Name != "" { t := template.Must(template.ParseFiles("./static/main.html", "./static/manage.html")) @@ -645,7 +649,7 @@ func main() { Boards = GetBoardCollection(db) } - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, "/" + *Key, http.StatusSeeOther) }) http.HandleFunc("/verify", func(w http.ResponseWriter, r *http.Request){ @@ -706,8 +710,10 @@ func main() { http.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request){ id := r.URL.Query().Get("id") - board := r.URL.Query().Get("board") - actor := GetActorFromPath(db, id, "/") + manage := r.URL.Query().Get("manage") + board := r.URL.Query().Get("board") + col := GetCollectionFromID(id) + actor := col.OrderedItems[0].Actor _, auth := GetPasswordFromSession(r) if id == "" || auth == "" { @@ -722,29 +728,58 @@ func main() { return } + var obj ObjectBase + obj.Id = id + obj.Actor = actor + + isOP := CheckIfObjectOP(db, obj.Id) + + var OP string + if len(col.OrderedItems[0].InReplyTo) > 0 { + OP = col.OrderedItems[0].InReplyTo[0].Id + } + if !IsIDLocal(db, id) { - CreateLocalDeleteDB(db, id, "post") - CloseLocalReportDB(db, id, board) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + if(!isOP) { + CloseLocalReportDB(db, id, board) + CreateLocalDeleteDB(db, id, "post") + } else { + + } + if(manage == "t") { + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) + } else if(OP != ""){ + http.Redirect(w, r, "/" + board + "/" + remoteShort(OP), http.StatusSeeOther) + } else { + http.Redirect(w, r, "/" + board, http.StatusSeeOther) + } + return } - var obj ObjectBase - obj.Id = id - obj.Actor = &actor - - isOP := CheckIfObjectOP(db, obj.Id) + if !isOP { + DeleteReportActivity(db, id) DeleteObjectRequest(db, id) DeleteObject(db, obj.Id) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + if(manage == "t"){ + http.Redirect(w, r, "/" + *Key + "/" + board , http.StatusSeeOther) + }else{ + http.Redirect(w, r, OP, http.StatusSeeOther) + } return + } else { + DeleteReportActivity(db, id) DeleteObjectAndRepliesRequest(db, id) DeleteObjectAndReplies(db, obj.Id) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + if(manage == "t"){ + http.Redirect(w, r, "/" + *Key + "/" + board , http.StatusSeeOther) + }else{ + http.Redirect(w, r, "/" + board, http.StatusSeeOther) + } return } @@ -755,7 +790,18 @@ func main() { http.HandleFunc("/deleteattach", func(w http.ResponseWriter, r *http.Request){ id := r.URL.Query().Get("id") + manage := r.URL.Query().Get("manage") + board := r.URL.Query().Get("board") + col := GetCollectionFromID(id) + actor := col.OrderedItems[0].Actor + var OP string + if (len(col.OrderedItems[0].InReplyTo) > 0 && col.OrderedItems[0].InReplyTo[0].Id != "") { + OP = col.OrderedItems[0].InReplyTo[0].Id + } else { + OP = id + } + _, auth := GetPasswordFromSession(r) if id == "" || auth == "" { @@ -764,23 +810,32 @@ func main() { return } - actor := GetActorFromPath(db, id, "/") - if !HasAuth(db, auth, actor.Id) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("")) return - } + } + + if !IsIDLocal(db, id) { CreateLocalDeleteDB(db, id, "attachment") - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + if(manage == "t") { + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) + } else { + http.Redirect(w, r, "/" + board + "/" + remoteShort(OP), http.StatusSeeOther) + } return } DeleteAttachmentFromFile(db, id) DeletePreviewFromFile(db, id) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + + if(manage == "t") { + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) + } else { + http.Redirect(w, r, OP, http.StatusSeeOther) + } }) http.HandleFunc("/report", func(w http.ResponseWriter, r *http.Request){ @@ -817,13 +872,13 @@ func main() { if !IsIDLocal(db, id) { CloseLocalReportDB(db, id, board) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) return } reported := DeleteReportActivity(db, id) if reported { - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) return } @@ -834,13 +889,13 @@ func main() { if !IsIDLocal(db, id) { CreateLocalReportDB(db, id, board, reason) - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, "/" + board + "/" + remoteShort(id), http.StatusSeeOther) return } reported := ReportActivity(db, id, reason) if reported { - http.Redirect(w, r, r.Header.Get("Referer"), http.StatusSeeOther) + http.Redirect(w, r, id, http.StatusSeeOther) return } @@ -869,6 +924,48 @@ func main() { w.Write([]byte("")) }) + http.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) { + acct := r.URL.Query()["resource"] + + if(len(acct) < 1) { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("resource needs a value")) + return + } + + acct[0] = strings.Replace(acct[0], "acct:", "", -1) + + actorDomain := strings.Split(acct[0], "@") + + if(len(actorDomain) < 2) { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("accpets only subject form of acct:board@instance")) + return + } + + if !IsActorLocal(db, TP + "" + actorDomain[1] + "/" + actorDomain[0]) { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("actor not local")) + return + } + + var finger Webfinger + var link WebfingerLink + + finger.Subject = "acct:" + actorDomain[0] + "@" + actorDomain[1] + link.Rel = "self" + link.Type = "application/activity+json" + link.Href = TP + "" + actorDomain[1] + "/" + actorDomain[0] + + finger.Links = append(finger.Links, link) + + enc, _ := json.Marshal(finger) + + w.Header().Set("Content-Type", activitystreams) + w.Write(enc) + + }) + fmt.Println("Server for " + Domain + " running on port " + Port) fmt.Println("Mod key: " + *Key) @@ -2028,3 +2125,103 @@ func RunDatabaseSchema(db *sql.DB) { CheckError(err, "could not exec databaseschema.psql") } } + +func CreatedNeededDirectories() { + if _, err := os.Stat("./public"); os.IsNotExist(err) { + os.Mkdir("./public", 0755) + } + + if _, err := os.Stat("./pem/board"); os.IsNotExist(err) { + os.MkdirAll("./pem/board", 0700) + } +} + +//looks for actor with pattern of board@instance +func FingerActor(path string) Actor{ + + actor, instance := GetActorInstance(path) + + r := FingerRequest(actor, instance) + + var nActor Actor + + if r.StatusCode == 200 { + defer r.Body.Close() + + body, _ := ioutil.ReadAll(r.Body) + + err := json.Unmarshal(body, &nActor) + + CheckError(err, "error getting fingerrequet resp from json body") + } + + return nActor +} + +func FingerRequest(actor string, instance string) (*http.Response){ + acct := "acct:" + actor + "@" + instance + req, err := http.NewRequest("GET", "http://" + instance + "/.well-known/webfinger?resource=" + acct, nil) + + CheckError(err, "could not get finger request from id req") + + req.Header.Set("Accept", activitystreams) + + resp, err := http.DefaultClient.Do(req) + + var finger Webfinger + + if err != nil { + CheckError(err, "could not get actor from finger resp with id " + acct) + } + + if resp.StatusCode == 200 { + defer resp.Body.Close() + + body, _ := ioutil.ReadAll(resp.Body) + + err := json.Unmarshal(body, &finger) + + CheckError(err, "error getting fingerrequet resp from json body") + } + + if(len(finger.Links) > 0) { + for _, e := range finger.Links { + if(e.Type == "application/activity+json"){ + req, err := http.NewRequest("GET", e.Href, nil) + + CheckError(err, "could not get finger request from id req") + + req.Header.Set("Accept", activitystreams) + + resp, err := http.DefaultClient.Do(req) + return resp + } + } + } + + return resp +} + +func GetActorInstance(path string) (string, string) { + re := regexp.MustCompile(`([@]?([\w\d.-_]+)[@](.+))`) + atFormat := re.MatchString(path) + + if(atFormat) { + match := re.FindStringSubmatch(path) + if(len(match) > 1) { + return match[1], match[2] + } + } + + re = regexp.MustCompile(`(http:\\|https:\\)?(www)?([\w\d-_.:]+)\/([\w\d-_.]+)`) + httpFormat := re.MatchString(path) + + if(httpFormat) { + match := re.FindStringSubmatch(path) + if(len(match) > 3) { + return match[4], match[3] + } + } + + return "", "" +} |