Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProcessMessage.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import java.util.regex.PatternSyntaxException;
  24. /**
  25. * Process PRIVMSGs and NOTICEs.
  26. * This horrible handles PRIVMSGs and NOTICES<br>
  27. * This inclues CTCPs and CTCPReplies<br>
  28. * It handles all 3 targets (Channel, Private, Unknown)<br>
  29. * Actions are handled here aswell separately from CTCPs.<br>
  30. * Each type has 5 Calls, making 15 callbacks handled here.
  31. */
  32. public class ProcessMessage extends IRCProcessor {
  33. /**
  34. * Process PRIVMSGs and NOTICEs.
  35. * This horrible thing handles PRIVMSGs and NOTICES<br>
  36. * This inclues CTCPs and CTCPReplies<br>
  37. * It handles all 3 targets (Channel, Private, Unknown)<br>
  38. * Actions are handled here aswell separately from CTCPs.<br>
  39. * Each type has 5 Calls, making 15 callbacks handled here.
  40. *
  41. * @param sParam Type of line to process ("NOTICE", "PRIVMSG")
  42. * @param token IRCTokenised line to process
  43. */
  44. @Override
  45. public void process(final String sParam, String[] token) {
  46. // Ignore people!
  47. String sMessage = "";
  48. if (token[0].charAt(0) == ':') { sMessage = token[0].substring(1); } else { sMessage = token[0]; }
  49. // We use sMessage to be the users host (first token in the line)
  50. try {
  51. if (myParser.getIgnoreList().matches(sMessage) > -1) { return; }
  52. } catch (PatternSyntaxException pse) {
  53. final ParserError pe = new ParserError(ParserError.ERROR_WARNING, "Error with ignore list regex: "+pse, myParser.getLastLine());
  54. pe.setException(pse);
  55. callErrorInfo(pe);
  56. }
  57. // Lines such as:
  58. // "nick!user@host PRIVMSG"
  59. // are invalid, stop processing.
  60. if (token.length < 3) { return; }
  61. // Is this actually a notice auth?
  62. if (token[0].indexOf('!') == -1 && token[1].equalsIgnoreCase("NOTICE") && token[2].equalsIgnoreCase("AUTH")) {
  63. try {
  64. myParser.getProcessingManager().process("Notice Auth", token);
  65. } catch (ProcessorNotFoundException e) { }
  66. return;
  67. }
  68. ChannelClientInfo iChannelClient = null;
  69. ChannelInfo iChannel = null;
  70. ClientInfo iClient = null;
  71. // "nick!user@host PRIVMSG #Channel" should be processed as "nick!user@host PRIVMSG #Channel :"
  72. if (token.length < 4) {
  73. sMessage = "";
  74. } else {
  75. sMessage = token[token.length-1];
  76. }
  77. String bits[] = sMessage.split(" ", 2);
  78. final Character Char1 = Character.valueOf((char)1);
  79. String sCTCP = "";
  80. boolean isAction = false;
  81. boolean isCTCP = false;
  82. if (sMessage.length() > 1) {
  83. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  84. // Actions are special CTCPs
  85. // Bits is the message been split into 2 parts, the first word and the rest
  86. if (bits[0].equalsIgnoreCase(Char1+"ACTION") && Character.valueOf(sMessage.charAt(sMessage.length()-1)).equals(Char1)) {
  87. isAction = true;
  88. if (bits.length > 1) {
  89. sMessage = bits[1];
  90. sMessage = sMessage.substring(0, sMessage.length()-1);
  91. } else { sMessage = ""; }
  92. }
  93. }
  94. // If the message is not an action, check if it is another type of CTCP
  95. if (!isAction) {
  96. // CTCPs have Character(1) at the start/end of the line
  97. if (Character.valueOf(sMessage.charAt(0)).equals(Char1) && Character.valueOf(sMessage.charAt(sMessage.length()-1)).equals(Char1)) {
  98. isCTCP = true;
  99. // Bits is the message been split into 2 parts, the first word and the rest
  100. // Some CTCPs have messages and some do not
  101. if (bits.length > 1) { sMessage = bits[1]; } else { sMessage = ""; }
  102. // Remove the leading char1
  103. bits = bits[0].split(Char1.toString(),2);
  104. sCTCP = bits[1];
  105. // remove the trailing char1
  106. if (!sMessage.isEmpty()) { sMessage = sMessage.split(Char1.toString(),2)[0]; }
  107. else { sCTCP = sCTCP.split(Char1.toString(),2)[0]; }
  108. callDebugInfo(IRCParser.DEBUG_INFO, "CTCP: \"%s\" \"%s\"",sCTCP,sMessage);
  109. }
  110. }
  111. }
  112. // Remove the leading : from the host.
  113. if (token[0].charAt(0) == ':' && token[0].length() > 1) { token[0] = token[0].substring(1); }
  114. iClient = getClientInfo(token[0]);
  115. if (IRCParser.ALWAYS_UPDATECLIENT && iClient != null) {
  116. // Facilitate DMDIRC Formatter
  117. if (iClient.getHost().isEmpty()) {iClient.setUserBits(token[0],false); }
  118. }
  119. // Fire the appropriate callbacks.
  120. // OnChannel* Callbacks are fired if the target was a channel
  121. // OnPrivate* Callbacks are fired if the target was us
  122. // OnUnknown* Callbacks are fired if the target was neither of the above
  123. // Actions and CTCPs are send as PRIVMSGS
  124. // CTCPReplies are sent as Notices
  125. if (isValidChannelName(token[2])) {
  126. iChannel = getChannelInfo(token[2]);
  127. if (iChannel == null) {
  128. // callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got message for channel ("+token[2]+") that I am not on.", myParser.getLastLine()));
  129. return;
  130. }
  131. if (iClient != null) { iChannelClient = iChannel.getUser(iClient); }
  132. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  133. if (!isAction) {
  134. if (isCTCP) {
  135. callChannelCTCP(iChannel, iChannelClient, sCTCP, sMessage, token[0]);
  136. } else {
  137. callChannelMessage(iChannel, iChannelClient, sMessage, token[0]);
  138. }
  139. } else {
  140. callChannelAction(iChannel, iChannelClient, sMessage, token[0]);
  141. }
  142. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  143. if (isCTCP) {
  144. callChannelCTCPReply(iChannel, iChannelClient, sCTCP, sMessage, token[0]);
  145. } else {
  146. callChannelNotice(iChannel, iChannelClient, sMessage, token[0]);
  147. }
  148. }
  149. } else if (myParser.getIRCStringConverter().equalsIgnoreCase(token[2], myParser.getMyNickname())) {
  150. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  151. if (!isAction) {
  152. if (isCTCP) {
  153. callPrivateCTCP(sCTCP, sMessage, token[0]);
  154. } else {
  155. callPrivateMessage(sMessage, token[0]);
  156. }
  157. } else {
  158. callPrivateAction(sMessage, token[0]);
  159. }
  160. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  161. if (isCTCP) {
  162. callPrivateCTCPReply(sCTCP, sMessage, token[0]);
  163. } else {
  164. callPrivateNotice(sMessage, token[0]);
  165. }
  166. }
  167. } else {
  168. callDebugInfo(IRCParser.DEBUG_INFO, "Message for Other ("+token[2]+")");
  169. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  170. if (!isAction) {
  171. if (isCTCP) {
  172. callUnknownCTCP(sCTCP, sMessage, token[2], token[0]);
  173. } else {
  174. callUnknownMessage(sMessage, token[2], token[0]);
  175. }
  176. } else {
  177. callUnknownAction(sMessage, token[2], token[0]);
  178. }
  179. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  180. if (isCTCP) {
  181. callUnknownCTCPReply(sCTCP, sMessage, token[2], token[0]);
  182. } else {
  183. callUnknownNotice(sMessage, token[2], token[0]);
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Callback to all objects implementing the ChannelAction Callback.
  190. *
  191. * @see com.dmdirc.parser.irc.callbacks.interfaces.IChannelAction
  192. * @param cChannel Channel where the action was sent to
  193. * @param cChannelClient ChannelClient who sent the action (may be null if server)
  194. * @param sMessage action contents
  195. * @param sHost Hostname of sender (or servername)
  196. * @return true if a method was called, false otherwise
  197. */
  198. protected boolean callChannelAction(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  199. return getCallbackManager().getCallbackType("OnChannelAction").call(cChannel, cChannelClient, sMessage, sHost);
  200. }
  201. /**
  202. * Callback to all objects implementing the ChannelCTCP Callback.
  203. *
  204. * @see com.dmdirc.parser.irc.callbacks.interfaces.IChannelCTCP
  205. * @param cChannel Channel where CTCP was sent
  206. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  207. * @param sType Type of CTCP (VERSION, TIME etc)
  208. * @param sMessage Additional contents
  209. * @param sHost Hostname of sender (or servername)
  210. * @return true if a method was called, false otherwise
  211. */
  212. protected boolean callChannelCTCP(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient, final String sType, final String sMessage, final String sHost) {
  213. return getCallbackManager().getCallbackType("OnChannelCTCP").call(cChannel, cChannelClient, sType, sMessage, sHost);
  214. }
  215. /**
  216. * Callback to all objects implementing the ChannelCTCPReply Callback.
  217. *
  218. * @see com.dmdirc.parser.irc.callbacks.interfaces.IChannelCTCPReply
  219. * @param cChannel Channel where CTCPReply was sent
  220. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  221. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  222. * @param sMessage Reply Contents
  223. * @param sHost Hostname of sender (or servername)
  224. * @return true if a method was called, false otherwise
  225. */
  226. protected boolean callChannelCTCPReply(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient, final String sType, final String sMessage, final String sHost) {
  227. return getCallbackManager().getCallbackType("OnChannelCTCPReply").call(cChannel, cChannelClient, sType, sMessage, sHost);
  228. }
  229. /**
  230. * Callback to all objects implementing the ChannelMessage Callback.
  231. *
  232. * @see com.dmdirc.parser.irc.callbacks.interfaces.IChannelMessage
  233. * @param cChannel Channel where the message was sent to
  234. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  235. * @param sMessage Message contents
  236. * @param sHost Hostname of sender (or servername)
  237. * @return true if a method was called, false otherwise
  238. */
  239. protected boolean callChannelMessage(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  240. return getCallbackManager().getCallbackType("OnChannelMessage").call(cChannel, cChannelClient, sMessage, sHost);
  241. }
  242. /**
  243. * Callback to all objects implementing the ChannelNotice Callback.
  244. *
  245. * @see com.dmdirc.parser.irc.callbacks.interfaces.IChannelNotice
  246. * @param cChannel Channel where the notice was sent to
  247. * @param cChannelClient ChannelClient who sent the notice (may be null if server)
  248. * @param sMessage notice contents
  249. * @param sHost Hostname of sender (or servername)
  250. * @return true if a method was called, false otherwise
  251. */
  252. protected boolean callChannelNotice(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient, final String sMessage, final String sHost) {
  253. return getCallbackManager().getCallbackType("OnChannelNotice").call(cChannel, cChannelClient, sMessage, sHost);
  254. }
  255. /**
  256. * Callback to all objects implementing the PrivateAction Callback.
  257. *
  258. * @see com.dmdirc.parser.irc.callbacks.interfaces.IPrivateAction
  259. * @param sMessage action contents
  260. * @param sHost Hostname of sender (or servername)
  261. * @return true if a method was called, false otherwise
  262. */
  263. protected boolean callPrivateAction(final String sMessage, final String sHost) {
  264. return getCallbackManager().getCallbackType("OnPrivateAction").call(sMessage, sHost);
  265. }
  266. /**
  267. * Callback to all objects implementing the PrivateCTCP Callback.
  268. *
  269. * @see com.dmdirc.parser.irc.callbacks.interfaces.IPrivateCTCP
  270. * @param sType Type of CTCP (VERSION, TIME etc)
  271. * @param sMessage Additional contents
  272. * @param sHost Hostname of sender (or servername)
  273. * @return true if a method was called, false otherwise
  274. */
  275. protected boolean callPrivateCTCP(final String sType, final String sMessage, final String sHost) {
  276. return getCallbackManager().getCallbackType("OnPrivateCTCP").call(sType, sMessage, sHost);
  277. }
  278. /**
  279. * Callback to all objects implementing the PrivateCTCPReply Callback.
  280. *
  281. * @see com.dmdirc.parser.irc.callbacks.interfaces.IPrivateCTCPReply
  282. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  283. * @param sMessage Reply Contents
  284. * @param sHost Hostname of sender (or servername)
  285. * @return true if a method was called, false otherwise
  286. */
  287. protected boolean callPrivateCTCPReply(final String sType, final String sMessage, final String sHost) {
  288. return getCallbackManager().getCallbackType("OnPrivateCTCPReply").call(sType, sMessage, sHost);
  289. }
  290. /**
  291. * Callback to all objects implementing the PrivateMessage Callback.
  292. *
  293. * @see com.dmdirc.parser.irc.callbacks.interfaces.IPrivateMessage
  294. * @param sMessage Message contents
  295. * @param sHost Hostname of sender (or servername)
  296. * @return true if a method was called, false otherwise
  297. */
  298. protected boolean callPrivateMessage(final String sMessage, final String sHost) {
  299. return getCallbackManager().getCallbackType("OnPrivateMessage").call(sMessage, sHost);
  300. }
  301. /**
  302. * Callback to all objects implementing the PrivateNotice Callback.
  303. *
  304. * @see com.dmdirc.parser.irc.callbacks.interfaces.IPrivateNotice
  305. * @param sMessage Notice contents
  306. * @param sHost Hostname of sender (or servername)
  307. * @return true if a method was called, false otherwise
  308. */
  309. protected boolean callPrivateNotice(final String sMessage, final String sHost) {
  310. return getCallbackManager().getCallbackType("OnPrivateNotice").call(sMessage, sHost);
  311. }
  312. /**
  313. * Callback to all objects implementing the UnknownAction Callback.
  314. *
  315. * @see com.dmdirc.parser.irc.callbacks.interfaces.IUnknownAction
  316. * @param sMessage Action contents
  317. * @param sTarget Actual target of action
  318. * @param sHost Hostname of sender (or servername)
  319. * @return true if a method was called, false otherwise
  320. */
  321. protected boolean callUnknownAction(final String sMessage, final String sTarget, final String sHost) {
  322. return getCallbackManager().getCallbackType("OnUnknownAction").call(sMessage, sTarget, sHost);
  323. }
  324. /**
  325. * Callback to all objects implementing the UnknownCTCP Callback.
  326. *
  327. * @see com.dmdirc.parser.irc.callbacks.interfaces.IUnknownCTCP
  328. * @param sType Type of CTCP (VERSION, TIME etc)
  329. * @param sMessage Additional contents
  330. * @param sTarget Actual Target of CTCP
  331. * @param sHost Hostname of sender (or servername)
  332. * @return true if a method was called, false otherwise
  333. */
  334. protected boolean callUnknownCTCP(final String sType, final String sMessage, final String sTarget, final String sHost) {
  335. return getCallbackManager().getCallbackType("OnUnknownCTCP").call(sType, sMessage, sTarget, sHost);
  336. }
  337. /**
  338. * Callback to all objects implementing the UnknownCTCPReply Callback.
  339. *
  340. * @see com.dmdirc.parser.irc.callbacks.interfaces.IUnknownCTCPReply
  341. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  342. * @param sMessage Reply Contents
  343. * @param sTarget Actual Target of CTCPReply
  344. * @param sHost Hostname of sender (or servername)
  345. * @return true if a method was called, false otherwise
  346. */
  347. protected boolean callUnknownCTCPReply(final String sType, final String sMessage, final String sTarget, final String sHost) {
  348. return getCallbackManager().getCallbackType("OnUnknownCTCPReply").call(sType, sMessage, sTarget, sHost);
  349. }
  350. /**
  351. * Callback to all objects implementing the UnknownMessage Callback.
  352. *
  353. * @see com.dmdirc.parser.irc.callbacks.interfaces.IUnknownMessage
  354. * @param sMessage Message contents
  355. * @param sTarget Actual target of message
  356. * @param sHost Hostname of sender (or servername)
  357. * @return true if a method was called, false otherwise
  358. */
  359. protected boolean callUnknownMessage(final String sMessage, final String sTarget, final String sHost) {
  360. return getCallbackManager().getCallbackType("OnUnknownMessage").call(sMessage, sTarget, sHost);
  361. }
  362. /**
  363. * Callback to all objects implementing the UnknownNotice Callback.
  364. *
  365. * @see com.dmdirc.parser.irc.callbacks.interfaces.IUnknownNotice
  366. * @param sMessage Notice contents
  367. * @param sTarget Actual target of notice
  368. * @param sHost Hostname of sender (or servername)
  369. * @return true if a method was called, false otherwise
  370. */
  371. protected boolean callUnknownNotice(final String sMessage, final String sTarget, final String sHost) {
  372. return getCallbackManager().getCallbackType("OnUnknownNotice").call(sMessage, sTarget, sHost);
  373. }
  374. /**
  375. * What does this IRCProcessor handle.
  376. *
  377. * @return String[] with the names of the tokens we handle.
  378. */
  379. @Override
  380. public String[] handles() {
  381. return new String[]{"PRIVMSG", "NOTICE"};
  382. }
  383. /**
  384. * Create a new instance of the IRCProcessor Object.
  385. *
  386. * @param parser IRCParser That owns this IRCProcessor
  387. * @param manager ProcessingManager that is in charge of this IRCProcessor
  388. */
  389. protected ProcessMessage (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  390. }