Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TwitterClientInfo.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) 2006-2009 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.parser_twitter;
  23. import com.dmdirc.addons.parser_twitter.api.TwitterUser;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  26. import com.dmdirc.parser.interfaces.LocalClientInfo;
  27. import com.dmdirc.parser.interfaces.Parser;
  28. import com.dmdirc.parser.interfaces.callbacks.ChannelNickChangeListener;
  29. import com.dmdirc.parser.interfaces.callbacks.ChannelUserModeChangeListener;
  30. import com.dmdirc.plugins.Plugin;
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. /**
  36. * ClientInfo class for the Twitter plugin.
  37. *
  38. * @author shane
  39. */
  40. public class TwitterClientInfo implements LocalClientInfo {
  41. /** This Clients User */
  42. private String myUser;
  43. /** My Parser */
  44. private Twitter myParser;
  45. /** Is this a fake client created just for a callback? */
  46. private boolean isFake = false;
  47. /** Map of random objects. */
  48. final Map<Object, Object> myMap = new HashMap<Object, Object>();
  49. /** List of my channelclients. */
  50. final List<ChannelClientInfo> channelClients = new ArrayList<ChannelClientInfo>();
  51. /**
  52. * Parse an IRC Hostname into its separate parts.
  53. *
  54. * @param hostname Hostname to parse
  55. * @return String array of nick, ident and host.
  56. */
  57. static String[] parseHostFull(final String hostname) {
  58. return parseHostFull(hostname, null, null);
  59. }
  60. /**
  61. * Parse an IRC Hostname into its separate parts.
  62. *
  63. * @param hostname Hostname to parse.
  64. * @param plugin Plugin to use to get domain from.
  65. * @return String array of nick, ident and host.
  66. */
  67. static String[] parseHostFull(String hostname, final Plugin plugin, final Twitter parser) {
  68. boolean hadAt = false;
  69. if (plugin != null && parser != null && parser.getConfigManager().getOptionBool(plugin.getDomain(), "autoAt")) {
  70. if (!hostname.isEmpty() && hostname.charAt(0) == '@') {
  71. hostname = hostname.substring(1);
  72. hadAt = true;
  73. }
  74. }
  75. String[] temp = null;
  76. final String[] result = new String[3];
  77. if (!hostname.isEmpty() && hostname.charAt(0) == ':') { hostname = hostname.substring(1); }
  78. temp = hostname.split("@", 2);
  79. if (temp.length == 1) { result[2] = ""; } else { result[2] = temp[1]; }
  80. temp = temp[0].split("!", 2);
  81. if (temp.length == 1) { result[1] = ""; } else { result[1] = temp[1]; }
  82. result[0] = (hadAt ? "@" : "") + temp[0];
  83. return result;
  84. }
  85. /**
  86. * Return the nickname from an irc hostname.
  87. *
  88. * @param hostname host to parse
  89. * @return nickname
  90. */
  91. static String parseHost(final String hostname) {
  92. return parseHostFull(hostname)[0];
  93. }
  94. /**
  95. * Create a new TwitterClientInfo
  96. *
  97. * @param user User object for this client.
  98. * @param parser Parser that owns this client.
  99. */
  100. public TwitterClientInfo(final String user, final Twitter parser) {
  101. this.myUser = user;
  102. this.myParser = parser;
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public void setNickname(final String name) {
  107. if (this == myParser.getLocalClient()) {
  108. // TODO: throw new UnsupportedOperationException("Not supported yet.");
  109. } else {
  110. // TODO: throw new UnsupportedOperationException("Can not set nickname on non-local clients");
  111. }
  112. }
  113. /**
  114. * Get the user object for this client.
  115. *
  116. * @return User object for this client.
  117. */
  118. public TwitterUser getUser() {
  119. return myParser.getApi().getCachedUser(myUser);
  120. }
  121. /**
  122. * Check if this is a fake client.
  123. *
  124. * @return True if this is a fake client, else false
  125. */
  126. public boolean isFake() { return isFake; }
  127. /**
  128. * Check if this client is actually a server.
  129. *
  130. * @return True if this client is actually a server.
  131. */
  132. public boolean isServer() { return !(myUser.indexOf(':') == -1); }
  133. /**
  134. * Set if this is a fake client.
  135. * This returns "this" and thus can be used in the construction line.
  136. *
  137. * @param newValue new value for isFake - True if this is a fake client, else false
  138. * @return this Object
  139. */
  140. public TwitterClientInfo setFake(final boolean newValue) {
  141. isFake = newValue;
  142. return this;
  143. }
  144. /** {@inheritDoc} */
  145. @Override
  146. public String getModes() {
  147. return "";
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public void setAway(final String reason) {
  152. return;
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public void setBack() {
  157. return;
  158. }
  159. /** {@inheritDoc} */
  160. @Override
  161. public void alterMode(final boolean add, final Character mode) {
  162. return;
  163. }
  164. /** {@inheritDoc} */
  165. @Override
  166. public void flushModes() {
  167. return;
  168. }
  169. /** {@inheritDoc} */
  170. @Override
  171. public String getNickname() {
  172. return myUser;
  173. }
  174. /** {@inheritDoc} */
  175. @Override
  176. public String getUsername() {
  177. return "user";
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. public String getHostname() {
  182. return "twitter.com";
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public String toString() {
  187. return getNickname() + "!" + getUsername() + "@" + getHostname();
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public String getRealname() {
  192. return String.format("%s - http://%s/%s", getUser().getRealName(), getHostname(), getNickname());
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public int getChannelCount() {
  197. synchronized (channelClients) {
  198. return channelClients.size();
  199. }
  200. }
  201. /**
  202. * Get a list of all the channel clients associated with this user.
  203. *
  204. * @return Channel Clients for this Client.
  205. */
  206. public List<ChannelClientInfo> getChannelClients() {
  207. synchronized (channelClients) {
  208. return new ArrayList<ChannelClientInfo>(channelClients);
  209. }
  210. }
  211. /** {@inheritDoc} */
  212. @Override
  213. public Map<Object, Object> getMap() {
  214. return myMap;
  215. }
  216. /** {@inheritDoc} */
  217. @Override
  218. public Parser getParser() {
  219. return myParser;
  220. }
  221. /**
  222. * Add a channelClient to this Client.
  223. *
  224. * @param channelClient channelClient to add as us.
  225. */
  226. public void addChannelClient(final TwitterChannelClientInfo channelClient) {
  227. synchronized (channelClients) {
  228. if (!channelClients.contains(channelClient)) {
  229. channelClients.add(channelClient);
  230. }
  231. }
  232. }
  233. /**
  234. * Remove a channelclient from this client..
  235. *
  236. * @param channelClient channelClient to remove.
  237. */
  238. public void delChannelClient(final TwitterChannelClientInfo channelClient) {
  239. synchronized (channelClients) {
  240. if (channelClients.contains(channelClient)) {
  241. channelClients.remove(channelClient);
  242. }
  243. }
  244. }
  245. }