Skip to content

Commit

Permalink
preliminary API draft
Browse files Browse the repository at this point in the history
fixes #8
  • Loading branch information
donuts-are-good committed Apr 21, 2023
1 parent cc879ac commit 05dd863
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
6 changes: 6 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ func initBoardSchema(db *sqlx.DB) {
log.Fatalln(err)
}
}


func createReply(db *sqlx.DB, postID int, authorHash string, replyBody string) error {
_, err := db.Exec("INSERT INTO replies (discussion_id, author, message) VALUES (?, ?, ?)", postID, authorHash, replyBody)
return err
}
26 changes: 19 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func main() {
}
defer listener.Close()
fmt.Println("Listening on :" + os.Args[1])

go api(db)

for {
conn, err := listener.Accept()
if err != nil {
Expand Down Expand Up @@ -181,6 +184,12 @@ func handleConnection(db *sqlx.DB, channel ssh.Channel, sshConn *ssh.ServerConn,
listDiscussions(db, term)
case strings.HasPrefix(input, "/history"):
printCachedMessages(term)
case strings.HasPrefix(input, "/tokens new"):
handleTokenNew(db, term, hash)
case strings.HasPrefix(input, "/tokens list"):
handleTokenList(db, term, hash)
case strings.HasPrefix(input, "/tokens revoke"):
handleTokenRevoke(db, input, term, hash)
case strings.HasPrefix(input, "/quit") || strings.HasPrefix(input, "/q") ||
strings.HasPrefix(input, "/exit") || strings.HasPrefix(input, "/x") ||
strings.HasPrefix(input, "/leave") || strings.HasPrefix(input, "/part"):
Expand All @@ -195,7 +204,7 @@ func handleConnection(db *sqlx.DB, channel ssh.Channel, sshConn *ssh.ServerConn,
term.Write([]byte("Unrecognized command. Type /help for available commands.\n"))
} else {
message := fmt.Sprintf("[%s] %s: %s", time.Now().String()[11:16], hash, input)
broadcast(message + "\r")
broadcast(hash, message+"\r")
}
}
}
Expand Down Expand Up @@ -261,9 +270,12 @@ func disconnect(hash string) {
removeUser(hash)
}

func broadcast(message string) {
func broadcast(senderHash, message string) {
addToCache(message)
for _, user := range getAllUsers() {
if user.Hash == senderHash {
continue
}
saveCursorPos(user.Conn)
moveCursorUp(user.Conn, 1)
fmt.Fprintln(user.Conn, message)
Expand Down Expand Up @@ -663,12 +675,12 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`))
`))
}
func writeVersionInfo(term *term.Terminal) {
term.Write([]byte(`
shhhbb bbs ` + semverInfo + `
MIT License 2023 donuts-are-good
https://github.com/donuts-are-good/shhhbb
`))
shhhbb bbs ` + semverInfo + `
MIT License 2023 donuts-are-good
https://github.com/donuts-are-good/shhhbb
`))
}

0 comments on commit 05dd863

Please sign in to comment.