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
  
// This is a roxen module. Copyright © 1996 - 2009, Roxen IS. 
 
// Gopher proxy module. 
 
constant cvs_version = "$Id$"; 
constant thread_safe = 1; 
 
#include <config.h> 
 
#define CONNECTION_REFUSED "HTTP/1.0 500 Connection refused by remote "       \ 
"host\r\nContent-type: text/html\r\n\r\n<title>Roxen: Connection "      \ 
"refused </title>\n<h1>Proxy request failed</h1><hr /><font "           \ 
"size=\"+2\"><i>Connection refused by remote host</i></font><hr><font " \ 
"size=\"-2\"><a href=http://www.roxen.com/>Roxen</a></font>" 
 
#ifdef GOPHER_DEBUG 
# define GOPHER_WERR(X) werror("GOPHER: "+X+"\n") 
#else 
# define GOPHER_WERR(X) 
#endif 
 
inherit "module"; 
inherit "socket"; 
 
#include <module.h> 
#include <proxyauth.pike> 
 
constant module_type = MODULE_LOCATION|MODULE_PROXY; 
constant module_name = "Gopher Gateway"; 
constant module_doc  = "This is a caching gopher gateway, might be useful for firewall sites, if " 
  "anyone is still using gopher."; 
 
string query_location() { return query("loc"); } 
 
void create() 
{ 
  defvar("loc", "gopher:/", "Location", TYPE_LOCATION|VAR_MORE, 
         "The mountpoint of the gopher proxy"); 
} 
 
 
string make_html_line(array(string) s) 
{ 
  if(s) 
    return sprintf("<a href=\"%s\"> <img hspace=5 border=0 " 
                   "src=\"%s\"> %s</a><br>\n", 
                   replace(s[1], " ", "%20"), s[0], replace(s[2], "_", " ")); 
  else 
    return "<p>"; 
} 
 
void my_pipe_done(array (object) a) 
{ 
//if(a[1]) destruct(a[1]); 
  if(a[0]) ::my_pipe_done(a[0]); 
} 
 
void write_to_client_and_cache(object client, string data, string key) 
{ 
  object cache; 
  object pip; 
  if(key) 
    cache = roxen->create_cache_file("gopher", key); 
 
  pip=Pipe.pipe(); 
  if(cache) 
  { 
    pip->set_done_callback(my_pipe_done, ({ cache, client })); 
    pip->output(cache->file); 
  } 
  if(client->my_fd) 
  { 
    pip->output(client->my_fd); 
    pip->write(data); 
  } 
} 
 
 
void done_dir_data(array in) 
{ 
  int i; 
  array  dirl=(in[0]-"\r")/"\n"; 
  object to=in[1]; 
  destruct(in[2]); 
 
  if(!objectp(to)) 
    return; 
 
  if(dirl[0] && dirl[0][0] != '<') 
  { 
    GOPHER_WERR("Done with dir data."); 
    dirl -= ({ ".", "" }); 
    for(i=0; i < sizeof(dirl); i++) 
    { 
      array a; 
      a=dirl[i][1..]/"\t"; 
#define URL (a[2]+((int)a[3]==70?"":":"+a[3])+"/"+dirl[i][0..0]+a[1]) 
      switch(dirl[i][0]) 
      { 
       case '0': /* File */ 
        dirl[i] = ({ "internal-gopher-text", "gopher://"+URL, a[0] }); 
        break; 
 
       case '1': /* Dir  */ 
        dirl[i] = ({ "internal-gopher-menu", "gopher://"+URL, a[0] }); 
        break; 
 
       case '2': /* Phonebook */ 
        dirl[i] = ({ "internal-gopher-index", "gopher://"+URL, a[0] }); 
        break; 
 
       case '3': /* Error? */ 
        dirl[i] = ({ "internal-gopher-binary", "gopher://"+URL, a[0] }); 
        break; 
 
       case '4': /* BinHex */ 
       case '5': /* Dos binary */ 
       case '6': /* Unix UUENCODED file */ 
       case '9': /* Binary */ 
        dirl[i] = ({ "internal-gopher-binary", "gopher://"+URL, a[0] }); 
        break; 
 
       case '7': /* Search */ 
        dirl[i] = ({ "internal-gopher-index", "gopher://"+URL, a[0] }); 
        break; 
 
       case '8': 
        dirl[i] = ({ "internal-gopher-telnet", "telnet://"+URL, a[0] }); 
        break; 
 
       case 'T': 
        dirl[i] = ({ "internal-gopher-telnet", "tn3270://"+URL, a[0] }); 
        break; 
 
       case '+': 
        dirl[i] = ({ "internal-gopher-menu", "gopher://"+URL, a[0] }); 
        break; 
 
       case 'I': 
       case 'g': 
        dirl[i] = ({ "internal-gopher-image", "gopher://"+URL, a[0] }); 
        break; 
 
       default:  /* Who knows.. */ 
        if(stringp(a[0] && strlen((string)a[0]))) 
           dirl[i] = ({ "internal-gopher-unknown", "gopher://"+URL, a[0] }); 
        else 
          dirl[i] = 0; 
      } 
#undef URL 
    } 
    GOPHER_WERR("Sending dir data to client."); 
    write_to_client_and_cache(to, Array.map(dirl, make_html_line)*"" +"<hr>", 
                              in[-1]); 
  } else { 
    GOPHER_WERR("Sending cached dir data to client."); 
    write_to_client_and_cache(to, dirl*"\n", 0); 
  } 
  destruct(to); 
} 
 
