Exemplo n.º 1
0
func queryChannelsOnServer(server string, channels []string) {
	defer func() {
		err, isErr := recover().(error)
		if isErr {
			fmt.Fprintf(os.Stderr, "Error occurred when querying channels on %s: %s.\n", server, err.Error())
		}
	}()

	var debugIRC io.Writer = nil
	if confDebugIRC {
		debugIRC = os.Stderr
	}

	bot, err := irc.Connect(server, confNickname, confUsername, debugIRC)
	if err != nil {
		panic(err)
	}
	defer bot.Disconnect()

	for i, ch := range channels {

		errs := []error{}

		for _, method := range confQueryMethods {
			res, err := method.Query(bot, ch)
			if err != nil {
				errs = append(errs, err)
				continue
			}
			fmt.Printf("%s\t%d\t%s\t%s\n", res.Name, res.NumberOfUsers, method.Name(), res.Topic)
			goto next
		}

		fmt.Fprintf(os.Stderr, "Failed to query %s@%s,\n", ch, server)
		for _, err := range errs {
			fmt.Fprintf(os.Stderr, "  %s\n", err.Error())
		}

	next:
		if i+1 != len(channels) {
			time.Sleep(confTimeout)
		}
	}

}
Exemplo n.º 2
0
func channelCheckServer(ctx *mainContext, db *sql.DB, network *dbNetwork, chs []channel) {
	defer func() {
		err, isErr := recover().(error)
		if isErr {
			log.Printf("Error occurred while checking channels on '%s': %s\n", network.network, err.Error())
		}
	}()

	network_chs := []channel{}
	for _, ch := range chs {
		if ch.Network() != network.network {
			continue
		}

		if ch.Checked() {
			dur := time.Now().Sub(ch.CheckTime())
			if dur < time.Hour*24*7 {
				continue
			}
		}

		network_chs = append(network_chs, ch)
	}

	if len(network_chs) == 0 {
		return
	}

	log.Printf("Checking channels on %s.\n", network.network)

	var bot *irc.Conn
	var err error
	for _, server := range network.servers {
		bot, err = irc.Connect(server, ctx.conf.IRCBotNickname(), ctx.conf.IRCBotRealname(), nil)
		if err != nil {
			log.Printf("Failed to connect to %s: %s.\n", server, err.Error())
			continue
		}
		defer bot.Disconnect()
		break
	}
	if bot == nil {
		log.Printf("Could not connect to any address associated with %s.\n", network.network)
		return
	}

	for _, ch := range network_chs {
		if ch.Network() != network.network {
			continue
		}

		log.Printf("Checking %s@%s.\n", ch.Name(), ch.Network())
		status, method, err := channelCheck(bot, ch.Name())
		str := ""
		if err != nil {
			str = err.Error()
			err = dbUpdateStatus(db, ch.Name(), ch.Network(), 0, "", "fail", str, time.Now())
		} else {
			log.Printf("%s@%s %d Topic: %s\n", ch.Name(), ch.Network(), status.NumberOfUsers, status.Topic)
			err = dbUpdateStatus(db, ch.Name(), ch.Network(), status.NumberOfUsers, status.Topic, method, str, time.Now())
		}
		if err != nil {
			log.Fatalf("Database error when updating channel status: %s.\n", err.Error())
		}
		time.Sleep(5 * time.Second)
	}

}