/
smsserver.go
339 lines (295 loc) · 10.3 KB
/
smsserver.go
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package main
import (
"database/sql"
"encoding/json"
"fmt"
"github.com/Narsil/xmpp"
"github.com/coopernurse/gorp"
_ "github.com/mattn/go-sqlite3"
"html"
"html/template"
"io"
"log"
"net/http"
"strings"
"time"
"errors"
)
type Message struct {
Id int64
Created int64
Message string
From string
To string
Sent bool
Incoming bool
}
type Contact struct {
Id int64
User string
ContactId string
ContactName string
Group string
}
func authenticate(username, password string) bool {
return true
}
func main() {
dbmap := initDb()
defer dbmap.Db.Close()
// create two posts
// dbmap.TruncateTables()
// p1 := newMessage("+33672317534", "Hey from go")
// // // insert rows - auto increment PKs will be set properly after the insert
// dbmap.Insert(&p1)
http.HandleFunc("/pending/", func(w http.ResponseWriter, r *http.Request) {
var messages []Message
_, err := dbmap.Select(&messages, "SELECT * FROM messages WHERE Sent=? AND Incoming=?", false, false)
checkErr(err, "Select failed")
str, err := json.Marshal(messages)
checkErr(err, "Marshalling failed")
fmt.Fprintf(w, "%s", str)
})
http.HandleFunc("/send/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkErr(err, "Parsing form failed")
if r.Form.Get("To") != "" && r.Form.Get("Message") != "" {
message := newMessage(r.Form.Get("To"), r.Form.Get("Message"))
err = dbmap.Insert(&message)
checkErr(err, "Could not insert new message")
http.Redirect(w, r, "/send/", 302)
return
}
var messages []Message
_, err = dbmap.Select(&messages, "SELECT * FROM messages ORDER BY Created DESC")
checkErr(err, "Select failed")
tmpl, _ := template.New("send").Parse(`
<html>
<body>
<form action="." method="POST">
<input type="text" name="To" placeholder="To"/>
<input type="text" name="Message" placeholder="Message"/>
<input type="submit" value="Envoyer"/>
</form>
{{range .Messages}}
<p style="border: 1px solid black">
<span style="font-weight:bold">From: {{.From}} to {{.To}}</span> {{.Message}}
<span style="float:right">
Sent: {{.Sent}}
Created: {{.Created}}
Incoming: {{.Incoming}}
</span>
</p>
{{end}}
</body>
</html>
`)
tmpl.Execute(w, map[string]interface{}{
"Messages": messages,
})
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkErr(err, "Parsing form failed")
if r.Form.Get("To") != "" && r.Form.Get("Message") != "" {
message := newMessage(r.Form.Get("To"), r.Form.Get("Message"))
err = dbmap.Insert(&message)
checkErr(err, "Could not insert new message")
http.Redirect(w, r, "/", 302)
return
}
tmpl, _ := template.New("homepage").Parse(`
<!DOCTYPE html>
<html>
<head>
<link href="/static/bootstrap/css/bootstrap.min.css" type="text/css" rel="stylesheet">
<link href="/static/bootstrap/css/bootstrap-responsive.min.css" type="text/css" rel="stylesheet">
<script src="/static/jquery-1.10.2.min.js" type="text/javascript"></script>
<script src="/static/bootstrap/js/bootstrap.js" type="text/javascript"></script>
<script src="/static/sms.js" type="text/javascript"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
</body>
</html>
`)
tmpl.Execute(w, nil)
})
http.HandleFunc("/history/", func(w http.ResponseWriter, r *http.Request) {
var messages []Message
_, err := dbmap.Select(&messages, "SELECT * FROM messages ORDER BY Created DESC")
checkErr(err, "Select failed")
b, err := json.Marshal(messages)
checkErr(err, "Encoding failed")
fmt.Fprintf(w, "%s", b)
})
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
http.HandleFunc("/sent/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkErr(err, "Parsing form failed")
ids_string := r.Form.Get("Id")
ids := strings.Split(ids_string, ",")
new_ids_string := "\"" + strings.Join(ids, "\", \"") + "\""
query := fmt.Sprintf("UPDATE messages SET Sent='true' WHERE Id IN (%s)", new_ids_string)
_, err = dbmap.Exec(query)
if err == nil {
fmt.Fprintf(w, "Ok")
} else {
fmt.Fprintf(w, "Error", err)
}
})
srv := xmpp.NewServer("sms.nicolas.kwyk.fr")
http.HandleFunc("/received/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkErr(err, "Parsing form failed")
from := r.Form.Get("From")
message := r.Form.Get("Message")
if from != "" && message != "" {
m := newIncomingMessage(from, message, srv)
err = dbmap.Insert(&m)
}
if err == nil {
fmt.Fprintf(w, "Ok")
} else {
fmt.Fprintf(w, "Error", err)
}
})
http.HandleFunc("/contacts/add/", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
checkErr(err, "Parsing form failed")
user := r.Form.Get("User")
contactid := r.Form.Get("Id")
contactname := r.Form.Get("Name")
group := r.Form.Get("Group")
err = insertOrUpdateContact(dbmap, user, contactid, contactname, group)
if err == nil {
fmt.Fprintf(w, "Ok")
} else {
fmt.Fprintf(w, "Error", err)
}
})
err := srv.SetKeyPair("private.cert", "private.key")
if err != nil {
}
srv.SetAuthFunc(authenticate)
srv.SetHandleIncomingMessage(
func(msg xmpp.XmppMessage) {
for _, body := range msg.Bodies {
number := strings.Split(msg.To, "@")[0]
message := newMessage(number, html.UnescapeString(body.Body))
err := dbmap.Insert(&message)
checkErr(err, "Could not insert new message")
}
})
srv.HandleQuery("http://jabber.org/protocol/disco#info", func(w io.Writer, req xmpp.Request) {
w.Write([]byte(`<iq type='result' from='sms.nicolas.kwyk.fr' to='nicolas@kwyk.fr/YY9R80gi' id='` + req.Id + `'>
<query xmlns='http://jabber.org/protocol/disco#info'>
<feature var='http://jabber.org/protocol/disco#info'/>
<feature var='http://jabber.org/protocol/disco#items'/>
</query>
</iq>
`))
})
srv.HandleQuery("http://jabber.org/protocol/disco#items", func(w io.Writer, req xmpp.Request) {
w.Write([]byte(`<iq type='result' from='sms.nicolas.kwyk.fr' to='` + req.User + `' id='` + req.Id + `'>
<error code="501" type="cancel"><feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/></error>
</iq>
`))
})
srv.HandleQuery("jabber:iq:roster", func(w io.Writer, req xmpp.Request) {
var contacts []Contact
dbmap.Select(&contacts, "SELECT * FROM contacts WHERE User=?", req.User)
str := ""
for _, contact := range(contacts){
str += `<item jid="` + contact.ContactId + `" subscription="both" name="` + contact.ContactName + `"><group>` + contact.Group + `</group></item>`
}
w.Write([]byte(`<iq type='result' to='` + req.User + `' id='` + req.Id + `'>
<query xmlns="jabber:iq:roster">`+ str + `</query>
</iq>
`))
for _, contact := range(contacts){
w.Write([]byte(`<presence from="` + contact.ContactId + `" to="` + contact.User + `"><caps:c node="http://www.android.com/gtalk/client/caps" ver="1.1" xmlns:caps="http://jabber.org/protocol/caps"/></presence>`))
}
})
go func() {
srv.ListenAndServe("tcp", ":5222")
}()
log.Fatal(http.ListenAndServe(":8080", nil))
}
func newMessage(destination, message string) Message {
fmt.Println("Sending SMS to: ", destination)
return Message{
Created: time.Now().UnixNano(),
From: "+33672317534",
Message: message,
To: destination,
Sent: false,
Incoming: false,
}
}
func newIncomingMessage(author, message string, srv xmpp.Server) Message {
fmt.Println("Received SMS from: ", author)
go func() {
srv.MessageChannel <- xmpp.Message{From: author, Message: message, To: "nicolas"}
}()
return Message{
Created: time.Now().UnixNano(),
From: author,
Message: message,
To: "+33672317534",
Sent: true,
Incoming: true,
}
}
func insertOrUpdateContact(dbmap *gorp.DbMap, user, contactid, contactname, group string) error{
var contacts []Contact
_, err := dbmap.Select(&contacts, "SELECT * FROM contacts WHERE User=? AND ContactId=?", user, contactid)
if err != nil{
return err
}
if len(contacts) == 0{
contact := Contact{
User:user,
ContactId:contactid,
ContactName:contactname,
Group:group,
}
err = dbmap.Insert(&contact)
if err != nil{
return err
}
}else if len(contacts) == 1{
contact := contacts[0]
contact.ContactName = contactname
contact.Group = group
_, err = dbmap.Update(&contact)
if err != nil{
return err
}
}else{
return errors.New("You have more than one contacts")
}
return nil
}
func initDb() *gorp.DbMap {
// connect to db using standard Go database/sql API
// use whatever database/sql driver you wish
db, err := sql.Open("sqlite3", "db.bin")
checkErr(err, "sql.Open failed")
// construct a gorp DbMap
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
// add a table, setting the table name to 'posts' and
// specifying that the Id property is an auto incrementing PK
dbmap.AddTableWithName(Message{}, "messages").SetKeys(true, "Id")
dbmap.AddTableWithName(Contact{}, "contacts").SetKeys(true, "Id")
// create the table. in a production system you'd generally
// use a migration tool, or create the tables via scripts
err = dbmap.CreateTablesIfNotExists()
checkErr(err, "Create tables failed")
return dbmap
}
func checkErr(err error, msg string) {
if err != nil {
log.Fatalln(msg, err)
}
}