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 18KB

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