aboutsummaryrefslogtreecommitdiff
path: root/Sources/GopherHelpers/GopherHelpers.swift
blob: 15f14cb52dddbf31bf952913c3189c1af1de13d8 (plain)
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
//
//  File.swift
//
//
//  Created by Navan Chauhan on 12/16/23.
//

import Foundation
import NIOCore

/*

 From Wikipedia

 Canonical types
 0    Text file
 1    Gopher submenu
 2    CCSO Nameserver
 3    Error code returned by a Gopher server to indicate failure
 4    BinHex-encoded file (primarily for Macintosh computers)
 5    DOS file
 6    uuencoded file
 7    Gopher full-text search
 8    Telnet
 9    Binary file
 +    Mirror or alternate server (for load balancing or in case of primary server downtime)
 g    GIF file
 I    Image file
 T    Telnet 3270
 gopher+ types
 :    Bitmap image
 ;    Movie file
 <    Sound file
 Non-canonical types
 d    Doc. Seen used alongside PDF's and .DOC's
 h    HTML file
 i    Informational message, widely used.[25]
 p    image file "(especially the png format)"
 r    document rtf file "rich text Format")
 s    Sound file (especially the WAV format)
 P    document pdf file "Portable Document Format")
 X    document xml file "eXtensive Markup Language" )
 */

public enum gopherItemType {
  case text
  case directory
  case nameserver
  case error
  case binhex
  case bindos
  case uuencoded
  case search
  case telnet
  case binary
  case mirror
  case gif
  case image
  case tn3270Session
  case bitmap
  case movie
  case sound
  case doc
  case html
  case info
}

public struct gopherItem {

  public var rawLine: String
  public var rawData: ByteBuffer?
  public var message: String = ""
  public var parsedItemType: gopherItemType = .info
  public var host: String = "error.host"
  public var port: Int = 1
  public var selector: String = ""
  public var valid: Bool = true

  public init(rawLine: String) {
    self.rawLine = rawLine
  }
}

public func getGopherFileType(item: String) -> gopherItemType {
  switch item {
  case "0":
    return .text
  case "1":
    return .directory
  case "2":
    return .nameserver
  case "3":
    return .error
  case "4":
    return .binhex
  case "5":
    return .bindos
  case "6":
    return .uuencoded
  case "7":
    return .search
  case "8":
    return .telnet
  case "9":
    return .binary
  case "+":
    return .mirror
  case "g":
    return .gif
  case "I":
    return .image
  case "T":
    return .tn3270Session
  case ":":
    return .bitmap
  case ";":
    return .movie
  case "<":
    return .sound
  case "d":
    return .doc
  case "h":
    return .html
  case "i":
    return .info
  case "p":
    return .image
  case "r":
    return .doc
  case "s":
    return .doc
  case "P":
    return .doc
  case "X":
    return .doc
  default:
    return .info
  }
}

public func getFileType(fileExtension: String) -> gopherItemType {
  switch fileExtension {
  case "txt":
    return .text
  case "md":
    return .text
  case "html":
    return .html
  case "pdf":
    return .doc
  case "png":
    return .image
  case "gif":
    return .gif
  case "jpg":
    return .image
  case "jpeg":
    return .image
  case "mp3":
    return .sound
  case "wav":
    return .sound
  case "mp4":
    return .movie
  case "mov":
    return .movie
  case "avi":
    return .movie
  case "rtf":
    return .doc
  case "xml":
    return .doc
  default:
    return .binary
  }
}

public func fileTypeToGopherItem(fileType: gopherItemType) -> String {
  switch fileType {
  case .text:
    return "0"
  case .directory:
    return "1"
  case .nameserver:
    return "2"
  case .error:
    return "3"
  case .binhex:
    return "4"
  case .bindos:
    return "5"
  case .uuencoded:
    return "6"
  case .search:
    return "7"
  case .telnet:
    return "8"
  case .binary:
    return "9"
  case .mirror:
    return "+"
  case .tn3270Session:
    return "T"
  case .gif:
    return "g"
  case .image:
    return "I"
  case .bitmap:
    return "b"
  case .movie:
    return "M"
  case .sound:
    return "s"
  case .doc:
    return "d"
  case .html:
    return "h"
  case .info:
    return "i"
  //  case .png:
  //    return "p"
  //  case .rtf:
  //    return "t"
  //  case .wavfile:
  //    return "w"
  //  case .pdf:
  //    return "P"
  //  case .xml:
  //    return "x"
  }
}

public func itemToImageType(_ item: gopherItem) -> String {
  switch item.parsedItemType {
  case .text:
    return "doc.plaintext"
  case .directory:
    return "folder"
  case .error:
    return "exclamationmark.triangle"
  case .gif:
    return "photo.stack"
  case .image:
    return "photo"
  case .doc:
    return "doc.richtext"
  case .sound:
    return "music.note"
  case .bitmap:
    return "photo"
  case .html:
    return "globe"
  case .movie:
    return "videoprojector"
  default:
    return "questionmark.square.dashed"
  }
}