Module: CARPS

Defined in:
lib/carps.rb,
lib/carps/test.rb,
lib/carps/mod/mod.rb,
lib/carps/ui/warn.rb,
lib/carps/mod/dice.rb,
lib/carps/mod/rule.rb,
lib/carps/ui/error.rb,
lib/carps/util/init.rb,
lib/carps/wizard/dm.rb,
lib/carps/crypt/peer.rb,
lib/carps/email/imap.rb,
lib/carps/email/smtp.rb,
lib/carps/mod/action.rb,
lib/carps/mod/dm/mod.rb,
lib/carps/mod/launch.rb,
lib/carps/util/error.rb,
lib/carps/util/files.rb,
lib/carps/mod/answers.rb,
lib/carps/mod/dm/room.rb,
lib/carps/service/mod.rb,
lib/carps/ui/question.rb,
lib/carps/util/config.rb,
lib/carps/util/editor.rb,
lib/carps/crypt/mailer.rb,
lib/carps/email/config.rb,
lib/carps/email/string.rb,
lib/carps/mod/question.rb,
lib/carps/service/game.rb,
lib/carps/ui/highlight.rb,
lib/carps/util/process.rb,
lib/carps/util/timeout.rb,
lib/carps/util/windows.rb,
lib/carps/wizard/steps.rb,
lib/carps/crypt/mailbox.rb,
lib/carps/mod/interface.rb,
lib/carps/wizard/player.rb,
lib/carps/mod/player/mod.rb,
lib/carps/mod/sheet/type.rb,
lib/carps/service/invite.rb,
lib/carps/crypt/handshake.rb,
lib/carps/mod/client_turn.rb,
lib/carps/mod/dm/reporter.rb,
lib/carps/mod/dm/resource.rb,
lib/carps/service/session.rb,
lib/carps/crypt/public_key.rb,
lib/carps/mod/dm/interface.rb,
lib/carps/mod/sheet/editor.rb,
lib/carps/mod/sheet/schema.rb,
lib/carps/protocol/keyword.rb,
lib/carps/protocol/message.rb,
lib/carps/service/dm/start.rb,
lib/carps/mod/status_report.rb,
lib/carps/service/dm/config.rb,
lib/carps/service/dm/mailer.rb,
lib/carps/service/interface.rb,
lib/carps/mod/sheet/verifier.rb,
lib/carps/mod/sheet/character.rb,
lib/carps/mod/sheet/new_sheet.rb,
lib/carps/service/dm/new_game.rb,
lib/carps/mod/player/interface.rb,
lib/carps/service/player/start.rb,
lib/carps/service/start/config.rb,
lib/carps/service/start/mailer.rb,
lib/carps/service/client_parser.rb,
lib/carps/service/player/config.rb,
lib/carps/service/player/mailer.rb,
lib/carps/service/server_parser.rb,
lib/carps/crypt/accept_handshake.rb,
lib/carps/crypt/default_messages.rb,
lib/carps/service/start/interface.rb

Overview

You should have received a copy of the GNU General Public License along with CARPS. If not, see <www.gnu.org/licenses/>.

Defined Under Namespace

Modules: ControlInterface, DM, Dice, Launcher, Player, Protocol, Setup, Sheet, Test, UI Classes: AcceptHandshake, Action, Answers, ClientTurn, Editor, EmailConfig, Expected, GameClient, GameServer, Handshake, IMAP, Interface, Invite, Mailbox, Mailer, Message, MessageParser, Mod, ModMailer, Peer, Process, PublicKey, Question, QuitInterface, Reporter, Resource, RolePlayInterface, Room, Rule, SMTP, SessionConfig, SessionManager, StartGameInterface, StatusReport, SystemConfig, UserConfig, YamlConfig

Constant Summary collapse

VERSION =
'0.3.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_dir(dir) ⇒ Object

Set the configuration directory



73
74
75
# File 'lib/carps/util/init.rb', line 73

def CARPS::config_dir dir
   $CONFIG = CARPS::root_config + dir + "/"
end

.enter_quit(status = 0) ⇒ Object

Press enter to quit



25
26
27
28
# File 'lib/carps/util/error.rb', line 25

def CARPS::enter_quit status=0
   UI::question "Press enter to quit."
   CARPS::shutdown_properly status 
end

.fatal(msg) ⇒ Object

Output an error message and quit with exit code 1



31
32
33
34
35
36
# File 'lib/carps/util/error.rb', line 31

def CARPS::fatal msg
   h = HighLine.new
   $stderr.write h.color("\nFATAL ERROR\n#{msg}\n", :error)
   puts "\a"
   exit 1
end

.init(safe = 1, dir = nil) ⇒ Object

Initialize carps

Optionally set the config_dir at the same time

Sets $SAFE to safe, default 1

FIXME: instead of setting a global variable, should use the singleton pattern



84
85
86
87
88
89
90
91
# File 'lib/carps/util/init.rb', line 84

def CARPS::init safe=1, dir=nil

   $SAFE = safe
   if dir
      config_dir dir
   end
   CARPS::init_threading
end

.init_threadingObject

Set up multi-threading

Can be called more than once



48
49
50
# File 'lib/carps/util/init.rb', line 48

def CARPS::init_threading
   Thread.abort_on_exception = true
end

.root_configObject

The root config directory

This performs an untaint operation



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/carps/util/init.rb', line 55

def CARPS::root_config
   loc = nil
   # If it's windows
   if CARPS::windows?
      loc = ENV["USERPROFILE"]
   else
      # Otherwise assume it's a unix-like system
      loc = ENV["HOME"]
   end
   if loc
      loc += "/carps/"
      loc.untaint
   else
      CARPS::fatal "Could not find home directory."
   end
