aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: b88e710c243c9d304751a814226a5ff78f3d3b68 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Infodump is a commandline social network that works over IPFS and communicates through PubSub
package main

import (
	"bufio"
	"database/sql"
	"fmt"
	"net/url"
	"os"
	"strings"
	"time"

	"git.kiefte.eu/lapingvino/infodump/message"
	shell "github.com/ipfs/go-ipfs-api"
	_ "modernc.org/sqlite"
)

var LocalMessages = message.Messages{}
var DatabasePath = "infodump.db"
var DB *sql.DB
var MessageCache string

// Readline reads from a buffered stdin and returns the line
func Readline() string {
	reader := bufio.NewReader(os.Stdin)
	line, _ := reader.ReadString('\n')
	return line[:len(line)-1]
}

// MenuElements is a list of options for the menu, consisting of a description and a function
type MenuElements struct {
	Description string
	Function    func()
}

// Menu presents a menu to the user
func Menu(menu []MenuElements) {
	// Present the user with a menu
	for i, e := range menu {
		fmt.Println(i+1, e.Description)
	}
	fmt.Println("Enter your choice: ")
	var choice int
	fmt.Scanln(&choice)
	if choice > 0 && choice <= len(menu) {
		menu[choice-1].Function()
	} else {
		fmt.Println("Invalid Choice")
	}
}

func main() {
	fmt.Println("Welcome to Infodump")

	// Set message IPFS client to use the local IPFS node
	message.IPFSGateway = "http://localhost:5001"

	// If the first argument to the command is a valid link, use that for the IPFSGateway instead
	if len(os.Args) > 1 {
		u, err := url.Parse(os.Args[1])
		if err == nil {
			message.IPFSGateway = u.String()
		}
	}

	err := TestIPFSGateway(message.IPFSGateway)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Run a loop and present a menu to the user to
	// read messages
	// write messages
	// sync messages
	// set the IPFS gateway
	// set the database
	// configure the followed tags
	// quit the program
	for {
		// Check if the database is set, if so, show the followed tags
		if DB != nil {
			fmt.Println("Following tags:")
			for _, tag := range GetFollowedTags(DB) {
				fmt.Print(tag, " ")
				fmt.Println()
			}
		}
		// Present the user with a menu
		Menu([]MenuElements{
			{"Start OLN Listener", StartOLNListener},
			{"Read Messages", ReadMessages},
			{"Write Message", WriteMessage},
			{"Sync Messages", SyncMenu},
			{"Settings", SettingsMenu},
			{"Quit", func() { os.Exit(0) }},
		})

		// After the user has selected an option and before the user gets to chose again, ask to press enter to continue
		fmt.Println("Press enter to continue...")
		Readline()
	}
}

// A submenu for all settings
func SettingsMenu() {
	// Present the user with a menu
	Menu([]MenuElements{
		{"Set IPFS Gateway", SetIPFSGateway},
		{"Set Database", SetDatabase},
		{"Configure Followed Tags", ConfigureFollowedTags},
		{"Back", func() {}},
	})
}

// A menu for the several parts of Sync:
// saving messages to the local database
// reading messages from the local database
// reading messages from the network
// writing messages to the network
func SyncMenu() {
	// Present the user with a menu
	Menu([]MenuElements{
		{"Save Messages to Database", SaveMessagesToDatabase},
		{"Read Messages from Database", ReadMessagesFromDatabase},
		{"Read Messages from Network", ReadMessagesFromNetwork},
		{"Write Messages to Network", WriteMessagesToNetwork},
		{"Back", func() {}},
	})
}

// StartOLNListener starts a PubSub listener that listens for messages from the network
// and adds them to LocalMessages
// For this it uses the IPFS gateway and listens on the topic "OLN", as well as
// the list of followed tags from the database
func StartOLNListener() {
	// Get the IPFS gateway
	gateway := message.IPFSGateway
	// Get the database
	db := GetDatabase()
	// Get the list of followed tags
	followedTags := GetFollowedTags(db)
	// Create a new IPFS client
	myIPFS := shell.NewShell(gateway)
	var subs []*shell.PubSubSubscription
	olnsub, err := myIPFS.PubSubSubscribe("OLN")
	if err != nil {
		fmt.Println(err)
		return
	}
	subs = append(subs, olnsub)
	for _, tag := range followedTags {
		tagssub, err := myIPFS.PubSubSubscribe("oln-" + tag)
		if err != nil {
			fmt.Println(err)
		} else {
			subs = append(subs, tagssub)
		}
	}
	// Start a goroutine for each of the subscriptions in subs,
	// read the CID from the Next method, look up the CID on IPFS,
	// read this in via message.MessagesFromIPFS and add the message to LocalMessages
	for _, sub := range subs {
		go func(sub *shell.PubSubSubscription) {
			for {
				msg, err := sub.Next()
				if err != nil {
					fmt.Println(err)
					return
				}
				msgs, err := message.MessagesFromIPFS(string(msg.Data))
				if err != nil {
					fmt.Println(err)
					return
				}
				LocalMessages.AddMany(msgs)
			}
		}(sub)
	}
}

