Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ProcessMessage.java 18KB

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