ID:195142
 
//Title: Inter-World Communication
//Credit to: PirateHead (cleaned up a bit by Jtgibson)
//Contributed by: Jtgibson


//This is a nifty tool designed by PirateHead. It allows you to pass
// commands through world/Export() from an external source. Every "topic_action"
// you define will be called automatically if the URL contains that topic_action's
// name as its "action" parameter.
//For example, if my world was running on the IP 206.126.169.69, and I used
// world.Export("byond://206.126.169.69?logins") from another world which had
// world.key properly set, the target world would bounce back a signal describing
// the number of people in that world. This transparently loads up a
// /topic_action/logins object, then runs its proc to produce output.

//Here is a list of the default actions this file includes:
//Hard-coded:
// comm-mode (returns the version of BYOND that the world was compiled for)
// ping (returns an instantaneous 1)
// cpu (returns the current drain on the CPU of the target world)
//Soft-coded:
// logins (returns the number of clients connected to the target world)
// who (returns a list of the keys connected)

//To define your own actions, create a new object type under topic_action,
// and return a value from its Action() proc.


#define validation_key null

topic_action
proc/Action()
//Do nothing by default

world.Topic(href,addr,master,key)
if(!master)
//If the remote world isn't running on the same server, and
// the key doesn't match the validation key, then we return no secrets.
if(key != validation_key) return null

//handle simple cases
switch(href)
if("comm-mode") return world.version
if("ping") return 1
if("cpu") return world.cpu

//handle other cases
var/list/href_list = params2list(href)
var/topic_action/action
var/action_name
var/argument

if(href_list.Find("action")) action_name = lowertext(href_list["action"])
else action_name = href

for(var/i in typesof(/topic_action) - /topic_action)
if("[i]" == "/topic_action/[action_name]")
action = new i
break
if(!action)
//No topic_actions were matched -- return null, as a fallback.
return null

if(href_list.Find("arg")) argument = href_list["arg"]
else
var/import = world.Import()
if(import) argument = new import
else argument = null

return action.Action(argument)


topic_action
logins
Action()
var/retn = 0
for(var/client/C in world) retn++
return retn
who
Action()
var/list/clients = list()
for(var/client/C in world) clients.Add(C.key)
return clients