| 1 | package VUser::SquirrelMail::Prefs::SQL; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # Copyright 2007 Randy Smith <perlstalker@vuser.org> | ||
| 6 | # $Id: SQL.pm,v 1.2 2007/04/10 21:17:22 perlstalker Exp $ | ||
| 7 | |||
| 8 | use vars qw(@ISA); | ||
| 9 | |||
| 10 | our $REVISION = (split (' ', '$Revision: 1.2 $'))[1]; | ||
| 11 | our $VERSION = "0.1.1"; | ||
| 12 | |||
| 13 | use VUser::Meta; | ||
| 14 | use VUser::ResultSet; | ||
| 15 | use VUser::ExtLib qw(:config); | ||
| 16 | use VUser::ExtLib::SQL; | ||
| 17 | use VUser::Log qw(:levels); | ||
| 18 | use VUser::Extension; | ||
| 19 | push @ISA, 'VUser::Extension'; | ||
| 20 | |||
| 21 | my $csec = 'Extension SquirrelMail::Prefs::SQL'; # config section | ||
| 22 | |||
| 23 | my $extlib = undef; | ||
| 24 | my $log; | ||
| 25 | |||
| 26 | our %meta; | ||
| 27 | |||
| 28 | sub depends { qw(SquirrelMail::Prefs); } | ||
| 29 | |||
| 30 | sub init | ||
| 31 | { | ||
| 32 | my $eh = shift; | ||
| 33 | my %cfg = @_; | ||
| 34 | |||
| 35 | %meta = VUser::SquirrelMail::Prefs::meta(); | ||
| 36 | |||
| 37 | if ( defined $main::log ) { | ||
| 38 | $log = $main::log; | ||
| 39 | } else { | ||
| 40 | $log = VUser::Log->new( \%cfg, 'vuser' ); | ||
| 41 | } | ||
| 42 | |||
| 43 | my $dsn = strip_ws($cfg{$csec}{dsn}); | ||
| 44 | |||
| 45 | if (not defined $dsn) { | ||
| 46 | |||
| 47 | my $db_type = VUser::ExtLib::strip_ws($cfg{$csec}{'db type'}); | ||
| 48 | $dsn = "dbi:$db_type:"; | ||
| 49 | if ($db_type and $db_type ne 'none') { | ||
| 50 | |||
| 51 | my $db_name = VUser::ExtLib::strip_ws($cfg{$csec}{'db name'}); | ||
| 52 | my $db_host = VUser::ExtLib::strip_ws($cfg{$csec}{'db host'}); | ||
| 53 | $dsn .= "$db_host:"; | ||
| 54 | if ($db_type eq 'mysql') { | ||
| 55 | $dsn .= "database=$db_name"; | ||
| 56 | $dsn .= ";host=$db_host" if $db_host; | ||
| 57 | } else { | ||
| 58 | # Semi-reasonable default if we don't know what the DB is. | ||
| 59 | $dsn .= "$db_name"; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | my $user = VUser::ExtLib::strip_ws($cfg{$csec}{'db user'}); | ||
| 65 | my $pass = VUser::ExtLib::strip_ws($cfg{$csec}{'db password'}); | ||
| 66 | |||
| 67 | $extlib = VUser::ExtLib::SQL->new(\%cfg, | ||
| 68 | {'dsn' => $dsn, | ||
| 69 | 'user' => $user, | ||
| 70 | 'password' => $pass, | ||
| 71 | 'macros' => { 'u' => 'username', | ||
| 72 | 'o' => 'option', | ||
| 73 | 'v' => 'value' | ||
| 74 | } | ||
| 75 | }); | ||
| 76 | |||
| 77 | # smprefs|add | ||
| 78 | $eh->register_task('smprefs', 'add', \&do_sql); | ||
| 79 | |||
| 80 | # smprefs|mod | ||
| 81 | $eh->register_task('smprefs', 'mod', \&do_sql); | ||
| 82 | |||
| 83 | # smprefs|del | ||
| 84 | $eh->register_task('smprefs', 'del', \&do_sql); | ||
| 85 | |||
| 86 | # smprefs|show | ||
| 87 | $eh->register_task('smprefs', 'show', \&smprefs_show); | ||
| 88 | |||
| 89 | # smprefs|delall | ||
| 90 | $eh->register_task('smprefs', 'delall', \&do_sql); | ||
| 91 | |||
| 92 | # email|del | ||
| 93 | $eh->register_task('email', 'del', \&email_del); | ||
| 94 | } | ||
| 95 | |||
| 96 | sub unload {}; | ||
| 97 | |||
| 98 | sub do_sql { | ||
| 99 | my ( $cfg, $opts, $action, $eh ) = @_; | ||
| 100 | |||
| 101 | my $query; | ||
| 102 | |||
| 103 | if ( $action eq 'add' ) { | ||
| 104 | $query = 'addpref_query'; | ||
| 105 | } elsif ( $action eq 'del' ) { | ||
| 106 | $query = 'delpref_query'; | ||
| 107 | } elsif ( $action eq 'mod' ) { | ||
| 108 | $query = 'modpref_query'; | ||
| 109 | } elsif ( $action eq 'delall' ) { | ||
| 110 | $query = 'delallpref_query'; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $sql = strip_ws( $cfg->{$csec}{$query} ); | ||
| 114 | |||
| 115 | if (not $sql) { | ||
| 116 | $log->log(LOG_WARN, "No query specified for $query. Skipping."); | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | |||
| 120 | my $sth = $extlib->execute( $opts, $sql ); | ||
| 121 | $sth->finish; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub smprefs_show | ||
| 125 | { | ||
| 126 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 127 | |||
| 128 | my $query; | ||
| 129 | if ($opts->{'username'} and $opts->{'option'}) { | ||
| 130 | $query = 'showuseropt_query'; | ||
| 131 | } elsif ($opts->{'username'}) { | ||
| 132 | $query = 'showuser_query'; | ||
| 133 | } elsif ($opts->{'option'}) { | ||
| 134 | $query = 'showopt_query'; | ||
| 135 | } else { | ||
| 136 | $query = 'showall_query'; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $sql = strip_ws($cfg->{$csec}{$query}); | ||
| 140 | |||
| 141 | if (not $sql) { | ||
| 142 | $log->log(LOG_WARN, "No query specified for $query. Skipping"); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | my $rs = VUser::ResultSet->new(); | ||
| 147 | $rs->add_meta($meta{'username'}); | ||
| 148 | $rs->add_meta($meta{'option'}); | ||
| 149 | $rs->add_meta($meta{'value'}); | ||
| 150 | |||
| 151 | my $sth = $extlib->execute($opts, $sql); | ||
| 152 | my @res; | ||
| 153 | while (@res = $sth->fetchrow_array()) { | ||
| 154 | $rs->add_data([ @res[0..2] ]); | ||
| 155 | } | ||
| 156 | $sth->finish; | ||
| 157 | |||
| 158 | return $rs; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub email_del | ||
| 162 | { | ||
| 163 | my ($cfg, $opts, $action, $eh) = @_; | ||
| 164 | |||
| 165 | my $query = 'delallpref_query'; | ||
| 166 | my $sql = strip_ws( $cfg->{$csec}{$query} ); | ||
| 167 | |||
| 168 | if (not $sql) { | ||
| 169 | $log->log(LOG_WARN, "No query specified for $query. Skipping."); | ||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | my $sth = $extlib->execute( $opts, $sql ); | ||
| 174 | $sth->finish; | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | __END__ |