| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
package net.sourceforge.pebble.event.response; |
| 33 | |
|
| 34 | |
import net.sourceforge.pebble.PluginProperties; |
| 35 | |
import net.sourceforge.pebble.domain.Response; |
| 36 | |
import org.apache.commons.logging.Log; |
| 37 | |
import org.apache.commons.logging.LogFactory; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 5492 | public class IpAddressListener extends BlogEntryResponseListenerSupport { |
| 50 | |
|
| 51 | |
|
| 52 | 4 | private static final Log log = LogFactory.getLog(IpAddressListener.class); |
| 53 | |
|
| 54 | |
|
| 55 | |
public static final String WHITELIST_KEY = "IpAddressListener.whitelist"; |
| 56 | |
|
| 57 | |
|
| 58 | |
public static final String BLACKLIST_KEY = "IpAddressListener.blacklist"; |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
protected void blogEntryResponseAdded(Response response) { |
| 66 | 212 | PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties(); |
| 67 | |
|
| 68 | 212 | if (isListed(response, props.getProperty(BLACKLIST_KEY))) { |
| 69 | 8 | log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " is on blacklist"); |
| 70 | 8 | response.setPending(); |
| 71 | 8 | response.incrementSpamScore(); |
| 72 | 204 | } else if (isListed(response, props.getProperty(WHITELIST_KEY))) { |
| 73 | |
|
| 74 | |
} else { |
| 75 | 184 | log.info(response.getTitle() + " marked as pending : IP address " + response.getIpAddress() + " not on blacklist or whitelist"); |
| 76 | 184 | response.setPending(); |
| 77 | |
} |
| 78 | 212 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
protected void blogEntryResponseApproved(Response response) { |
| 86 | 104 | PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties(); |
| 87 | |
|
| 88 | 104 | if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) { |
| 89 | 12 | return; |
| 90 | |
} |
| 91 | |
|
| 92 | 92 | synchronized (props) { |
| 93 | 92 | String whitelist = props.getProperty(WHITELIST_KEY); |
| 94 | 92 | String blacklist = props.getProperty(BLACKLIST_KEY); |
| 95 | 92 | whitelist = addIpAddress(response, whitelist); |
| 96 | 92 | blacklist = removeIpAddress(response, blacklist); |
| 97 | 92 | props.setProperty(WHITELIST_KEY, whitelist); |
| 98 | 92 | props.setProperty(BLACKLIST_KEY, blacklist); |
| 99 | 92 | props.store(); |
| 100 | 92 | } |
| 101 | 92 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
protected void blogEntryResponseRejected(Response response) { |
| 109 | 48 | PluginProperties props = response.getBlogEntry().getBlog().getPluginProperties(); |
| 110 | |
|
| 111 | 48 | if (response.getIpAddress() == null || response.getIpAddress().trim().length() == 0) { |
| 112 | 0 | return; |
| 113 | |
} |
| 114 | |
|
| 115 | 48 | synchronized (props) { |
| 116 | 48 | String blacklist = props.getProperty(BLACKLIST_KEY); |
| 117 | 48 | String whitelist = props.getProperty(WHITELIST_KEY); |
| 118 | 48 | blacklist = addIpAddress(response, blacklist); |
| 119 | 48 | whitelist = removeIpAddress(response, whitelist); |
| 120 | 48 | props.setProperty(BLACKLIST_KEY, blacklist); |
| 121 | 48 | props.setProperty(WHITELIST_KEY, whitelist); |
| 122 | 48 | props.store(); |
| 123 | 48 | } |
| 124 | 48 | } |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
private boolean isListed(Response response, String list) { |
| 136 | 512 | if (response.getIpAddress() == null) { |
| 137 | 32 | return false; |
| 138 | |
} |
| 139 | |
|
| 140 | 480 | String ipAddresses[] = null; |
| 141 | 480 | if (list != null) { |
| 142 | 148 | ipAddresses = list.split(","); |
| 143 | |
} else { |
| 144 | 332 | ipAddresses = new String[0]; |
| 145 | |
} |
| 146 | |
|
| 147 | 504 | for (int i = 0; i < ipAddresses.length; i++) { |
| 148 | 148 | if (response.getIpAddress().equals(ipAddresses[i])) { |
| 149 | 124 | return true; |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | 356 | return false; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
private String addIpAddress(Response response, String list) { |
| 164 | 140 | if (list == null || list.trim().length() == 0) { |
| 165 | 44 | return response.getIpAddress(); |
| 166 | 96 | } else if (!isListed(response, list)) { |
| 167 | 0 | return list + "," + response.getIpAddress(); |
| 168 | |
} else { |
| 169 | 96 | return list; |
| 170 | |
} |
| 171 | |
} |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
private String removeIpAddress(Response response, String list) { |
| 181 | 140 | if (response.getIpAddress() == null) { |
| 182 | 0 | return list; |
| 183 | |
} |
| 184 | |
|
| 185 | 140 | String ipAddresses[] = null; |
| 186 | 140 | if (list != null) { |
| 187 | 104 | ipAddresses = list.split(","); |
| 188 | |
} else { |
| 189 | 36 | ipAddresses = new String[0]; |
| 190 | |
} |
| 191 | |
|
| 192 | 140 | StringBuffer buf = new StringBuffer(); |
| 193 | 276 | for (int i = 0; i < ipAddresses.length; i++) { |
| 194 | 136 | if (!response.getIpAddress().equals(ipAddresses[i])) { |
| 195 | 104 | if (buf.length() > 0) { |
| 196 | 8 | buf.append(","); |
| 197 | |
} |
| 198 | 104 | buf.append(ipAddresses[i]); |
| 199 | |
} |
| 200 | |
} |
| 201 | 140 | return buf.toString(); |
| 202 | |
} |
| 203 | |
|
| 204 | |
} |