1#!/usr/local/bin/swipl
    2:- module(logicmoo_swish,
    3          [ swish/0,
    4            swish/1                     % ?Port
    5          ]).

Remote SWISH as an IDE for developing a Remote application. */

   12:- use_module(library(sanity_must)).   13
   14
   15:- if(\+ current_prolog_flag(lm_no_autoload,_)).   16:- set_prolog_flag(lm_no_autoload,false).   17:- wdmsg("WARNING: PFC_AUTOLOAD").   18:- endif.   19
   20%:- set_prolog_flag(lm_pfc_lean,false).
   21
   22:- use_module(library(http/thread_httpd)).   23:- use_module(library(http/http_dispatch)).   24:- use_module(library(http/http_path)).   25:- use_module(library(www_browser)).   26
   27:- if(exists_source(library(uid))).   28:- use_module(library(uid)).   29:- endif.   30
   31%:- multifile(owl2_model:datatype/2).
   32%:- dynamic(owl2_model:datatype/2).
   33                                  
   34
   35/* < module>
   36
   37Open SWISH as an IDE for developing a remote application.
   38*/
   39
   40
   41
   42:- meta_predicate(from_http(0)).   43from_http(G):- with_output_to(main_error,G).
   44:- ignore((stream_property(X,file_no(2)),
   45   stream_property(X,alias(Was)),
   46   set_stream(X,alias(main_error)),
   47   set_stream(X,alias(Was)))).   48
   49:- use_module(library(aleph),[]).   50
   51
   52:- use_module(library(sanity_must)).   53
   54reexport_from(ReExporter,From:P):-
   55    From:export(From:P),
   56    ReExporter:import(From:P),
   57    ReExporter:export(From:P).
   58
   59
   60
   61:- multifile(pldoc_register:process_stored_comments/0).   62:- dynamic(pldoc_register:process_stored_comments/0).   63:- assert(pldoc_register:process_stored_comments).   64
   65
   66                 /*******************************
   67                 *             CONFIG           *
   68                 *******************************/
   69
   70:- multifile
   71        swish_config:config/2,                  % Name, Value
   72        swish_config:source_alias/2,            % Alias, Options
   73        swish_config:verify_write_access/3,     % Request, File, Options
   74        pengines:authentication_hook/3,         % Request, Application, User
   75        pengines:not_sandboxed/2,               % User, Application
   76        pengines:allowed/2,                     % Peer, Application
   77        user:file_search_path/2.                % Alias, Path
   78
   79is_symlink_to(File,AbsLink):-
   80         read_link(File,Link,Link),
   81         absolute_file_name(Link,AbsLink,[access(read),file_errors(fail)]),
   82         absolute_file_name(File,AFile,[access(read),file_errors(fail)]),
   83         AFile \== Link.
   84
   85swish_home(AbsDir):- swish_home0(Dir),resolve_symlinks(Dir,AbsDir).
   86
   87resolve_symlinks(File,Link):- is_symlink_to(File,AbsLink)-> resolve_symlinks(AbsLink,Link) ; File=Link.
   88
   89swish_home0(Dir):- absolute_file_name(pack(swish),Dir,[file_type(directory),access(read),file_errors(fail)]).
   90swish_home0(Dir):- absolute_file_name(pack('swish-with-filesystem-interaction'),Dir,[file_type(directory),access(read),file_errors(fail)]).
   91swish_home0(Dir):- absolute_file_name(cpack(swish),Dir,[file_type(directory),access(read),file_errors(fail)]).
   92swish_home0(Dir):- is_symlink_to('./remote_ide.pl',To),file_directory_name(To,Dir).
   93
   94:- discontiguous(user:file_search_path/2).   95user:file_search_path('swish', Dir):-  swish_home(Dir),!.
   96
   97:- nodebug.   98
   99:- dynamic(non_swish_file_search_path/2).  100:- forall(retract(user:file_search_path(config_enabled, Was)),
  101          assert(non_swish_file_search_path(config_enabled, Was))).  102
  103user:file_search_path(project, '.').
  104user:file_search_path(config_enabled, 'config-enabled-swish').
  105user:file_search_path(config_enabled, swish('config-enabled')).
  106
  107:- dynamic http:location/3.  108:- multifile http:location/3.  109http:location(root, '/', [priority(1100)]).
  110http:location(swish, root('swish'), [priority(500)]).
  111http:location(root, '/swish', []).
  112
  113% http:location(swish, '/example', [priority(500)]).
  114%http:location(root, root('example'), [priority(500)]).
  115%http:location(example, root('example'), [priority(500)]).
  116% http:location(root, '/example', []).
  117:- http_handler(root(example), swish,[]).  118
  119
  120
  121nowdmsg(_).
  122
  123% :- use_module(library(r/r_sandbox)).
  124
  125:- if((false, exists_source(library(trill)))).  126
  127:- system:use_module(library(trill)).  128
  129:- reexport_from(system,trill:end_bdd/1),
  130   reexport_from(system,trill:add_var/5),
  131   reexport_from(system,trill:and/4),
  132   reexport_from(system,trill:em/8),
  133   reexport_from(system,trill:randomize/1),
  134   reexport_from(system,trill:rand_seed/1),
  135   reexport_from(system,trill:or/4),
  136   reexport_from(system,trill:ret_prob/3),
  137   reexport_from(system,trill:init_test/2),
  138   reexport_from(system,trill:end/1),
  139   reexport_from(system,trill:end_test/1),
  140   reexport_from(system,trill:bdd_not/3),
  141   reexport_from(system,trill:zero/2),
  142   reexport_from(system,trill:one/2),
  143   reexport_from(system,trill:equality/4),
  144   reexport_from(system,trill:init_bdd/2),
  145   reexport_from(system,trill:init/3).  146
  147
  148:- use_module(library(pita),[]).  149
  150:- endif.  151
  152:- if(false).  153:- use_module(library(cplint_r),[]).  154:- use_module(library(mcintyre)).  155:- use_module(library(slipcover)).  156:- use_module(library(lemur),[]).  157:- use_module(library(auc)).  158:- use_module(library(matrix)).  159
  160%:- dynamic(user:db/1).
  161%:- thread_local(user:db/1).
  162
  163:- asserta(sandbox:safe(_,_,_,_,_):-!).  164
  165:- use_module(library(clpr)).  166
  167:- multifile sandbox:safe_primitive/1.  168
  169sandbox:safe_primitive(nf_r:{_}).
  170
  171:- endif.  172
  173
  174/*
  175
  176%prolog:prolog_load_file(library(swish/X),How):- trace, prolog:load_files([swish(lib/X)],How),!.
  177%prolog:prolog_load_file(swish(lib/swish/X),How):- prolog:load_files([swish(lib/X)],How),!.
  178*/
  179
  180swish_config:config(show_beware,        false).
  181swish_config:config(community_examples, true).
  182
  183swish_config:source_alias(project, [access(both), search('*.pl')]).
  184swish_config:source_alias(library, []).
  185
  186swish_config:verify_write_access(Request, File, Options) :- currently_logged_in(swish_config:verify_write_access(Request, File, Options),_).
  187
  188pengines:authentication_hook(Request, swish, User) :-
  189   fail, currently_logged_in(pengines:authentication_hook(Request, swish, User),User),!.
  190
  191:- multifile pengines:allowed/2.  192:- dynamic pengines:allowed/2.  193
  194% dmiles runs in very well protected VM
  195pengines:not_sandboxed(_Maybe, _Application) :- gethostname(X),X=gitlab.
  196pengines:not_sandboxed(Maybe, Application) :- currently_logged_in(pengines:not_sandboxed(Maybe, Application),_User),!.
  197
  198current_user(User):- currently_logged_in(why,User).
  199
  200currently_logged_in(_Why,D):- thread_self(main), ignore(D=default).
  201currently_logged_in(Why,User):-
  202  from_http((http_session:
  203    (http_in_session(_SessionID),
  204     http_session_data(oauth2(OAuth, _)),
  205     http_session_data(user_info(OAuth, Info))),
  206   User=Info.name,!,
  207    nop(wdmsg(currently_logged_in(Why,User=Info.name))))),!.
  208
  209currently_logged_in(Why,User):-
  210  from_http((http_session:
  211    (session_data(S,oauth2(OAuth, Y)),
  212      nop(wdmsg(currently_logged_in(User=Why,
  213      session_data(S,oauth2(OAuth, Y)))))))),!,
  214  ignore(User="guest1"),!.
  215
  216
  217currently_logged_in(Why,D):- http_session:http_in_session(SessionID),!,
  218
  219  from_http(
  220  ((wdmsg(fail_dispite_http_in_session(SessionID,D,Why)),
  221    http_session:http_in_session(SessionID),
  222    listing(http_session: session_data(SessionID,_Data))))),!,fail.
  223
  224
  225
  226currently_logged_in(Why,D):- thread_self(S),wdmsg(fail_currently_logged_in(Why,S,D)),!,fail.
  227
  228
  229no_auth_needed(Request):- is_list(Request),memberchk(path_info(Path),Request),mimetype:file_mime_type(Path,Type),memberchk(Type,[image/_,_/javascript]),!.
  230no_auth_needed(Request):- is_list(Request),!,memberchk(path(Path),Request),no_auth_needed(Path).
  231no_auth_needed(X):- \+ atom(X),!,fail.
  232no_auth_needed(X):- atom_concat('/swish',XX,X),!,no_auth_needed(XX).
  233no_auth_needed('/chat').
  234%no_auth_needed('/login').
  235no_auth_needed('/').
  236no_auth_needed('').
  237
  238
  239:- multifile swish_config:authenticate/2.  240:- dynamic swish_config:authenticate/2.  241
  242swish_config:authenticate(_Request, User) :-
  243  http_session:
  244    (http_in_session(_SessionID),
  245     http_session_data(oauth2(OAuth, _)),
  246     http_session_data(user_info(OAuth, Info))),
  247   User=Info.name,!.
  248
  249% swish_config:authenticate(Request, User) :- \+ http_session:http_in_session(_),current_user(User),with_output_to(current_error,nowdmsg((swish_config:authenticate(Request, User)))),!.
  250
  251% :- use_module(pack('swish-with-filesystem-interaction'/swish)).
  252
  253:- use_module(swish(swish)).  254
  255
  256% swish_config:authenticate(Request, "bad_user") :- wdmsg(swish_config:authenticate(Request, "bad_user")),!.
  257swish_config:authenticate(Request, User) :-
  258        swish_http_authenticate:logged_in(Request, User), !.
  259
  260
  261
  262:- maplist(  ( [F] >> (ensure_loaded('config-enabled-swish'/F))),
  263  [auth_google,  auth_stackoverflow,  data,   email,  hdt,
  264          % rlimit,
  265            % r_serve,
  266              user_profile, % network,
  267   auth_unity,          debug,      logging,  notifications,
  268    rpc , auth_http]).
 swish
