Created
November 27, 2016 17:17
-
-
Save godwhoa/9a19ab8cdab0e0996d486192ef43c189 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"gopkg.in/sorcix/irc.v1" | |
"net" | |
) | |
var ( | |
nick = "yourgobotname" | |
channel = "#whateverchan" | |
server = "chat.freenode.net:6667" | |
conn net.Conn | |
irc_conn *irc.Conn | |
message *irc.Message | |
err error | |
) | |
func join_cmds(taken bool) { | |
if taken { | |
nick += "_" | |
} | |
irc_conn.Encode(&irc.Message{Command: irc.NICK, | |
Params: []string{nick}}) | |
irc_conn.Encode(&irc.Message{Command: irc.USER, | |
Params: []string{nick, "0", "*", nick}}) | |
} | |
func main() { | |
// Try to connect via. TLS | |
conn, err = tls.Dial("tcp", server, &tls.Config{InsecureSkipVerify: true}) | |
// If that fails fallback to tcp | |
if err != nil { | |
conn, err = net.Dial("tcp", server) | |
if err != nil { | |
fmt.Println("Failed to connect.") | |
} | |
} | |
// pass it net.Conn | |
irc_conn = irc.NewConn(conn) | |
join_cmds(false) | |
for { | |
message, err = irc_conn.Decode() | |
if err != nil { | |
fmt.Println("Failed to decode.") | |
break | |
} | |
switch message.Command { | |
case irc.RPL_WELCOME: | |
irc_conn.Encode(&irc.Message{Command: irc.JOIN, | |
Params: []string{channel}}) | |
case irc.ERR_NICKNAMEINUSE: | |
join_cmds(true) | |
case irc.JOIN: | |
fmt.Printf("%s joined\n", message.Name) | |
case irc.PART: | |
fmt.Printf("%s left\n", message.Name) | |
case irc.PRIVMSG: | |
fmt.Printf("from: %s body: %s\n", message.Name, message.Trailing) | |
case irc.PING: | |
irc_conn.Encode(&irc.Message{ | |
Command: irc.PONG, | |
Params: message.Params, | |
Trailing: message.Trailing, | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment