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.

TwitterChannelInfo.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.parser_twitter;
  23. import com.dmdirc.addons.parser_twitter.api.TwitterUser;
  24. import com.dmdirc.parser.common.ChannelListModeItem;
  25. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  26. import com.dmdirc.parser.interfaces.ChannelInfo;
  27. import com.dmdirc.parser.interfaces.ClientInfo;
  28. import com.dmdirc.parser.interfaces.Parser;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.HashMap;
  32. import java.util.Map;
  33. /**
  34. * ChannelInfo class for the Twitter plugin.
  35. */
  36. public class TwitterChannelInfo implements ChannelInfo {
  37. /** Name of this channel. */
  38. private final String myName;
  39. /** The Parser that owns this object. */
  40. private final Twitter myParser;
  41. /** Topic of this channel. */
  42. private String myTopic = "";
  43. /** When was the topic set? */
  44. private long topicTime = 0;
  45. /** Who set the topic? */
  46. private String topicSetter = "";
  47. /** Known clients of this channel. */
  48. private Map<String, TwitterChannelClientInfo> channelClients = new HashMap<String, TwitterChannelClientInfo>();
  49. /** Map to store misc stuff in. */
  50. private Map<Object, Object> myMap = new HashMap<Object, Object>();
  51. /** Known blocked users. */
  52. private Collection<ChannelListModeItem> bannedList = new ArrayList<ChannelListModeItem>();
  53. /**
  54. * Create a new TwitterChannelInfo.
  55. *
  56. * @param myName Name of this channel
  57. * @param myParser parser that owns this TwitterChannelInfo
  58. */
  59. public TwitterChannelInfo(final String myName, final Twitter myParser) {
  60. this.myName = myName;
  61. this.myParser = myParser;
  62. }
  63. /** {@inheritDoc} */
  64. @Override
  65. public String getName() {
  66. return myName;
  67. }
  68. /** {@inheritDoc} */
  69. @Override
  70. public String toString() {
  71. return getName();
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public void setTopic(final String topic) {
  76. myParser.setStatus(topic);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public String getTopic() {
  81. return myTopic;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public long getTopicTime() {
  86. return topicTime;
  87. }
  88. /** {@inheritDoc} */
  89. @Override
  90. public String getTopicSetter() {
  91. return topicSetter;
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public String getModes() {
  96. return "";
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public String getMode(final char mode) {
  101. return "";
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public void sendMessage(final String message) {
  106. myParser.sendMessage(myName, message);
  107. }
  108. /** {@inheritDoc} */
  109. @Override
  110. public void sendAction(final String action) {
  111. myParser.sendAction(myName, action);
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public void part(final String reason) {
  116. myParser.partChannel(this);
  117. }
  118. /** {@inheritDoc} */
  119. @Override
  120. public void sendWho() {
  121. // Ignore, twitter parser doesn't support WHOs
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public void alterMode(final boolean add, final Character mode, final String parameter) {
  126. if (mode == 'b') {
  127. final String[] bits = parameter.split(" ");
  128. if (add) {
  129. myParser.getApi().blockUser(bits[0]);
  130. } else {
  131. myParser.getApi().unblockUser(bits[0]);
  132. }
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. @Override
  137. public void flushModes() {
  138. // Ignore, all mode changes are instantaneous
  139. }
  140. /** {@inheritDoc} */
  141. @Override
  142. public ChannelClientInfo getChannelClient(final ClientInfo client) {
  143. return getChannelClient(client.getNickname());
  144. }
  145. /** {@inheritDoc} */
  146. @Override
  147. public ChannelClientInfo getChannelClient(final String client) {
  148. return channelClients.containsKey(client.toLowerCase()) ? channelClients.get(client.toLowerCase()) : null;
  149. }
  150. /** {@inheritDoc} */
  151. @Override
  152. public ChannelClientInfo getChannelClient(final String client, final boolean create) {
  153. ChannelClientInfo cci = getChannelClient(client);
  154. if (create && cci == null) {
  155. cci = new TwitterChannelClientInfo(this, (TwitterClientInfo) myParser.getClient(client));
  156. }
  157. return cci;
  158. }
  159. /** {@inheritDoc} */
  160. @Override
  161. public Collection<ChannelClientInfo> getChannelClients() {
  162. return new ArrayList<ChannelClientInfo>(channelClients.values());
  163. }
  164. /** {@inheritDoc} */
  165. protected void clearChannelClients() {
  166. channelClients.clear();
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public int getChannelClientCount() {
  171. return channelClients.size();
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public Parser getParser() {
  176. return myParser;
  177. }
  178. /**
  179. * Add a channel client to this channel.
  180. *
  181. * @param cci Channel Client to add.
  182. */
  183. public void addChannelClient(final TwitterChannelClientInfo cci) {
  184. synchronized (channelClients) {
  185. channelClients.put(cci.getClient().getNickname().toLowerCase(), cci);
  186. }
  187. }
  188. /**
  189. * Remove a channel client from this channel.
  190. *
  191. * @param cci Channel Client to remove.
  192. */
  193. public void delChannelClient(final TwitterChannelClientInfo cci) {
  194. synchronized (channelClients) {
  195. channelClients.remove(cci.getClient().getNickname().toLowerCase());
  196. }
  197. }
  198. /** {@inheritDoc} */
  199. @Override
  200. public void requestListModes() {
  201. new Thread() {
  202. /** {@inheritDoc} */
  203. @Override
  204. public void run() {
  205. final ArrayList<ChannelListModeItem> items = new ArrayList<ChannelListModeItem>();
  206. final long time = System.currentTimeMillis() / 1000;
  207. for (final TwitterUser user : myParser.getApi().getBlocked()) {
  208. items.add(new ChannelListModeItem(user.getScreenName(), myParser.getApi().getDisplayUsername(), time));
  209. }
  210. bannedList = items;
  211. }
  212. }.start();
  213. }
  214. /** {@inheritDoc} */
  215. @Override
  216. public Collection<ChannelListModeItem> getListMode(final char mode) {
  217. if (mode == 'b') {
  218. return bannedList;
  219. } else {
  220. return new ArrayList<ChannelListModeItem>();
  221. }
  222. }
  223. /**
  224. * Set the topic for this channel.
  225. *
  226. * @param string Topic to set.
  227. */
  228. void setLocalTopic(final String string) {
  229. myTopic = string;
  230. }
  231. /**
  232. * Set the topic time for this channel.
  233. *
  234. * @param newValue New Time
  235. */
  236. public void setTopicTime(final long newValue) {
  237. topicTime = newValue;
  238. }
  239. /**
  240. * Set the topic setter for this channel.
  241. *
  242. * @param newValue New Setter
  243. */
  244. public void setTopicSetter(final String newValue) {
  245. topicSetter = newValue;
  246. }
  247. /** {@inheritDoc} */
  248. @Override
  249. public Map<Object, Object> getMap() {
  250. return myMap;
  251. }
  252. }