]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/OnJoin.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[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 = &sqlSelect('onjoin', 'message', { nick => $nick, channel => $chan } ) || 0;
20
21         # look for a default message
22         if (!$message){
23                 $message = &sqlSelect('onjoin', 'message', { nick => $nick, channel => '_default' } ) || 0;
24         }
25
26         # print the message, if there was one
27         if ($message){
28                 $message = substVars($message, 1);
29                 if ($message =~ m/^<action>\s*(.*)/){
30                         &status("OnJoin: $nick arrived, performing action");
31                         &action($chan, $1);
32                 }
33                 else{
34                         $message =~ s/^<reply>\s*//;
35                         &status("OnJoin: $nick arrived, printing message");
36                         &msg($chan, $message);
37                 }
38         }
39
40         return;
41 }
42
43 # set and get messages
44 sub Cmdonjoin {
45         $_ = shift;
46         m/(\S*)(\s*(\S*)(\s*(.*)|)|)/;
47         my $ch = $1;
48         my $nick = $3;
49         my $msg = $5;
50
51         # get options
52         my $strict = &getChanConf('onjoinStrict');
53         my $ops = &getChanConf('onjoinOpsOnly');
54
55         # see if they specified a channel
56         if ($ch !~ m/^\#/ && $ch ne '_default'){
57                 $msg = $nick . ($msg ? " $msg" : '');
58                 $nick = $ch;
59                 $ch = $chan;
60         }
61
62         $nick = lc $nick;
63
64         if ($nick =~ m/^-(.*)/){
65                 $nick = $1;
66                 if ($ops){
67                         if (!$channels{$chan}{o}{$who}){
68                                 &performReply("sorry, you're not an operator");
69                         }
70                 }
71                 elsif ($strict){
72                         # regardless of strict mode, ops can always change
73                         if (!$channels{$chan}{o}{$who} and $nick ne $who){
74                                 &performReply("I can't alter a message for another user (strict mode)");
75                         }
76                 }
77                 else{
78                         &sqlDelete('onjoin', { nick => $nick, channel => $ch });
79                         &performReply('ok');
80                 }
81                 return;
82         }
83
84         # if msg not set, show what the message would be
85         if (!$msg){
86                 $nick = $who if (!$nick);
87                 my %row = &sqlSelectRowHash('onjoin', 'message, modified_by, modified_time', { nick => $nick, channel => $ch } );
88                 if ($row{'message'}){
89                         &performStrictReply("onjoin for $nick set by $row{modified_by} on " . localtime($row{modified_time}) . ": $row{message}");
90                 }
91                 return;
92         }
93
94         # only allow changes by ops
95         if ($ops){
96                 if (!$channels{$chan}{o}{$who}){
97                         &performReply("sorry, you're not an operator");
98                         return;
99                 }
100         }
101         # only allow people to change their own message (superceded by OpsOnly)
102         elsif ($strict){
103                 # regardless of strict mode, ops can always change
104                 if (!$channels{$chan}{o}{$who} and $nick ne $who){
105                         &performReply("I can't alter a message for another user (strict mode)");
106                         return;
107                 }
108         }
109
110         # remove old one (if exists) and add new message
111         &sqlDelete('onjoin', { nick => $nick, channel => $ch });
112         my $insert = &sqlInsert('onjoin', { nick => $nick, channel => $ch, message => $msg, modified_by => $who, modified_time => time() });
113         if ($insert){
114                 &performReply('ok');
115         }
116         else{
117                 &performReply('whoops. database error');
118         }
119         return;
120 }
121
122 1;
123
124 # vim:ts=4:sw=4:expandtab:tw=80