void got_dir_data(array i, string s) 
{ 
  GOPHER_WERR("Got some dir data."); 
  i[0] += s; 
  if(i[0][-1] == '.' && i[0][-2]=='\n') 
    done_dir_data(i); 
} 
 
void gopher_done(object id) 
{ 
  id->end(); 
} 
 
void connected(object ok, string file, object send_to, string query, 
               string key) 
{ 
  string type; 
 
  GOPHER_WERR("Connected"); 
 
  if(!send_to) 
  { 
    destruct(ok); 
    return; 
  } 
 
  if(!ok) 
  { 
    send_to->end(CONNECTION_REFUSED); 
    return; 
  } 
 
  if(strlen(file) < 2) 
  { 
    type="1"; 
    file=""; 
  } else { 
    type=file[0..0]; 
    file=file[1..strlen(file)-1]; 
  } 
 
  GOPHER_WERR("Requesting file"); 
 
  switch(type) 
  { 
   case "1": /* Directory, must be parsed. */ 
     GOPHER_WERR("Is a menu"); 
    ok->write(file + "\r\n"); 
    ok->set_id(({ "", send_to, ok, key})); 
    ok->set_nonblocking(got_dir_data, lambda(){}, done_dir_data); 
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher dir comming soon to a " 
                          "screen near you\nContent-Type: text/html\n\n" 
                          "<h1>Gopher menu</h1><hr>"); 
    return; 
 
    /* 2 is phonebook(?). Should probably be parsed.. */ 
    /* 3 is error (?) */ 
 
   case "4": /* Mac binhex (of all types...) */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a " 
                          "screen near you\n" 
                          "Content-Type: application/mac-binhex\n\n"); 
    break; 
 
   case "5": /* Dos binary (of all types...) */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n" 
                          "Content-Type: application/x-dosbinary\n\n"); 
    break; 
 
   case "6": /* Unix UUENCODE */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n" 
                   "Content-Type: application/x-uuencode\n\n"); 
    break; 
 
   case "9": /* Binary */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Yo!\n" 
                          "Content-Type: application/binary\n\n"); 
    break; 
 
   case "g": /* Gif image */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Gopher data\n" 
                          "Content-Type: image/gif\n\n"); 
    break; 
 
   case "I": /* _some_ image, lets pretend it's a jpeg.. :) */ 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200  to a screen near you\n" 
                          "Content-Type: image/jpeg\n\n"); 
    break; 
 
   case "7": /* Search, should be handled separately.. */ 
    if(!query) 
    { 
      send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a " 
                            "screen near you\nContent-Type: text/html\n\n" 
                            "<h1>Gopher search</h1>" 
                            "<isindex prompt=\"Gopher search:  \">"); 
      destruct(ok); 
      destruct(send_to); 
      return; 
    } 
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a " 
                   "screen near you\nContent-Type: text/html\n\n" 
                   "<h1>Gopher Search</h1>" 
                   "<isindex prompt=\"Gopher search:  \">"); 
    ok->write(file + "    " + query + "\r\n"); 
    ok->set_id(({ "", send_to, ok})); 
    ok->set_nonblocking(got_dir_data, lambda(){}, done_dir_data); 
    return; 
 
   case "T": /* Tn 3270, shouldn't be here */ 
   case "+": /* Extra server, shouldn't be here */ 
   case "8": /* Shouldn't be here... */ 
   default: 
    ok->write(file + "\r\n"); 
    send_to->my_fd->write("HTTP/1.0 200 Yo! Gopher data comming soon to a " 
                          "screen near you\nContent-Type: text/plain\n\n"); 
 
  } 
  async_pipe(send_to->my_fd, ok, 0, 0, "gopher", key); 
  send_to->disconnect(); 
  /* Go for it... :) */ 
  return; 
} 
 
mapping find_file(string fi, object id) 
{ 
  mixed tmp; 
  string h, f, q; 
  int p; 
  GOPHER_WERR("find_file()"); 
 
  if(tmp = proxy_auth_needed(id)) 
    return tmp; 
 
  sscanf(fi, "%[^/]/%s", h, f); 
  if(!f)  f=""; 
  if (has_prefix(h, "[")) { 
    //  IPv6 
    if (sscanf(h, "[%s]:%d", h, p)) 
      h = "[" + h + "]"; 
  } else { 
    sscanf(h, "%s:%d", h, p); 
  } 
  if(!p)  p=70; 
  GOPHER_WERR("host = "+h+"\nfile = "+f+"\nport = "+p); 
  sscanf(id->raw_url, "%*s?%s", q); 
  if(id->pragma["no-cache"] || id->method != "GET") 
  { 
    async_connect(h, p, connected, f, id, q, h+":"+p+"/"+f); 
   } else { 
    async_cache_connect(h, p, "gopher", h+":"+p+"/"+f, 
                        connected, f, id, q, h+":"+p+"/"+f); 
  } 
  id->do_not_disconnect = 1; 
  return http_pipe_in_progress(); 
} 
 
string info() 
{ 
  return "This is a simple gopher proxy, useful for firewall sites."; 
}