X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=go%2Fr3-netstatus%2Fr3xmppbot%2Fr3xmppbot.go;h=293be184f644c09a943d3e1ebff1d31bd9eccbcb;hb=36c81737d355f02eb05b150472c9bf44fbf46120;hp=48a9f5c16b552c353f09b05c52a9529cf3e11104;hpb=70bd6ea215fdd1dbd2eb9556ff4aa0a2acde24bc;p=svn42.git diff --git a/go/r3-netstatus/r3xmppbot/r3xmppbot.go b/go/r3-netstatus/r3xmppbot/r3xmppbot.go index 48a9f5c..293be18 100644 --- a/go/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/go/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -1,8 +1,9 @@ +// (c) Bernhard Tittelbach, 2013 + package r3xmppbot import ( xmpp "code.google.com/p/goexmpp" - "log" "crypto/tls" "os" "time" @@ -75,7 +76,7 @@ func (botdata *XmppBot) makeXMPPPresence(to, ptype, show, status string) *xmpp.P gen_status = nil } else { gen_status = &xmpp.Generic{Chardata: status} - } + } return &xmpp.Presence{Header: xmppmsgheader, Show: gen_show, Status: gen_status} } @@ -141,13 +142,13 @@ type XmppBot struct { func (data RealraumXmppNotifierConfig) saveTo(filepath string) () { fh, err := os.Create(filepath) if err != nil { - log.Println(err) + Syslog_.Println(err) return } defer fh.Close() enc := json.NewEncoder(fh) if err = enc.Encode(&data); err != nil { - log.Println(err) + Syslog_.Println(err) return } } @@ -155,13 +156,13 @@ func (data RealraumXmppNotifierConfig) saveTo(filepath string) () { func (data RealraumXmppNotifierConfig) loadFrom(filepath string) () { fh, err := os.Open(filepath) if err != nil { - log.Println(err) + Syslog_.Println(err) return } defer fh.Close() dec := json.NewDecoder(fh) if err = dec.Decode(&data); err != nil { - log.Println(err) + Syslog_.Println(err) return } for to, jiddata := range data { @@ -183,13 +184,16 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc defer func() { if x := recover(); x != nil { - log.Printf("handleEventsforXMPP: run time panic: %v", x) + Syslog_.Printf("handleEventsforXMPP: run time panic: %v", x) + //FIXME: signal that xmpp bot has crashed } }() for { select { - case pe := <-presence_events: + case pe, pe_still_open := <-presence_events: + if ! pe_still_open { break } + Debug_.Printf("handleEventsforXMPP<-presence_events: %T %+v", pe, pe) switch pec := pe.(type) { case xmpp.Stanza: xmppout <- pec @@ -200,10 +204,10 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc xmppout <- botdata.makeXMPPMessage(to, pec, nil) } } - + case XMPPStatusEvent: xmppout <- botdata.makeXMPPPresence("", "", pec.Show, pec.Status) - + case XMPPMsgEvent: if pec.RememberAsStatus { last_status_msg = &pec.Msg @@ -217,11 +221,20 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc break } - case je := <-jabber_events: + case je, je_still_open := <-jabber_events: + if ! je_still_open { break } + Debug_.Printf("handleEventsforXMPP<-jabber_events: %T %+v", je, je) simple_jid := removeJIDResource(je.JID) jid_data, jid_in_map := botdata.realraum_jids_[simple_jid] + + //send status if requested, even if user never changed any settings and thus is not in map + if last_status_msg != nil && je.StatusNow { + xmppout <- botdata.makeXMPPMessage(je.JID, last_status_msg, nil) + } + if jid_in_map { - if last_status_msg != nil && (je.StatusNow || (! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo) ) { + //if R3OnlineOnlyWithRecapInfo, we want a status update when coming online + if last_status_msg != nil && ! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo { xmppout <- botdata.makeXMPPMessage(je.JID, last_status_msg, nil) } jid_data.Online = je.Online @@ -247,7 +260,6 @@ func removeJIDResource(jid string) string { func (botdata *XmppBot) isAuthenticated(jid string) bool { authtime, in_map := botdata.jid_lastauthtime_[jid] - //~ log.Println("isAuthenticated", in_map, authtime, time.Now().Unix(), auth_timeout_, time.Now().Unix() - authtime > auth_timeout_) return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_ } @@ -261,7 +273,6 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout return } bodytext :=inmsg.Body.Chardata - //~ log.Println("Message Body:", bodytext) if botdata.isAuthenticated(inmsg.GetHeader().From) { switch bodytext { case "on", "*on*": @@ -314,7 +325,7 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm defer func() { if x := recover(); x != nil { - log.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + Syslog_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) close(jabber_events) } }() @@ -331,7 +342,7 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm switch stanza.GetHeader().Type { case "subscribe": xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed", "", "") - jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false} + jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false} xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribe", "", "") case "unsubscribe", "unsubscribed": jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NeverInfo, false} @@ -365,8 +376,6 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json") - //~ log.Println(botdata.config_file_) - //~ logger := &StdLogger{} //~ xmpp.Debug = logger //~ xmpp.Info = logger @@ -379,18 +388,19 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure client_jid.Set(botdata.my_jid_) botdata.xmppclient_, err = xmpp.NewClient(client_jid, botdata.my_login_password_, nil) if err != nil { - log.Println("Error connecting to xmpp server", err) + Syslog_.Println("Error connecting to xmpp server", err) return nil, nil, err } err = botdata.xmppclient_.StartSession(true, &xmpp.Presence{}) if err != nil { - log.Println("'Error StartSession:", err) + Syslog_.Println("'Error StartSession:", err) return nil, nil, err } roster := xmpp.Roster(botdata.xmppclient_) for _, entry := range roster { + Debug_.Print(entry) if entry.Subscription == "from" { botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe", "","") }