5 pubsub "github.com/tuxychandru/pubsub"
12 type SpaceState struct {
14 buttonpress_until int64
20 presence_socket_path_ string
21 xmpp_presence_events_chan_ chan interface{}
22 xmpp_login_ struct {jid string; pass string}
23 xmpp_bot_authstring_ string
24 xmpp_state_save_dir_ string
25 button_press_timeout_ int64 = 3600
31 flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID")
32 flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password")
33 flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot")
34 flag.StringVar(&presence_socket_path_,"presencesocket", "/var/run/tuer/presence.socket", "Path to presence socket")
35 flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/", "Directory to save XMPP bot state in")
41 func IfThenElseStr(c bool, strue, sfalse string) string {
42 if c {return strue} else {return sfalse}
45 func composeMessage(present, locked, shut bool, who string, ts int64) string {
46 return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)",
47 IfThenElseStr(present, "Somebody is present!" , "Everybody left."),
48 IfThenElseStr(locked, "locked","unlocked"),
49 IfThenElseStr(shut, "shut","ajar"),
50 IfThenElseStr(len(who) == 0,"", " by " + who),
51 time.Unix(ts,0).String())
54 func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
55 events := ps.Sub("presence","door","buttons")
58 if x := recover(); x != nil {
59 fmt.Printf("handleIncomingXMPPStanzas: run time panic: %v", x)
60 ps.Unsub(events, "presence","door","buttons")
61 close(xmpp_presence_events_chan_)
65 var present, locked, shut bool = false, true, true
66 var last_buttonpress int64 = 0
68 button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
69 button_status := r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowFreeForChat, "The button has been pressed :-)"}
71 xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe, "Nobody is here"}
73 for eventinterface := range(events) {
74 switch event := eventinterface.(type) {
76 present = event.Present
77 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
79 if time.Now().Unix() - last_buttonpress < button_press_timeout_ {
80 xmpp_presence_events_chan_ <- button_status
82 xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowOnline,"Somebody is present"}
85 xmpp_presence_events_chan_ <- r3xmppbot.XMPPStatusEvent{r3xmppbot.ShowNotAvailabe,"Nobody is here"}
87 case DoorCommandEvent:
88 if len(event.Who) > 0 && len(event.Using) > 0 {
89 who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
93 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
94 case DoorStatusUpdate:
97 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
98 case ButtonPressUpdate:
99 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
100 xmpp_presence_events_chan_ <- button_status
101 last_buttonpress = event.Ts
108 var bot *r3xmppbot.XmppBot
109 bot, xmpp_presence_events_chan_, xmpperr = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
111 newlinequeue := make(chan string, 1)
113 //~ brn := brain.New()
114 defer close(newlinequeue)
116 //~ defer brn.Shutdown()
121 go EventToXMPP(ps, xmpp_presence_events_chan_)
124 fmt.Println("XMPP Bot disabled")
126 go ReadFromUSocket(presence_socket_path_, newlinequeue)
127 ticker := time.NewTicker(time.Duration(7) * time.Minute)
131 case e := <-newlinequeue:
132 ParseSocketInputLine(e, ps) //, brn)
134 ps.Pub(TimeTick{time.Now().Unix()}, "publishjson")