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.

TwitterClientInfo.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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.AwayState;
  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.plugins.Plugin;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. /**
  34. * ClientInfo class for the Twitter plugin.
  35. *
  36. * @author shane
  37. */
  38. public class TwitterClientInfo implements LocalClientInfo {
  39. /** This Clients User */
  40. private String myUser;
  41. /** This Clients User Id. */
  42. private long myUserId;
  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(final String hostname, final Plugin plugin,
  68. final Twitter parser) {
  69. boolean hadAt = false;
  70. String sanitisedHostname = hostname;
  71. if (plugin != null && parser != null
  72. && parser.getConfigManager().getOptionBool(plugin.getDomain(), "autoAt")
  73. && !sanitisedHostname.isEmpty() && sanitisedHostname.charAt(0) == '@') {
  74. sanitisedHostname = sanitisedHostname.substring(1);
  75. hadAt = true;
  76. }
  77. String[] temp = null;
  78. final String[] result = new String[3];
  79. if (!sanitisedHostname.isEmpty() && sanitisedHostname.charAt(0) == ':') {
  80. sanitisedHostname = sanitisedHostname.substring(1);
  81. }
  82. temp = sanitisedHostname.split("@", 2);
  83. if (temp.length == 1) {
  84. result[2] = "";
  85. } else {
  86. result[2] = temp[1];
  87. }
  88. temp = temp[0].split("!", 2);
  89. if (temp.length == 1) {
  90. result[1] = "";
  91. } else {
  92. result[1] = temp[1];
  93. }
  94. result[0] = (hadAt ? "@" : "") + temp[0];
  95. return result;
  96. }
  97. /**
  98. * Return the nickname from an irc hostname.
  99. *
  100. * @param hostname host to parse
  101. * @return nickname
  102. */
  103. static String parseHost(final String hostname) {
  104. return parseHostFull(hostname)[0];
  105. }
  106. /**
  107. * Create a new TwitterClientInfo
  108. *
  109. * @param user User object for this client.
  110. * @param parser Parser that owns this client.
  111. */
  112. public TwitterClientInfo(final String user, final Twitter parser) {
  113. this.myParser = parser;
  114. setUser(user);
  115. }
  116. /**
  117. * Set the user for this TwitterClientInfo
  118. *
  119. * @param user Name of user.
  120. */
  121. public void setUser(final String user) {
  122. this.myUser = user;
  123. final TwitterUser tu = myParser.getApi().getCachedUser(myUser);
  124. this.myUserId = tu == null ? -1 : tu.getID();
  125. }
  126. /**
  127. * Set the user for this TwitterClientInfo.
  128. *
  129. * @param user User object
  130. */
  131. public void setUser(final TwitterUser user) {
  132. if (user == null) {
  133. return;
  134. }
  135. this.myUser = user.getScreenName();
  136. this.myUserId = user.getID();
  137. }
  138. /** {@inheritDoc} */
  139. @Override
  140. public void setNickname(final String name) {
  141. // TODO: Implement?
  142. }
  143. /**
  144. * Get the user object for this client.
  145. *
  146. * @return User object for this client.
  147. */
  148. public TwitterUser getUser() {
  149. return myParser.getApi().getCachedUser(myUser);
  150. }
  151. /**
  152. * Get the user ID this client.
  153. *
  154. * @return User ID for this client.
  155. */
  156. public long getUserID() {
  157. return myUserId;
  158. }
  159. /**
  160. * Check if this is a fake client.
  161. *
  162. * @return True if this is a fake client, else false
  163. */
  164. public boolean isFake() {
  165. return isFake;
  166. }
  167. /**
  168. * Check if this client is actually a server.
  169. *
  170. * @return True if this client is actually a server.
  171. */
  172. public boolean isServer() {
  173. return !(myUser.indexOf(':') == -1);
  174. }
  175. /**
  176. * Set if this is a fake client.
  177. * This returns "this" and thus can be used in the construction line.
  178. *
  179. * @param newValue new value for isFake - True if this is a fake client, else false
  180. * @return this Object
  181. */
  182. public TwitterClientInfo setFake(final boolean newValue) {
  183. isFake = newValue;
  184. return this;
  185. }
  186. /** {@inheritDoc} */
  187. @Override
  188. public String getModes() {
  189. return "";
  190. }
  191. /** {@inheritDoc} */
  192. @Override
  193. public void setAway(final String reason) {
  194. // Do nothing
  195. }
  196. /** {@inheritDoc} */
  197. @Override
  198. public void setBack() {
  199. // Do nothing
  200. }
  201. /** {@inheritDoc} */
  202. @Override
  203. public void alterMode(final boolean add, final Character mode) {
  204. // Do nothing
  205. }
  206. /** {@inheritDoc} */
  207. @Override
  208. public void flushModes() {
  209. // Do nothing
  210. }
  211. /** {@inheritDoc} */
  212. @Override
  213. public String getNickname() {
  214. return myUser;
  215. }
  216. /** {@inheritDoc} */
  217. @Override
  218. public String getUsername() {
  219. return "user";
  220. }
  221. /** {@inheritDoc} */
  222. @Override
  223. public String getHostname() {
  224. return "twitter.com";
  225. }
  226. /** {@inheritDoc} */
  227. @Override
  228. public String toString() {
  229. return getNickname() + "!" + getUsername() + "@" + getHostname();
  230. }
  231. /** {@inheritDoc} */
  232. @Override
  233. public String getRealname() {
  234. return String.format("%s - http://%s/%s", getUser().getRealName(), getHostname(), getNickname());
  235. }
  236. /** {@inheritDoc} */
  237. @Override
  238. public String getAccountName() {
  239. return getNickname();
  240. }
  241. /** {@inheritDoc} */
  242. @Override
  243. public int getChannelCount() {
  244. synchronized (channelClients) {
  245. return channelClients.size();
  246. }
  247. }
  248. /**
  249. * Get a list of all the channel clients associated with this user.
  250. *
  251. * @return Channel Clients for this Client.
  252. */
  253. public List<ChannelClientInfo> getChannelClients() {
  254. synchronized (channelClients) {
  255. return new ArrayList<ChannelClientInfo>(channelClients);
  256. }
  257. }
  258. /** {@inheritDoc} */
  259. @Override
  260. public Map<Object, Object> getMap() {
  261. return myMap;
  262. }
  263. /** {@inheritDoc} */
  264. @Override
  265. public Parser getParser() {
  266. return myParser;
  267. }
  268. /**
  269. * Add a channelClient to this Client.
  270. *
  271. * @param channelClient channelClient to add as us.
  272. */
  273. public void addChannelClient(final TwitterChannelClientInfo channelClient) {
  274. synchronized (channelClients) {
  275. if (!channelClients.contains(channelClient)) {
  276. channelClients.add(channelClient);
  277. }
  278. }
  279. }
  280. /**
  281. * Remove a channelclient from this client..
  282. *
  283. * @param channelClient channelClient to remove.
  284. */
  285. public void delChannelClient(final TwitterChannelClientInfo channelClient) {
  286. synchronized (channelClients) {
  287. if (channelClients.contains(channelClient)) {
  288. channelClients.remove(channelClient);
  289. }
  290. }
  291. }
  292. /** {@inheritDoc} */
  293. @Override
  294. public AwayState getAwayState() {
  295. return AwayState.HERE;
  296. }
  297. /** {@inheritDoc} */
  298. @Override
  299. public String getAwayReason() {
  300. return "";
  301. }
  302. }