-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathm1ch3l.go
139 lines (123 loc) · 4.69 KB
/
m1ch3l.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
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"os"
)
type EventType int
// Types of Event
const (
E_PRIVMSG = iota // A PRIVMSG sent in private or on a channel
E_NOTICE // Same as PRIVMSG but for notices
E_JOIN // A user joined (origin is the channel, data is the user)
E_PART // A user left a chan (same as E_JOIN)
E_DISCONNECT // The bot has been disconnected from a server
E_QUIT // A user has quit (origin is the user)
E_NICK // A user changed nick (origin is old nick, data is the new nick)
E_PING // A ping has been sent by the server
E_KICK // A user has been kicked (user is the admin, data is the target)
E_NAMES // List of names
E_ENDOFNAMES // End of list of names
)
type ActionType int
// Types of Action
const (
A_SAY = iota // Say something (requires server, channel, data)
A_KICK // Kick someone (requires server, channel, user, optionally data)
A_JOIN // Join a channel (requires server, channel)
A_PART // Leave a channel (requires server, channel, optionally data)
A_OP // Op a user (requires server, channel, user)
A_RAW // Send a raw IRC command (requires server), not yet implemented
A_NAMES // Send a NAMES command (requires channel)
)
// Events are sent by the server to each module
type Event struct {
AdminCmd bool // Is this event admin-issued?
Server string // The server on which the event occured
Channel string // The #channel on which the event occured
User string // Nickname of the user who triggered the event
Type EventType // Type of the event
Data string // Additional data (may change depending on the event)
Raw string // Raw command
CmdId int // Id of the command
}
// Priority of the action (meaningful when anti-flood protection is enabled)
const (
PRIORITY_LOW = 1
PRIORITY_MEDIUM = 2
PRIORITY_HIGH = 3
)
// Actions are sent by modules to perform an action on the server
type Action struct {
Server string // What server to operate on
Channel string // What channel to operate on
User string // Who is concerned
Data string // Additional data
Priority int // Priority of the message
Type ActionType // What to do
Raw string // If Type = RAW, send this directly over the server
}
// Main config of gorobot
type Config struct {
AutoRejoinOnKick bool // Rejoin channel when kicked
Logs ConfigLogs // Log config
Servers map[string]*ConfigServer // Servers to connects to
Broadcast BroadcastConfig // Configuration of the broadcast module
Pathwar PathwarConfig // Configuration of the pathwar module
Scripts ScriptsConfig // Configuration of the scripts module
WebAPI WebAPIConfig // Configuration of the WebAPI module
}
// Config for logs
type ConfigLogs struct {
Enable bool // Enable logging
Directory string // Directory to store logs
}
// Config for an IRC serv to connect to
type ConfigServer struct {
Name string // Alias of the server
Host string // Address of the server
FloodControl bool // Enable flood control
Nickname string // Nickname of the IRC robot
Realname string // Real name of the IRC robot
Username string // Username of the IRC robot
Password string // Password of the IRC server
NickServPassword string // Password to identify to NickServ
Channels map[string]*ConfigChannel // List of channels to join
}
// Config for an IRC channel to join
type ConfigChannel struct {
Name string // Name of the channel
Password string // Password of the channel
Master bool // Enable admin commands on that channel
}
// Flag settings
var (
configPath = flag.String("c", "gorobot.json", "path to the configuration file (e.g, gorobot.json)")
)
// Creates a new Config from a config file
func newConfig(path string) *Config {
file, e := ioutil.ReadFile(path)
if e != nil {
log.Fatalf("config error: %v", e)
}
var cfg Config
e = json.Unmarshal(file, &cfg)
if e != nil {
log.Fatalf("config error: %v", e)
}
// redirect logging to a file
writer, err := os.OpenFile("gorobot.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Unable to open file log: %v", err)
}
log.SetOutput(writer)
return &cfg
}
func main() {
flag.Parse()
cfg := newConfig(*configPath)
bot := NewBot(cfg)
bot.Run()
}