// GetDatabase checks if DB is already set and opened, if not it Sets the database first
func GetDatabase() *sql.DB {
	if DB == nil {
		fmt.Println("Database not set, setting database...")
		SetDatabase()
	}
	return DB
}

func GetMessagesFromDatabase(db *sql.DB) *message.Messages {
	// Get all messages from the database
	rows, err := db.Query("SELECT hash, message, nonce, timestamp FROM messages")
	if err != nil {
		fmt.Println(err)
	}
	defer rows.Close()
	// Create a new Messages object
	msgs := message.Messages{}
	// Loop through all messages
	fmt.Println("Getting messages from database...")
	for rows.Next() {
		var hash, msg string
		var nonce int
		var timestamp int64
		// Get the values from the database
		err := rows.Scan(&hash, &msg, &nonce, &timestamp)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println("Got message from database:", hash)

		// Create a new message object
		m := message.Message{
			Message:   msg,
			Nonce:     nonce,
			Timestamp: timestamp,
		}
		// Add the message to the Messages object
		fmt.Println("Adding message to Messages object...")
		msgs.Add(&m)
	}
	return &msgs
}

// ReadMessages shows the messages in LocalMessages sorted by importance, 10 at a time
func ReadMessages() {
	// Use LocalMessages to get the messages and get the sorted list of messages
	// through the MessageList method
	msgs := LocalMessages.MessageList()
	// Loop through the messages and print them
	for i, m := range msgs {
		fmt.Println(m)
		if i%10 == 9 {
			fmt.Println("Press enter to continue... Type anything to stop")
			contp := Readline()
			if contp != "" {
				return
			}
		}
	}
}

func WriteMessage() {
	// Get a message and an urgency from the user.
	// The urgency is used to set the strength of the Proof of Work
	// If there is a message in the MessageCache, ask the user if they want to use it
	// If there is no message in the MessageCache, ask the user to write a message
	var m string
	if MessageCache != "" {
		fmt.Println("You wrote a message before that you didn't send yet. Do you want to use that message?")
		fmt.Println("The message is:", MessageCache)
		fmt.Println("Type 'yes' to use the message, or anything else to write a new message")
		usep := Readline()
		if usep == "yes" {
			fmt.Println("Using message from cache")
			m = MessageCache
		} else {
			fmt.Println("Writing new message")
			m = usep
		}
	} else {
		fmt.Println("Write a message:")
		m = Readline()
	}
	fmt.Println("Enter an urgency (higher is stronger but takes longer to produce): ")
	var urgency int
	fmt.Scanln(&urgency)
	fmt.Println("How many seconds should we wait for the POW to be done? (default is 5): ")
	var powtime int
	fmt.Scanln(&powtime)
	if powtime == 0 {
		powtime = 5
	}
	// Create a new message object
	msg, err := message.New(m, urgency, time.Now().Unix(), time.Duration(powtime)*time.Second)
	if err != nil {
		fmt.Println(err)
		fmt.Println("Want to try again with another urgency or timeout? (y/n)")
		contp := Readline()
		if contp == "y" {
			MessageCache = m
			WriteMessage()
		}
		return
	}
	// MessageCache can be discarded after this point
	MessageCache = ""
	// Add the message to LocalMessages
	LocalMessages.Add(msg)

	// Ask if the user wants to write another message or save the messages to the database
	fmt.Println("Do you want to write another message? (y/n)")
	contp := Readline()
	if contp == "y" {
		WriteMessage()
	} else {
		fmt.Println("Do you want to save the messages to the database? (y/n)")
		contp := Readline()
		if contp == "y" {
			SaveMessagesToDatabase()
		}
	}
}

// Implementing the Sync Menu options
// SaveMessagesToDatabase, ReadMessagesFromDatabase, ReadMessagesFromNetwork and WriteMessagesToNetwork

// SaveMessagesToDatabase saves the messages in LocalMessages to the database
func SaveMessagesToDatabase() {
	// Update the database with the messages in LocalMessages
	db := GetDatabase()
	// Put the messages in the database
	LocalMessages.Each(func(m *message.Message) {
		_, err := db.Exec("INSERT INTO messages(hash, message, nonce, timestamp) VALUES(?,?,?,?)", m.Stamp(), m.Message, m.Nonce, m.Timestamp)
		if err != nil {
			fmt.Println(err)
		} else {
			fmt.Println("Message", m.Stamp(), "saved to database")
		}
	})
}

