GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — hipchat/hipchat.go at master · dane/hipchat Expand file tree/ Copy pathhipchat.goMore file actions 279 lines (245 loc) · 7.05 KB / Copy pathhipchat.goFile metadata and controls279 lines (245 loc) · 7.05 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 package hipchat import ( "bytes" "encoding/xml" "errors" "time" "github.com/daneharrigan/hipchat/xmpp" ) const ( defaultAuthType = "plain" // or "oauth" defaultConf = "conf.hipchat.com" defaultDomain = "chat.hipchat.com" defaultHost = "chat.hipchat.com" ) // A Client represents the connection between the application to the HipChat // service. type Client struct { AuthType string Username string Password string Resource string Id string // private mentionNames map[string]string connection *xmpp.Conn receivedUsers chan []*User receivedRooms chan []*Room receivedMessage chan *Message host string domain string conf string } // A Message represents a message received from HipChat. type Message struct { From string To string Body string MentionName string } // A User represents a member of the HipChat service. type User struct { Email string Id string Name string MentionName string } // A Room represents a room in HipChat the Client can join to communicate with // other members.. type Room struct { Id string LastActive string Name string NumParticipants string Owner string Privacy string RoomId string Topic string } // NewClient creates a new Client connection from the user name, password and // resource passed to it. It uses default host URL and conf URL. func NewClient(user, pass, resource, authType string) (*Client, error) { return NewClientWithServerInfo(user, pass, resource, authType, defaultHost, defaultDomain, defaultConf) } // NewClientWithServerInfo creates a new Client connection from the user name, password, // resource, host URL and conf URL passed to it. func NewClientWithServerInfo(user, pass, resource, authType, host, domain, conf string) (*Client, error) { connection, err := xmpp.Dial(host) var b bytes.Buffer if err := xml.EscapeText(&b, []byte(pass)); err != nil { return nil, err } c := &Client{ AuthType: authType, Username: user, Password: b.String(), Resource: resource, Id: user + "@" + domain, // private connection: connection, mentionNames: make(map[string]string), receivedUsers: make(chan []*User), receivedRooms: make(chan []*Room), receivedMessage: make(chan *Message), host: host, domain: domain, conf: conf, } if err != nil { return c, err } err = c.authenticate() if err != nil { return c, err } go c.listen() return c, nil } // Messages returns a read-only channel of Message structs. After joining a // room, messages will be sent on the channel. func (c *Client) Messages() <-chan *Message { return c.receivedMessage } // Rooms returns a channel of Room slices func (c *Client) Rooms() <-chan []*Room { return c.receivedRooms } // Users returns a channel of User slices func (c *Client) Users() <-chan []*User { return c.receivedUsers } // Status sends a string to HipChat to indicate whether the client is available // to chat, away or idle. func (c *Client) Status(s string) { c.connection.Presence(c.Id, s) } // Join accepts the room id and the name used to display the client in the // room. func (c *Client) Join(roomId, resource string) { c.connection.MUCPresence(roomId+"/"+resource, c.Id) } // Part accepts the room id to part. func (c *Client) Part(roomId, name string) { c.connection.MUCPart(roomId + "/" + name) } // Say accepts a room id, the name of the client in the room, and the message // body and sends the message to the HipChat room. func (c *Client) Say(roomId, name, body string) { c.connection.MUCSend("groupchat", roomId, c.Id+"/"+name, body) } // PrivSay accepts a client id, the name of the client, and the message // body and sends the private message to the HipChat // user. func (c *Client) PrivSay(user, name, body string) { c.connection.MUCSend("chat", user, c.Id+"/"+name, body) } // KeepAlive is meant to run as a goroutine. It sends a single whitespace // character to HipChat every 60 seconds. This keeps the connection from // idling after 150 seconds. func (c *Client) KeepAlive() { for _ = range time.Tick(60 * time.Second) { c.connection.KeepAlive() } } // RequestRooms will send an outgoing request to get // the room information for all rooms func (c *Client) RequestRooms() { c.connection.Discover(c.Id, c.conf) } // RequestUsers will send an outgoing request to get // the user information for all users func (c *Client) RequestUsers() { c.connection.Roster(c.Id, c.domain) } func (c *Client) authenticate() error { var errStr string c.connection.Stream(c.Id, c.host) for { element, err := c.connection.Next() if err != nil { return err } switch element.Name.Local + element.Name.Space { case "stream" + xmpp.NsStream: features := c.connection.Features() if features.StartTLS != nil { c.connection.StartTLS() } else { for _, m := range features.Mechanisms { if m == "PLAIN" && c.AuthType == "plain" { c.connection.Auth(c.Username, c.Password, c.Resource) } else if m == "X-HIPCHAT-OAUTH2" && c.AuthType == "oauth" { c.connection.Oauth(c.Password, c.Resource) } } } case "proceed" + xmpp.NsTLS: c.connection.UseTLS(c.host) c.connection.Stream(c.Id, c.host) case "iq" + xmpp.NsJabberClient: for _, attr := range element.Attr { if attr.Name.Local == "type" && attr.Value == "result" { return nil // authenticated } } return errors.New("could not authenticate") // oauth: case "failure" + xmpp.NsSASL: errStr = "Unable to authenticate:" case "invalid-authzid" + xmpp.NsSASL: errStr += " no identity provided" case "not-authorized" + xmpp.NsSASL: errStr += " token not authorized" return errors.New(errStr) case "success" + xmpp.NsSASL: return nil } } return errors.New("unexpectedly ended auth loop") } func (c *Client) listen() { for { element, err := c.connection.Next() if err != nil { return } switch element.Name.Local + element.Name.Space { case "iq" + xmpp.NsJabberClient: // rooms and rosters query := c.connection.Query() switch query.XMLName.Space { case xmpp.NsDisco: items := make([]*Room, len(query.Items)) for i, item := range query.Items { items[i] = &Room{Id: item.Jid, LastActive: item.LastActive, NumParticipants: item.NumParticipants, Name: item.Name, Owner: item.Owner, Privacy: item.Privacy, RoomId: item.RoomId, Topic: item.Topic, } } c.receivedRooms <- items case xmpp.NsIqRoster: items := make([]*User, len(query.Items)) for i, item := range query.Items { items[i] = &User{Email: item.Email, Id: item.Jid, Name: item.Name, MentionName: item.MentionName, } } c.receivedUsers <- items } case "message" + xmpp.NsJabberClient: attr := xmpp.ToMap(element.Attr) c.receivedMessage <- &Message{ From: attr["from"], To: attr["to"], Body: c.connection.Body(), } } } panic("unreachable") } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |