db.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package BotDb;
  2. use common::sense;
  3. use DBI;
  4. our $db;
  5. my $table_schemata = {
  6. # upgrade from 0 to 1: initial structure
  7. 0 => [
  8. "CREATE TABLE users (username TEXT PRIMARY KEY NOT NULL,
  9. displayname TEXT,
  10. priv TEXT NOT NULL DEFAULT '')",
  11. "CREATE TABLE plugins (name TEXT PRIMARY KEY NOT NULL,
  12. version INT NOT NULL DEFAULT 0)",
  13. "CREATE TABLE settings (name TEXT PRIMARY KEY NOT NULL,
  14. value TEXT NOT NULL)",
  15. ],
  16. };
  17. my @connect_pragmas = (
  18. "journal_mode = TRUNCATE", "locking_mode = EXCLUSIVE", "synchronous = OFF",
  19. );
  20. my %privs = ();
  21. sub init() {
  22. $db = DBI->connect("dbi:SQLite:dbname=$BotIrc::config->{db_file}", "", "");
  23. update_schema();
  24. }
  25. sub update_schema($$$$) {
  26. my ($plugin, $schemata, $error, $info) = @_;
  27. $error //= \&BotIrc::error;
  28. $info //= \&BotIrc::info;
  29. my $version;
  30. if (!defined $plugin) {
  31. $version = $db->selectrow_array("PRAGMA user_version");
  32. $schemata = $table_schemata;
  33. } else {
  34. die("Code error: called update_schema with plugin $plugin but without schemata") if (!$schemata);
  35. $version = $db->selectrow_array("SELECT version FROM plugins WHERE name = ?", {}, $plugin);
  36. }
  37. $plugin //= 'core';
  38. SCHEMATA: while (exists $schemata->{$version}) {
  39. $info->("Upgrading database ($plugin) from v$version to v".($version+1).".");
  40. $db->begin_work;
  41. for my $s (@{$schemata->{$version}}) {
  42. if (!defined $db->do($s)) {
  43. $db->rollback;
  44. $error->("Error upgrading database ($plugin): ". $db->errstr);
  45. $error->("Query was: $s");
  46. return undef;
  47. }
  48. }
  49. $version++;
  50. if ($plugin eq 'core') {
  51. $db->do("PRAGMA user_version=$version");
  52. } else {
  53. $db->do("UPDATE plugins SET version = ? WHERE name = ?", {}, $version, $plugin);
  54. }
  55. $db->commit;
  56. }
  57. return 1;
  58. }
  59. # USER ACTIONS {{{ ###########################################################
  60. sub _fetch_privs($) {
  61. my $nick = lc(shift);
  62. return 1 if (exists $privs{$nick});
  63. my $priv = $db->selectrow_array("SELECT priv FROM users WHERE username = ?", {}, $nick);
  64. if (!defined $priv) {
  65. if (defined $db->err) {
  66. BotIrc::error("Failed fetching privs for $nick: ".$db->errstr);
  67. return undef;
  68. }
  69. return 0;
  70. }
  71. my %p = ();
  72. foreach my $p (split(/,/, $priv)) {
  73. $p{$p} = 1;
  74. }
  75. $privs{$nick} = \%p;
  76. 1;
  77. }
  78. sub _store_privs($) {
  79. my $nick = lc(shift);
  80. $db->do("UPDATE users SET priv=? WHERE username=?", {}, join(',', keys %{$privs{$nick}}), $nick);
  81. }
  82. sub is_superadmin($) {
  83. lc(shift) eq lc($BotIrc::config->{superadmin});
  84. }
  85. sub has_priv($$) {
  86. my ($nick, $priv) = map(lc, @_);
  87. # !control/!guest: hacks for control connections
  88. if (is_superadmin($nick) || $nick eq '!control') {
  89. return 0 if ($priv =~ /^no_/);
  90. return 1;
  91. }
  92. return 1 if ($priv =~ /^no_/ && $nick eq '!guest');
  93. return 0 if ($nick eq '!guest');
  94. return 0 if (!_fetch_privs($nick));
  95. return exists($privs{$nick}{$priv});
  96. }
  97. sub add_priv($$) {
  98. my ($nick, $priv) = map(lc, @_);
  99. my $fp = _fetch_privs($nick);
  100. return 0 if (!defined $fp);
  101. add_user($nick) if (!$fp);
  102. $privs{$nick}{$priv} = 1;
  103. _store_privs($nick);
  104. }
  105. sub del_priv($$) {
  106. my ($nick, $priv) = map(lc, @_);
  107. $privs{$nick} //= {};
  108. delete $privs{$nick}{$priv};
  109. _store_privs($nick);
  110. }
  111. sub add_user($) {
  112. my $nick = lc shift;
  113. $privs{$nick} = {};
  114. $db->do("INSERT INTO users (username) VALUES(?)", {}, $nick);
  115. }
  116. sub del_user($) {
  117. my $nick = lc shift;
  118. delete $privs{$nick};
  119. $db->do("DELETE FROM users WHERE username = ?", {}, $nick);
  120. }
  121. # }}} ########################################################################
  122. 1;