2020-04-17 15:39:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-04-22 12:41:01 +00:00
|
|
|
var FileExtMap = map[string]ItemType{
|
2020-04-29 17:28:51 +00:00
|
|
|
".out": TypeBin,
|
|
|
|
".a": TypeBin,
|
|
|
|
".o": TypeBin,
|
|
|
|
".ko": TypeBin, /* ... Though tbh, kernel extensions?!!! */
|
|
|
|
".msi": TypeBin,
|
|
|
|
".exe": TypeBin,
|
|
|
|
|
|
|
|
".lz": TypeBinArchive,
|
|
|
|
".gz": TypeBinArchive,
|
|
|
|
".bz2": TypeBinArchive,
|
|
|
|
".7z": TypeBinArchive,
|
|
|
|
".zip": TypeBinArchive,
|
|
|
|
|
|
|
|
".gitignore": TypeFile,
|
|
|
|
".txt": TypeFile,
|
|
|
|
".json": TypeFile,
|
|
|
|
".yaml": TypeFile,
|
|
|
|
".ocaml": TypeFile,
|
|
|
|
".s": TypeFile,
|
|
|
|
".c": TypeFile,
|
|
|
|
".py": TypeFile,
|
|
|
|
".h": TypeFile,
|
|
|
|
".go": TypeFile,
|
|
|
|
".fs": TypeFile,
|
|
|
|
".odin": TypeFile,
|
|
|
|
".nanorc": TypeFile,
|
|
|
|
".bashrc": TypeFile,
|
|
|
|
".mkshrc": TypeFile,
|
|
|
|
".vimrc": TypeFile,
|
|
|
|
".vim": TypeFile,
|
|
|
|
".viminfo": TypeFile,
|
|
|
|
".sh": TypeFile,
|
|
|
|
".conf": TypeFile,
|
|
|
|
".xinitrc": TypeFile,
|
|
|
|
".jstarrc": TypeFile,
|
|
|
|
".joerc": TypeFile,
|
|
|
|
".jpicorc": TypeFile,
|
|
|
|
".profile": TypeFile,
|
|
|
|
".bash_profile": TypeFile,
|
|
|
|
".bash_logout": TypeFile,
|
2020-04-30 10:00:07 +00:00
|
|
|
".log": TypeFile,
|
2020-04-30 11:06:12 +00:00
|
|
|
".ovpn": TypeFile,
|
2020-04-29 17:28:51 +00:00
|
|
|
|
|
|
|
".md": TypeMarkup,
|
|
|
|
|
|
|
|
".xml": TypeXml,
|
|
|
|
|
|
|
|
".doc": TypeDoc,
|
|
|
|
".docx": TypeDoc,
|
|
|
|
".pdf": TypeDoc,
|
|
|
|
|
|
|
|
".jpg": TypeImage,
|
|
|
|
".jpeg": TypeImage,
|
|
|
|
".png": TypeImage,
|
|
|
|
".gif": TypeImage,
|
|
|
|
|
|
|
|
".html": TypeHtml,
|
|
|
|
".htm": TypeHtml,
|
|
|
|
|
|
|
|
".ogg": TypeAudio,
|
|
|
|
".mp3": TypeAudio,
|
|
|
|
".wav": TypeAudio,
|
|
|
|
".mod": TypeAudio,
|
|
|
|
".it": TypeAudio,
|
|
|
|
".xm": TypeAudio,
|
|
|
|
".mid": TypeAudio,
|
|
|
|
".vgm": TypeAudio,
|
|
|
|
".opus": TypeAudio,
|
|
|
|
".m4a": TypeAudio,
|
|
|
|
".aac": TypeAudio,
|
|
|
|
|
|
|
|
".mp4": TypeVideo,
|
|
|
|
".mkv": TypeVideo,
|
|
|
|
".webm": TypeVideo,
|
2020-04-19 12:42:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:18:35 +00:00
|
|
|
func buildError(selector string) []byte {
|
|
|
|
ret := string(TypeError)
|
|
|
|
ret += selector + DOSLineEnd
|
2020-04-24 11:28:38 +00:00
|
|
|
ret += LastLine
|
2020-04-20 14:18:35 +00:00
|
|
|
return []byte(ret)
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Build gopher compliant line with supplied information */
|
2020-04-22 18:42:54 +00:00
|
|
|
func buildLine(t ItemType, name, selector, host string, port string) []byte {
|
2020-04-17 15:39:25 +00:00
|
|
|
ret := string(t)
|
|
|
|
|
|
|
|
/* Add name, truncate name if too long */
|
2020-04-22 18:42:54 +00:00
|
|
|
if len(name) > Config.PageWidth {
|
2020-04-24 11:09:54 +00:00
|
|
|
ret += name[:Config.PageWidth-5]+"...\t"
|
2020-04-17 15:39:25 +00:00
|
|
|
} else {
|
|
|
|
ret += name+"\t"
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add selector. If too long use err, skip if empty */
|
|
|
|
selectorLen := len(selector)
|
|
|
|
if selectorLen > MaxSelectorLen {
|
|
|
|
ret += SelectorErrorStr+"\t"
|
|
|
|
} else if selectorLen > 0 {
|
|
|
|
ret += selector+"\t"
|
|
|
|
}
|
|
|
|
|
2020-04-19 19:58:28 +00:00
|
|
|
/* Add host + port */
|
2020-04-22 18:42:54 +00:00
|
|
|
ret += host+"\t"+port+DOSLineEnd
|
2020-04-17 15:39:25 +00:00
|
|
|
|
|
|
|
return []byte(ret)
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Build gopher compliant info line */
|
2020-04-17 15:39:25 +00:00
|
|
|
func buildInfoLine(content string) []byte {
|
2020-04-19 19:58:28 +00:00
|
|
|
return buildLine(TypeInfo, content, NullSelector, NullHost, NullPort)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Get item type for named file on disk */
|
2020-04-20 14:18:35 +00:00
|
|
|
func getItemType(name string) ItemType {
|
2020-04-22 12:41:01 +00:00
|
|
|
/* Split, name MUST be lower */
|
|
|
|
split := strings.Split(strings.ToLower(name), ".")
|
2020-04-19 12:42:56 +00:00
|
|
|
|
|
|
|
/* First we look at how many '.' in name string */
|
2020-04-22 12:41:01 +00:00
|
|
|
splitLen := len(split)
|
|
|
|
switch splitLen {
|
2020-04-19 12:42:56 +00:00
|
|
|
case 0:
|
|
|
|
/* Always return TypeDefault. We can never tell */
|
|
|
|
return TypeDefault
|
|
|
|
|
|
|
|
default:
|
2020-04-22 12:41:01 +00:00
|
|
|
/* Get index of str after last ".", look in FileExtMap */
|
|
|
|
fileType, ok := FileExtMap["."+split[splitLen-1]]
|
2020-04-19 12:42:56 +00:00
|
|
|
if ok {
|
|
|
|
return fileType
|
|
|
|
} else {
|
|
|
|
return TypeDefault
|
|
|
|
}
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:58:29 +00:00
|
|
|
/* Build a line separator of supplied width */
|
|
|
|
func buildLineSeparator(count int) string {
|
|
|
|
ret := ""
|
|
|
|
for i := 0; i < count; i += 1 {
|
|
|
|
ret += "_"
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Formats an info-text footer from string. Add last line as we use the footer to contain last line (regardless if empty) */
|
|
|
|
func formatGophermapFooter(text string, useSeparator bool) []byte {
|
|
|
|
ret := make([]byte, 0)
|
|
|
|
if text != "" {
|
|
|
|
ret = append(ret, buildInfoLine("")...)
|
|
|
|
if useSeparator {
|
|
|
|
ret = append(ret, buildInfoLine(buildLineSeparator(Config.PageWidth))...)
|
|
|
|
}
|
|
|
|
for _, line := range strings.Split(text, "\n") {
|
|
|
|
ret = append(ret, buildInfoLine(line)...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = append(ret, []byte(LastLine)...)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-04-24 11:09:54 +00:00
|
|
|
/* Parse line type from contents */
|
2020-04-17 15:39:25 +00:00
|
|
|
func parseLineType(line string) ItemType {
|
|
|
|
lineLen := len(line)
|
|
|
|
|
|
|
|
if lineLen == 0 {
|
|
|
|
return TypeInfoNotStated
|
|
|
|
} else if lineLen == 1 {
|
|
|
|
/* The only accepted types for a length 1 line */
|
|
|
|
switch ItemType(line[0]) {
|
|
|
|
case TypeEnd:
|
|
|
|
return TypeEnd
|
|
|
|
case TypeEndBeginList:
|
|
|
|
return TypeEndBeginList
|
|
|
|
case TypeComment:
|
|
|
|
return TypeComment
|
|
|
|
case TypeInfo:
|
|
|
|
return TypeInfo
|
|
|
|
case TypeTitle:
|
|
|
|
return TypeTitle
|
|
|
|
default:
|
|
|
|
return TypeUnknown
|
|
|
|
}
|
|
|
|
} else if !strings.Contains(line, string(Tab)) {
|
|
|
|
/* The only accepted types for a line with no tabs */
|
|
|
|
switch ItemType(line[0]) {
|
|
|
|
case TypeComment:
|
|
|
|
return TypeComment
|
|
|
|
case TypeTitle:
|
|
|
|
return TypeTitle
|
|
|
|
case TypeInfo:
|
|
|
|
return TypeInfo
|
|
|
|
case TypeHiddenFile:
|
|
|
|
return TypeHiddenFile
|
|
|
|
case TypeSubGophermap:
|
|
|
|
return TypeSubGophermap
|
|
|
|
case TypeExec:
|
|
|
|
return TypeExec
|
|
|
|
default:
|
|
|
|
return TypeInfoNotStated
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ItemType(line[0])
|
|
|
|
}
|