X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=r3-netstatus%2Fr3xmppbot%2Fr3xmppbot.go;h=48a9f5c16b552c353f09b05c52a9529cf3e11104;hb=4c36e4d9f491d1f75295e7c8a3c31e5aaa59acc8;hp=465ee951c7b5919bd4592e40a69d251294e6e3dd;hpb=b78efe951c3f3bbf89f366f6e8e305d3a835f1e8;p=svn42.git diff --git a/r3-netstatus/r3xmppbot/r3xmppbot.go b/r3-netstatus/r3xmppbot/r3xmppbot.go index 465ee95..48a9f5c 100644 --- a/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -2,7 +2,6 @@ package r3xmppbot import ( xmpp "code.google.com/p/goexmpp" - "fmt" "log" "crypto/tls" "os" @@ -22,7 +21,8 @@ import ( //~ log.Printf(fmt, v...) //~ } -func (botdata *XmppBot) makeXMPPMessage(to, message string, subject interface{}) *xmpp.Message { + +func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message { xmppmsgheader := xmpp.Header{To: to, From: botdata.my_jid_, Id: <-xmpp.Id, @@ -31,14 +31,32 @@ func (botdata *XmppBot) makeXMPPMessage(to, message string, subject interface{}) Innerxml: "", Error: nil, Nested: make([]interface{},0)} - msgsubject := xmpp.Generic{} - if subject != nil { - msgsubject.Chardata = subject.(string) + + var msgsubject, msgbody *xmpp.Generic + switch cast_msg := message.(type) { + case string: + msgbody = &xmpp.Generic{Chardata: cast_msg} + case *string: + msgbody = &xmpp.Generic{Chardata: *cast_msg} + case *xmpp.Generic: + msgbody = cast_msg + default: + msgbody = &xmpp.Generic{} + } + switch cast_msg := subject.(type) { + case string: + msgsubject = &xmpp.Generic{Chardata: cast_msg} + case *string: + msgsubject = &xmpp.Generic{Chardata: *cast_msg} + case *xmpp.Generic: + msgsubject = cast_msg + default: + msgsubject = &xmpp.Generic{} } - return &xmpp.Message{Header: xmppmsgheader , Subject: &msgsubject, Body: &xmpp.Generic{Chardata:message}, Thread: &xmpp.Generic{}} + return &xmpp.Message{Header: xmppmsgheader , Subject: msgsubject, Body: msgbody, Thread: &xmpp.Generic{}} } -func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence { +func (botdata *XmppBot) makeXMPPPresence(to, ptype, show, status string) *xmpp.Presence { xmppmsgheader := xmpp.Header{To: to, From: botdata.my_jid_, Id: <-xmpp.Id, @@ -47,19 +65,39 @@ func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence { Innerxml: "", Error: nil, Nested: make([]interface{},0)} - return &xmpp.Presence{Header: xmppmsgheader} + var gen_show, gen_status *xmpp.Generic + if len(show) == 0 { + gen_show = nil + } else { + gen_show = &xmpp.Generic{Chardata: show} + } + if len(status) == 0 { + gen_status = nil + } else { + gen_status = &xmpp.Generic{Chardata: status} + } + return &xmpp.Presence{Header: xmppmsgheader, Show: gen_show, Status: gen_status} } type R3JIDDesire int const ( R3NoChange R3JIDDesire = -1 - R3NoInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier - R3NoOfflineInfo - R3AllInfo + R3NeverInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier + R3OnlineOnlyInfo + R3OnlineOnlyWithRecapInfo + R3AlwaysInfo R3DebugInfo ) +const ( + ShowOnline string = "" + ShowAway string = "away" + ShowNotAvailabe string = "xa" + ShowDoNotDisturb string = "dnd" + ShowFreeForChat string = "chat" +) + type JidData struct { Online bool Wants R3JIDDesire @@ -69,12 +107,18 @@ type JabberEvent struct { JID string Online bool Wants R3JIDDesire + StatusNow bool } -type PresenceEvent struct { - Present bool - DoorLock bool - DoorAjar bool +type XMPPMsgEvent struct { + Msg string + DistributeLevel R3JIDDesire + RememberAsStatus bool +} + +type XMPPStatusEvent struct { + Show string + Status string } type RealraumXmppNotifierConfig map[string]JidData @@ -84,12 +128,13 @@ type XmppBot struct { realraum_jids_ RealraumXmppNotifierConfig password_ string auth_cmd_ string + auth_cmd2_ string my_jid_ string auth_timeout_ int64 config_file_ string my_login_password_ string xmppclient_ *xmpp.Client - presence_events_ *chan interface{} + presence_events_ *chan interface{} } @@ -134,12 +179,15 @@ func init() { } func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) { - var msg, presence_str, lock_str, ajar_str string - last_presence := false - var debug_msg bool - + var last_status_msg *string + + defer func() { + if x := recover(); x != nil { + log.Printf("handleEventsforXMPP: run time panic: %v", x) + } + }() + for { - debug_msg = false select { case pe := <-presence_events: switch pec := pe.(type) { @@ -147,59 +195,47 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc xmppout <- pec continue case string: - msg = pec - case PresenceEvent: - if pec.Present { - presence_str = "Somebody is present !" - } else { - presence_str = "Everybody left." - } - if pec.DoorLock { - lock_str = "locked" - } else { - lock_str = "unlocked" + for to, jiddata := range botdata.realraum_jids_ { + if jiddata.Wants >= R3DebugInfo { + xmppout <- botdata.makeXMPPMessage(to, pec, nil) + } } - if pec.DoorAjar { - ajar_str = "ajar" - } else { - ajar_str = "shut" + + case XMPPStatusEvent: + xmppout <- botdata.makeXMPPPresence("", "", pec.Show, pec.Status) + + case XMPPMsgEvent: + if pec.RememberAsStatus { + last_status_msg = &pec.Msg } - msg = fmt.Sprintf("%s (Door is %s and %s)", presence_str, lock_str, ajar_str) - if last_presence == pec.Present { - debug_msg = true - } else { - last_presence = pec.Present + for to, jiddata := range botdata.realraum_jids_ { + if jiddata.Wants >= pec.DistributeLevel && ((jiddata.Wants >= R3OnlineOnlyInfo && jiddata.Online) || jiddata.Wants >= R3AlwaysInfo) { + xmppout <- botdata.makeXMPPMessage(to, pec.Msg, nil) + } } default: break } - - for to, jiddata := range botdata.realraum_jids_ { - if debug_msg && jiddata.Wants < R3DebugInfo { - continue - } - if (jiddata.Wants == R3NoOfflineInfo && jiddata.Online) || jiddata.Wants > R3NoOfflineInfo { - xmppout <- botdata.makeXMPPMessage(to, msg, nil) - } //else { - //~ fmt.Println("Not sending to ", to, jiddata) - //~ } - } - + case je := <-jabber_events: - jid_data, jid_in_map := botdata.realraum_jids_[je.JID] + simple_jid := removeJIDResource(je.JID) + jid_data, jid_in_map := botdata.realraum_jids_[simple_jid] if jid_in_map { + if last_status_msg != nil && (je.StatusNow || (! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo) ) { + xmppout <- botdata.makeXMPPMessage(je.JID, last_status_msg, nil) + } jid_data.Online = je.Online if je.Wants > R3NoChange { jid_data.Wants = je.Wants } - botdata.realraum_jids_[je.JID] = jid_data + botdata.realraum_jids_[simple_jid] = jid_data botdata.realraum_jids_.saveTo(botdata.config_file_) } else if je.Wants > R3NoChange { - botdata.realraum_jids_[je.JID] = JidData{je.Online, je.Wants} + botdata.realraum_jids_[simple_jid] = JidData{je.Online, je.Wants} botdata.realraum_jids_.saveTo(botdata.config_file_) } } - } + } } func removeJIDResource(jid string) string { @@ -215,7 +251,8 @@ func (botdata *XmppBot) isAuthenticated(jid string) bool { return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_ } -const help_text_ string = "\nauth \n .... enables you to use the other commands\non\n .... you will be notified of r3 status changes\noff\n .... you will no longer recieve notifications\non_while_offline\n .... you will be notified of r3 status changes even if you are offline" +const help_text_ string = "\n*auth** ...Enables you to use more commands.\n*time* ...Returns bot time." +const help_text_auth string = "You are authorized to use the following commands:\n*off* ...You will no longer receive notifications.\n*on* ...You will be notified of r3 status changes while you are online.\n*on_with_recap* ...Like *on* but additionally you will receive the current status when you come online.\n*on_while_offline* ...You will receive all r3 status changes, wether you are online or offline.\n*status* ...Use it to query the current status.\n*time* ...Returns bot time.\n*bye* ...Logout." //~ var re_msg_auth_ *regexp.Regexp = regexp.MustCompile("auth\s+(\S+)") @@ -227,32 +264,45 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout //~ log.Println("Message Body:", bodytext) if botdata.isAuthenticated(inmsg.GetHeader().From) { switch bodytext { - case "on": - jabber_events <- JabberEvent{removeJIDResource(inmsg.GetHeader().From), true, R3NoOfflineInfo} - xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 information while online" , "Your New Status") - case "off": - jabber_events <- JabberEvent{removeJIDResource(inmsg.GetHeader().From), true, R3NoInfo} - xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Do not receive r3 information" , "Your New Status") - case "on_while_offline": - jabber_events <- JabberEvent{removeJIDResource(inmsg.GetHeader().From), true, R3AllInfo} - xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 information even while offline" , "Your New Status") + case "on", "*on*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3OnlineOnlyInfo, false} + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 status updates while online." , "Your New Status") + case "off", "*off*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NeverInfo, false} + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Do not receive anything." , "Your New Status") + case "on_with_recap", "*on_with_recap*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3OnlineOnlyWithRecapInfo, false} + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 status updates while and current status on coming, online." , "Your New Status") + case "on_while_offline", "*on_while_offline*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3AlwaysInfo, false} + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive all r3 status updates, even if you are offline." , "Your New Status") case "debug": - jabber_events <- JabberEvent{removeJIDResource(inmsg.GetHeader().From), true, R3DebugInfo} + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3DebugInfo, false} xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Debug mode enabled" , "Your New Status") - case "bye", "Bye", "quit", "logout": + case "bye", "Bye", "quit", "logout", "*bye*": botdata.jid_lastauthtime_[inmsg.GetHeader().From] = 0 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Bye Bye !" ,nil) + case "open","close": + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, I can't operate the door for you." ,nil) + case "status", "*status*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoChange, true} + case "time", "*time*": + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil) default: //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata) - xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands") + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil) } } else { switch bodytext { case "Hilfe","hilfe","help","Help","?","hallo","Hallo","Yes","yes","ja","ja bitte","bitte","sowieso": xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands") - case botdata.auth_cmd_: + case botdata.auth_cmd_, botdata.auth_cmd2_: botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix() - xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "You are now authorized to use commands" , "Authorized") + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil) + case "status", "*status*", "off", "*off*", "on", "*on*", "on_while_offline", "*on_while_offline*", "on_with_recap", "*on_with_recap*": + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that." , nil) + case "time", "*time*": + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil) default: //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata) xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "A nice day to you too !\nDo you need \"help\" ?", nil) @@ -261,6 +311,14 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout } func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xmppout chan<- xmpp.Stanza, jabber_events chan JabberEvent) { + + defer func() { + if x := recover(); x != nil { + log.Printf("handleIncomingXMPPStanzas: run time panic: %v", x) + close(jabber_events) + } + }() + var incoming_stanza interface{} for incoming_stanza = range xmppin { switch stanza := incoming_stanza.(type) { @@ -270,10 +328,21 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm if stanza.GetHeader() == nil { continue } - if stanza.GetHeader().Type == "subscribe" { - xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed") + switch stanza.GetHeader().Type { + case "subscribe": + xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed", "", "") + 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} + botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout + xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "unsubscribe", "","") + case "unavailable": + jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NoChange, false} + botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout + default: + jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false} } - jabber_events <- JabberEvent{stanza.GetHeader().From, stanza.GetHeader().Type != "unavailable", R3NoChange} case *xmpp.Iq: if stanza.GetHeader() == nil { continue @@ -282,28 +351,27 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm } } -var default_state_save_dir_ string = "/flash/var/lib/r3netstatus/" - -func NewStartedBot(loginjid, loginpwd, password string, insecuretls bool) (*XmppBot, chan interface{}, error) { +func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecuretls bool) (*XmppBot, chan interface{}, error) { var err error botdata := new(XmppBot) botdata.realraum_jids_ = make(map[string]JidData, 1) botdata.jid_lastauthtime_ = make(map[string]int64,1) botdata.auth_cmd_ = "auth " + password + botdata.auth_cmd2_ = "*auth*" + password+"*" botdata.my_jid_ = loginjid botdata.my_login_password_ = loginpwd - botdata.auth_timeout_ = 1200 - - botdata.config_file_ = path.Join(default_state_save_dir_, "r3xmpp."+removeJIDResource(loginjid)+".json") - + botdata.auth_timeout_ = 3600*2 + + 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 //~ xmpp.Warn = logger - + xmpp.TlsConfig = tls.Config{InsecureSkipVerify: insecuretls} botdata.realraum_jids_.loadFrom(botdata.config_file_) @@ -315,12 +383,22 @@ func NewStartedBot(loginjid, loginpwd, password string, insecuretls bool) (*Xmpp return nil, nil, err } - err = botdata.xmppclient_.StartSession(false, &xmpp.Presence{}) + err = botdata.xmppclient_.StartSession(true, &xmpp.Presence{}) if err != nil { log.Println("'Error StartSession:", err) return nil, nil, err } + roster := xmpp.Roster(botdata.xmppclient_) + for _, entry := range roster { + if entry.Subscription == "from" { + botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe", "","") + } + if entry.Subscription == "none" { + delete(botdata.realraum_jids_, entry.Jid) + } + } + presence_events := make(chan interface{},1) jabber_events := make(chan JabberEvent,1) @@ -341,27 +419,3 @@ func (botdata *XmppBot) StopBot() { close(*botdata.presence_events_) } } - -//~ func main() { - //~ bot, presence_events, err := NewStartedBot("realrauminfo@realraum.at/Tuer", "d7ynC6Dg", "r3alraumOLGAXMPPInfos", true) - - //~ if err != nil { - //~ log.Println(err) - //~ os.Exit(1) - //~ } - - //~ presence_events <- PresenceEvent{true, true, true} - //~ presence_events <- PresenceEvent{true, true, false} - //~ presence_events <- PresenceEvent{true, false, false} - - //~ p := make([]byte, 1024) - //~ for - //~ { - //~ nr, _ := os.Stdin.Read(p) - //~ if nr == 0 { - //~ break - //~ } - //~ } - - //~ bot.StopBot() -//~ }