-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
103 lines (80 loc) · 2.25 KB
/
main.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
package main
import (
"fmt"
"os"
"github.com/alexcesaro/log"
"github.com/alexcesaro/log/golog"
"github.com/voldyman/ssh-chat-notify/client"
"github.com/voldyman/ssh-chat-notify/notifyi"
"github.com/voldyman/ssh-chat-notify/parser"
)
const username = "notifyi"
const sshChatHost = "localhost:2022"
var logger log.Logger
func main() {
logger = golog.New(os.Stderr, log.Debug)
if err := run(); err != nil {
logger.Error("Failed:", err)
}
}
type clientComms struct {
client *client.Client
}
func (c *clientComms) PrivateMessage(toUsername, message string) error {
c.client.WriteLine(fmt.Sprintf("/msg %s %s", toUsername, message))
return nil
}
func (c *clientComms) PublicMessage(message string) error {
c.client.WriteLine(message)
return nil
}
func run() error {
dest := sshChatHost
if len(os.Args) >= 2 {
dest = os.Args[1]
}
client, err := client.CreateClient(dest, username)
if err != nil {
return err
}
defer client.Close()
bot := notifyi.New(username, &clientComms{client})
lineParser := parser.New()
for {
line, err := client.ScanLine()
if err != nil {
return err
}
fmt.Println("Got Line", line)
parsedResult, err := lineParser.Parse([]byte(line))
if err != nil {
logger.Warningf("parsing failed: %+v", err)
continue
}
switch result := parsedResult.(type) {
case parser.PrivateMsg:
logger.Info("Private Message", quote(result.From), "->", result.Message)
bot.PrivateMessage(result.From, result.Message)
case parser.PublicMsg:
logger.Info("Public Message", quote(result.From), "->", result.Message)
bot.PublicMessage(result.From, result.Message)
case parser.ActionMsg:
logger.Info("Smartass says:", quote(result.From), "->", result.Message)
bot.ActionMessage(result.From, result.Message)
case parser.UsernameChangeMsg:
logger.Info("Nick change", quote(result.FromUsername), "->", result.ToUsername)
bot.UsernameChangeMessage(result.FromUsername, result.ToUsername)
case parser.JoinMsg:
switch result.Status {
case parser.UserJoined:
bot.UserJoinedMessage(result.Username)
case parser.UserLeft:
bot.UserLeftMessage(result.Username)
}
logger.Info("User", quote(result.Username), "has", result.Status)
}
}
}
func quote(s string) string {
return "\"" + s + "\""
}