end

.shutdown_properly(status) ⇒ Object

Kill all threads and exit with status



40
41
42
43
44
45
46
47
48
49
# File 'lib/carps/util/error.rb', line 40

def CARPS::shutdown_properly status
   # Stop all threads
   Thread.list.each do |thr|
      unless thr == Thread.main
         Thread.kill thr
         thr.join
      end
   end
   exit status
end

.timeout(n, description, &todo) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/carps/util/timeout.rb', line 23

def CARPS::timeout n, description, &todo
   begin
      Timeout::timeout n, &todo
   rescue Timeout::Error => e
      raise Timeout::Error, "'#{description}' timed out after #{n} seconds."
   end
end

.windows?Boolean

Is this windows?

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/carps/util/windows.rb', line 21

def CARPS::windows?
   if RUBY_PLATFORM.match(/(win|w)32/)
      true
   else
      false
   end
end

.with_crash_report(wait = false) ⇒ Object

Catch errors and print out a stack trace if they occur.

If wait is true, then wait for the user to press enter.

Pass a block.

Intented to be run at the top level (Eg by the binaries)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/carps/util/error.rb', line 58

def CARPS::with_crash_report wait=false
   begin
      yield
   rescue SystemExit => e
      CARPS::shutdown_properly e.status
   rescue Interrupt => e
      CARPS::shutdown_properly 1
   rescue Exception => e
      UI::put_error "CRASHED!\n#{e.class} reports:\n   #{e.message}\n\nStack trace:\n#{e.backtrace.join("\n")}", false
      if wait
         CARPS::enter_quit 1
      else
         CARPS::shutdown_properly 1
      end
   end
end

Instance Method Details

#clean_end(blob) ⇒ Object

Clean the end of an email

Strip the last end marker and any text after it



33
34
35
36
37
38
39
40
# File 'lib/carps/crypt/peer.rb', line 33

def clean_end blob
   rb = blob.reverse
   before, after = rb.split K.end.reverse, 2
   if after
      return after.reverse
   end
   nil
end

#default_messagesObject

Services will be built by extending upon these messages



25
26
27
# File 'lib/carps/crypt/default_messages.rb', line 25

def default_messages 
   [Handshake, AcceptHandshake, PublicKey]
end

#files(dir) ⇒ Object

Get the file names from inside a directory



21
22
23
24
25
26
27
28
29
# File 'lib/carps/util/files.rb', line 21

def files dir
   file_names = Dir.open(dir).entries.reject do |file|
      file.untaint
      file[0] == "." or File.ftype(dir + "/" + file) != "file" 
   end
   file_names.map do |fn|
      dir + "/" + fn
   end
end

#find(field, text) ⇒ Object

Find a field in semi-structured text



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/carps/protocol/keyword.rb', line 87

def find field, text
   Protocol::check text, "Expected String for input"
   if field.start_with? mark_prefix
      forget, blob = text.split field, 2
      Protocol::check blob, field
      return ["", blob]
   elsif field.start_with? prefix
      forget, blob = text.split field, 2
      Protocol::check blob, field
      value, blob = blob.split K.end, 2
      Protocol::check value, K.end
      return [value, blob] 
   else
      raise StandardError, "Invalid keyword"
   end
end

#from_mail(msg) ⇒ Object



26
27
28
# File 'lib/carps/email/string.rb', line 26

def from_mail msg
   Base64.decode64 msg    
end

#keyword_endObject

End the prefix, so there are no ambiguities



68
69
70
# File 'lib/carps/protocol/keyword.rb', line 68

def keyword_end
"\3"
end

#load_modsObject

Load the available mods

Performs untaint



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/carps/service/mod.rb', line 29

def load_mods
   mod_file = CARPS::root_config + "/mods.yaml"
   mods = {}
   begin
      mods = YAML.load(File.read mod_file)
   rescue
      UI::warn "Cannot find mods: could not read #{mod_file}"
   end
   mods.each_value do |mod|
      mod.untaint
   end
   mods
end

#mark_prefixObject

CARP protocol markers are associated with ASCII start of heading



63
64
65
# File 'lib/carps/protocol/keyword.rb', line 63

def mark_prefix
"\1"
end

#prefixObject

CARP protocol keywords associated with values are prefixed by ASCII start of text.



58
59
60
# File 'lib/carps/protocol/keyword.rb', line 58

def prefix
"\2"
end

#security_info(blob) ⇒ Object

Fetch security information from an email



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/carps/crypt/peer.rb', line 43

def security_info blob
   blob = clean_end blob
   sig = nil   
   begin
      sig, blob = find K.sig, blob
   rescue
      UI::warn "Message signature was malformed", blob
      return nil
   end
   # If the digest is the hash of the message and the signature matches the digest then all is well
   dig = Digest::MD5.digest blob
   [[sig, dig], blob]
end

#to_mail(msg) ⇒ Object



22
23
24
# File 'lib/carps/email/string.rb', line 22

def to_mail msg
   Base64.encode64 msg 
end

#write_file_in(dir, contents) ⇒ Object

Write contents into a new file in a directory with an arbitrary, unique name

Returns the path



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/carps/util/files.rb', line 34

def write_file_in dir, contents
   t = Time.now
   valid_path = false
   path = $CONFIG + dir + "/" + t.to_f.to_s.gsub(/(\.|@, \?!#'"~\(\))/, "")
   until valid_path
      path += "_"
      valid_path = not(File.exists?(path))
   end
   file = File.new path, "w"
   file.write contents
   file.close
   path
end