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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2006-2010 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.addons.urlcatcher;
  23. import com.dmdirc.Raw;
  24. import com.dmdirc.actions.ActionManager;
  25. import com.dmdirc.actions.interfaces.ActionType;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.commandparser.CommandManager;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ActionListener;
  30. import com.dmdirc.interfaces.ConfigChangeListener;
  31. import com.dmdirc.plugins.Plugin;
  32. import com.dmdirc.ui.messages.Styliser;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. /**
  36. * A plugin which logs all observed URLs and allows the user to list them
  37. * later.
  38. *
  39. * @author chris
  40. */
  41. public class UrlCatcherPlugin extends Plugin implements ActionListener,
  42. ConfigChangeListener {
  43. private final Map<String, Integer> urls = new HashMap<String, Integer>();
  44. /** Whether to capture URLs from the raw window. */
  45. private boolean captureRaw = false;
  46. private final UrlListCommand command = new UrlListCommand(this);
  47. /** {@inheritDoc} */
  48. @Override
  49. public void onLoad() {
  50. ActionManager.addListener(this, CoreActionType.CLIENT_LINE_ADDED);
  51. CommandManager.registerCommand(command);
  52. IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
  53. updateConfig();
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public void onUnload() {
  58. ActionManager.removeListener(this);
  59. CommandManager.unregisterCommand(command);
  60. IdentityManager.getGlobalConfig().removeListener(this);
  61. }
  62. /**
  63. * Re-reads the configuration of the URL catcher plugin.
  64. */
  65. private void updateConfig() {
  66. captureRaw = IdentityManager.getGlobalConfig().getOptionBool(getDomain(), "captureraw");
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public void processEvent(final ActionType type, final StringBuffer format,
  71. final Object... arguments) {
  72. if (arguments[0] instanceof Raw && !captureRaw) {
  73. return;
  74. }
  75. final String message = Styliser.doLinks((String) arguments[1]);
  76. if (message.indexOf(Styliser.CODE_HYPERLINK) > -1) {
  77. final String[] parts = message.split("" + Styliser.CODE_HYPERLINK);
  78. for (int i = 1; i < parts.length; i += 2) {
  79. addURL(parts[i]);
  80. }
  81. }
  82. }
  83. /**
  84. * Adds an URL to the list of tracked URLs, or increases its counter by one.
  85. *
  86. * @param url The URL to be added
  87. */
  88. private void addURL(final String url) {
  89. if (urls.containsKey(url)) {
  90. urls.put(url, urls.get(url) + 1);
  91. } else {
  92. urls.put(url, 1);
  93. }
  94. }
  95. /**
  96. * Retrieves the URLs that have been recorded.
  97. *
  98. * @return A map of URLs to the number of times they've been seen.
  99. */
  100. public Map<String, Integer> getURLS() {
  101. return urls;
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public void configChanged(final String domain, final String key) {
  106. updateConfig();
  107. }
  108. }