~humaid/whatsmorse

ref: 2d60712369c14e99f4cf24f41e447da41bae6be2 whatsmorse/main.go -rw-r--r-- 3.5 KiB
2d607123 — Humaid AlQassimi first 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main

import (
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"os"
	"strings"
	"time"

	"github.com/go-macaron/session"
	macaron "gopkg.in/macaron.v1"
)

type UserData struct {
	transmissions map[int]string
}

var connectedUsers map[string]*UserData

func main() {
	port := os.Getenv("PORT")

	if port == "" {
		log.Fatal("$PORT must be set")
	}
	rand.Seed(time.Now().UTC().UnixNano())
	connectedUsers = make(map[string]*UserData)
	macaron.Env = macaron.PROD
	m := macaron.Classic()
	m.Use(macaron.Renderer())
	m.Use(session.Sessioner())

	m.Get("/", func(ctx *macaron.Context, sess session.Store) {
		if sess.Get("Code") == nil {
			number := fmt.Sprint(rand.Intn(8999) + 1000)
			connectedUsers[number] = &UserData{make(map[int]string)}
			sess.Set("Code", number)
			ctx.Data["Code"] = number
		} else {
			ctx.Data["Code"] = sess.Get("Code")
		}

		ctx.HTML(200, "index")
	})

	m.Get("/transmit", func(ctx *macaron.Context, sess session.Store) {
		_, ok := connectedUsers[ctx.Query("to")]
		if sess.Get("Code") != nil && ctx.Query("message") != "" && ctx.Query("to") != "" && ok {
			connectedUsers[ctx.Query("to")].transmissions[len(connectedUsers[ctx.Query("to")].transmissions)] = fmt.Sprint(sess.Get("Code")) + ": " + toMorse(ctx.Query("message"))
			fmt.Println("All okay!")
		}
	})

	m.Get("/clear", func(ctx *macaron.Context, sess session.Store) {
		connectedUsers[fmt.Sprint(sess.Get("Code"))] = &UserData{make(map[int]string)}
	})

	m.Get("/incoming", func(ctx *macaron.Context, sess session.Store) {
		if sess.Get("Code") != nil {
			ctx.Data["Trans"] = connectedUsers[fmt.Sprint(sess.Get("Code"))].transmissions
		}
		ctx.HTML(200, "incoming")
	})
	log.Println(http.ListenAndServe("0.0.0.0:"+port, m))

}

// Function modified from https://github.com/pravj/morser
func toMorse(text string) string {
	text = strings.ToUpper(text)
	reverseMap := make(map[string]string)

	// Alphabates
	reverseMap["A"] = ".-"
	reverseMap["B"] = "-..."
	reverseMap["C"] = "-.-."
	reverseMap["D"] = "-.."
	reverseMap["E"] = "."
	reverseMap["F"] = "..-."
	reverseMap["G"] = "--."
	reverseMap["H"] = "...."
	reverseMap["I"] = ".."
	reverseMap["J"] = ".---"
	reverseMap["K"] = "-.-"
	reverseMap["L"] = ".-.."
	reverseMap["M"] = "--"
	reverseMap["N"] = "-."
	reverseMap["O"] = "---"
	reverseMap["P"] = ".--."
	reverseMap["Q"] = "--.-"
	reverseMap["R"] = ".-."
	reverseMap["S"] = "..."
	reverseMap["T"] = "-"
	reverseMap["U"] = "..-"
	reverseMap["V"] = "...-"
	reverseMap["W"] = ".--"
	reverseMap["X"] = "-..-"
	reverseMap["Y"] = "-.--"
	reverseMap["Z"] = "--.."

	// Decimals
	reverseMap["1"] = ".----"
	reverseMap["2"] = "..---"
	reverseMap["3"] = "...--"
	reverseMap["4"] = "....-"
	reverseMap["5"] = "....."
	reverseMap["6"] = "-...."
	reverseMap["7"] = "--..."
	reverseMap["8"] = "---.."
	reverseMap["9"] = "----."
	reverseMap["0"] = "-----"

	// Punctuation marks and miscellaneous signs
	reverseMap["."] = ".-.-.-"
	reverseMap[","] = "--..--"
	reverseMap[":"] = "---..."
	reverseMap["?"] = "..--.."
	reverseMap["'"] = ".----."
	reverseMap["-"] = "-....-"
	reverseMap["/"] = "-..-."
	reverseMap["("] = "-.--."
	reverseMap[")"] = "-.--.-"
	reverseMap["+"] = ".-.-."
	reverseMap["×"] = "-..-"
	reverseMap["@"] = ".--.-."
	result := ""

	for j := 0; j < len(text); j++ {
		if text[j] == ' ' {
			result = result + " / "
		} else {
			result = result + " "
		}

		// validates the input text, character by character
		_, isPresent := reverseMap[fmt.Sprintf("%c", text[j])]

		// invalid input provided
		if isPresent {
			result = result + reverseMap[fmt.Sprintf("%c", text[j])]
		}

	}
	return result

}