aboutsummaryrefslogtreecommitdiff
path: root/captain/external.f90
blob: 1a27be07ba2898127e5b2f5618d6a769b1e81733 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
module external_handling
implicit none

contains

    function generate_jobs_gemini() result(res)
    use captain_db
    implicit none
    
        character(len=:), pointer::res
        type(job), dimension(:), pointer::jobs
        integer::n, i, nsize
        
        n = get_player_count()
        if(n == 0) then
        
            allocate(character(len=1024) :: res)
            
            res = "None Yet"
            
        else
        
        end if
        
    end function generate_jobs_gemini

    function generate_players_gemini() result(res)
    use captain_db
    implicit none
    
        character(len=:), pointer::res
        character(len=PLAYER_NAME_LENGTH), dimension(:), pointer::players
        integer::n, i, nsize

        character(len=3*PLAYER_NAME_LENGTH)::one_player
        
        n = get_player_count()
        if(n == 0) then
        
            allocate(character(len=1024) :: res)
            
            res = "None Yet"
            
        else

            players => get_player_names()
            
            nsize = 1024
            do i = 1, size(players)
                nsize = nsize + 16 + 2*len_trim(players(i))
            end do
            
            allocate(character(len=nsize) :: res)
            res = "## Existing Players"
            
            do i = 1, n
                one_player = "=> /players/"//trim(players(i))//".gmi "//trim(players(i))
                if(i == 1) then
                    res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//trim(one_player)
                else
                    res = trim(res)//new_line(res(1:1))//trim(one_player)
                end if
            end do
            
            deallocate(players)
        end if
        
        res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//"## Management"// &
              new_line(res(1:1))//"=> /players/add.gmi Add Player"
        
    end function generate_players_gemini
    
    function generate_instructions_gemini() result(res)
    use captain_db
    implicit none
    
        character(len=:), pointer::res
        character(len=PLAYER_NAME_LENGTH), dimension(:), pointer::instruction_names
        integer::n, i, nsize

        character(len=3*PLAYER_NAME_LENGTH)::one_player
        
        n = get_instuctions_count()
        
        if(n == 0) then
        
            allocate(character(len=1024) :: res)
            res = "None Yet"
            
        else
        
            instruction_names => get_instruction_names()
            nsize = 1024
            do i = 1, size(instruction_names)
                nsize = nsize + 16 + 2*len_trim(instruction_names(i))
            end do
            
            allocate(character(len=nsize) :: res)
            res = "## Instructions"
            
            do i = 1, n
                one_player = "=> /instructions/"//trim(instruction_names(i))//".json "//trim(instruction_names(i))
                if(i == 1) then
                    res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//trim(one_player)
                else
                    res = trim(res)//new_line(res(1:1))//trim(one_player)
                end if
            end do
            
            deallocate(instruction_names)
        
        end if
        
        res = trim(res)//new_line(res(1:1))//new_line(res(1:1))//"## Management"// &
              new_line(res(1:1))//"=> /instructions/scan.gmi Scan for Instructions"
    
    end function generate_instructions_gemini
    
    pure function is_input_provided_request(req)
    use server_response, only: request
    implicit none
        
        class(request), intent(in)::req
        logical::is_input_provided_request
        
        is_input_provided_request = associated(req%query_string)
        
    end function is_input_provided_request
    
    pure function is_input_required_request(req)
    use server_response, only: request
    implicit none
        
        class(request), intent(in)::req
        logical::is_input_required_request
        
        is_input_required_request = .false.
        if(req%location == "/players/add.gmi") then
            is_input_required_request = .true.
        end if
        
    end function is_input_required_request
    
    function external_input_required_gemini(req) result(resp)
    use server_response
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp
        
        resp%code = GEMINI_CODE_INPUT
        
        if(req%location == "/players/add.gmi") then
            call resp%set_message("Enter name of new player to add")
        end if
        
    end function external_input_required_gemini
    
    function external_input_request_gemini(req) result(resp)
    use server_response
    use captain_db
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp
        
        if(req%location == "/players/add.gmi") then
            call add_player_db(req%query_string)
            resp%code = GEMINI_CODE_REDIRECT
            call resp%set_url("/players.gmi")
            
        end if
        
    end function external_input_request_gemini
    
    function is_request_static(req)
    use server_response
    use logging
    implicit none
    
        class(request), intent(in)::req
        logical::is_request_static
        character(64)::first
        
        call req%path_component(1, first)
        
        call write_log("Static check: "//trim(first))
        
        is_request_static = ((trim(first) == "releases") .or. &
                             (trim(first) == "uploads") .or. &
                             (trim(first) == "results") .or. &
                             (trim(first) == "static") .or. &
                             (trim(first) == "favicon.txt") .or. &
                             (trim(first) == "instructions"))
        
    end function is_request_static
    
    function is_redirect_action(req)
    use server_response
    implicit none
    
        class(request), intent(in)::req
        logical::is_redirect_action
        
        is_redirect_action = .false.
        if(req%location == "/instructions/scan.gmi") then
            is_redirect_action = .true.
        end if
        
    end function is_redirect_action
    
    function external_redirect_action_request_gemini(req) result(resp)
    use captain_db
    use server_response
    use logging
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp

        resp%code = GEMINI_CODE_REDIRECT

        if(req%location == "/instructions/scan.gmi") then
            call scan_instructions_for_db()
            call resp%set_url("/instructions.gmi")
        end if
        
    end function external_redirect_action_request_gemini
    
    function external_request_static(req) result(resp)
    use logging, only: write_log
    use config
    use utilities
    use server_response
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp
        character(64)::category
        character(256)::filename
        logical::exists
    
        resp%temporary_file = .false.
    
        call req%path_component(1, category)
        call req%last_component(filename)
        
        call write_log("Catgeory: "//trim(category)//"  File: "//trim(filename))
        
        if(category == "releases") then
            allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: resp%body_filename)
            call combine_paths(release_dir, filename, resp%body_filename)
        else if(category == "uploads") then
            allocate(character(len=(len_trim(release_dir)+len_trim(filename)+1)) :: resp%body_filename)
            call combine_paths(release_dir, filename, resp%body_filename)
        else if(category == "results") then
            allocate(character(len=(len_trim(results_dir)+len_trim(filename)+1)) :: resp%body_filename)
            call combine_paths(results_dir, filename, resp%body_filename)
        else if(category == "static") then
            allocate(character(len=(len_trim(static_dir)+len_trim(filename)+1)) :: resp%body_filename)
            call combine_paths(static_dir, filename, resp%body_filename)
        else if(category == "favicon.txt") then
            allocate(character(len=(len_trim(static_dir)+len_trim(filename)+1)) :: resp%body_filename)
            call combine_paths(static_dir, filename, resp%body_filename)
        end if
    
        inquire(file=resp%body_filename, exist=exists)
        if(.not. exists) then
            resp%code = GEMINI_CODE_PERMFAIL
            call write_log("File did not exist: "//resp%body_filename)
        else
            resp%code = GEMINI_CODE_SUCCESS
            
            if(index(filename, ".gmi") /= 0) then
                resp%body_mimetype = "text/gemini"
                
            else if(index(filename, ".txt") /= 0) then
                resp%body_mimetype = "text/plain"
                
            else if(index(filename, ".json") /= 0) then
                resp%body_mimetype = "text/plain"
                
            ! Just a catch-all, whatever...
            else
                resp%body_mimetype = "application/octet-stream"
                
            end if
        end if
    
    end function external_request_static
    
    function external_request_templated(req) result(resp)
    use page_template
    use config, only: template_filepath, project
    use logging, only: write_log
    use server_response
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp
        character(1024)::template_file
        type(template)::page
        character(len=:), pointer::contents
    
        ! Open the base template
        call template_filepath("index.gmi", template_file)
        call page%init(trim(template_file))
        
        call write_log("Processing request")
    
        if(trim(req%location) == "/" .or. trim(req%location) == "/index.gmi") then
        
            call write_log("Assign")
            call page%assign('title', 'Home')
            call write_log("Assign done")
            
        else if(trim(req%location) == "/releases.gmi") then
        
            call page%assign('title', 'Releases')
        
        else if(trim(req%location) == "/jobs.gmi") then
        
            call page%assign('title', 'Jobs')
            contents => generate_jobs_gemini()
            call page%assign('contents', contents)
        
        else if(trim(req%location) == "/players.gmi") then
        
            call page%assign('title', 'Players')
            contents => generate_players_gemini()
            call page%assign('contents', contents)
            
        else if(req%location(1:9) == '/players/') then
        
        else if(trim(req%location) == "/about.gmi") then
        
            call page%assign('title', 'About')
            
        else if(trim(req%location) == "/instructions.gmi") then
            
            call page%assign('title', 'Build Instructions')
            contents => generate_instructions_gemini()
            call page%assign('contents', contents)
            
        else
        
            call page%assign('title', 'Not Found')
   
        end if
        
        call page%assign('project', project)
        
        
        call write_log("Rendering page for "//req%location)
        call page%render()

        call write_log("Finalizing response")
        resp%temporary_file = .true.
        resp%body_filename => page%output_filename
        resp%body_mimetype = "text/gemini"
        resp%code = GEMINI_CODE_SUCCESS
        
    end function external_request_templated

    function external_request_gemini(req) result(resp)
    use server_response
    use logging
    implicit none
    
        class(request), intent(in)::req
        type(response)::resp
    
        if(is_redirect_action(req)) then
            call write_log("Action request")
            resp = external_redirect_action_request_gemini(req)
        
        else if(is_input_provided_request(req)) then
            call write_log("Input request")
            resp = external_input_request_gemini(req)
            
        else if(is_input_required_request(req)) then
            call write_log("Input required")
            resp = external_input_required_gemini(req)
    
        else if(is_request_static(req)) then
            call write_log("Req static")
            resp = external_request_static(req)
    
        else 
            call write_log("Req template")
            resp = external_request_templated(req)
    
        end if
        
    end function external_request_gemini

end module external_handling