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"
|
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
"github.com/khlieng/dispatch/config"
|
2016-01-26 21:10:44 +00:00
|
|
|
"github.com/khlieng/dispatch/storage"
|
2018-11-22 11:00:18 +00:00
|
|
|
"github.com/khlieng/dispatch/version"
|
2016-01-26 21:10:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type connectDefaults struct {
|
2018-05-31 21:24:59 +00:00
|
|
|
Name string
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
Channels []string
|
|
|
|
Password bool
|
|
|
|
SSL bool
|
|
|
|
ReadOnly bool
|
|
|
|
ShowDetails bool
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:00:18 +00:00
|
|
|
type dispatchVersion struct {
|
|
|
|
Tag string
|
|
|
|
Commit string
|
|
|
|
Date string
|
|
|
|
}
|
|
|
|
|
2016-01-26 21:10:44 +00:00
|
|
|
type indexData struct {
|
2018-12-11 09:51:20 +00:00
|
|
|
Defaults *config.Defaults
|
2018-05-31 21:24:59 +00:00
|
|
|
Servers []Server
|
2018-06-01 02:16:38 +00:00
|
|
|
Channels []*storage.Channel
|
2020-05-06 02:19:55 +00:00
|
|
|
OpenDMs []storage.Tab
|
2018-08-10 18:24:29 +00:00
|
|
|
HexIP bool
|
2018-11-22 11:00:18 +00:00
|
|
|
Version dispatchVersion
|
2016-01-26 21:10:44 +00:00
|
|
|
|
2018-10-15 06:56:17 +00:00
|
|
|
Settings *storage.ClientSettings
|
|
|
|
|
2016-01-26 21:10:44 +00:00
|
|
|
// Users in the selected channel
|
2018-05-31 21:24:59 +00:00
|
|
|
Users *Userlist
|
2016-01-26 21:10:44 +00:00
|
|
|
|
|
|
|
// Last messages in the selected channel
|
2018-05-31 21:24:59 +00:00
|
|
|
Messages *Messages
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
func (d *Dispatch) getIndexData(r *http.Request, state *State) *indexData {
|
2018-12-11 09:51:20 +00:00
|
|
|
cfg := d.Config()
|
|
|
|
|
2018-08-10 18:24:29 +00:00
|
|
|
data := indexData{
|
2019-06-09 00:29:35 +00:00
|
|
|
Defaults: &cfg.Defaults,
|
2018-12-11 09:51:20 +00:00
|
|
|
HexIP: cfg.HexIP,
|
2018-11-22 11:00:18 +00:00
|
|
|
Version: dispatchVersion{
|
|
|
|
Tag: version.Tag,
|
|
|
|
Commit: version.Commit,
|
|
|
|
Date: version.Date,
|
|
|
|
},
|
2018-08-10 18:24:29 +00:00
|
|
|
}
|
2018-05-22 01:56:48 +00:00
|
|
|
|
2018-12-11 09:51:20 +00:00
|
|
|
if data.Defaults.Password != "" {
|
|
|
|
data.Defaults.Password = "******"
|
2018-05-22 01:56:48 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
if state == nil {
|
2018-10-15 07:06:43 +00:00
|
|
|
data.Settings = storage.DefaultClientSettings()
|
2018-05-22 01:56:48 +00:00
|
|
|
return &data
|
|
|
|
}
|
|
|
|
|
2018-10-15 06:56:17 +00:00
|
|
|
data.Settings = state.user.GetClientSettings()
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
servers, err := state.user.GetServers()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
connections := state.getConnectionStates()
|
2017-07-02 01:31:00 +00:00
|
|
|
for _, server := range servers {
|
|
|
|
server.Password = ""
|
|
|
|
server.Username = ""
|
|
|
|
server.Realname = ""
|
|
|
|
|
2019-01-27 07:53:07 +00:00
|
|
|
s := Server{
|
2017-07-02 01:31:00 +00:00
|
|
|
Server: server,
|
|
|
|
Status: newConnectionUpdate(server.Host, connections[server.Host]),
|
2019-01-27 07:53:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if i, ok := state.irc[server.Host]; ok {
|
|
|
|
s.Features = i.Features.Map()
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Servers = append(data.Servers, s)
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 21:24:59 +00:00
|
|
|
channels, err := state.user.GetChannels()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-26 21:10:44 +00:00
|
|
|
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
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
openDMs, err := state.user.GetOpenDMs()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
2017-06-13 02:25:59 +00:00
|
|
|
}
|
2020-05-06 02:19:55 +00:00
|
|
|
data.OpenDMs = openDMs
|
2017-06-13 02:25:59 +00:00
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
tab, err := tabFromRequest(r)
|
|
|
|
if err == nil && hasTab(channels, openDMs, tab.Server, tab.Name) {
|
|
|
|
data.addUsersAndMessages(tab.Server, tab.Name, state)
|
2018-04-24 19:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &data
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
func (d *indexData) addUsersAndMessages(server, name string, state *State) {
|
|
|
|
if isChannel(name) {
|
|
|
|
users := channelStore.GetUsers(server, name)
|
|
|
|
if len(users) > 0 {
|
|
|
|
d.Users = &Userlist{
|
|
|
|
Server: server,
|
|
|
|
Channel: name,
|
|
|
|
Users: users,
|
|
|
|
}
|
2018-05-31 21:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
messages, hasMore, err := state.user.GetLastMessages(server, name, 50)
|
2018-05-31 21:24:59 +00:00
|
|
|
if err == nil && len(messages) > 0 {
|
|
|
|
m := Messages{
|
|
|
|
Server: server,
|
2020-05-06 02:19:55 +00:00
|
|
|
To: name,
|
2018-05-31 21:24:59 +00:00
|
|
|
Messages: messages,
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasMore {
|
|
|
|
m.Next = messages[0].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Messages = &m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
func hasTab(channels []*storage.Channel, openDMs []storage.Tab, server, name string) bool {
|
|
|
|
if name != "" {
|
2017-06-13 02:25:59 +00:00
|
|
|
for _, ch := range channels {
|
2020-05-06 02:19:55 +00:00
|
|
|
if server == ch.Server && name == ch.Name {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tab := range openDMs {
|
|
|
|
if server == tab.Server && name == tab.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
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
func tabFromRequest(r *http.Request) (Tab, error) {
|
|
|
|
tab := Tab{}
|
|
|
|
|
|
|
|
var path string
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/ws") {
|
|
|
|
path = r.URL.EscapedPath()[3:]
|
|
|
|
} else {
|
|
|
|
referer, err := url.Parse(r.Referer())
|
|
|
|
if err != nil {
|
|
|
|
return tab, err
|
2017-06-13 02:25:59 +00:00
|
|
|
}
|
2020-05-06 02:19:55 +00:00
|
|
|
|
|
|
|
path = referer.EscapedPath()
|
2017-06-13 02:25:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 07:53:47 +00:00
|
|
|
if path == "/" {
|
|
|
|
cookie, err := r.Cookie("tab")
|
2020-05-06 02:19:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return tab, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := url.PathUnescape(cookie.Value)
|
|
|
|
if err != nil {
|
|
|
|
return tab, err
|
|
|
|
}
|
2017-05-02 21:21:25 +00:00
|
|
|
|
2020-05-06 02:19:55 +00:00
|
|
|
parts := strings.SplitN(v, ";", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
tab.Server = parts[0]
|
|
|
|
tab.Name = parts[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
parts := strings.Split(strings.Trim(path, "/"), "/")
|
|
|
|
if len(parts) > 0 && len(parts) < 3 {
|
|
|
|
if len(parts) == 2 {
|
|
|
|
name, err := url.PathUnescape(parts[1])
|
|
|
|
if err != nil {
|
|
|
|
return tab, err
|
2017-06-21 07:45:47 +00:00
|
|
|
}
|
2020-05-06 02:19:55 +00:00
|
|
|
|
|
|
|
tab.Name = name
|
2017-05-21 07:53:47 +00:00
|
|
|
}
|
2020-05-06 02:19:55 +00:00
|
|
|
|
|
|
|
tab.Server = parts[0]
|
2017-04-19 23:51:55 +00:00
|
|
|
}
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
2020-05-06 02:19:55 +00:00
|
|
|
|
|
|
|
return tab, nil
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|