From: Bernhard Tittelbach Date: Sun, 25 Aug 2013 04:18:47 +0000 (+0000) Subject: status cmd, nicer help, longer auth timeout X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=63eb752b0e125d962bcd5a4d56afb2616ab2af67;p=svn42.git status cmd, nicer help, longer auth timeout --- diff --git a/r3-netstatus/main.go b/r3-netstatus/main.go index abec0c1..f7828b9 100644 --- a/r3-netstatus/main.go +++ b/r3-netstatus/main.go @@ -39,7 +39,7 @@ func init() { func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) { events := ps.Sub("presence","door","buttons") - var present, locked, shut bool + var present, locked, shut bool = false, true, true var who string = "Unknown" for eventinterface := range(events) { @@ -70,7 +70,7 @@ func main() { } defer bot.StopBot() - newlinequeue := make(chan string) + newlinequeue := make(chan string, 1) ps := pubsub.New(1) //~ brn := brain.New() defer close(newlinequeue) @@ -78,9 +78,9 @@ func main() { //~ defer brn.Shutdown() ticker := time.NewTicker(time.Duration(7) * time.Minute) - go ReadFromUSocket(presence_socket_path_, newlinequeue) go EventToWeb(ps) go EventToXMPP(ps, xmpp_presence_events_chan_) + go ReadFromUSocket(presence_socket_path_, newlinequeue) for { select { case e := <-newlinequeue: diff --git a/r3-netstatus/make_deploy.zsh b/r3-netstatus/make_deploy.zsh index 3cc441c..408b049 100644 --- a/r3-netstatus/make_deploy.zsh +++ b/r3-netstatus/make_deploy.zsh @@ -3,5 +3,4 @@ export GO386=387 go-linux-386 clean #go-linux-386 build #strip ${PWD:t} -go-linux-386 build -ldflags "-s" -rsync -v ${PWD:t} wuzzler.realraum.at:/flash/tuer/ +go-linux-386 build -ldflags "-s" && rsync -v ${PWD:t} wuzzler.realraum.at:/flash/tuer/ diff --git a/r3-netstatus/r3xmppbot/r3xmppbot.go b/r3-netstatus/r3xmppbot/r3xmppbot.go index b73445b..f94410c 100644 --- a/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -22,7 +22,7 @@ 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,11 +31,29 @@ 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 { @@ -69,6 +87,7 @@ type JabberEvent struct { JID string Online bool Wants R3JIDDesire + StatusNow bool } type XMPPPresenceEvent struct { @@ -86,6 +105,7 @@ type XmppBot struct { realraum_jids_ RealraumXmppNotifierConfig password_ string auth_cmd_ string + auth_cmd2_ string my_jid_ string auth_timeout_ int64 config_file_ string @@ -135,9 +155,24 @@ func init() { //~ xmpp.Warn = logger } -func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) { +func composeMessage(pec *XMPPPresenceEvent, both bool) *string { var msg, presence_str, lock_str, ajar_str string + msg = "" + if pec.Button { + msg = "The button has been pressed ! Propably someone is bored and in need of company ! ;-)" + if both { msg += "\n"; } else { return &msg; } + } + if pec.Present { presence_str = "Somebody is present !" } else { presence_str = "Everybody left." } + if pec.DoorLock { lock_str = "locked" } else { lock_str = "unlocked" } + if pec.DoorShut { ajar_str = "shut" } else { ajar_str = "ajar" } + msg += fmt.Sprintf("%s (Door is %s and %s and was last used by %s)", presence_str, lock_str, ajar_str, pec.Who) + return &msg +} + +func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) { var debug_msg bool + var last_presence_event *XMPPPresenceEvent + var msg *string for { debug_msg = false @@ -148,33 +183,17 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc xmppout <- pec continue case string: - msg = pec + msg = &pec debug_msg = true case XMPPPresenceEvent: - if pec.Present { - presence_str = "Somebody is present !" - } else { - presence_str = "Everybody left." - } - if pec.DoorLock { - lock_str = "locked" - } else { - lock_str = "unlocked" - } - if pec.DoorShut { - ajar_str = "shut" - } else { - ajar_str = "ajar" - } - if pec.Button { - msg = "The button has been pressed ! Propably someone is bored and need company ! ;-)" - } else { - msg = fmt.Sprintf("%s (Door is %s and %s and was last used by %s)", presence_str, lock_str, ajar_str, pec.Who) - } + last_presence_event = &pec + msg = composeMessage(last_presence_event, false) default: break } + if msg == nil { continue } + for to, jiddata := range botdata.realraum_jids_ { if debug_msg && jiddata.Wants < R3DebugInfo { continue @@ -185,16 +204,20 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc } 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 { jid_data.Online = je.Online + if je.StatusNow && last_presence_event != nil { + xmppout <- botdata.makeXMPPMessage(je.JID, composeMessage(last_presence_event, true), nil) + } 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_) } } @@ -214,7 +237,7 @@ 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 the other commands\n*on* ...you will be notified of r3 status changes\n*off* ...you will no longer recieve notifications\n*on_while_offline* ...you will be notified of r3 status changes even if you are offline\n*status* ...query current status\n*bye* ...logout" //~ var re_msg_auth_ *regexp.Regexp = regexp.MustCompile("auth\s+(\S+)") @@ -226,23 +249,25 @@ 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} + case "on", "*on*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoOfflineInfo, false} 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} + case "off", "*off*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoInfo, false} 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} + case "on_while_offline", "*on_while_offline*": + jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3AllInfo, false} xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 information even while 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} default: //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata) xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands") @@ -251,9 +276,11 @@ func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout 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") + case "status", "*status*": + xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that." , 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) @@ -274,7 +301,8 @@ func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xm if stanza.GetHeader().Type == "subscribe" { xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed") } - jabber_events <- JabberEvent{stanza.GetHeader().From, stanza.GetHeader().Type != "unavailable", R3NoChange} + jabber_events <- JabberEvent{stanza.GetHeader().From, stanza.GetHeader().Type != "unavailable", R3NoChange, false} + if stanza.GetHeader().Type == "unavailable" { botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0} //logout if offline case *xmpp.Iq: if stanza.GetHeader() == nil { continue @@ -290,9 +318,10 @@ func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecure 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.auth_timeout_ = 3600*2 botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json") diff --git a/r3-netstatus/sockettoevent.go b/r3-netstatus/sockettoevent.go index 405dbbf..c49ad6f 100644 --- a/r3-netstatus/sockettoevent.go +++ b/r3-netstatus/sockettoevent.go @@ -76,6 +76,7 @@ func ParseSocketInputLine(line string, ps *pubsub.PubSub) { //, brn *brain.Brain var tidbit interface{} ts := time.Now().Unix() if match_presence != nil { + if match_presence[2] != "" { ps.Pub(DoorStatusUpdate{match_presence[2] == "closed", true, ts}, "door"); } tidbit = PresenceUpdate{match_presence[1] == "yes", ts} //~ brn.Oboite("presence", tidbit) ps.Pub(tidbit, "presence")