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