X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;ds=sidebyside;f=r3-netstatus%2Fr3xmppbot%2Fr3xmppbot.go;h=1a2d9f024f7f010795fa197edfafe52444505498;hb=098333f9a1d3fa389378a212dea39cf48030cd91;hp=b73445b7a80a798321b7c7a2d3acb8e19c849709;hpb=3532254fdc34a8649a2894e8e746a4bf61947bde;p=svn42.git diff --git a/r3-netstatus/r3xmppbot/r3xmppbot.go b/r3-netstatus/r3xmppbot/r3xmppbot.go index b73445b..1a2d9f0 100644 --- a/r3-netstatus/r3xmppbot/r3xmppbot.go +++ b/r3-netstatus/r3xmppbot/r3xmppbot.go @@ -22,7 +22,11 @@ import ( //~ log.Printf(fmt, v...) //~ } -func (botdata *XmppBot) makeXMPPMessage(to, message string, subject interface{}) *xmpp.Message { +func IfThenElseStr(c bool, strue, sfalse string) string { + if c {return strue} else {return sfalse} +} + +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 +35,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{} } - return &xmpp.Message{Header: xmppmsgheader , Subject: &msgsubject, Body: &xmpp.Generic{Chardata:message}, Thread: &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: msgbody, Thread: &xmpp.Generic{}} } func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence { @@ -56,7 +78,7 @@ const ( R3NoChange R3JIDDesire = -1 R3NoInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier R3NoOfflineInfo - R3AllInfo + R3AllInfo R3DebugInfo ) @@ -69,6 +91,7 @@ type JabberEvent struct { JID string Online bool Wants R3JIDDesire + StatusNow bool } type XMPPPresenceEvent struct { @@ -77,6 +100,7 @@ type XMPPPresenceEvent struct { DoorLock bool DoorShut bool Button bool + Ts int64 } type RealraumXmppNotifierConfig map[string]JidData @@ -86,12 +110,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{} } @@ -135,10 +160,30 @@ func init() { //~ xmpp.Warn = logger } +func composeMessage(pec *XMPPPresenceEvent, both bool) *string { + var msg 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 { + msg += " --- " + time.Unix(pec.Ts,0).String() + return &msg; + } + } + msg += fmt.Sprintf("%s (Door is %s and %s and was last used by %s at %s)", + IfThenElseStr(pec.Present, "Somebody is present!" , "Everybody left."), + IfThenElseStr(pec.DoorLock, "locked","unlocked"), + IfThenElseStr(pec.DoorShut, "shut","ajar"), + pec.Who, + time.Unix(pec.Ts,0).String()) + return &msg +} + 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 var debug_msg bool - + var last_presence_event *XMPPPresenceEvent + var msg *string + for { debug_msg = false select { @@ -148,33 +193,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 @@ -183,22 +212,26 @@ func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presenc xmppout <- botdata.makeXMPPMessage(to, msg, nil) } } - + 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_) } } - } + } } func removeJIDResource(jid string) string { @@ -214,7 +247,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*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*time* ...Returns bot time.\n*bye* ...Logout." //~ var re_msg_auth_ *regexp.Regexp = regexp.MustCompile("auth\s+(\S+)") @@ -226,34 +260,42 @@ 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} + 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*": + 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) @@ -274,7 +316,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,19 +333,20 @@ 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") - + //~ 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_)