// ReadMessagesFromDatabase reads the messages from the database and adds them to LocalMessages
func ReadMessagesFromDatabase() {
	// Get the messages from the database
	messages := GetMessagesFromDatabase(GetDatabase())
	// Add the messages to LocalMessages
	LocalMessages.AddMany(messages)
}

// ReadMessagesFromNetwork reads the messages from the IPFS network and adds them to LocalMessages
func ReadMessagesFromNetwork() {
	// Get the messages from the IPFS network
	// TODO: Implement this
}

// WriteMessagesToNetwork writes the messages in LocalMessages to the IPFS network
func WriteMessagesToNetwork() {
	// Add the messages to the IPFS network
	cid, err := LocalMessages.AddToIPFS()
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println("Messages synced to IPFS: ", cid)
	}
}

func TestIPFSGateway(gateway string) error {
	// Test the IPFS gateway
	fmt.Println("Testing IPFS gateway...")
	ipfs := shell.NewShell(gateway)
	_, err := ipfs.ID()
	return err
}

func SetIPFSGateway() {
	// Set the IPFS gateway
	fmt.Println("Enter the IPFS gateway: ")
	var gateway string
	fmt.Scanln(&gateway)
	// Test the IPFS gateway
	err := TestIPFSGateway(gateway)
	if err != nil {
		fmt.Println(err)
		return
	} else {
		fmt.Println("IPFS gateway set to: ", gateway)
		message.IPFSGateway = gateway
	}
}

func InitDatabase(db *sql.DB) {
	var err error
	// Create the table "messages"
	_, err = db.Exec("CREATE TABLE messages(hash TEXT PRIMARY KEY, message TEXT, nonce INTEGER, timestamp INTEGER)")
	if err != nil {
		fmt.Println(err)
	}
	// Create the table "followed_tags"
	_, err = db.Exec("CREATE TABLE followed_tags(tag TEXT)")
	if err != nil {
		fmt.Println(err)
	}
}

// SetDatabase configures DB to be the database to use
// The name used is DatabasePath, but the user will be asked if this correct or if they want to change it
// If the database is already set, it will ask the user if they want to overwrite it
// If the database is not set, it will ask the user if they want to create it
// If the database is set but it doesn't contain the tables "messages" and "followed_tags",
// it will create them
func SetDatabase() {
	// First check if the user is okay with the database path
	fmt.Println("Database path: ", DatabasePath)
	fmt.Println("Is this correct? (y/n)")
	answer := Readline()
	if answer == "n" {
		fmt.Println("Enter the database path: ")
		DatabasePath = Readline()
	}
	// Open the database
	db, err := sql.Open("sqlite", DatabasePath)
	if err != nil {
		fmt.Println(err)
		return
	}
	// Check if the database is already set
	if DB != nil {
		fmt.Println("Database already set, overwrite? (y/n)")
		answer := Readline()
		if answer == "n" {
			return
		}
	}
	// Check if the database exists
	if _, err := os.Stat(DatabasePath); os.IsNotExist(err) {
		fmt.Println("Database does not exist, create? (y/n)")
		answer := Readline()
		if answer == "n" {
			return
		}
	}
	// Set the database
	DB = db
	// Check if the database contains the tables "messages" and "followed_tags"
	// If not, create them
	InitDatabase(DB)
}

func ConfigureFollowedTags() {
	db := GetDatabase()
	fmt.Println("At the moment you follow the following tags:")
	// Get the tags that the user is following
	tags := GetFollowedTags(db)
	// Show the tags
	for _, tag := range tags {
		fmt.Print(tag, " ")
	}
	fmt.Println()
	fmt.Println("Enter the tags you want to follow, separated by spaces\nTo remove tags, prefix them with a minus sign: ")
	newtags := Readline()
	// Split the tags into an array and insert them into database DB
	// using table "followed_tags"
	tagArray := strings.Split(newtags, " ")
	for _, tag := range tagArray {
		tag = strings.Trim(tag, " \n")
		if !strings.HasPrefix(tag, "-") {
			_, err := db.Exec("INSERT INTO followed_tags(tag) VALUES(?)", tag)
			if err != nil {
				fmt.Println(err)
			}
		} else {
			_, err := db.Exec("DELETE FROM followed_tags WHERE tag=?", tag[1:])
			if err != nil {
				fmt.Println(err)
			}
		}
	}
}

func GetFollowedTags(db *sql.DB) []string {
	// Get the tags from the database
	rows, err := db.Query("SELECT tag FROM followed_tags")
	if err != nil {
		fmt.Println(err)
	}
	var tags []string
	for rows.Next() {
		var tag string
		err = rows.Scan(&tag)
		if err != nil {
			fmt.Println(err)
		}
		tags = append(tags, tag)
	}
	return tags
}