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
30 flag.StringVar(&xmpp_login_.jid, "xjid", "realrauminfo@realraum.at/Tuer", "XMPP Bot Login JID")
31 flag.StringVar(&xmpp_login_.pass, "xpass", "", "XMPP Bot Login Password")
32 flag.StringVar(&xmpp_bot_authstring_, "xbotauth", "", "String that user use to authenticate themselves to the bot")
33 flag.StringVar(&presence_socket_path_,"presencesocket", "/var/run/tuer/presence.socket", "Path to presence socket")
34 flag.StringVar(&xmpp_state_save_dir_,"xstatedir","/flash/var/lib/r3netstatus/", "Directory to save XMPP bot state in")
40 func IfThenElseStr(c bool, strue, sfalse string) string {
41 if c {return strue} else {return sfalse}
44 func composeMessage(present, locked, shut bool, who string, ts int64) string {
45 return fmt.Sprintf("%s (Door is %s and %s and was last used%s at %s)",
46 IfThenElseStr(present, "Somebody is present!" , "Everybody left."),
47 IfThenElseStr(locked, "locked","unlocked"),
48 IfThenElseStr(shut, "shut","ajar"),
49 IfThenElseStr(len(who) == 0,"", " by " + who),
50 time.Unix(ts,0).String())
53 func EventToXMPP(ps *pubsub.PubSub, xmpp_presence_events_chan_ chan <- interface{}) {
54 events := ps.Sub("presence","door","buttons")
55 var present, locked, shut bool = false, true, true
57 button_msg := "The button has been pressed ! Propably someone is bored and in need of company ! ;-)"
59 for eventinterface := range(events) {
60 switch event := eventinterface.(type) {
62 present = event.Present
63 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3OnlineOnlyInfo, RememberAsStatus: true}
64 case DoorCommandEvent:
65 if len(event.Who) > 0 && len(event.Using) > 0 {
66 who = fmt.Sprintf("%s (%s)",event.Who, event.Using)
70 xmpp_presence_events_chan_ <- fmt.Sprintln("DoorCommand:",event.Command, "using", event.Using, "by", event.Who, time.Unix(event.Ts,0))
71 case DoorStatusUpdate:
74 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: composeMessage(present, locked, shut, who, event.Ts), DistributeLevel: r3xmppbot.R3DebugInfo, RememberAsStatus: true}
75 case ButtonPressUpdate:
76 xmpp_presence_events_chan_ <- r3xmppbot.XMPPMsgEvent{Msg: button_msg, DistributeLevel: r3xmppbot.R3OnlineOnlyInfo}
83 var bot *r3xmppbot.XmppBot
84 bot, xmpp_presence_events_chan_, err = r3xmppbot.NewStartedBot(xmpp_login_.jid, xmpp_login_.pass, xmpp_bot_authstring_, xmpp_state_save_dir_, true)
91 newlinequeue := make(chan string, 1)
93 //~ brn := brain.New()
94 defer close(newlinequeue)
96 //~ defer brn.Shutdown()
98 ticker := time.NewTicker(time.Duration(7) * time.Minute)
100 go EventToXMPP(ps, xmpp_presence_events_chan_)
101 go ReadFromUSocket(presence_socket_path_, newlinequeue)
104 case e := <-newlinequeue:
105 ParseSocketInputLine(e, ps) //, brn)
107 ps.Pub(TimeTick{time.Now().Unix()}, "publishjson")