renamed IrcConnection to Connection

This commit is contained in:
blackbeard420 2020-01-05 12:48:57 -05:00
parent e806485b5f
commit c57e7a4b12

46
irc.go
View File

@ -10,7 +10,8 @@ import (
"time"
)
type IrcConnection struct {
//Connection holds the callbacks for the client
type Connection struct {
Sock net.Conn
privmsgCallback func(string, string, string)
joinCallback func(string, string)
@ -21,54 +22,63 @@ type IrcConnection struct {
channels []string
}
func (c *IrcConnection) SendPong(ping string) {
//SendPong replies to the recieved PING
func (c *Connection) SendPong(ping string) {
pong := strings.Replace(ping, "PING", "PONG", 1)
c.Sock.Write([]byte(fmt.Sprintf("%s\n", pong)))
log.Println("pong sent")
}
func (c *IrcConnection) SendPrivmsg(channel, msg string) {
//SendPrivmsg sends a privmsg to channel/target
func (c *Connection) SendPrivmsg(channel, msg string) {
_, err := c.Sock.Write([]byte(fmt.Sprintf("PRIVMSG %s :%s\n", channel, msg)))
if err != nil {
log.Printf("Error sending private message: %s\n", err.Error())
}
}
func (c *IrcConnection) SendNotice(channel, msg string) {
//SendNotice sends a notice to channel/target
func (c *Connection) SendNotice(channel, msg string) {
_, err := c.Sock.Write([]byte(fmt.Sprintf("NOTICE %s :%s\n", channel, msg)))
if err != nil {
log.Printf("Error sending NOTICE message: %s\n", err.Error())
}
}
func (c *IrcConnection) SendNick(nick string) {
//SendNick changes the clients nick
func (c *Connection) SendNick(nick string) {
c.Sock.Write([]byte(fmt.Sprintf("NICK %s\n", nick)))
}
func (c *IrcConnection) SendJoin(channel string) {
//SendJoin sends a JOIN
func (c *Connection) SendJoin(channel string) {
c.Sock.Write([]byte(fmt.Sprintf("JOIN %s\n", channel)))
}
func (c *IrcConnection) SendPart(channel, msg string) {
//SendPart sends a PART
func (c *Connection) SendPart(channel, msg string) {
c.Sock.Write([]byte(fmt.Sprintf("PART %s :%s\n", channel, msg)))
}
func (c *IrcConnection) SendQuit(msg string) {
//SendQuit sends a QUIT
func (c *Connection) SendQuit(msg string) {
c.Sock.Write([]byte(fmt.Sprintf("QUIT :%s\n", msg)))
}
func (c *IrcConnection) DelayedSend(sock net.Conn, dur time.Duration, channel, msg string) {
//SendNames sends a NAMES request for the channel
func (c *Connection) SendNames(channel string) {
c.Sock.Write([]byte(fmt.Sprintf("NAMES %s\n", channel)))
}
//DelayedSend sends a privmsg AFTER the specified delay
func (c *Connection) DelayedSend(sock net.Conn, dur time.Duration, channel, msg string) {
time.Sleep(dur)
c.SendPrivmsg(channel, msg)
}
func (c *IrcConnection) Names(channel string) {
c.Sock.Write([]byte(fmt.Sprintf("NAMES %s\n", channel)))
}
//NewIrcConnection creates and connects an IrcConnection
func NewIrcConnection(server, nick, user, pass string) *IrcConnection {
irc := &IrcConnection{}
//NewConnection creates and connects an Connection
func NewConnection(server, nick, user, pass string, chans []string) *Connection {
irc := &Connection{channels: chans}
sock, err := net.Dial("tcp", server)
if err != nil {
@ -85,7 +95,7 @@ func getNick(name string) string {
return strings.Split(name, "!")[0]
}
func (c *IrcConnection) parseMessage(line string) {
func (c *Connection) parseMessage(line string) {
if line[0] == ':' {
buf := line[1:]
params := strings.SplitN(buf, " ", 3)
@ -138,7 +148,7 @@ func (c *IrcConnection) parseMessage(line string) {
}
//Run runs the irc event loop
func (c *IrcConnection) Run() {
func (c *Connection) Run() {
rdr := bufio.NewScanner(c.Sock)
c.joined = false