-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (67 loc) · 2.33 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
package tusk
import (
"github.com/JoshStrobl/trunk"
"github.com/lrstanley/girc"
"os"
"os/user"
"path/filepath"
"plugin"
)
// A Narwhal is no Narwhal without their tusk!
// Consistent paths
var Paths []string
// Config is our Narwhal Config
var Config NarwhalConfig
// PluginManager is our Plugin Manager
var PluginManager NarwhalPluginManager
func init() {
var getUserErr error
var currentUser *user.User
currentUser, getUserErr = user.Current() // Attempt to get the current user
if getUserErr != nil { // If we successfully got the user
trunk.LogFatal("Failed to get the current user: " + getUserErr.Error())
}
workdir, getWdErr := os.Getwd() // Get the current working directory
if getWdErr != nil { // If we failed to get the current working dir
trunk.LogFatal("Failed to get the current working directory: " + getWdErr.Error())
}
Paths = []string{
filepath.Join(currentUser.HomeDir, ".config", "narwhal"),
workdir,
"/etc/narwhal",
"/usr/share/defaults/narwhal",
}
}
// NewTusk will create a new tusk for our Narwhal, but only one tusk is allowed at a time.
func NewTusk() {
var newTuskErr error
Config, newTuskErr = ReadConfig()
if newTuskErr == nil { // Read our config
PluginManager.Modules = make(map[string]plugin.Symbol)
if loadPluginsErr := PluginManager.LoadPlugins(); loadPluginsErr != nil { // Failed to load a plugin
trunk.LogWarn("Failed to load plugin: " + loadPluginsErr.Error())
}
ircConfig := girc.Config{
Server: Config.Network,
Port: Config.Port,
Name: Config.Name,
Nick: Config.User,
User: Config.User,
SASL: &girc.SASLPlain{
User: Config.User,
Pass: Config.Password,
},
}
client := girc.New(ircConfig)
client.Handlers.Add(girc.CONNECTED, OnConnected) // On CONNECTED, trigger OnConnected
client.Handlers.Add(girc.INVITE, OnInvite) // On INVITE, trigger OnInvite
client.Handlers.Add(girc.JOIN, OnJoin) // On JOIN, trigger our OnJoin
client.Handlers.Add(girc.PRIVMSG, Parser) // On PRIVMSG, trigger our Parser
client.Handlers.Add(girc.KICK, OnKick) // On KICK, trigger our OnKick
if newTuskErr = client.Connect(); newTuskErr != nil { // Failed during run
trunk.LogFatal("Failed to run client: " + newTuskErr.Error())
}
} else {
trunk.LogFatal("Failed to read or parse config: " + newTuskErr.Error())
}
}