BigScreenBot.java 13 KB

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