Browse Source

Merge branch 'master' into database-store

Conflicts:
	nerds.go
master
Matt Baer 9 years ago
parent
commit
c98d5d5b8b
4 changed files with 48 additions and 24 deletions
  1. +3
    -3
      README.md
  2. +39
    -15
      nerds.go
  3. +1
    -1
      store/random.go
  4. +5
    -5
      store/store.go

+ 3
- 3
README.md View File

@@ -1,6 +1,6 @@
Write.as
========
[![Build Status](https://travis-ci.org/writeas/writeas-telnet.svg)](https://travis-ci.org/writeas/writeas-telnet)
[![Build Status](https://travis-ci.org/writeas/nerds.svg)](https://travis-ci.org/writeas/nerds)

This is a simple telnet-based interface for publishing text. Users connect and paste / type what they want to publish. Upon indicating that they're finished, a link is generated to access their new post on the web.

@@ -12,7 +12,7 @@ telnet nerds.write.as
## Run it yourself
```
Usage:
write-telnet [options]
nerds [options]

Options:
--debug
@@ -30,7 +30,7 @@ Options:
The default configuration (without any flags) is essentially:

```
write-telnet -o /var/write -s . -p 2323
nerds -o /var/write -s . -p 2323
```

## How it works


+ 39
- 15
nerds.go View File

@@ -9,14 +9,17 @@ import (
"io/ioutil"
"net"
"os"
"os/exec"

"github.com/writeas/nerds/store"
)

var (
banner []byte
outDir string
staticDir string
debugging bool
rsyncHost string
db *sql.DB
)

@@ -34,16 +37,22 @@ const (

func main() {
// Get any arguments
outDirPtr := flag.String("o", "/var/write", "Directory where text files will be stored.")
staticDirPtr := flag.String("s", "./static", "Directory where required static files exist.")
rsyncHostPtr := flag.String("h", "", "Hostname of the server to rsync saved files to.")
portPtr := flag.Int("p", 2323, "Port to listen on.")
debugPtr := flag.Bool("debug", false, "Enables garrulous debug logging.")
flag.Parse()

outDir = *outDirPtr
staticDir = *staticDirPtr
rsyncHost = *rsyncHostPtr
debugging = *debugPtr

fmt.Print("\nCONFIG:\n")
fmt.Printf("Output directory : %s\n", outDir)
fmt.Printf("Static directory : %s\n", staticDir)
fmt.Printf("rsync host : %s\n", rsyncHost)
fmt.Printf("Debugging enabled : %t\n\n", debugging)

fmt.Print("Initializing...")
@@ -57,21 +66,25 @@ func main() {
// Connect to database
dbUser := os.Getenv("WA_USER")
dbPassword := os.Getenv("WA_PASSWORD")
dbName := os.Getenv("WA_DB")
dbHost := os.Getenv("WA_HOST")

if dbUser == "" || dbPassword == "" {
fmt.Println("Database user or password not set.")
if outDir == "" && (dbUser == "" || dbPassword == "" || dbName == "") {
// Ensure parameters needed for storage (file or database) are given
fmt.Println("Database user, password, or database name not set.")
return
}

fmt.Print("Connecting to database...")
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/writeas?charset=utf8mb4", dbUser, dbPassword, dbHost))
if err != nil {
fmt.Printf("\n%s\n", err)
return
if outDir == "" {
fmt.Print("Connecting to database...")
db, err = sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8mb4", dbUser, dbPassword, dbHost, dbName))
if err != nil {
fmt.Printf("\n%s\n", err)
return
}
defer db.Close()
fmt.Println("CONNECTED")
}
defer db.Close()
fmt.Println("CONNECTED")

ln, err := net.Listen("tcp", fmt.Sprintf(":%d", *portPtr))
if err != nil {
@@ -169,13 +182,24 @@ func readInput(c net.Conn) {
friendlyId := store.GenerateFriendlyRandomString(store.FriendlyIdLen)
editToken := store.Generate62RandomString(32)

_, err := db.Exec("INSERT INTO posts (id, content, modify_token, text_appearance) VALUES (?, ?, ?, 'mono')", friendlyId, post.Bytes(), editToken)
if err != nil {
fmt.Printf("There was an error saving: %s\n", err)
output(c, "Something went terribly wrong, sorry. Try again later?\n\n")
break
if outDir == "" {
_, err := db.Exec("INSERT INTO posts (id, content, modify_token, text_appearance) VALUES (?, ?, ?, 'mono')", friendlyId, post.Bytes(), editToken)
if err != nil {
fmt.Printf("There was an error saving: %s\n", err)
output(c, "Something went terribly wrong, sorry. Try again later?\n\n")
break
}
output(c, fmt.Sprintf("\n%s\nPosted! View at %shttps://write.as/%s%s", hr, colBlue, friendlyId, noCol))
} else {
output(c, fmt.Sprintf("\n%s\nPosted to %shttp://nerds.write.as/%s%s", hr, colBlue, friendlyId, noCol))

if rsyncHost != "" {
output(c, "\nPosting to secure site...")
exec.Command("rsync", "-ptgou", outDir+"/"+friendlyId, rsyncHost+":").Run()
output(c, fmt.Sprintf("\nPosted! View at %shttps://write.as/%s%s", colBlue, friendlyId, noCol))
}
}
output(c, fmt.Sprintf("\n%s\nPosted! View at %shttps://write.as/%s%s", hr, colBlue, friendlyId, noCol))
output(c, "\nSee you later.\n\n")
break
}


+ 1
- 1
store/random.go View File

@@ -16,7 +16,7 @@ func GenerateRandomString(dictionary string, l int) string {
var bytes = make([]byte, l)
rand.Read(bytes)
for k, v := range bytes {
bytes[k] = dictionary[v%byte(len(dictionary))]
bytes[k] = dictionary[v%byte(len(dictionary))]
}
return string(bytes)
}

+ 5
- 5
store/store.go View File

@@ -1,13 +1,13 @@
package store

import (
"os"
"io"
"bytes"
"io"
"os"
)

const (
FriendlyIdLen = 12
FriendlyIdLen = 13
)

func SavePost(outDir string, post []byte) (string, error) {
@@ -16,9 +16,9 @@ func SavePost(outDir string, post []byte) (string, error) {
if err != nil {
return "", err
}
defer f.Close()
out := post[:0]
for _, b := range post {
if b < 32 && b != 10 && b != 13 {


Loading…
Cancel
Save