This commit is contained in:
tj 2010-09-29 10:07:33 +02:00
commit e9248114a7
6 changed files with 31 additions and 13 deletions

View File

@ -1,4 +1,8 @@
<<<<<<< HEAD:Makefile
include $(GOROOT)/src/Make.inc include $(GOROOT)/src/Make.inc
=======
include $(GOROOT)/src/Make.${GOARCH}
>>>>>>> 6f3c572eae2c00aaaf57248b767adf74b571ed01:Makefile
TARG=irc TARG=irc
GOFILES=irc.go irc_struct.go irc_callback.go GOFILES=irc.go irc_struct.go irc_callback.go

View File

@ -50,8 +50,11 @@ AddCallback Example
Commands Commands
-------- --------
irc.IRC("<nick>", "<user>") //Create new ircobj
ircobj.Password = "[server password]"
ircobj.Connect("irc.someserver.com:6667") //Connect to server
ircobj.Sendraw("<string>") //sends string to server. Adds \r\n ircobj.Sendraw("<string>") //sends string to server. Adds \r\n
ircobj.Join("#channel [password]") ircobj.Join("#channel [password]")
ircobj.Privmsg("#channel", "msg") ircobj.Privmsg("#channel", "msg")
ircobj.Privmsg("nickname", "msg") ircobj.Privmsg("nickname", "msg")
ircobj.Notice("nickname or #channel", "msg") ircobj.Notice("<nickname | #channel>", "msg")

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"irc" irc "github.com/thoj/Go-IRC-Client-Library"
"fmt" "fmt"
"os" "os"
) )

10
irc.go
View File

@ -48,7 +48,7 @@ func reader(irc *IRCConnection) {
if len(args) > 1 { if len(args) > 1 {
event.Message = args[1] event.Message = args[1]
} }
args = strings.Split(args[0], " ", 0) args = strings.Split(args[0], " ", -1)
event.Code = strings.ToUpper(args[0]) event.Code = strings.ToUpper(args[0])
if len(args) > 1 { if len(args) > 1 {
event.Arguments = args[1:len(args)] event.Arguments = args[1:len(args)]
@ -61,6 +61,9 @@ func reader(irc *IRCConnection) {
func writer(irc *IRCConnection) { func writer(irc *IRCConnection) {
for !error { for !error {
b := []byte(<-irc.pwrite) b := []byte(<-irc.pwrite)
if b == nil || irc.socket == nil {
return
}
_, err := irc.socket.Write(b) _, err := irc.socket.Write(b)
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
@ -153,10 +156,13 @@ func (i *IRCConnection) Connect(server string) os.Error {
go pinger(i) go pinger(i)
i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick) i.pwrite <- fmt.Sprintf("NICK %s\r\n", i.nick)
i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user) i.pwrite <- fmt.Sprintf("USER %s 0.0.0.0 0.0.0.0 :%s\r\n", i.user, i.user)
if len(i.Password) > 0 {
i.pwrite <- fmt.Sprintf("PASS %s\r\n", i.Password)
}
return nil return nil
} }
func IRC(nick string, user string) *IRCConnection { func IRC(nick, user string) *IRCConnection {
irc := new(IRCConnection) irc := new(IRCConnection)
irc.registered = false irc.registered = false
irc.pread = make(chan string, 100) irc.pread = make(chan string, 100)

View File

@ -91,7 +91,11 @@ func (irc *IRCConnection) setupCallbacks() {
}) })
irc.AddCallback("433", func(e *IRCEvent) { irc.AddCallback("433", func(e *IRCEvent) {
irc.nick = irc.nick + "_" if len(irc.nick) > 8 {
irc.nick = "_" + irc.nick;
} else {
irc.nick = irc.nick + "_"
}
irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick)) irc.SendRaw(fmt.Sprintf("NICK %s", irc.nick))
}) })

View File

@ -10,15 +10,16 @@ import (
) )
type IRCConnection struct { type IRCConnection struct {
socket net.Conn socket net.Conn
pread, pwrite chan string pread, pwrite chan string
Error chan os.Error Error chan os.Error
syncreader, syncwriter chan bool syncreader, syncwriter chan bool
nick string nick string
user string user string
registered bool registered bool
server string server string
Password string
events map[string][]func(*IRCEvent) events map[string][]func(*IRCEvent)
lastMessage int64 lastMessage int64