Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 167 lines (147 sloc) 5.484 kB
8a11956 @nhandler Add nmbot.pl
authored
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use vars qw($VERSION %IRSSI);
6 require LWP::UserAgent;
7 use JSON;
8 use Data::Dumper;
9
10 use Irssi;
11 $VERSION = '0.01';
12 %IRSSI = (
13 authors => 'Nathan Handler',
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
14 contact => 'nhandler@debian.org',
8a11956 @nhandler Add nmbot.pl
authored
15 name => 'NMBot',
16 description => 'NMBot updates the /topic and displays notifications related to the Debian NM Process',
17 license => 'GPLv3+',
18 );
19
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
20 my $debug = 1; # Set to a true value (1) to print extra information within irssi
8a11956 @nhandler Add nmbot.pl
authored
21
22 my %roles = (
23 'mm' => 'None',
24 'mm_ga' => 'None, with guest account',
25 'dm' => 'Debian Maintainer',
26 'dm_ga' => 'Debian Maintainer, with guest account',
27 'dd_u' => 'Debian Developer, uploading',
28 'dd_nu' => 'Debian Developer, non-uploading',
29 'dd_e' => 'Debian Developer, emeritus',
30 'dm_e' => 'Debian Maintainer, emeritus',
31 'dd_r' => 'Debian Developer, removed',
32 'dm_r' => 'Debian Maintainer, removed',
33 );
34
35 my %stages = (
36 'app_new' => 'Applicant asked to enter the process',
37 'app_rcvd' => 'Applicant replied to initial mail',
38 'app_hold' => 'On hold before entering the queue',
39 'adv_rcvd' => 'Received enough advocacies',
40 'app_ok' => 'Advocacies have been approved',
41 'am_rcvd' => 'Waiting for AM to confirm',
42 'am' => 'Interacting with an AM',
43 'am_hold' => 'AM put application on hold',
44 'am_ok' => 'AM approved the applicant',
45 'fd_hold' => 'FD puts application on hold',
46 'fd_ok' => 'FD approved the AM report',
47 'dam_hold' => 'DAM puts application on hold',
48 'dam_ok' => 'DAM accepted the applicant',
49 'done' => 'Process has completed successfully (account was created)',
50 'cancelled' => 'Process has been canceled',
51 );
52
53 Irssi::settings_add_str('nmbot', 'nmbot_json', 'https://nm.debian.org/public/stats/latest?days=30&json=true');
54 Irssi::settings_add_str('nmbot', 'nmbot_channel', '#debian-newmaint');
55 Irssi::settings_add_str('nmbot', 'nmbot_last_announce', '0');
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
56 Irssi::settings_add_str('nmbot', 'nmbot_topic_prefix', 'New nm.d.o up \o/ | NM := New Member | For packaging/sponsor questions, please join #debian-mentors | Queues: ');
57
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
58 Irssi::timeout_add(60000, 'main',''); #Run main() every 1 minute
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
59
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
60 main();
8a11956 @nhandler Add nmbot.pl
authored
61
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
62 # Downloads the JSON feed from nm.debian.org containing all of the data
63 # Parameters: none
64 # Returns the JSON file or nothing (if error)
8a11956 @nhandler Add nmbot.pl
authored
65 sub getJson {
66 my $url = Irssi::settings_get_str('nmbot_json');
67 my $ua = LWP::UserAgent->new;
68 $ua->timeout(10);
69 $ua->env_proxy;
70 my $response = $ua->get("$url");
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
71 if ($response->is_success) { # If we sucessfully downloaded the file...
72 return decode_json($response->decoded_content); # Return the JSON decoded into a hash
8a11956 @nhandler Add nmbot.pl
authored
73 }
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
74 else { # If we failed to download the file...
75 Irssi::print("Failed to get $url: " . $response->status_line); # Display an error message
8a11956 @nhandler Add nmbot.pl
authored
76 }
77 }
78
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
79 # Parameter: $topic : A string that will be set as the new IRC channel topic
80 # Returns nothing, just changes the channel topic
8a11956 @nhandler Add nmbot.pl
authored
81 sub setTopic {
82 my($topic) = @_;
83
84 my($server) = Irssi::server_find_tag('oftc');
85 my($channel) = Irssi::settings_get_str('nmbot_channel');
86 $server->send_raw("TOPIC $channel :$topic");
87 }
88
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
89 # Get and return the IRC channel topic
90 # Takes no parameters
8a11956 @nhandler Add nmbot.pl
authored
91 sub getTopic {
92 my($server) = Irssi::server_find_tag('oftc');
93 my($channel) = $server->channel_find(Irssi::settings_get_str('nmbot_channel'));;
94 my($topic) = $channel->{'topic'};
95
96 return $topic;
97 }
98
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
99 # Parameter: $json : Decoded JSON hash
100 # The JSON contains a field specifying what the IRC channel topic should be
101 # Return that field as a string
8a11956 @nhandler Add nmbot.pl
authored
102 sub getTopicFromJson {
103 my($json) = @_;
104
105 return $json->{'irc_topic'};
106 }
107
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
108 # Parameter: $json : Decoded JSON hash
109 # Checks if the current channel topic is set correctly
110 # If it is not, we call setTopic() to set it
111 # If it is, we do nothing
112 # Returns nothing
8a11956 @nhandler Add nmbot.pl
authored
113 sub updateTopic {
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
114 my $json = shift;
8a11956 @nhandler Add nmbot.pl
authored
115 my $newTopic = getTopicFromJson($json);
116 my $topic = getTopic();
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
117 my $prefix = Irssi::settings_get_str('nmbot_topic_prefix');
118 $topic = "$topic";
119 $newTopic = "$prefix$newTopic";
8a11956 @nhandler Add nmbot.pl
authored
120 Irssi::print("Old Topic: $topic") if($debug);
121 Irssi::print("New Topic: $newTopic") if($debug);
122
123 if($topic ne $newTopic) {
124 Irssi::print("Changing Topic to $newTopic") if($debug);
125 setTopic($newTopic);
126 }
127 else {
128 Irssi::print("Not changing topic") if($debug);
129 }
130 }
131
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
132 # Parameter: $json : Decoded JSON hash
133 # Displays a message in the IRC channel for each event in the JSON file
134 # Includes checks to prevent displaying the same event multiple times
135 # Returns nothing
8a11956 @nhandler Add nmbot.pl
authored
136 sub announce {
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
137 my $json = shift;
8a11956 @nhandler Add nmbot.pl
authored
138 my($server) = Irssi::server_find_tag('oftc');
139 my($channel) = Irssi::settings_get_str('nmbot_channel');
140 foreach my $event (@{$json->{'events'}}) {
141 if($event->{'status_changed_ts'} > Irssi::settings_get_str('nmbot_last_announce')) {
142 if($event->{'type'} eq "status") {
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
143 $server->command("MSG $channel " . $event->{'fn'} . " (" . $event->{'key'} . ") Status changed to: " . $roles{$event->{'status'}});
8a11956 @nhandler Add nmbot.pl
authored
144 }
145 elsif($event->{'type'} eq "progress") {
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
146 $server->command("MSG $channel " . $event->{'fn'} . " (" . $event->{'key'} . ") Progress changed to: " . $stages{$event->{'progress'}});
8a11956 @nhandler Add nmbot.pl
authored
147 }
148 else {
149 Irssi::print("Unknown Type (" . $event->{'type'} . ") for " . $event->{'key'});
150 }
151 }
152 else {
153 Irssi::print("NOT Announcing: " . $event->{'key'}) if($debug);
154 }
155 }
156 Irssi::settings_set_str('nmbot_last_announce', time);
157 }
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
158
b82e00b Add some comments to nmbot.pl
Nathan Handler authored
159 # Parameters: none
160 # This function is automatically called every minute
161 # Returns nothing
1767af2 @nhandler Use main() function to check for topic updates and announcements at t…
authored
162 sub main {
163 my $json = getJson();
164 announce($json);
165 updateTopic($json);
166 }
Something went wrong with that request. Please try again.