Пример #1
0
func main() {
	rand.Seed(time.Now().UnixNano())
	c := irc.SimpleClient("Jubeat")
	c.SSL = true
	c.SSLConfig = &tls.Config{InsecureSkipVerify: true}

	go readLog()
	disconnected := make(chan bool)
	stopRead := make(chan bool)
	c.AddHandler("connected", func(conn *irc.Conn, line *irc.Line) {
		conn.Join("#jubeater")
	})

	c.AddHandler("JOIN", func(conn *irc.Conn, line *irc.Line) {
		if line.Nick == conn.Me.Nick {
			go func() {
				for {
					select {
					case m := <-msg:
						log.Println(m)
						conn.Privmsg("#jubeater", m)
					case <-stopRead:
						return
					}
				}
			}()
		}
	})

	c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
		if len(line.Args) == 2 && line.Args[0][0] == '#' {
			cmds := strings.Fields(line.Args[1])
			if line.Nick == "s" {
				cmds = cmds[1:]
			}
			if len(cmds) > 0 && len(cmds[0]) > 2 && cmds[0][0] == '!' {
				switch cmds[0][1:] {
				case "선곡":
					var lv []int
					if len(cmds) > 1 {
						lvs := strings.Split(cmds[1], "-")
						for _, l := range lvs {
							lv = append(lv, atoi(l))
						}
					} else {
						lv = append(lv, 0)
					}
					sort.Ints(lv)
					selectMusic(conn, lv...)
				case "사찰":
					if len(cmds) == 3 {
						if err := addSachalUser(cmds[1], cmds[2]); err != nil {
							conn.Privmsg("#jubeater", err.Error())
						} else {
							conn.Privmsg("#jubeater", "사찰 등록 완료")
						}
					} else {
						conn.Privmsg("#jubeater", "!사찰 [Handle] [Rival ID]")
					}
				case "라이벌":
					if len(cmds) == 3 {
						if err := setRivalUser(cmds[1], cmds[2]); err != nil {
							conn.Privmsg("#jubeater", err.Error())
						} else {
							conn.Privmsg("#jubeater", "라이벌 등록 완료")
						}
					} else {
						conn.Privmsg("#jubeater", "!라이벌 [내 Rival ID] [라이벌 Rival ID]")
					}
				default:
					conn.Privmsg("#jubeater", "잘못된 명령입니다.")
				}
			}
		}
	})

	c.AddHandler("disconnected", func(conn *irc.Conn, line *irc.Line) {
		stopRead <- true
		disconnected <- true
	})

	for {
		if err := c.Connect("localhost:16661"); err != nil {
			log.Println("Connection error: ", err)
		} else {
			<-disconnected
		}
		time.Sleep(10 * time.Second)
	}
}
Пример #2
0
func main() {
	flag.Parse()

	// create new IRC connection
	c := irc.SimpleClient("GoTest", "gotest")
	c.EnableStateTracking()
	c.AddHandler("connected",
		func(conn *irc.Conn, line *irc.Line) { conn.Join(*channel) })

	// Set up a handler to notify of disconnect events.
	quit := make(chan bool)
	c.AddHandler("disconnected",
		func(conn *irc.Conn, line *irc.Line) { quit <- true })

	// set up a goroutine to read commands from stdin
	in := make(chan string, 4)
	reallyquit := false
	go func() {
		con := bufio.NewReader(os.Stdin)
		for {
			s, err := con.ReadString('\n')
			if err != nil {
				// wha?, maybe ctrl-D...
				close(in)
				break
			}
			// no point in sending empty lines down the channel
			if len(s) > 2 {
				in <- s[0 : len(s)-1]
			}
		}
	}()

	// set up a goroutine to do parsey things with the stuff from stdin
	go func() {
		for cmd := range in {
			if cmd[0] == ':' {
				switch idx := strings.Index(cmd, " "); {
				case cmd[1] == 'd':
					fmt.Printf(c.String())
				case cmd[1] == 'f':
					if len(cmd) > 2 && cmd[2] == 'e' {
						// enable flooding
						c.Flood = true
					} else if len(cmd) > 2 && cmd[2] == 'd' {
						// disable flooding
						c.Flood = false
					}
					for i := 0; i < 20; i++ {
						c.Privmsg("#", "flood test!")
					}
				case idx == -1:
					continue
				case cmd[1] == 'q':
					reallyquit = true
					c.Quit(cmd[idx+1 : len(cmd)])
				case cmd[1] == 'j':
					c.Join(cmd[idx+1 : len(cmd)])
				case cmd[1] == 'p':
					c.Part(cmd[idx+1 : len(cmd)])
				}
			} else {
				c.Raw(cmd)
			}
		}
	}()

	for !reallyquit {
		// connect to server
		if err := c.Connect(*host); err != nil {
			fmt.Printf("Connection error: %s\n", err)
			return
		}

		// wait on quit channel
		<-quit
	}
}