//~ log.Printf(fmt, v...)
//~ }
+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_,
R3NoChange R3JIDDesire = -1
R3NoInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier
R3NoOfflineInfo
- R3AllInfo
+ R3AllInfo
R3DebugInfo
)
DoorLock bool
DoorShut bool
Button bool
+ Ts int64
}
type RealraumXmppNotifierConfig map[string]JidData
config_file_ string
my_login_password_ string
xmppclient_ *xmpp.Client
- presence_events_ *chan interface{}
+ presence_events_ *chan interface{}
}
}
func composeMessage(pec *XMPPPresenceEvent, both bool) *string {
- var msg, presence_str, lock_str, ajar_str 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 { return &msg; }
+ if both { msg += "\n"; } else {
+ msg += " --- " + time.Unix(pec.Ts,0).String()
+ 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)
+ 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
}
var debug_msg bool
var last_presence_event *XMPPPresenceEvent
var msg *string
-
+
for {
debug_msg = false
select {
default:
break
}
-
+
if msg == nil { continue }
for to, jiddata := range botdata.realraum_jids_ {
xmppout <- botdata.makeXMPPMessage(to, msg, nil)
}
}
-
+
case je := <-jabber_events:
simple_jid := removeJIDResource(je.JID)
jid_data, jid_in_map := botdata.realraum_jids_[simple_jid]
botdata.realraum_jids_.saveTo(botdata.config_file_)
}
}
- }
+ }
}
func removeJIDResource(jid string) string {
return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_
}
-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"
+const help_text_ string = "\n*auth*<password>* ...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+)")
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 {
xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands")
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, 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)
botdata.my_jid_ = loginjid
botdata.my_login_password_ = loginpwd
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_)