func TestHistoryHeapUsage(t *testing.T) { hh1 := history.NewHistory(20) x := []string{"x", "y", "z"} a := []string{"a", "b", "c"} c := []string{"d", "e", "f"} g := []string{"j", "k", "l"} ChannelHistory := make(map[string]*history.HistoryHeap) heap.Init(hh1) fail := hh1.Hist(2) if len(fail) != 0 { t.Errorf("Asserting that empty string list returned from requesting bad history") } heap.Push(hh1, x) heap.Push(hh1, a) ChannelHistory["a"] = hh1 fail = hh1.Hist(2) if len(fail) != 0 { t.Errorf("Asserting that empty string list returned from requesting bad history") } if hh1.Len() != 2 { t.Errorf("Messages not being pushed correctly onto heap: %#v\n", hh1) } hh2 := history.NewHistory(20) heap.Init(hh2) ChannelHistory["b"] = hh2 d := ChannelHistory["b"] heap.Push(d, c) if ChannelHistory["b"].Len() != 1 { t.Errorf("item was not put onto the heap") } heap.Push(hh2, g) if ChannelHistory["b"].Len() != 2 { t.Errorf("Second item not pushed into heap") } }
//IRCConnect initializes and runs the irc connection and adds the GopherHandler to its event loop for parsing messages func IRCConnect(ircconfig parse.IRCConfig) { con := irc.IRC(ircconfig.UserName, ircconfig.UserName) con.Password = ircconfig.Passwd file, err := os.OpenFile("irc.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { panic(fmt.Sprintf("Failed to open log file:%s", err)) } irclog := log.New(file, "sheave:", log.Ldate|log.Ltime|log.Lshortfile) anagramfile, err := os.OpenFile("anagraming.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { panic(fmt.Sprintf("Failed to open anagram log file: %s", err)) } anagramlog := log.New(anagramfile, "sheave:", log.Ldate|log.Ltime|log.Lshortfile) con.Connect(ircconfig.Server) if err != nil { panic(fmt.Sprintf("Error connecting to server: %s", ircconfig.Server)) } con.AddCallback("001", func(e *irc.Event) { for _, v := range ircconfig.Channels { irclog.Printf("Initializing: %v\n", v) hist := history.NewHistory(20) heap.Init(hist) ChannelHistory[v] = hist con.Join(v) } }) con.AddCallback("PRIVMSG", func(e *irc.Event) { GopherHandler(e, con) }) con.AddCallback("PRIVMSG", func(e *irc.Event) { AnagramResponder(e, con, anagramlog) }) con.AddCallback("PRIVMSG", func(e *irc.Event) { ChannelHistorian(e) }) con.AddCallback("PRIVMSG", func(e *irc.Event) { irclog.Printf("%s %s: %s", e.Arguments[0], e.Nick, e.Arguments[1]) }) con.Loop() }