2012-08-17 20:38:15 +00:00
|
|
|
package bot
|
|
|
|
|
2012-10-11 20:28:00 +00:00
|
|
|
import (
|
2013-06-01 17:10:15 +00:00
|
|
|
"fmt"
|
2013-05-06 04:29:13 +00:00
|
|
|
"github.com/chrissexton/alepale/config"
|
2012-10-11 20:28:00 +00:00
|
|
|
irc "github.com/fluffle/goirc/client"
|
|
|
|
"labix.org/v2/mgo"
|
2013-06-01 17:10:15 +00:00
|
|
|
"net/http"
|
2012-10-11 20:28:00 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// Bot type provides storage for bot-wide information, configs, and database connections
|
|
|
|
type Bot struct {
|
2012-08-17 22:09:29 +00:00
|
|
|
// Each plugin must be registered in our plugins handler. To come: a map so that this
|
|
|
|
// will allow plugins to respond to specific kinds of events
|
2012-08-26 16:15:26 +00:00
|
|
|
Plugins map[string]Handler
|
|
|
|
PluginOrdering []string
|
2012-08-17 22:09:29 +00:00
|
|
|
|
|
|
|
// Users holds information about all of our friends
|
|
|
|
Users []User
|
2013-05-08 00:08:18 +00:00
|
|
|
// Represents the bot
|
|
|
|
Me User
|
2012-08-17 22:09:29 +00:00
|
|
|
|
|
|
|
// Conn allows us to send messages and modify our connection state
|
|
|
|
Conn *irc.Conn
|
|
|
|
|
|
|
|
Config *config.Config
|
|
|
|
|
|
|
|
// Mongo connection and db allow botwide access to the database
|
2012-08-17 21:37:49 +00:00
|
|
|
DbSession *mgo.Session
|
|
|
|
Db *mgo.Database
|
2012-08-17 22:56:44 +00:00
|
|
|
|
2012-08-26 23:23:51 +00:00
|
|
|
varColl *mgo.Collection
|
|
|
|
|
2012-10-11 20:28:00 +00:00
|
|
|
logIn chan Message
|
|
|
|
logOut chan Messages
|
|
|
|
|
2012-08-17 22:56:44 +00:00
|
|
|
Version string
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
// The entries to the bot's HTTP interface
|
|
|
|
httpEndPoints []string
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-11 20:28:00 +00:00
|
|
|
// Log provides a slice of messages in order
|
|
|
|
type Log Messages
|
|
|
|
type Messages []Message
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
in <-chan Message
|
|
|
|
out chan<- Messages
|
|
|
|
entries Messages
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(in chan Message, out chan Messages) *Logger {
|
|
|
|
return &Logger{in, out, make(Messages, 0)}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunNewLogger(in chan Message, out chan Messages) {
|
|
|
|
logger := NewLogger(in, out)
|
|
|
|
go logger.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) sendEntries() {
|
|
|
|
l.out <- l.entries
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Run() {
|
|
|
|
var msg Message
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case msg = <-l.in:
|
|
|
|
l.entries = append(l.entries, msg)
|
|
|
|
case l.out <- l.entries:
|
|
|
|
go l.sendEntries()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 17:39:27 +00:00
|
|
|
// User type stores user history. This is a vehicle that will follow the user for the active
|
2012-08-17 20:38:15 +00:00
|
|
|
// session
|
|
|
|
type User struct {
|
|
|
|
// Current nickname known
|
2012-08-17 21:37:49 +00:00
|
|
|
Name string
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// LastSeen DateTime
|
|
|
|
|
|
|
|
// Alternative nicknames seen
|
2012-08-17 21:37:49 +00:00
|
|
|
Alts []string
|
2012-08-17 20:38:15 +00:00
|
|
|
|
|
|
|
// Last N messages sent to the user
|
2012-08-17 21:37:49 +00:00
|
|
|
MessageLog []string
|
2012-08-26 23:23:51 +00:00
|
|
|
|
|
|
|
Admin bool
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
|
2012-08-17 22:09:29 +00:00
|
|
|
type Message struct {
|
|
|
|
User *User
|
|
|
|
Channel, Body string
|
2012-08-25 04:49:48 +00:00
|
|
|
Raw string
|
2012-08-18 01:39:26 +00:00
|
|
|
Command bool
|
2012-08-25 04:49:48 +00:00
|
|
|
Action bool
|
2012-10-11 20:28:00 +00:00
|
|
|
Time time.Time
|
2013-04-21 20:49:00 +00:00
|
|
|
Host string
|
2012-08-17 22:09:29 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 23:23:51 +00:00
|
|
|
type Variable struct {
|
|
|
|
Variable, Value string
|
|
|
|
}
|
|
|
|
|
2012-08-17 22:56:44 +00:00
|
|
|
// NewBot creates a Bot for a given connection and set of handlers.
|
|
|
|
func NewBot(config *config.Config, c *irc.Conn) *Bot {
|
2012-08-17 21:37:49 +00:00
|
|
|
session, err := mgo.Dial(config.DbServer)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db := session.DB(config.DbName)
|
|
|
|
|
2012-10-11 20:28:00 +00:00
|
|
|
logIn := make(chan Message)
|
|
|
|
logOut := make(chan Messages)
|
|
|
|
|
|
|
|
RunNewLogger(logIn, logOut)
|
|
|
|
|
2013-01-22 21:14:04 +00:00
|
|
|
config.Nick = c.Me.Nick
|
2013-01-22 19:46:51 +00:00
|
|
|
|
2013-05-07 23:05:40 +00:00
|
|
|
users := []User{
|
|
|
|
User{
|
|
|
|
Name: config.Nick,
|
|
|
|
MessageLog: make([]string, 0),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2013-06-01 17:10:15 +00:00
|
|
|
bot := &Bot{
|
2012-08-26 16:15:26 +00:00
|
|
|
Config: config,
|
|
|
|
Plugins: make(map[string]Handler),
|
|
|
|
PluginOrdering: make([]string, 0),
|
2013-05-07 23:05:40 +00:00
|
|
|
Users: users,
|
2013-05-08 00:08:18 +00:00
|
|
|
Me: users[0],
|
2012-08-26 16:15:26 +00:00
|
|
|
Conn: c,
|
|
|
|
DbSession: session,
|
|
|
|
Db: db,
|
2012-08-26 23:23:51 +00:00
|
|
|
varColl: db.C("variables"),
|
2012-10-11 20:28:00 +00:00
|
|
|
logIn: logIn,
|
|
|
|
logOut: logOut,
|
2012-08-26 16:15:26 +00:00
|
|
|
Version: config.Version,
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/", bot.serveRoot)
|
2013-06-01 17:29:12 +00:00
|
|
|
if config.HttpAddr == "" {
|
|
|
|
config.HttpAddr = "127.0.0.1:1337"
|
|
|
|
}
|
|
|
|
go http.ListenAndServe(config.HttpAddr, nil)
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
return bot
|
2012-08-17 20:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a constructed handler to the bots handlers list
|
2012-08-17 22:56:44 +00:00
|
|
|
func (b *Bot) AddHandler(name string, h Handler) {
|
|
|
|
b.Plugins[strings.ToLower(name)] = h
|
2012-08-26 16:15:26 +00:00
|
|
|
b.PluginOrdering = append(b.PluginOrdering, name)
|
2013-06-01 17:10:15 +00:00
|
|
|
if entry := h.RegisterWeb(); entry != nil {
|
|
|
|
b.httpEndPoints = append(b.httpEndPoints, *entry)
|
|
|
|
}
|
2012-08-17 21:37:49 +00:00
|
|
|
}
|
2012-08-26 20:15:50 +00:00
|
|
|
|
|
|
|
// Sends message to channel
|
|
|
|
func (b *Bot) SendMessage(channel, message string) {
|
|
|
|
b.Conn.Privmsg(channel, message)
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Notify plugins that we've said something
|
|
|
|
b.selfSaid(channel, message)
|
2012-08-26 20:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sends action to channel
|
|
|
|
func (b *Bot) SendAction(channel, message string) {
|
|
|
|
b.Conn.Action(channel, message)
|
2013-05-08 00:08:18 +00:00
|
|
|
|
|
|
|
// Notify plugins that we've said something
|
|
|
|
b.selfSaid(channel, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles incomming PRIVMSG requests
|
|
|
|
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
|
|
|
|
msg := b.buildMessage(conn, line)
|
|
|
|
|
|
|
|
if strings.HasPrefix(msg.Body, "help") && msg.Command {
|
|
|
|
parts := strings.Fields(strings.ToLower(msg.Body))
|
|
|
|
b.checkHelp(msg.Channel, parts)
|
|
|
|
goto RET
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range b.PluginOrdering {
|
|
|
|
p := b.Plugins[name]
|
|
|
|
if p.Message(msg) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RET:
|
|
|
|
b.logIn <- msg
|
|
|
|
return
|
2012-08-26 20:15:50 +00:00
|
|
|
}
|
2013-06-01 17:10:15 +00:00
|
|
|
|
|
|
|
func (b *Bot) serveRoot(w http.ResponseWriter, r *http.Request) {
|
|
|
|
for _, entry := range b.httpEndPoints {
|
|
|
|
fmt.Fprintf(w, "<a href=\"%s\">%s</a>", entry, entry)
|
|
|
|
}
|
|
|
|
}
|