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