]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/OnJoin.pl
* Merge back with trunk to r1810
[infobot.git] / src / Modules / OnJoin.pl
1 #!/usr/bin/perl
2 #
3 # OnJoin.pl: emit a message when a user enters the channel
4 #    Author: Corey Edwards <tensai@zmonkey.org>
5 #   Version: v0.3.1
6 #   Created: 20051222
7 #   Updated: 20060112
8
9 use strict;
10
11 use vars qw(%channels %param);
12 use vars qw($dbh $who $chan);
13
14 sub onjoin {
15     my ( $nick, $user, $host, $chan ) = @_;
16     $nick = lc $nick;
17
18     # look for a channel specific message
19     my $message =
20       &sqlSelect( 'onjoin', 'message', { nick => $nick, channel => $chan } )
21       || 0;
22
23     # look for a default message
24     if ( !$message ) {
25         $message =
26           &sqlSelect( 'onjoin', 'message',
27             { nick => $nick, channel => '_default' } )
28           || 0;
29     }
30
31     # print the message, if there was one
32     if ($message) {
33         $message = substVars( $message, 1 );
34         if ( $message =~ m/^<action>\s*(.*)/ ) {
35             &status("OnJoin: $nick arrived, performing action");
36             &action( $chan, $1 );
37         }
38         else {
39             $message =~ s/^<reply>\s*//;
40             &status("OnJoin: $nick arrived, printing message");
41             &msg( $chan, $message );
42         }
43     }
44
45     return;
46 }
47
48 # set and get messages
49 sub Cmdonjoin {
50     $_ = shift;
51     m/(\S*)(\s*(\S*)(\s*(.*)|)|)/;
52     my $ch   = $1;
53     my $nick = $3;
54     my $msg  = $5;
55
56     # get options
57     my $strict = &getChanConf('onjoinStrict');
58     my $ops    = &getChanConf('onjoinOpsOnly');
59
60     # see if they specified a channel
61     if ( $ch !~ m/^\#/ && $ch ne '_default' ) {
62         $msg  = $nick . ( $msg ? " $msg" : '' );
63         $nick = $ch;
64         $ch   = $chan;
65     }
66
67     $nick = lc $nick;
68
69     if ( $nick =~ m/^-(.*)/ ) {
70         $nick = $1;
71         if ($ops) {
72             if ( !$channels{$chan}{o}{$who} ) {
73                 &performReply("sorry, you're not an operator");
74             }
75         }
76         elsif ($strict) {
77
78             # regardless of strict mode, ops can always change
79             if ( !$channels{$chan}{o}{$who} and $nick ne $who ) {
80                 &performReply(
81                     "I can't alter a message for another user (strict mode)");
82             }
83         }
84         else {
85             &sqlDelete( 'onjoin', { nick => $nick, channel => $ch } );
86             &performReply('ok');
87         }
88         return;
89     }
90
91     # if msg not set, show what the message would be
92     if ( !$msg ) {
93         $nick = $who if ( !$nick );
94         my %row = &sqlSelectRowHash(
95             'onjoin',
96             'message, modified_by, modified_time',
97             { nick => $nick, channel => $ch }
98         );
99         if ( $row{'message'} ) {
100             &performStrictReply( "onjoin for $nick set by $row{modified_by} on "
101                   . localtime( $row{modified_time} )
102                   . ": $row{message}" );
103         }
104         return;
105     }
106
107     # only allow changes by ops
108     if ($ops) {
109         if ( !$channels{$chan}{o}{$who} ) {
110             &performReply("sorry, you're not an operator");
111             return;
112         }
113     }
114
115     # only allow people to change their own message (superceded by OpsOnly)
116     elsif ($strict) {
117
118         # regardless of strict mode, ops can always change
119         if ( !$channels{$chan}{o}{$who} and $nick ne $who ) {
120             &performReply(
121                 "I can't alter a message for another user (strict mode)");
122             return;
123         }
124     }
125
126     # remove old one (if exists) and add new message
127     &sqlDelete( 'onjoin', { nick => $nick, channel => $ch } );
128     my $insert = &sqlInsert(
129         'onjoin',
130         {
131             nick          => $nick,
132             channel       => $ch,
133             message       => $msg,
134             modified_by   => $who,
135             modified_time => time()
136         }
137     );
138     if ($insert) {
139         &performReply('ok');
140     }
141     else {
142         &performReply('whoops. database error');
143     }
144     return;
145 }
146
147 1;
148
149 # vim:ts=4:sw=4:expandtab:tw=80