Start the SWISH server and open the main page in your browser.
  275swish :-
  276        swish('0.0.0.0':3020).
  277
  278swish(Port) :-
  279        http_server_property(Port, goal(logicmoo_swish:http_dispatch)), !,
  280        open_browser(Port).
  281swish(_:Port) :-
  282        integer(Port),
  283        http_server_property(Port, goal(_)), !,
  284        open_browser(Port).
  285swish(Port) :-
  286        http_server(logicmoo_swish:http_dispatch,
  287                    [ port(Port),
  288                      workers(16)
  289                    ]),
  290        open_browser(Port).
  291
  292open_browser(Port):- !, dmsg(open_browser(Port)).
  293open_browser(Address) :-
  294        host_port(Address, Host, Port),
  295        http_server_property(Port, scheme(Scheme)),
  296        http_absolute_location(root(.), Path, []),
  297        format(atom(URL), '~w://~w:~w~w', [Scheme, Host, Port, Path]),
  298        wdmsg(www_open_url(URL)).
  299
  300host_port(Host:Port, Host, Port) :- !.
  301host_port(Port,Host, Port):- gethostname(Host),!.
  302host_port(Port,_, Port):-!.
  303
  304:- [library(pengines)].  305
  306:- multifile pengines:allowed/2.  307:- dynamic pengines:allowed/2.  308
  309:- asserta((
  310  pengines:allowed(Request, Application) :-
  311  Application=swish-> true;
  312   currently_logged_in(pengines:allowed(Request, Application),_User))).  313
  314% Or Sure we trust that our sandbox is good enough
  315% pengines:allowed(_,_).
  316
  317:- listing(pengines:allowed/2).  318
  319
  320pet:- pengine_rpc("http://prologmoo.com:3020",
  321                       sin_table(X,Y),
  322                       [ src_text(':- dynamic(sin_table/2). sin_table(1,2).'),
  323                         application(swish)
  324                       ]),
  325   wdmsg(sin_table(X,Y)).
  326
  327
  328user:file_search_path(What, Alias):- % maybe confirm this is not SWISH?
  329     non_swish_file_search_path(What, Alias).
  330
  331
  332:- if(false).  333:- debug.  334
  335
  336:- listing(swish_config:authenticate/2).  337:- listing(pengines:allowed/2).  338
  339:- debug(authenticate).  340:- debug(chat(_)).  341:- debug(cm(change)).  342:- debug(dot).  343:- debug(html(script)).  344:- debug(http(authenticate)).  345:- debug(http(error)).  346:- debug(http(redirect)).  347:- debug(http(request)).  348:- debug(http_authenticate).  349:- debug(http_path).  350:- debug(http_session).  351:- debug(hub(_)).  352:- debug(login).  353% :- debug(modules).
  354:- debug(notify(_)).  355:- debug(notify).  356:- debug(openid(_)).  357:- debug(openid).  358:- debug(openid_fake(_)).  359:- debug(pack(mirror)).  360:- debug(pengine(abort)).  361:- debug(pldoc).  362:- debug(plweb).  363:- debug(predicate_options).  364:- debug(setting).  365:- debug(settings).  366:- debug(stats).  367:- debug(storage).  368:- debug(swish(search)).  369:- debug(websocket(_)).  370% :- prolog_ide(debug_monitor).
  371
  372
  373
  374% :- initialization(user:ensure_loaded(run_clio)).
  375
  376:- listing(swish_config:authenticate/2).  377
  378:- endif.  379
  380:- multifile
  381	cp_menu:menu_item/2,
  382	cp_menu:menu_popup_order/2.  383:- dynamic
  384	cp_menu:menu_item/2,
  385	cp_menu:menu_popup_order/2.  386
  387:- asserta(cp_menu:menu_item(400=places/swish,		'SWISH')).  388:- asserta(cp_menu:menu_popup_order(swish,       550)).  389:- asserta(cp_menu:menu_item(200=swish/swish,		'SWISH')).  390
  391%:- initialization(swish).
  392%:- swish.