95fc87dc2a2ad9fa6e1b3e82546aa05d089863aa
[cacert-votebot.git] / src / org / cacert / votebot / CAcertVoteAuditor.java
1 package org.cacert.votebot;
2
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
7
8 import org.cacert.votebot.CAcertVoteMechanics.VoteType;
9
10 public class CAcertVoteAuditor extends IRCBot {
11
12 public CAcertVoteAuditor(IRCClient c, String toAudit) {
13 super(c);
14 this.toAudit = toAudit;
15 c.join(voteAuxChn);
16 }
17
18 String voteAuxChn = System.getProperty("auditor.target.voteChn", "vote");
19
20 String toAudit;
21
22 long warn = Long.parseLong(System.getProperty("voteBot.warnSecs", "90"));
23
24 long timeout = Long.parseLong(System.getProperty("voteBot.timeoutSecs", "120"));
25
26 CAcertVoteMechanics mech = new CAcertVoteMechanics();
27
28 String[] capturedResults = new String[VoteType.values().length];
29
30 int ctr = -1;
31
32 @Override
33 public synchronized void publicMessage(String from, String channel, String message) {
34 if (channel.equals(voteAuxChn)) {
35 if (from.equals(toAudit)) {
36 if (ctr >= 0) {
37 capturedResults[ctr++] = message;
38
39 if (ctr == capturedResults.length) {
40 String[] reals = mech.closeVote();
41
42 if (Arrays.equals(reals, capturedResults)) {
43 System.out.println("Audit for vote was successful.");
44 } else {
45 System.out.println("Audit failed! Vote Bot (or Auditor) is probably broken.");
46 }
47
48 ctr = -1;
49 }
50
51 return;
52 }
53 if (message.startsWith("New Vote: ")) {
54 System.out.println("detected vote-start");
55
56 Pattern p = Pattern.compile("New Vote: (.*) has started a vote on \"(.*)\"");
57 Matcher m = p.matcher(message);
58
59 if ( !m.matches()) {
60 System.out.println("error: vote-start malformed");
61 return;
62 }
63
64 mech.callVote(m.group(1), m.group(2));
65 } else if (message.startsWith("Results: ")) {
66 System.out.println("detected vote-end. Reading results");
67
68 ctr = 0;
69 }
70 } else {
71 if (ctr != -1) {
72 System.out.println("Vote after end.");
73 return;
74 }
75
76 System.out.println("detected vote");
77 mech.evaluateVote(from, message);
78 System.out.println("Current state: " + mech.getCurrentResult());
79 }
80 }
81 }
82
83 @Override
84 public synchronized void privateMessage(String from, String message) {
85
86 }
87
88 @Override
89 public synchronized void join(String cleanReferent, String chn) {
90
91 }
92
93 @Override
94 public synchronized void part(String cleanReferent, String chn) {
95
96 }
97
98 public static void main(String[] args) throws IOException, InterruptedException {
99 IRCClient ic = IRCClient.parseCommandLine(args, "dogcraft_de-Auditor");
100 String targetNick = System.getProperty("auditor.target.nick");
101
102 if (targetNick == null) {
103 System.out.println("use -Dauditor.target.nick=TargetNick to set a target nick");
104 System.exit(0);
105 }
106
107 ic.setBot(new CAcertVoteAuditor(ic, targetNick));
108 }
109 }