summaryrefslogtreecommitdiff
path: root/src/main/java/org/cacert/votebot/vote/CAcertVoteBot.java
blob: d4ed7ca3cb6b7f587602c6a912162989ef573d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright (c) 2015  Felix Doerre
 * Copyright (c) 2015  Benny Baumann
 * Copyright (c) 2016, 2018  Jan Dittberner
 *
 * This file is part of CAcert VoteBot.
 *
 * CAcert VoteBot is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * CAcert VoteBot is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CAcert VoteBot.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.cacert.votebot.vote;

import org.apache.commons.cli.ParseException;
import org.cacert.votebot.shared.CAcertVoteMechanics;
import org.cacert.votebot.shared.IRCBot;
import org.cacert.votebot.shared.IRCClient;
import org.cacert.votebot.shared.exceptions.IRCClientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.MessageFormat;
import java.time.Duration;
import java.util.Calendar;
import java.util.Locale;
import java.util.ResourceBundle;


/**
 * VoteBot main class.
 *
 * @author Felix Doerre
 * @author Jan Dittberner
 */
@SpringBootApplication(scanBasePackageClasses = {IRCClient.class, CAcertVoteBot.class})
@Component
public class CAcertVoteBot extends IRCBot implements Runnable, CommandLineRunner {
    private static final Logger LOGGER = LoggerFactory.getLogger(CAcertVoteBot.class);
    private final ResourceBundle messages = ResourceBundle.getBundle("messages");

    /**
     * Meeting channel where votes and results are published.
     */
    @Value("${voteBot.meetingChn:meeting}")
    private String meetingChannel;

    /**
     * Channel name where voting is performed.
     */
    @Value("${voteBot.voteChn:vote}")
    private String voteChannel;

    /**
     * Seconds to warn before a vote ends.
     */
    @Value("${voteBot.warnSecs:90}")
    private long warn;

    /**
     * Seconds before a vote times out.
     */
    @Value("${voteBot.timeoutSecs:120}")
    private long timeout;

    private final CAcertVoteMechanics voteMechanics;

    private final IRCClient ircClient;

    @Autowired
    public CAcertVoteBot(CAcertVoteMechanics voteMechanics, IRCClient ircClient) {
        this.voteMechanics = voteMechanics;
        this.ircClient = ircClient;
    }

    /**
     * {@inheritDoc}
     *
     * @param args command line arguments
     */
    @Override
    public final void run(final String... args) {
        try {
            getIrcClient().initializeFromArgs(args).assignBot(this);

            Thread.sleep(Duration.ofSeconds(3).toMillis());
            getIrcClient().join(meetingChannel);
            Thread.sleep(Duration.ofSeconds(1).toMillis());
            getIrcClient().join(voteChannel);

            new Thread(this).start();
        } catch (IOException | InterruptedException | ParseException | IRCClientException e) {
            LOGGER.error(MessageFormat.format(messages.getString("error_running_votebot"), e.getMessage()));
        }
    }

    @Override
    protected final IRCClient getIrcClient() {
        return ircClient;
    }

    @Override
    public final synchronized void publicMessage(final String from, final String channel, final String message) throws
            IRCClientException {
        if (channel.equals(voteChannel)) {
            sendPublicMessage(voteChannel, voteMechanics.evaluateVote(from, message));
        }
    }

    @Override
    public final synchronized void privateMessage(final String from, final String message) throws IRCClientException {
        if (message != null && message.length() > 0) {
            String[] parts = message.split("\\s+", 2);
            try {
                VoteBotCommand command = VoteBotCommand.valueOf(parts[0].toUpperCase(Locale.ENGLISH));
                switch (command) {
                    case VOTE:
                        startVote(from, parts[1]);
                        break;
                    case HELP:
                        giveHelp(from);
                        break;
                    case CANCEL:
                        cancelVote(from);
                        break;
                }
            } catch (IllegalArgumentException e) {
                sendUnknownCommand(from, parts[0]);
            }
        }
    }

    /**
     * Cancel a running vote before the end of the voting period.
     *
     * @param from initiator of the cancel command
     */
    private void cancelVote(String from) throws IRCClientException {
        LOGGER.debug(String.format("received cancel vote command from %s", from));
        try {
            announce(voteMechanics.stopVote(from));
            sendPrivateMessage(from, messages.getString("vote_canceled"));
        } catch (IllegalStateException e) {
            sendPrivateMessage(from, e.getMessage());
        }
    }

    private void sendUnknownCommand(String from, String command) throws IRCClientException {
        sendPrivateMessage(from, MessageFormat.format(messages.getString("unknown_command"), command));
    }

    private void giveHelp(String from) throws IRCClientException {
        sendPrivateMessage(from, messages.getString("help_message"));
    }

    private void startVote(final String from, final String message) throws IRCClientException {
        final String response = voteMechanics.callVote(message, warn, timeout);
        sendPrivateMessage(from, response);

        if (response.startsWith("Sorry,")) {
            return;
        }

        announce(MessageFormat.format(messages.getString("new_vote"), from, voteMechanics.getTopic()));
        sendPublicMessage(
                meetingChannel,
                MessageFormat.format(messages.getString("cast_vote_in_vote_channel"), voteChannel));
        sendPublicMessage(
                voteChannel, MessageFormat.format(messages.getString("cast_vote_in_next_seconds"), timeout));
    }

    private synchronized void announce(final String msg) throws IRCClientException {
        sendPublicMessage(meetingChannel, msg);
        sendPublicMessage(voteChannel, msg);
    }

    @Override
    public final void run() {
        try {
            //noinspection InfiniteLoopStatement
            while (true) {
                Thread.sleep(Duration.ofSeconds(1).toMillis());
                String topic = voteMechanics.getTopic();

                switch (voteMechanics.getState()) {
                    case IDLE:
                        break;
                    case RUNNING:
                        Calendar now = Calendar.getInstance();
                        if (now.after(voteMechanics.getEndTime())) {
                            announce(voteMechanics.stopVote("timeout"));
                        } else if (now.after(voteMechanics.getWarnTime()) && !voteMechanics.isWarned()) {
                            announce(MessageFormat.format(
                                    messages.getString("voting_will_end_in_n_seconds"),
                                    topic, timeout - warn));
                            voteMechanics.setWarned();
                        }
                        break;
                    case STOPPING:
                        announce(MessageFormat.format(
                                messages.getString("voting_has_closed"), topic));
                        final String[] res = voteMechanics.closeVote();
                        announce(MessageFormat.format(messages.getString("results_for_vote"), topic));

                        for (final String re : res) {
                            announce(re);
                        }
                        break;
                    default:
                        throw new IllegalStateException(messages.getString("illegal_vote_mechanics_state"));
                }
            }
        } catch (final InterruptedException | IRCClientException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }

    @Override
    public synchronized void join(final String referent, final String chn) {

    }

    @Override
    public synchronized void part(final String referent, final String channel) {

    }

    /**
     * Entry point for the vote bot.
     *
     * @param args command line arguments
     */
    public static void main(final String... args) {
        SpringApplication.run(CAcertVoteBot.class, args);
    }
}