BigScreenBot.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // Big Screen Bot
  2. //
  3. // Douglas Thrift
  4. //
  5. // BigScreenBot.java
  6. /* Copyright 2011 Douglas Thrift
  7. *
  8. * This file is part of Big Screen Bot.
  9. *
  10. * Big Screen Bot is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Big Screen Bot is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Big Screen Bot. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. package net.douglasthrift.bigscreenbot;
  24. import java.io.IOException;
  25. import java.security.GeneralSecurityException;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.TreeMap;
  30. import java.util.regex.Pattern;
  31. import javax.net.ssl.SSLSocketFactory;
  32. import org.apache.commons.cli.CommandLine;
  33. import org.apache.commons.cli.GnuParser;
  34. import org.apache.commons.cli.HelpFormatter;
  35. import org.apache.commons.cli.Options;
  36. import org.apache.commons.cli.ParseException;
  37. import org.jibble.pircbot.Colors;
  38. import org.jibble.pircbot.IrcException;
  39. public class BigScreenBot extends Bot
  40. {
  41. private static final int CHANNEL = 0x1;
  42. private static final int PRIVATE = 0x2;
  43. private static final int BOTH = 0x3;
  44. private static enum Action { RECONNECT, RESTART, QUIT }
  45. private abstract class Command
  46. {
  47. private boolean admin;
  48. private int access;
  49. private String arguments, description;
  50. protected Command(boolean admin, int access, String arguments, String description)
  51. {
  52. this.admin = admin;
  53. this.access = access;
  54. this.arguments = arguments;
  55. this.description = description;
  56. }
  57. public abstract void execute(String channel, String sender, boolean admin, String argument);
  58. public int getAccess()
  59. {
  60. return access;
  61. }
  62. public String getArguments()
  63. {
  64. return arguments;
  65. }
  66. public String getDescription()
  67. {
  68. return description;
  69. }
  70. public boolean isAdmin()
  71. {
  72. return admin;
  73. }
  74. public boolean isChannel()
  75. {
  76. return (access & CHANNEL) != 0;
  77. }
  78. public boolean isPrivate()
  79. {
  80. return (access & PRIVATE) != 0;
  81. }
  82. }
  83. private boolean verbose;
  84. private Settings settings = new Settings();
  85. private Remote remote;
  86. private List<Pattern> admins = new ArrayList<Pattern>();
  87. private Map<String, Command> commands = new TreeMap<String, Command>();
  88. private Action action = Action.RECONNECT;
  89. private BigScreenBot(boolean verbose)
  90. {
  91. super();
  92. this.verbose = verbose;
  93. try
  94. {
  95. settings.load();
  96. remote = new Remote(settings);
  97. }
  98. catch (IOException exception)
  99. {
  100. error(exception, 1);
  101. }
  102. setAutoNickChange(true);
  103. setFinger("Big Screen Bot");
  104. setMessageDelay(0);
  105. setVersion("Big Screen Bot (" + System.getProperty("os.name") + ")");
  106. if (settings.getBooleanProperty("ssl", false))
  107. {
  108. // TODO: figure out how to make this work with Jay's certificate
  109. setSocketFactory(SSLSocketFactory.getDefault());
  110. }
  111. setLogin(System.getProperty("user.name"));
  112. setName(settings.getProperty("nick", "bigscreenbot"));
  113. setVerbose(verbose);
  114. for (String admin : settings.getListProperty("admins"))
  115. admins.add(compileNickMask(admin));
  116. commands.put("googletv", new Command(false, BOTH, "url [device]", "fling url to a Google TV device")
  117. {
  118. @Override
  119. public void execute(String channel, String sender, boolean admin, String argument)
  120. {
  121. }
  122. });
  123. commands.put("help", new Command(false, PRIVATE, "[command]", "show this help message")
  124. {
  125. @Override
  126. public void execute(String channel, String sender, boolean admin, String arguments)
  127. {
  128. String argument = arguments.split("\\s", 2)[0].toLowerCase();
  129. if (argument.startsWith("!"))
  130. argument = argument.substring(1);
  131. Command command = commands.get(argument);
  132. sendMessage(channel, sender, Colors.BOLD + "command arguments access description" + Colors.NORMAL);
  133. if (command != null)
  134. help(channel, sender, admin, argument, command);
  135. else
  136. for (Map.Entry<String, Command> nameCommand : commands.entrySet())
  137. help(channel, sender, admin, nameCommand.getKey(), nameCommand.getValue());
  138. }
  139. private void help(String channel, String sender, boolean admin, String name, Command command)
  140. {
  141. boolean unavailable = command.isAdmin() && !admin;
  142. String access;
  143. switch (command.getAccess())
  144. {
  145. case CHANNEL:
  146. access = "channel"; break;
  147. case PRIVATE:
  148. access = "private"; break;
  149. case BOTH: default:
  150. access = "channel/private"; break;
  151. }
  152. sendMessage(channel, sender, (unavailable ? Colors.UNDERLINE : "") + String.format("%1$-11s %2$-23s %3$-15s %4$s", name, command.getArguments(), access, command.getDescription()) + (unavailable ? Colors.NORMAL : ""));
  153. }
  154. });
  155. commands.put("pair", new Command(true, PRIVATE, "[device [code]]", "pair with a Google TV device")
  156. {
  157. @Override
  158. public void execute(final String channel, final String sender, boolean admin, String argument)
  159. {
  160. final String[] arguments = argument.split("\\s", 2);
  161. if (arguments.length == 1)
  162. if (arguments[0].isEmpty())
  163. new Thread()
  164. {
  165. @Override
  166. public void run()
  167. {
  168. List<String> devices;
  169. synchronized (remote)
  170. {
  171. devices = remote.listDevices();
  172. }
  173. if (devices.isEmpty())
  174. {
  175. sendMessage(channel, sender, "there are no devices to pair with");
  176. return;
  177. }
  178. sendMessage(channel, sender, Colors.BOLD + "devices" + Colors.NORMAL);
  179. for (String device : devices)
  180. sendMessage(channel, sender, device);
  181. }
  182. }.start();
  183. else
  184. new Thread()
  185. {
  186. @Override
  187. public void run()
  188. {
  189. synchronized (remote)
  190. {
  191. try
  192. {
  193. remote.beginPairDevice(arguments[0]);
  194. }
  195. catch (GeneralSecurityException exception)
  196. {
  197. error(channel, sender, exception);
  198. }
  199. }
  200. }
  201. }.start();
  202. else
  203. {
  204. }
  205. }
  206. });
  207. commands.put("quit", new Command(true, PRIVATE, "[message]", "quit and do not come back")
  208. {
  209. @Override
  210. public void execute(String channel, String sender, boolean admin, String argument)
  211. {
  212. synchronized (action)
  213. {
  214. action = Action.QUIT;
  215. }
  216. quitServer(!argument.isEmpty() ? argument : "oh no!");
  217. }
  218. });
  219. commands.put("restart", new Command(true, PRIVATE, "", "quit and join running more up to date code")
  220. {
  221. @Override
  222. public void execute(String channel, String sender, boolean admin, String argument)
  223. {
  224. synchronized (action)
  225. {
  226. action = Action.RESTART;
  227. }
  228. quitServer("restarting");
  229. }
  230. });
  231. commands.put("say", new Command(true, PRIVATE, "nick|channel message", "say message to nick or channel")
  232. {
  233. @Override
  234. public void execute(String channel, String sender, boolean admin, String argument)
  235. {
  236. String[] arguments = argument.split("\\s", 2);
  237. if (arguments.length != 2)
  238. {
  239. help(channel, sender, admin, "say");
  240. return;
  241. }
  242. if (arguments[0].equalsIgnoreCase(getNick()))
  243. {
  244. sendMessage(channel, sender, "nice try");
  245. return;
  246. }
  247. sendMessage(arguments[0], arguments[1]);
  248. sendMessage(channel, sender, String.format("successfully sent message (\"%1$s\") to nick/channel (\"%2$s\")", arguments[1], arguments[0]));
  249. }
  250. });
  251. try
  252. {
  253. connect(settings.getProperty("server"), settings.getIntegerProperty("port", 6667));
  254. }
  255. catch (IOException exception)
  256. {
  257. error(exception, 1);
  258. }
  259. catch (IrcException exception)
  260. {
  261. error(exception, 1);
  262. }
  263. }
  264. public void sendMessage(String channel, String sender, String message)
  265. {
  266. sendMessage(channel != null ? channel : sender, message);
  267. }
  268. @Override
  269. protected void onConnect()
  270. {
  271. joinChannel("#bigscreenbot");
  272. }
  273. @Override
  274. protected void onDisconnect()
  275. {
  276. synchronized (action)
  277. {
  278. switch (action)
  279. {
  280. case RESTART:
  281. dispose();
  282. System.exit(2);
  283. case QUIT:
  284. dispose();
  285. break;
  286. default:
  287. while (true)
  288. {
  289. try
  290. {
  291. reconnect();
  292. }
  293. catch (IOException exception)
  294. {
  295. continue;
  296. }
  297. catch (IrcException exception)
  298. {
  299. error(exception, 1);
  300. }
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. @Override
  307. protected void onMessage(String channel, String sender, String login, String hostname, String message)
  308. {
  309. message = Colors.removeFormattingAndColors(message);
  310. doCommandFromMessage(channel, sender, login, hostname, message);
  311. }
  312. @Override
  313. protected void onPrivateMessage(String sender, String login, String hostname, String message)
  314. {
  315. message = Colors.removeFormattingAndColors(message);
  316. doCommandFromMessage(null, sender, login, hostname, message);
  317. }
  318. private void doCommandFromMessage(String channel, String sender, String login, String hostname, String message)
  319. {
  320. boolean admin = matchNickMasks(sender, login, hostname, admins);
  321. if (!admin && !isNickInChannels(sender))
  322. return;
  323. String[] arguments = message.split("\\s", 2);
  324. String argument = arguments[0].toLowerCase();
  325. if (argument.startsWith("!"))
  326. argument = argument.substring(1);
  327. Command command = commands.get(argument);
  328. if (channel != null)
  329. {
  330. if (command == null)
  331. return;
  332. if (!admin && command.isAdmin())
  333. return;
  334. if (!command.isChannel())
  335. return;
  336. }
  337. else
  338. {
  339. if (command == null)
  340. {
  341. sendMessage(channel, sender, "unknown command (\"" + argument + "\"); try \"help\"");
  342. return;
  343. }
  344. if (!admin && command.isAdmin())
  345. {
  346. sendMessage(channel, sender, "unauthorized command (\"" + argument + "\")");
  347. return;
  348. }
  349. if (!command.isPrivate())
  350. {
  351. sendMessage(channel, sender, "inappropriate command (\"" + argument + "\")");
  352. return;
  353. }
  354. }
  355. command.execute(channel, sender, admin, arguments.length == 2 ? arguments[1] : "");
  356. }
  357. private void error(Exception exception)
  358. {
  359. if (verbose)
  360. exception.printStackTrace();
  361. else
  362. System.err.println("bigscreenbot: " + exception.getMessage());
  363. }
  364. private void error(Exception exception, int code)
  365. {
  366. error(exception);
  367. System.exit(code);
  368. }
  369. private void error(String channel, String sender, Exception exception)
  370. {
  371. error(exception);
  372. sendMessage(channel, sender, "an error occurred");
  373. }
  374. private void help(String channel, String sender, boolean admin, String command)
  375. {
  376. commands.get("help").execute(channel, sender, admin, command);
  377. }
  378. public static void main(String[] args)
  379. {
  380. Options options = new Options();
  381. options.addOption("h", "help", false, "show this help message and exit");
  382. options.addOption("v", "verbose", false, "");
  383. CommandLine line = null;
  384. try
  385. {
  386. line = new GnuParser().parse(options, args);
  387. }
  388. catch (ParseException exception)
  389. {
  390. System.err.println("bigscreenbot: " + exception.getMessage());
  391. }
  392. if (line.hasOption('h'))
  393. {
  394. new HelpFormatter().printHelp("bigscreenbot", options, true);
  395. return;
  396. }
  397. new BigScreenBot(line.hasOption('v'));
  398. }
  399. }
  400. // vim: expandtab