您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ProcessMessage.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 bits[] = token[0].split(":",2);
  78. String sMessage = "";
  79. if (bits.length > 1) { sMessage = bits[1]; } else { sMessage = bits[0]; }
  80. // We use sMessage to be the users host (first token in the line)
  81. if (myParser.getIgnoreList().matches(sMessage) > -1) { return; }
  82. ChannelClientInfo iChannelClient = null;
  83. ChannelInfo iChannel = null;
  84. ClientInfo iClient = null;
  85. sMessage = token[token.length-1];
  86. bits = sMessage.split(" ", 2);
  87. Character Char1 = Character.valueOf((char)1);
  88. String sCTCP = "";
  89. boolean isAction = false;
  90. boolean isCTCP = false;
  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());
  112. sCTCP = bits[1];
  113. // remove the trailing char1
  114. if (sMessage == "") { sMessage = sMessage.split(Char1.toString())[0]; }
  115. else { sCTCP = sCTCP.split(Char1.toString())[0]; }
  116. callDebugInfo(myParser.ndInfo, "CTCP: \"%s\" \"%s\"",sCTCP,sMessage);
  117. }
  118. }
  119. iClient = getClientInfo(token[0]);
  120. if (myParser.alwaysUpdateClient && iClient != null) {
  121. // Facilitate DMDIRC Formatter
  122. if (iClient.getHost().equals("")) {iClient.setUserBits(token[0],false); }
  123. }
  124. // Fire the appropriate callbacks.
  125. // OnChannel* Callbacks are fired if the target was a channel
  126. // OnPrivate* Callbacks are fired if the target was us
  127. // OnUnknown* Callbacks are fired if the target was neither of the above
  128. // Actions and CTCPs are send as PRIVMSGS
  129. // CTCPReplies are sent as Notices
  130. if (isValidChannelName(token[2])) {
  131. iChannel = getChannelInfo(token[2]);
  132. if (iClient != null && iChannel != null) { iChannelClient = iChannel.getUser(iClient); }
  133. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  134. if (!isAction) {
  135. if (isCTCP) {
  136. callChannelCTCP(iChannel, iChannelClient, sCTCP, sMessage, token[0]);
  137. } else {
  138. callChannelMessage(iChannel, iChannelClient, sMessage, token[0]);
  139. }
  140. } else {
  141. callChannelAction(iChannel, iChannelClient, sMessage, token[0]);
  142. }
  143. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  144. if (isCTCP) {
  145. callChannelCTCPReply(iChannel, iChannelClient, sCTCP, sMessage, token[0]);
  146. } else {
  147. callChannelNotice(iChannel, iChannelClient, sMessage, token[0]);
  148. }
  149. }
  150. } else if (token[2].equalsIgnoreCase(myParser.cMyself.getNickname())) {
  151. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  152. if (!isAction) {
  153. if (isCTCP) {
  154. callPrivateCTCP(sCTCP, sMessage, token[0]);
  155. } else {
  156. callPrivateMessage(sMessage, token[0]);
  157. }
  158. } else {
  159. callPrivateAction(sMessage, token[0]);
  160. }
  161. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  162. if (isCTCP) {
  163. callPrivateCTCPReply(sCTCP, sMessage, token[0]);
  164. } else {
  165. callPrivateNotice(sMessage, token[0]);
  166. }
  167. }
  168. } else {
  169. callDebugInfo(myParser.ndInfo, "Message for Other ("+token[2]+")");
  170. if (sParam.equalsIgnoreCase("PRIVMSG")) {
  171. if (!isAction) {
  172. if (isCTCP) {
  173. callUnknownCTCP(sCTCP, sMessage, token[2], token[0]);
  174. } else {
  175. callUnknownMessage(sMessage, token[2], token[0]);
  176. }
  177. } else {
  178. callUnknownAction(sMessage, token[2], token[0]);
  179. }
  180. } else if (sParam.equalsIgnoreCase("NOTICE")) {
  181. if (isCTCP) {
  182. callUnknownCTCPReply(sCTCP, sMessage, token[2], token[0]);
  183. } else {
  184. callUnknownNotice(sMessage, token[2], token[0]);
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Callback to all objects implementing the ChannelAction Callback.
  191. *
  192. * @see IChannelAction
  193. * @param cChannel Channel where the action was sent to
  194. * @param cChannelClient ChannelClient who sent the action (may be null if server)
  195. * @param sMessage action contents
  196. * @param sHost Hostname of sender (or servername)
  197. */
  198. protected boolean callChannelAction(ChannelInfo cChannel, ChannelClientInfo cChannelClient, String sMessage, String sHost) {
  199. CallbackOnChannelAction cb = (CallbackOnChannelAction)getCallbackManager().getCallbackType("OnChannelAction");
  200. if (cb != null) { return cb.call(cChannel, cChannelClient, sMessage, sHost); }
  201. return false;
  202. }
  203. /**
  204. * Callback to all objects implementing the ChannelCTCP Callback.
  205. *
  206. * @see IChannelCTCP
  207. * @param cChannel Channel where CTCP was sent
  208. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  209. * @param sType Type of CTCP (VERSION, TIME etc)
  210. * @param sMessage Additional contents
  211. * @param sHost Hostname of sender (or servername)
  212. */
  213. protected boolean callChannelCTCP(ChannelInfo cChannel, ChannelClientInfo cChannelClient, String sType, String sMessage, String sHost) {
  214. CallbackOnChannelCTCP cb = (CallbackOnChannelCTCP)getCallbackManager().getCallbackType("OnChannelCTCP");
  215. if (cb != null) { return cb.call(cChannel, cChannelClient, sType, sMessage, sHost); }
  216. return false;
  217. }
  218. /**
  219. * Callback to all objects implementing the ChannelCTCPReply Callback.
  220. *
  221. * @see IChannelCTCPReply
  222. * @param cChannel Channel where CTCPReply was sent
  223. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  224. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  225. * @param sMessage Reply Contents
  226. * @param sHost Hostname of sender (or servername)
  227. */
  228. protected boolean callChannelCTCPReply(ChannelInfo cChannel, ChannelClientInfo cChannelClient, String sType, String sMessage, String sHost) {
  229. CallbackOnChannelCTCPReply cb = (CallbackOnChannelCTCPReply)getCallbackManager().getCallbackType("OnChannelCTCPReply");
  230. if (cb != null) { return cb.call(cChannel, cChannelClient, sType, sMessage, sHost); }
  231. return false;
  232. }
  233. /**
  234. * Callback to all objects implementing the ChannelMessage Callback.
  235. *
  236. * @see IChannelMessage
  237. * @param cChannel Channel where the message was sent to
  238. * @param cChannelClient ChannelClient who sent the message (may be null if server)
  239. * @param sMessage Message contents
  240. * @param sHost Hostname of sender (or servername)
  241. */
  242. protected boolean callChannelMessage(ChannelInfo cChannel, ChannelClientInfo cChannelClient, String sMessage, String sHost) {
  243. CallbackOnChannelMessage cb = (CallbackOnChannelMessage)getCallbackManager().getCallbackType("OnChannelMessage");
  244. if (cb != null) { return cb.call(cChannel, cChannelClient, sMessage, sHost); }
  245. return false;
  246. }
  247. /**
  248. * Callback to all objects implementing the ChannelNotice Callback.
  249. *
  250. * @see IChannelNotice
  251. * @param cChannel Channel where the notice was sent to
  252. * @param cChannelClient ChannelClient who sent the notice (may be null if server)
  253. * @param sMessage notice contents
  254. * @param sHost Hostname of sender (or servername)
  255. */
  256. protected boolean callChannelNotice(ChannelInfo cChannel, ChannelClientInfo cChannelClient, String sMessage, String sHost) {
  257. CallbackOnChannelNotice cb = (CallbackOnChannelNotice)getCallbackManager().getCallbackType("OnChannelNotice");
  258. if (cb != null) { return cb.call(cChannel, cChannelClient, sMessage, sHost); }
  259. return false;
  260. }
  261. /**
  262. * Callback to all objects implementing the PrivateAction Callback.
  263. *
  264. * @see IPrivateAction
  265. * @param sMessage action contents
  266. * @param sHost Hostname of sender (or servername)
  267. */
  268. protected boolean callPrivateAction(String sMessage, String sHost) {
  269. CallbackOnPrivateAction cb = (CallbackOnPrivateAction)getCallbackManager().getCallbackType("OnPrivateAction");
  270. if (cb != null) { return cb.call(sMessage, sHost); }
  271. return false;
  272. }
  273. /**
  274. * Callback to all objects implementing the PrivateCTCP Callback.
  275. *
  276. * @see IPrivateCTCP
  277. * @param sType Type of CTCP (VERSION, TIME etc)
  278. * @param sMessage Additional contents
  279. * @param sHost Hostname of sender (or servername)
  280. */
  281. protected boolean callPrivateCTCP(String sType, String sMessage, String sHost) {
  282. CallbackOnPrivateCTCP cb = (CallbackOnPrivateCTCP)getCallbackManager().getCallbackType("OnPrivateCTCP");
  283. if (cb != null) { return cb.call(sType, sMessage, sHost); }
  284. return false;
  285. }
  286. /**
  287. * Callback to all objects implementing the PrivateCTCPReply Callback.
  288. *
  289. * @see IPrivateCTCPReply
  290. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  291. * @param sMessage Reply Contents
  292. * @param sHost Hostname of sender (or servername)
  293. */
  294. protected boolean callPrivateCTCPReply(String sType, String sMessage, String sHost) {
  295. CallbackOnPrivateCTCPReply cb = (CallbackOnPrivateCTCPReply)getCallbackManager().getCallbackType("OnPrivateCTCPReply");
  296. if (cb != null) { return cb.call(sType, sMessage, sHost); }
  297. return false;
  298. }
  299. /**
  300. * Callback to all objects implementing the PrivateMessage Callback.
  301. *
  302. * @see IPrivateMessage
  303. * @param sMessage Message contents
  304. * @param sHost Hostname of sender (or servername)
  305. */
  306. protected boolean callPrivateMessage(String sMessage, String sHost) {
  307. CallbackOnPrivateMessage cb = (CallbackOnPrivateMessage)getCallbackManager().getCallbackType("OnPrivateMessage");
  308. if (cb != null) { return cb.call(sMessage, sHost); }
  309. return false;
  310. }
  311. /**
  312. * Callback to all objects implementing the PrivateNotice Callback.
  313. *
  314. * @see IPrivateNotice
  315. * @param sMessage Notice contents
  316. * @param sHost Hostname of sender (or servername)
  317. */
  318. protected boolean callPrivateNotice(String sMessage, String sHost) {
  319. CallbackOnPrivateNotice cb = (CallbackOnPrivateNotice)getCallbackManager().getCallbackType("OnPrivateNotice");
  320. if (cb != null) { return cb.call(sMessage, sHost); }
  321. return false;
  322. }
  323. /**
  324. * Callback to all objects implementing the UnknownAction Callback.
  325. *
  326. * @see IUnknownAction
  327. * @param sMessage Action contents
  328. * @param sTarget Actual target of action
  329. * @param sHost Hostname of sender (or servername)
  330. */
  331. protected boolean callUnknownAction(String sMessage, String sTarget, String sHost) {
  332. CallbackOnUnknownAction cb = (CallbackOnUnknownAction)getCallbackManager().getCallbackType("OnUnknownAction");
  333. if (cb != null) { return cb.call(sMessage, sTarget, sHost); }
  334. return false;
  335. }
  336. /**
  337. * Callback to all objects implementing the UnknownCTCP Callback.
  338. *
  339. * @see IUnknownCTCP
  340. * @param sType Type of CTCP (VERSION, TIME etc)
  341. * @param sMessage Additional contents
  342. * @param sTarget Actual Target of CTCP
  343. * @param sHost Hostname of sender (or servername)
  344. */
  345. protected boolean callUnknownCTCP(String sType, String sMessage, String sTarget, String sHost) {
  346. CallbackOnUnknownCTCP cb = (CallbackOnUnknownCTCP)getCallbackManager().getCallbackType("OnUnknownCTCP");
  347. if (cb != null) { return cb.call(sType, sMessage, sTarget, sHost); }
  348. return false;
  349. }
  350. /**
  351. * Callback to all objects implementing the UnknownCTCPReply Callback.
  352. *
  353. * @see IUnknownCTCPReply
  354. * @param sType Type of CTCPRReply (VERSION, TIME etc)
  355. * @param sMessage Reply Contents
  356. * @param sTarget Actual Target of CTCPReply
  357. * @param sHost Hostname of sender (or servername)
  358. */
  359. protected boolean callUnknownCTCPReply(String sType, String sMessage, String sTarget, String sHost) {
  360. CallbackOnUnknownCTCPReply cb = (CallbackOnUnknownCTCPReply)getCallbackManager().getCallbackType("OnUnknownCTCPReply");
  361. if (cb != null) { return cb.call(sType, sMessage, sTarget, sHost); }
  362. return false;
  363. }
  364. /**
  365. * Callback to all objects implementing the UnknownMessage Callback.
  366. *
  367. * @see IUnknownMessage
  368. * @param sMessage Message contents
  369. * @param sTarget Actual target of message
  370. * @param sHost Hostname of sender (or servername)
  371. */
  372. protected boolean callUnknownMessage(String sMessage, String sTarget, String sHost) {
  373. CallbackOnUnknownMessage cb = (CallbackOnUnknownMessage)getCallbackManager().getCallbackType("OnUnknownMessage");
  374. if (cb != null) { return cb.call(sMessage, sTarget, sHost); }
  375. return false;
  376. }
  377. /**
  378. * Callback to all objects implementing the UnknownNotice Callback.
  379. *
  380. * @see IUnknownNotice
  381. * @param sMessage Notice contents
  382. * @param sTarget Actual target of notice
  383. * @param sHost Hostname of sender (or servername)
  384. */
  385. protected boolean callUnknownNotice(String sMessage, String sTarget, String sHost) {
  386. CallbackOnUnknownNotice cb = (CallbackOnUnknownNotice)getCallbackManager().getCallbackType("OnUnknownNotice");
  387. if (cb != null) { return cb.call(sMessage, sTarget, sHost); }
  388. return false;
  389. }
  390. /**
  391. * What does this IRCProcessor handle.
  392. *
  393. * @return String[] with the names of the tokens we handle.
  394. */
  395. public String[] handles() {
  396. String[] iHandle = new String[2];
  397. iHandle[0] = "PRIVMSG";
  398. iHandle[1] = "NOTICE";
  399. return iHandle;
  400. }
  401. /**
  402. * Create a new instance of the IRCProcessor Object
  403. *
  404. * @param parser IRCParser That owns this IRCProcessor
  405. * @param manager ProcessingManager that is in charge of this IRCProcessor
  406. */
  407. protected ProcessMessage (IRCParser parser, ProcessingManager manager) { super(parser, manager); }
  408. }