示例#1
0
文件: jabber.go 项目: pepl/ircflu
func (sys *JabberSubSystem) Run(channelIn, channelOut chan msgsystem.Message) {
	if len(sys.server) == 0 {
		return
	}

	var talk *xmpp.Client
	var err error
	if sys.notls {
		talk, err = xmpp.NewClientNoTLS(sys.server, sys.username, sys.password, false)
	} else {
		talk, err = xmpp.NewClient(sys.server, sys.username, sys.password, false)
	}
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		for {
			chat, err := talk.Recv()
			if err != nil {
				log.Fatal(err)
			}
			switch v := chat.(type) {
			case xmpp.Chat:
				if len(v.Text) > 0 {
					fmt.Println(v.Remote, v.Text)

					msg := msgsystem.Message{
						//						To:     []string{sys.username},
						Msg:    v.Text,
						Source: v.Remote,
						Authed: auth.IsAuthed(v.Remote),
					}
					channelIn <- msg
				}
			case xmpp.Presence:
				fmt.Println(v.From, v.Show)
			}
		}
	}()
}
示例#2
0
文件: irc.go 项目: pepl/ircflu
func (sys *IrcSubSystem) Run(channelIn, channelOut chan msgsystem.Message) {
	if len(sys.irchost) == 0 {
		return
	}

	// channel signaling irc connection status
	sys.ConnectedState = make(chan bool)

	// setup IRC client:
	sys.client = irc.SimpleClient(sys.ircnick, "ircflu", "ircflu")
	sys.client.Config().SSL = sys.ircssl

	sys.client.HandleFunc("connected",
		func(conn *irc.Conn, line *irc.Line) { sys.ConnectedState <- true })

	sys.client.HandleFunc("disconnected",
		func(conn *irc.Conn, line *irc.Line) { sys.ConnectedState <- false })

	sys.client.HandleFunc("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
		channel := line.Args[0]
		text := ""
		if len(line.Args) > 1 {
			text = line.Args[1]
		}
		if channel == sys.ircnick {
			log.Println("PM from " + line.Src)
			channel = line.Src // replies go via PM too.
		} else {
			log.Println("Message in channel " + line.Args[0] + " from " + line.Src)
		}

		msg := msgsystem.Message{
			To:     []string{channel},
			Msg:    text,
			Source: line.Src,
			Authed: auth.IsAuthed(line.Src),
		}
		channelIn <- msg
	})

	// loop on IRC dis/connected events
	go func() {
		for {
			log.Println("Connecting to IRC:", sys.irchost)
			sys.client.Config().Server = sys.irchost
			sys.client.Config().Pass = sys.ircpassword
			err := sys.client.Connect()
			if err != nil {
				log.Println("Failed to connect to IRC:", sys.irchost)
				log.Println(err)
				continue
			}
			for {
				status := <-sys.ConnectedState
				if status {
					log.Println("Connected to IRC:", sys.irchost)

					if len(sys.channels) == 0 {
						// join default channel
						sys.Join(sys.ircchannel)
					} else {
						// we must have been disconnected, rejoin channels
						sys.Rejoin()
					}
				} else {
					log.Println("Disconnected from IRC:", sys.irchost)
					break
				}
			}
			time.Sleep(5 * time.Second)
		}
	}()
}