Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ProcessMessage.java 19KB

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