2016-01-26 21:10:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-06-13 02:25:59 +00:00
|
|
|
"net/url"
|
2016-01-26 21:10:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/spf13/viper"
|
2016-01-26 21:10:44 +00:00
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type connectDefaults struct {
|
2018-05-16 03:02:48 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
Port int `json:"port,omitempty"`
|
|
|
|
Channels []string `json:"channels,omitempty"`
|
2018-04-14 23:02:28 +00:00
|
|
|
Password bool `json:"password,omitempty"`
|
|
|
|
SSL bool `json:"ssl,omitempty"`
|
|
|
|
ReadOnly bool `json:"readonly,omitempty"`
|
|
|
|
ShowDetails bool `json:"showDetails,omitempty"`
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type indexData struct {
|
|
|
|
Defaults connectDefaults `json:"defaults"`
|
2017-07-02 01:31:00 +00:00
|
|
|
Servers []Server `json:"servers,omitempty"`
|
2016-01-26 21:10:44 +00:00
|
|
|
Channels []storage.Channel `json:"channels,omitempty"`
|
|
|
|
|
|
|
|
// Users in the selected channel
|
|
|
|
Users *Userlist `json:"users,omitempty"`
|
|
|
|
|
|
|
|
// Last messages in the selected channel
|
2017-04-19 23:51:55 +00:00
|
|
|
Messages *Messages `json:"messages,omitempty"`
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 07:53:47 +00:00
|
|
|
func (d *indexData) addUsersAndMessages(server, channel string, session *Session) {
|
|
|
|
users := channelStore.GetUsers(server, channel)
|
|
|
|
if len(users) > 0 {
|
|
|
|
d.Users = &Userlist{
|
|
|
|
Server: server,
|
|
|
|
Channel: channel,
|
|
|
|
Users: users,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
messages, hasMore, err := session.user.GetLastMessages(server, channel, 50)
|
|
|
|
if err == nil && len(messages) > 0 {
|
|
|
|
m := Messages{
|
|
|
|
Server: server,
|
|
|
|
To: channel,
|
|
|
|
Messages: messages,
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMore {
|
|
|
|
m.Next = messages[0].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Messages = &m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:10:44 +00:00
|
|
|
func getIndexData(r *http.Request, session *Session) *indexData {
|
2017-07-02 01:31:00 +00:00
|
|
|
data := indexData{}
|
2018-05-22 01:56:48 +00:00
|
|
|
|
|
|
|
data.Defaults = connectDefaults{
|
|
|
|
Name: viper.GetString("defaults.name"),
|
|
|
|
Host: viper.GetString("defaults.host"),
|
|
|
|
Port: viper.GetInt("defaults.port"),
|
|
|
|
Channels: viper.GetStringSlice("defaults.channels"),
|
|
|
|
Password: viper.GetString("defaults.password") != "",
|
|
|
|
SSL: viper.GetBool("defaults.ssl"),
|
|
|
|
ReadOnly: viper.GetBool("defaults.readonly"),
|
|
|
|
ShowDetails: viper.GetBool("defaults.show_details"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if session == nil {
|
|
|
|
return &data
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:10:44 +00:00
|
|
|
servers := session.user.GetServers()
|
|
|
|
connections := session.getConnectionStates()
|
2017-07-02 01:31:00 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
server.Password = ""
|
|
|
|
server.Username = ""
|
|
|
|
server.Realname = ""
|
|
|
|
|
|
|
|
data.Servers = append(data.Servers, Server{
|
|
|
|
Server: server,
|
|
|
|
Status: newConnectionUpdate(server.Host, connections[server.Host]),
|
|
|
|
})
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
channels := session.user.GetChannels()
|
|
|
|
for i, channel := range channels {
|
|
|
|
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
|
|
|
|
}
|
2017-07-02 01:31:00 +00:00
|
|
|
data.Channels = channels
|
|
|
|
|
2017-06-13 02:25:59 +00:00
|
|
|
server, channel := getTabFromPath(r.URL.EscapedPath())
|
2018-04-24 19:21:42 +00:00
|
|
|
if isInChannel(channels, server, channel) {
|
2017-06-13 02:25:59 +00:00
|
|
|
data.addUsersAndMessages(server, channel, session)
|
|
|
|
return &data
|
|
|
|
}
|
|
|
|
|
|
|
|
server, channel = parseTabCookie(r, r.URL.Path)
|
2018-04-24 19:21:42 +00:00
|
|
|
if isInChannel(channels, server, channel) {
|
|
|
|
data.addUsersAndMessages(server, channel, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &data
|
|
|
|
}
|
|
|
|
|
|
|
|
func isInChannel(channels []storage.Channel, server, channel string) bool {
|
2017-06-13 02:25:59 +00:00
|
|
|
if channel != "" {
|
|
|
|
for _, ch := range channels {
|
|
|
|
if server == ch.Server && channel == ch.Name {
|
2018-04-24 19:21:42 +00:00
|
|
|
return true
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-21 07:53:47 +00:00
|
|
|
}
|
2018-04-24 19:21:42 +00:00
|
|
|
return false
|
2017-05-21 07:53:47 +00:00
|
|
|
}
|
2017-05-02 21:21:25 +00:00
|
|
|
|
2017-06-13 02:25:59 +00:00
|
|
|
func getTabFromPath(rawPath string) (string, string) {
|
|
|
|
path := strings.Split(strings.Trim(rawPath, "/"), "/")
|
|
|
|
if len(path) == 2 {
|
|
|
|
name, err := url.PathUnescape(path[1])
|
|
|
|
if err == nil && isChannel(name) {
|
|
|
|
return path[0], name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", ""
|
|
|
|
}
|
|
|
|
|
2017-05-21 07:53:47 +00:00
|
|
|
func parseTabCookie(r *http.Request, path string) (string, string) {
|
|
|
|
if path == "/" {
|
|
|
|
cookie, err := r.Cookie("tab")
|
|
|
|
if err == nil {
|
2017-06-21 07:45:47 +00:00
|
|
|
v, err := url.PathUnescape(cookie.Value)
|
|
|
|
if err == nil {
|
|
|
|
tab := strings.SplitN(v, ";", 2)
|
2017-05-02 21:21:25 +00:00
|
|
|
|
2017-06-21 07:45:47 +00:00
|
|
|
if len(tab) == 2 && isChannel(tab[1]) {
|
|
|
|
return tab[0], tab[1]
|
|
|
|
}
|
2017-05-21 07:53:47 +00:00
|
|
|
}
|
2017-04-19 23:51:55 +00:00
|
|
|
}
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
2017-05-21 07:53:47 +00:00
|
|
|
return "", ""
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|