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.

UrlCatcherPlugin.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  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.FrameContainer;
  24. import com.dmdirc.Raw;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.actions.CoreActionType;
  27. import com.dmdirc.interfaces.actions.ActionType;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ActionListener;
  30. import com.dmdirc.interfaces.ConfigChangeListener;
  31. import com.dmdirc.plugins.BasePlugin;
  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. public class UrlCatcherPlugin extends BasePlugin implements ActionListener,
  40. ConfigChangeListener {
  41. /* URLs and the number of times they were mentioned. */
  42. private final Map<String, Integer> urls = new HashMap<String, Integer>();
  43. /** Whether to capture URLs from the raw window. */
  44. private boolean captureRaw = false;
  45. /** Creates a new instance of this plugin. */
  46. public UrlCatcherPlugin() {
  47. super();
  48. registerCommand(new UrlListCommand(this), UrlListCommand.INFO);
  49. }
  50. /** {@inheritDoc} */
  51. @Override
  52. public void onLoad() {
  53. ActionManager.getActionManager().registerListener(this,
  54. CoreActionType.CLIENT_LINE_ADDED);
  55. IdentityManager.getIdentityManager().getGlobalConfiguration().addChangeListener(getDomain(), this);
  56. updateConfig();
  57. super.onLoad();
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public void onUnload() {
  62. ActionManager.getActionManager().unregisterListener(this);
  63. IdentityManager.getIdentityManager().getGlobalConfiguration().removeListener(this);
  64. super.onUnload();
  65. }
  66. /**
  67. * Re-reads the configuration of the URL catcher plugin.
  68. */
  69. private void updateConfig() {
  70. captureRaw = IdentityManager.getIdentityManager().getGlobalConfiguration().getOptionBool(getDomain(), "captureraw");
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public void processEvent(final ActionType type, final StringBuffer format,
  75. final Object... arguments) {
  76. if (arguments[0] instanceof Raw && !captureRaw) {
  77. return;
  78. }
  79. final String message = ((FrameContainer) arguments[0]).getStyliser()
  80. .doLinks((String) arguments[1]);
  81. if (message.indexOf(Styliser.CODE_HYPERLINK) > -1) {
  82. final String[] parts = message.split(Character.toString(Styliser.CODE_HYPERLINK));
  83. for (int i = 1; i < parts.length; i += 2) {
  84. addURL(parts[i]);
  85. }
  86. }
  87. }
  88. /**
  89. * Adds an URL to the list of tracked URLs, or increases its counter by one.
  90. *
  91. * @param url The URL to be added
  92. */
  93. private void addURL(final String url) {
  94. if (urls.containsKey(url)) {
  95. urls.put(url, urls.get(url) + 1);
  96. } else {
  97. urls.put(url, 1);
  98. }
  99. }
  100. /**
  101. * Retrieves the URLs that have been recorded.
  102. *
  103. * @return A map of URLs to the number of times they've been seen.
  104. */
  105. public Map<String, Integer> getURLS() {
  106. return urls;
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public void configChanged(final String domain, final String key) {
  111. updateConfig();
  112. }
  113. }