//~ 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,
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 {
JID string
Online bool
Wants R3JIDDesire
+ StatusNow bool
}
type XMPPPresenceEvent struct {
realraum_jids_ RealraumXmppNotifierConfig
password_ string
auth_cmd_ string
+ auth_cmd2_ string
my_jid_ string
auth_timeout_ int64
config_file_ string
//~ 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
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
}
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_)
}
}
return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_
}
-const help_text_ string = "\nauth <password>\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*<password>* ...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+)")
//~ 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")
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)
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
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")