|
|
@ -4,10 +4,16 @@ import ( |
|
|
|
"crypto/tls" |
|
|
|
"flag" |
|
|
|
"fmt" |
|
|
|
"goqdpmbot/events_alerter" |
|
|
|
"goqdpmbot/works" |
|
|
|
"os" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
|
|
|
|
// "runtime"
|
|
|
|
// "strconv"
|
|
|
|
"strings" |
|
|
|
"sync" |
|
|
|
"time" |
|
|
|
|
|
|
|
"github.com/mattn/go-xmpp" |
|
|
@ -22,6 +28,11 @@ type cfgStruct struct { |
|
|
|
notls bool |
|
|
|
} |
|
|
|
|
|
|
|
type commandMsg struct { |
|
|
|
remote string |
|
|
|
message string |
|
|
|
} |
|
|
|
|
|
|
|
func serverName(host string) string { |
|
|
|
return strings.Split(host, ":")[0] |
|
|
|
} |
|
|
@ -96,32 +107,100 @@ func jabberClient(cfg cfgStruct) *xmpp.Client { |
|
|
|
return talk |
|
|
|
} |
|
|
|
|
|
|
|
func jabberReader(talk *xmpp.Client, in <-chan int) { |
|
|
|
// читаем из джаббера и отправляем в канал
|
|
|
|
func jabberReader(talk *xmpp.Client, command chan<- commandMsg, in <-chan int) { |
|
|
|
for { |
|
|
|
_, err := talk.Recv() |
|
|
|
chat, err := talk.Recv() |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err) |
|
|
|
} |
|
|
|
switch v := chat.(type) { |
|
|
|
case xmpp.Chat: |
|
|
|
com := commandMsg{remote: v.Remote, message: v.Text} |
|
|
|
command <- com |
|
|
|
//case xmpp.Presence:
|
|
|
|
// com := commandMsg{remote: v.From, message: v.Show}
|
|
|
|
// command <- com
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// отправляем ответы из канала
|
|
|
|
func jabberSender(talk *xmpp.Client, msgChan <-chan commandMsg) { |
|
|
|
for { |
|
|
|
msg := <-msgChan |
|
|
|
remote := strings.Split(msg.remote, "/")[0] |
|
|
|
talk.Send(xmpp.Chat{Remote: remote, Type: "chat", Text: msg.message}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// воркер
|
|
|
|
func worker(workerNum int, msgChan <-chan commandMsg, readerClose chan int, ansChan chan<- commandMsg) { |
|
|
|
for { |
|
|
|
message := <-msgChan |
|
|
|
fmt.Println("Worker = ", workerNum, "message= ", message.message) |
|
|
|
cmd := strings.Split(message.message, " ") |
|
|
|
workMap := works.Works() |
|
|
|
_, workExists := workMap[cmd[0]] |
|
|
|
if workExists { |
|
|
|
work, _ := workMap[cmd[0]] |
|
|
|
ans, err := work.(func(...string) (string, error))(cmd[1:]...) |
|
|
|
if err != nil { |
|
|
|
ansChan <- commandMsg{remote: message.remote, message: err.Error()} |
|
|
|
} else { |
|
|
|
ansChan <- commandMsg{remote: message.remote, message: ans} |
|
|
|
} |
|
|
|
} else if len(message.message) > 0 { |
|
|
|
ans := "неизвестная комманда" |
|
|
|
ansChan <- commandMsg{remote: message.remote, message: ans} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func main() { |
|
|
|
readerClose := make(chan int, 1) |
|
|
|
cfg := cfgRead("goxmppalert.ini") |
|
|
|
var send_remote string |
|
|
|
var send_text string |
|
|
|
if len(os.Args) == 3 { |
|
|
|
send_remote = os.Args[1] |
|
|
|
send_text = os.Args[2] |
|
|
|
} else { |
|
|
|
send_remote = cfg.dest |
|
|
|
send_text = os.Args[1] |
|
|
|
//нопоминальщик
|
|
|
|
func reminder(readerClose chan int, ansChan chan<- commandMsg) { |
|
|
|
for { |
|
|
|
msgSl := events_alerter.BuildAlert() |
|
|
|
fmt.Println("alerter works") |
|
|
|
fmt.Println(msgSl) |
|
|
|
for _, msgInt := range msgSl { |
|
|
|
tmpMsg := commandMsg{remote: msgInt.Remote(), message: msgInt.Message()} |
|
|
|
fmt.Println(tmpMsg) |
|
|
|
ansChan <- tmpMsg |
|
|
|
} |
|
|
|
time.Sleep(60 * time.Second) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func main() { |
|
|
|
cfg := cfgRead("goqdpmbot.ini") // читаем конфигу
|
|
|
|
|
|
|
|
goroutinesNum := 5 // количество потоков
|
|
|
|
wg := &sync.WaitGroup{} // ожидаем интеррапта
|
|
|
|
signal_channel := make(chan os.Signal, 1) |
|
|
|
readerClose := make(chan int, 1) // для закрытия всего зоопарка воркеров
|
|
|
|
// каналы обмена инфой
|
|
|
|
comChan := make(chan commandMsg, 5) |
|
|
|
ansChan := make(chan commandMsg, 5) |
|
|
|
// запускаем клиента
|
|
|
|
talk := jabberClient(cfg) |
|
|
|
go jabberReader(talk, readerClose) |
|
|
|
go talk.Send(xmpp.Chat{Remote: send_remote, Type: "chat", Text: send_text}) |
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
go jabberReader(talk, comChan, readerClose) |
|
|
|
go jabberSender(talk, ansChan) |
|
|
|
// и воркеров
|
|
|
|
for i := 0; i < goroutinesNum; i++ { |
|
|
|
go worker(i, comChan, readerClose, ansChan) |
|
|
|
fmt.Println("worker started", i) |
|
|
|
} |
|
|
|
go reminder(readerClose, ansChan) |
|
|
|
// ожидаем выхода
|
|
|
|
wg.Add(1) |
|
|
|
go func() { |
|
|
|
<-signal_channel |
|
|
|
wg.Done() |
|
|
|
}() |
|
|
|
wg.Wait() |
|
|
|
close(readerClose) |
|
|
|
time.Sleep(1 * time.Second) |
|
|
|
fmt.Println("interrupt") |
|
|
|
talk.Close() |
|
|
|
} |