4 xmpp "code.google.com/p/goexmpp"
13 //~ type StdLogger struct {
16 //~ func (s *StdLogger) Log(v ...interface{}) {
20 //~ func (s *StdLogger) Logf(fmt string, v ...interface{}) {
21 //~ log.Printf(fmt, v...)
25 func (botdata *XmppBot) makeXMPPMessage(to string, message interface{}, subject interface{}) *xmpp.Message {
26 xmppmsgheader := xmpp.Header{To: to,
27 From: botdata.my_jid_,
33 Nested: make([]interface{},0)}
35 var msgsubject, msgbody *xmpp.Generic
36 switch cast_msg := message.(type) {
38 msgbody = &xmpp.Generic{Chardata: cast_msg}
40 msgbody = &xmpp.Generic{Chardata: *cast_msg}
44 msgbody = &xmpp.Generic{}
46 switch cast_msg := subject.(type) {
48 msgsubject = &xmpp.Generic{Chardata: cast_msg}
50 msgsubject = &xmpp.Generic{Chardata: *cast_msg}
54 msgsubject = &xmpp.Generic{}
56 return &xmpp.Message{Header: xmppmsgheader , Subject: msgsubject, Body: msgbody, Thread: &xmpp.Generic{}}
59 func (botdata *XmppBot) makeXMPPPresence(to, ptype string) *xmpp.Presence {
60 xmppmsgheader := xmpp.Header{To: to,
61 From: botdata.my_jid_,
67 Nested: make([]interface{},0)}
68 return &xmpp.Presence{Header: xmppmsgheader}
74 R3NoChange R3JIDDesire = -1
75 R3NeverInfo R3JIDDesire = iota // ignore first value by assigning to blank identifier
77 R3OnlineOnlyWithRecapInfo
87 type JabberEvent struct {
94 type XMPPMsgEvent struct {
96 DistributeLevel R3JIDDesire
100 type RealraumXmppNotifierConfig map[string]JidData
102 type XmppBot struct {
103 jid_lastauthtime_ map[string]int64
104 realraum_jids_ RealraumXmppNotifierConfig
111 my_login_password_ string
112 xmppclient_ *xmpp.Client
113 presence_events_ *chan interface{}
117 func (data RealraumXmppNotifierConfig) saveTo(filepath string) () {
118 fh, err := os.Create(filepath)
124 enc := json.NewEncoder(fh)
125 if err = enc.Encode(&data); err != nil {
131 func (data RealraumXmppNotifierConfig) loadFrom(filepath string) () {
132 fh, err := os.Open(filepath)
138 dec := json.NewDecoder(fh)
139 if err = dec.Decode(&data); err != nil {
143 for to, jiddata := range data {
144 jiddata.Online = false
151 //~ logger := &StdLogger{}
152 //~ xmpp.Debug = logger
153 //~ xmpp.Info = logger
154 //~ xmpp.Warn = logger
157 func (botdata *XmppBot) handleEventsforXMPP(xmppout chan <- xmpp.Stanza, presence_events <- chan interface{}, jabber_events <- chan JabberEvent) {
158 var last_status_msg *string
162 case pe := <-presence_events:
163 switch pec := pe.(type) {
168 for to, jiddata := range botdata.realraum_jids_ {
169 if jiddata.Wants >= R3DebugInfo {
170 xmppout <- botdata.makeXMPPMessage(to, pec, nil)
175 if pec.RememberAsStatus {
176 last_status_msg = &pec.Msg
178 for to, jiddata := range botdata.realraum_jids_ {
179 if jiddata.Wants >= pec.DistributeLevel && ((jiddata.Wants >= R3OnlineOnlyInfo && jiddata.Online) || jiddata.Wants >= R3AlwaysInfo) {
180 xmppout <- botdata.makeXMPPMessage(to, pec.Msg, nil)
187 case je := <-jabber_events:
188 simple_jid := removeJIDResource(je.JID)
189 jid_data, jid_in_map := botdata.realraum_jids_[simple_jid]
191 if last_status_msg != nil && (je.StatusNow || (! jid_data.Online && je.Online && jid_data.Wants == R3OnlineOnlyWithRecapInfo) ) {
192 xmppout <- botdata.makeXMPPMessage(je.JID, last_status_msg, nil)
194 jid_data.Online = je.Online
195 if je.Wants > R3NoChange {
196 jid_data.Wants = je.Wants
198 botdata.realraum_jids_[simple_jid] = jid_data
199 botdata.realraum_jids_.saveTo(botdata.config_file_)
200 } else if je.Wants > R3NoChange {
201 botdata.realraum_jids_[simple_jid] = JidData{je.Online, je.Wants}
202 botdata.realraum_jids_.saveTo(botdata.config_file_)
208 func removeJIDResource(jid string) string {
212 return jidjid.String()
215 func (botdata *XmppBot) isAuthenticated(jid string) bool {
216 authtime, in_map := botdata.jid_lastauthtime_[jid]
217 //~ log.Println("isAuthenticated", in_map, authtime, time.Now().Unix(), auth_timeout_, time.Now().Unix() - authtime > auth_timeout_)
218 return in_map && time.Now().Unix() - authtime < botdata.auth_timeout_
221 const help_text_ string = "\n*auth*<password>* ...Enables you to use more commands.\n*time* ...Returns bot time."
222 const help_text_auth string = "You are authorized to use the following commands:\n*off* ...You will no longer receive notifications.\n*on* ...You will be notified of r3 status changes.\n*on_with_recap* ...Like *on* but additionally you will receive the current r3 status when you come online.\n*on_while_offline* ...You will receive all r3 status changes, wether your are online or offline.\n*status* ...Use it to query the current status.\n*time* ...Returns bot time.\n*bye* ...Logout."
224 //~ var re_msg_auth_ *regexp.Regexp = regexp.MustCompile("auth\s+(\S+)")
226 func (botdata *XmppBot) handleIncomingMessageDialog(inmsg xmpp.Message, xmppout chan<- xmpp.Stanza, jabber_events chan JabberEvent) {
227 if inmsg.Body == nil || inmsg.GetHeader() == nil {
230 bodytext :=inmsg.Body.Chardata
231 //~ log.Println("Message Body:", bodytext)
232 if botdata.isAuthenticated(inmsg.GetHeader().From) {
235 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3OnlineOnlyInfo, false}
236 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 status updates while online." , "Your New Status")
238 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NeverInfo, false}
239 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Do not receive anything." , "Your New Status")
240 case "on_with_recap", "*on_with_recap*":
241 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3OnlineOnlyWithRecapInfo, false}
242 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive r3 status updates while and current status on coming, online." , "Your New Status")
243 case "on_while_offline", "*on_while_offline*":
244 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3AlwaysInfo, false}
245 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Receive all r3 status updates, even if you are offline." , "Your New Status")
247 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3DebugInfo, false}
248 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Debug mode enabled" , "Your New Status")
249 case "bye", "Bye", "quit", "logout", "*bye*":
250 botdata.jid_lastauthtime_[inmsg.GetHeader().From] = 0
251 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Bye Bye !" ,nil)
253 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, I can't operate the door for you." ,nil)
254 case "status", "*status*":
255 jabber_events <- JabberEvent{inmsg.GetHeader().From, true, R3NoChange, true}
256 case "time", "*time*":
257 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil)
259 //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata)
260 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
264 case "Hilfe","hilfe","help","Help","?","hallo","Hallo","Yes","yes","ja","ja bitte","bitte","sowieso":
265 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_, "Available Commands")
266 case botdata.auth_cmd_, botdata.auth_cmd2_:
267 botdata.jid_lastauthtime_[inmsg.GetHeader().From] = time.Now().Unix()
268 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, help_text_auth, nil)
269 case "status", "*status*", "off", "*off*", "on", "*on*", "on_while_offline", "*on_while_offline*", "on_with_recap", "*on_with_recap*":
270 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "Sorry, you need to be authorized to do that." , nil)
271 case "time", "*time*":
272 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, time.Now().String() , nil)
274 //~ auth_match = re_msg_auth_.FindStringSubmatch(inmsg.Body.Chardata)
275 xmppout <- botdata.makeXMPPMessage(inmsg.GetHeader().From, "A nice day to you too !\nDo you need \"help\" ?", nil)
280 func (botdata *XmppBot) handleIncomingXMPPStanzas(xmppin <- chan xmpp.Stanza, xmppout chan<- xmpp.Stanza, jabber_events chan JabberEvent) {
281 var incoming_stanza interface{}
282 for incoming_stanza = range xmppin {
283 switch stanza := incoming_stanza.(type) {
285 botdata.handleIncomingMessageDialog(*stanza, xmppout, jabber_events)
287 if stanza.GetHeader() == nil {
290 switch stanza.GetHeader().Type {
292 xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribed")
293 jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
294 xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "subscribe")
295 case "unsubscribe", "unsubscribed":
296 jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NeverInfo, false}
297 botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout
298 xmppout <- botdata.makeXMPPPresence(stanza.GetHeader().From, "unsubscribe")
300 jabber_events <- JabberEvent{stanza.GetHeader().From, false, R3NoChange, false}
301 botdata.jid_lastauthtime_[stanza.GetHeader().From] = 0 //logout
303 jabber_events <- JabberEvent{stanza.GetHeader().From, true, R3NoChange, false}
306 if stanza.GetHeader() == nil {
313 func NewStartedBot(loginjid, loginpwd, password, state_save_dir string, insecuretls bool) (*XmppBot, chan interface{}, error) {
315 botdata := new(XmppBot)
317 botdata.realraum_jids_ = make(map[string]JidData, 1)
318 botdata.jid_lastauthtime_ = make(map[string]int64,1)
319 botdata.auth_cmd_ = "auth " + password
320 botdata.auth_cmd2_ = "*auth*" + password+"*"
321 botdata.my_jid_ = loginjid
322 botdata.my_login_password_ = loginpwd
323 botdata.auth_timeout_ = 3600*2
325 botdata.config_file_ = path.Join(state_save_dir, "r3xmpp."+removeJIDResource(loginjid)+".json")
327 //~ log.Println(botdata.config_file_)
329 //~ logger := &StdLogger{}
330 //~ xmpp.Debug = logger
331 //~ xmpp.Info = logger
332 //~ xmpp.Warn = logger
334 xmpp.TlsConfig = tls.Config{InsecureSkipVerify: insecuretls}
335 botdata.realraum_jids_.loadFrom(botdata.config_file_)
337 client_jid := new(xmpp.JID)
338 client_jid.Set(botdata.my_jid_)
339 botdata.xmppclient_, err = xmpp.NewClient(client_jid, botdata.my_login_password_, nil)
341 log.Println("Error connecting to xmpp server", err)
345 err = botdata.xmppclient_.StartSession(true, &xmpp.Presence{})
347 log.Println("'Error StartSession:", err)
351 roster := xmpp.Roster(botdata.xmppclient_)
352 for _, entry := range roster {
353 if entry.Subscription == "from" {
354 botdata.xmppclient_.Out <- botdata.makeXMPPPresence(entry.Jid, "subscribe")
356 if entry.Subscription == "none" {
357 delete(botdata.realraum_jids_, entry.Jid)
361 presence_events := make(chan interface{},1)
362 jabber_events := make(chan JabberEvent,1)
364 go botdata.handleEventsforXMPP(botdata.xmppclient_.Out, presence_events, jabber_events)
365 go botdata.handleIncomingXMPPStanzas(botdata.xmppclient_.In, botdata.xmppclient_.Out, jabber_events)
367 botdata.presence_events_ = &presence_events
369 return botdata, presence_events, nil
372 func (botdata *XmppBot) StopBot() {
373 if botdata.xmppclient_ != nil {
374 close(botdata.xmppclient_.Out)
376 if botdata.presence_events_ != nil {
377 *botdata.presence_events_ <- false
378 close(*botdata.presence_events_)