Ejemplo n.º 1
0
Archivo: scli.go Proyecto: dorsha/slack
// saveHistory periodically saves the line history to the history file
func saveHistory(line *liner.State, stop chan bool) {
	ticker := time.NewTicker(1 * time.Minute)
	defer ticker.Stop()
	for {
		select {
		case <-stop:
			break
		case <-ticker.C:
			if f, err := os.Create(*hist); err != nil {
				if *verbose {
					log.Print("Error writing history file: ", err)
				}
			} else {
				line.WriteHistory(f)
				f.Close()
			}
		}
	}
}
Ejemplo n.º 2
0
func receiveMessages(line *liner.State, s *slack.Slack, in chan *slack.Message, stop chan bool) {
	ticker := time.NewTicker(1 * time.Minute)
	defer ticker.Stop()
	latest := make(map[string]string)
	for {
		select {
		case <-stop:
			break
		case <-ticker.C:
			for k, v := range latest {
				s.Mark(k, v)
			}
		case msg := <-in:
			if msg == nil || msg.Type == "error" {
				if msg == nil {
					line.PrintAbovePrompt("Input channel closed - Reconnecting...")
				} else {
					line.PrintAbovePrompt(msg.Error.Msg + " - Reconnecting...")
				}
				var err error
				// Try the channel and messaging
				for err != nil {
					time.Sleep(1 * time.Minute)
					in := make(chan *slack.Message)
					info, err = s.RTMStart("", in, nil)
					if err == nil {
						line.PrintAbovePrompt("Reconnected...")
					} else {
						close(in)
					}
				}
			} else if msg.Type == "message" && msg.User != info.Self.ID {
				if err := line.PrintAbovePrompt(format(msg)); err != nil {
					fmt.Println(err)
				}
				latest[msg.Channel] = msg.Timestamp
			}
		}
	}
}