- if inmsg.Body == nil || inmsg.GetHeader() == nil {
- return
- }
- if inmsg.GetHeader().Error != nil {
- Syslog_.Printf("XMPP Message Error: %s", inmsg.GetHeader().Error.Error())
- }
- bodytext :=inmsg.Body.Chardata
- if botdata.isAuthenticated(inmsg.GetHeader().From) {
- switch bodytext {
- 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{inmsg.GetHeader().From, true, R3DebugInfo, false}
- xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Debug mode enabled" , "Your New Status")
- 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_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_, botdata.auth_cmd2_:
- botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix()
- 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)
- }
- }
+ if inmsg.Body == nil || inmsg.GetHeader() == nil {
+ return
+ }
+ bodytext_args := strings.Split(strings.Replace(inmsg.Body.Chardata, "*", " ", -1), " ")
+ for len(bodytext_args) > 1 && len(bodytext_args[0]) == 0 {
+ bodytext_args = bodytext_args[1:len(bodytext_args)] //get rid of empty first strings resulting from " text"
+ }
+ bodytext_lc_cmd := strings.ToLower(bodytext_args[0])
+ if botdata.isAuthenticated(inmsg.GetHeader().From) {
+ switch bodytext_lc_cmd {
+ case "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":
+ 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":
+ 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":
+ 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{inmsg.GetHeader().From, true, R3DebugInfo, false}
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Debug mode enabled", "Your New Status")
+ case "bye", "quit", "logout":
+ 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'm just weak software, not strong enough to operate the door for you.", nil)
+ case "status":
+ jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoChange, true}
+ case "time":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String(), nil)
+ case "ping":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Pong with auth", nil)
+ default:
+ //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata)
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
+ }
+ } else {
+ switch bodytext_lc_cmd {
+ case "hilfe", "help", "?", "hallo", "yes", "ja", "ja bitte", "bitte", "sowieso":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands")
+ case "auth":
+ authindex := 1
+ for len(bodytext_args) > authindex && len(bodytext_args[authindex]) == 0 {
+ authindex++
+ }
+ if len(bodytext_args) > authindex && bodytext_args[authindex] == botdata.password_ {
+ botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix()
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
+ }
+ case "status", "off", "on", "on_while_offline", "on_with_recap":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that.", nil)
+ case "time":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String(), nil)
+ case "ping":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Pong", nil)
+ case "":
+ xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "You're a quiet one, aren't you?", 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)
+ }
+ }
+}
+
+func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <-chan xmpp.Stanza, xmppout chan<- xmpp.Stanza, jabber_events chan JabberEvent) {
+
+ defer func() {
+ if x := recover(); x != nil {
+ Syslog_.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
+ }
+ }()
+
+ var error_count int = 0
+ var incoming_stanza interface{}
+
+ handleStanzaError := func() bool {
+ error_count++
+ if error_count > 15 {
+ Syslog_.Println("handleIncomingXMPPStanzas: too many errors in series.. bailing out")
+ botdata.StopBot()
+ return true
+ }
+ return false
+ }
+
+ for incoming_stanza = range xmppin {
+ switch stanza := incoming_stanza.(type) {
+ case *xmpp.Message:
+ if stanza.GetHeader() == nil {
+ continue
+ }
+ if stanza.Type == "error" || stanza.Error != nil {
+ Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+ if stanza.Error.Type == "cancel" {
+ // asume receipient not reachable -> disable
+ Syslog_.Printf("Error reaching %s. Disabling user, please reenable manually", stanza.From)
+ jabber_events <- JabberEvent{stanza.From, false, R3NeverInfo, false}
+ continue
+ }
+ if handleStanzaError() {
+ return
+ }
+ continue
+ } else {
+ error_count = 0
+ }
+ botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
+ case *xmpp.Presence:
+ if stanza.GetHeader() == nil {
+ continue
+ }
+ if stanza.Type == "error" || stanza.Error != nil {
+ Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+ if handleStanzaError() {
+ return
+ }
+ continue
+ } else {
+ error_count = 0
+ }
+ 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}
+ }
+
+ case *xmpp.Iq:
+ if stanza.GetHeader() == nil {
+ continue
+ }
+ if stanza.Type == "error" || stanza.Error != nil {
+ Syslog_.Printf("XMPP %T Error: %s", stanza, stanza)
+ if handleStanzaError() {
+ return
+ }
+ continue
+ } else {
+ error_count = 0
+ }
+
+ if HandleServerToClientPing(stanza, xmppout) {
+ continue
+ } //if true then routine handled it and we can continue
+ Debug_.Printf("Unhandled Iq: %s", stanza)
+ }
+ }