]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/DBCommon.pl
6176d6115545667c6cf5399b7498f6f9e1d096dc
[infobot.git] / src / Factoids / DBCommon.pl
1 #
2 #  DBStubs.pl: DB independent (I hope, heh) factoid support
3 #      Author: dms
4 #     Version: v0.6d (20000223)
5 #     Created: 19991020
6 #
7
8 # use strict;   # TODO
9
10 #####
11 # Usage: &setFactInfo($faqtoid, $key, $val);
12 sub setFactInfo {
13     &sqlSet( 'factoids', { factoid_key => $_[0] }, { $_[1] => $_[2] } );
14 }
15
16 #####
17 # Usage: &getFactInfo($faqtoid, [$what]);
18 sub getFactInfo {
19     return &sqlSelect( 'factoids', $_[1], { factoid_key => $_[0] } );
20 }
21
22 #####
23 # Usage: &getFactoid($faqtoid);
24 sub getFactoid {
25     my $val = getFactInfo( $_[0], 'factoid_value' );
26     DEBUG("getFactoid: $val is_utf8:".is_utf8($val));
27     return encode_utf8($val) if is_utf8($val);
28     return $val;
29 }
30
31 #####
32 # Usage: &delFactoid($faqtoid);
33 sub delFactoid {
34     my ($faqtoid) = @_;
35
36     &sqlDelete( 'factoids', { factoid_key => $faqtoid } );
37     &status("DELETED $faqtoid");
38
39     return 1;
40 }
41
42 #####
43 # Usage: &IsLocked($faqtoid);
44 sub IsLocked {
45     my ($faqtoid) = @_;
46     my $thisnuh = &getFactInfo( $faqtoid, 'locked_by' );
47
48     if ( defined $thisnuh and $thisnuh ne '' ) {
49         if ( !&IsHostMatch($thisnuh) and &IsFlag('o') ne 'o' ) {
50             &performReply("cannot alter locked factoids");
51             return 1;
52         }
53     }
54
55     return 0;
56 }
57
58 #####
59 # Usage: &AddModified($faqtoid,$nuh);
60 sub AddModified {
61     my ( $faqtoid, $nuh ) = @_;
62     my $modified_by = &getFactInfo( $faqtoid, 'modified_by' );
63     my ( @modifiedlist, @modified, %modified );
64
65     if ( defined $modified_by ) {
66         push( @modifiedlist, split( /\,/, $modified_by ) );
67     }
68     push( @modifiedlist, $nuh );
69
70     foreach ( reverse @modifiedlist ) {
71         /^(\S+)!(\S+)@(\S+)$/;
72         my $nick = lc $1;
73         next if ( exists $modified{$nick} );
74
75         $modified{$nick} = $_;
76         push( @modified, $nick );
77     }
78
79     undef @modifiedlist;
80
81     foreach ( reverse @modified ) {
82         push( @modifiedlist, $modified{$_} );
83     }
84     shift(@modifiedlist) while ( scalar @modifiedlist > 3 );
85
86     &setFactInfo( $faqtoid, 'modified_by', join( ",", @modifiedlist ) );
87     &setFactInfo( $faqtoid, 'modified_time', time() );
88
89     return 1;
90 }
91
92 #####
93 ### Commands which use the fundamental functions... Helpers?
94 #####
95
96 #####
97 # Usage: &CmdLock($function,$faqtoid);
98 sub CmdLock {
99     my ($faqtoid) = @_;
100
101     my $thisnuh = &getFactInfo( $faqtoid, 'locked_by' );
102
103     if ( defined $thisnuh and $thisnuh ne '' ) {
104         my $locked_by = ( split( /\!/, $thisnuh ) )[0];
105         &msg( $who,
106             "factoid \002$faqtoid\002 has already been locked by $locked_by." );
107         return 0;
108     }
109
110     $thisnuh ||= &getFactInfo( $faqtoid, 'created_by' );
111
112     # fixes bug found on 19991103.
113     # code needs to be reorganised though.
114     if ( $thisnuh ne '' ) {
115         if ( !&IsHostMatch($thisnuh) && IsFlag('o') ne 'o' ) {
116             &msg( $who, "sorry, you are not allowed to lock '$faqtoid'." );
117             return 0;
118         }
119     }
120
121     &performReply("locking factoid \002$faqtoid\002");
122     &setFactInfo( $faqtoid, 'locked_by',   $nuh );
123     &setFactInfo( $faqtoid, 'locked_time', time() );
124
125     return 1;
126 }
127
128 #####
129 # Usage: &CmdUnLock($faqtoid);
130 sub CmdUnLock {
131     my ($faqtoid) = @_;
132
133     my $thisnuh = &getFactInfo( $faqtoid, 'locked_by' );
134
135     if ( !defined $thisnuh ) {
136         &msg( $who, "factoid \002$faqtoid\002 is not locked." );
137         return 0;
138     }
139
140     if ( $thisnuh ne '' and !&IsHostMatch($thisnuh) and &IsFlag('o') ne 'o' ) {
141         &msg( $who,
142             "sorry, you are not allowed to unlock factoid '$faqtoid'." );
143         return 0;
144     }
145
146     &performReply("unlocking factoid \002$faqtoid\002");
147     &setFactInfo( $faqtoid, 'locked_by',   '' );
148     &setFactInfo( $faqtoid, 'locked_time', '0' )
149       ;    # pgsql complains if NOT NULL set. So set 0 which is the default
150
151     return 1;
152 }
153
154 1;
155
156 # vim:ts=4:sw=4:expandtab:tw=80