aboutsummaryrefslogtreecommitdiff
path: root/lib/MasterServer/Database/Pg/dbCiphers.pm
blob: e2e8191dac17385a93123542812785b6fe288466 (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

package MasterServer::Database::Pg::dbCiphers;

use strict;
use warnings;
use Exporter 'import';

our @EXPORT = qw| clear_ciphers 
                  insert_cipher 
                  get_cipher 
                  get_default_port |;

################################################################################
## Clear all existing ciphers from the database
################################################################################
sub clear_ciphers {
  my $self = shift;

  # delete ALL entries
  my $u = $self->{dbh}->do("DELETE FROM games");
  
  # notify 
  $self->log("delete", "Removed all ciphers") if ($u > 0);

  # removed from games
  return 2 if ($u > 0);

  # or else report notice
  $self->log("notice", "No ciphers deleted!");
  return -1;

}

################################################################################
## Insert the list of supported games and their ciphers / default ports / 
## descriptions included from the data/supportedgames.pl file.
################################################################################
sub insert_cipher {
  my ($self, %opt) = @_;
  
  # insert a single cipher/key combo
  my $u = $self->{dbh}->do(
     "INSERT INTO games (
          gamename, 
          cipher, 
          description, 
          default_qport) 
      VALUES(?, ?, ?, ?)", undef, 
      $opt{gamename}, $opt{cipher}, $opt{description}, $opt{default_qport});
  
  # notify
  $self->log("add", "Added cipher for $opt{gamename}") if ($u and $u > 0);
  return 1 if ($u and $u > 0);
  
  # or else report error
  $self->log("error", "An error occurred adding a cipher for $opt{gamename}");
  return -1;
  
}


################################################################################
## get the cipher that goes with gamename
################################################################################
sub get_cipher {
  my ($self, $gn) = @_;

  # no gamename specified? "undef" is not a known cipher, so send that instead.  
  return 'undef' if !$gn;
  
  # get cipher from db if gamename exists
  my $cipher = $self->{dbh}->selectall_arrayref(
    'SELECT cipher FROM games WHERE gamename = ?', undef, 
    lc $gn)->[0]->[0];
  
  # return a non-zero-length string
  return ($cipher ? $cipher : 'undef');
}

################################################################################
## get the default query port that goes with gamename
################################################################################
sub get_default_port {
  my ($self, $gn) = @_;

  # no gamename specified? default port is 0
  return 0 if !$gn;
  
  # get port from db if gamename exists
  my $p = $self->{dbh}->selectall_arrayref(
    'SELECT default_qport FROM games WHERE gamename = ?', undef, 
    lc $gn)->[0]->[0];
  
  # return port or zero
  return $p || 0;
}

1;