-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.go
64 lines (53 loc) · 1.03 KB
/
irc.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
// IRC quotes handling.
// Author:
// Andrea Cervesato <andrea.cervesato@mailbox.org>
package main
import (
"bufio"
"log"
"math/rand"
"os"
"time"
)
// Quotes file rapresentation.
type IRCQuotes struct {
Location string
Lines []string
}
// Read a random quote inside quotes file.
func (quotes IRCQuotes) RandQuote() string {
rand.Seed(time.Now().UnixNano())
count := len(quotes.Lines)
randline := rand.Intn(count)
quote := quotes.Lines[randline]
return quote
}
// Create a new quotes file object.
func NewIRCQuotes(location string) *IRCQuotes {
quotes := IRCQuotes{
Location: location,
Lines: make([]string, 0),
}
// open file
file, err := os.Open(location)
if err != nil {
log.Fatal(err)
}
// always close quotes file
defer func() {
err = file.Close()
if err != nil {
log.Fatal(err)
}
}()
// read all quotes
scanner := bufio.NewScanner(file)
for scanner.Scan() {
quotes.Lines = append(quotes.Lines, scanner.Text())
}
err = scanner.Err()
if err != nil {
log.Fatal(err)
}
return "es
}