OmnomIRCActivity.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. package org.omnimaga.omnomirc;
  2. import com.loopj.android.http.*;
  3. import java.io.IOException;
  4. import java.text.DateFormat;
  5. import java.util.ArrayList;
  6. import java.util.Date;
  7. import java.util.List;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.entity.UrlEncodedFormEntity;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.cookie.Cookie;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.apache.http.protocol.HTTP;
  18. import org.apache.http.util.EntityUtils;
  19. import android.app.Activity;
  20. import android.content.Intent;
  21. import android.content.SharedPreferences;
  22. import android.os.Bundle;
  23. import android.preference.PreferenceManager;
  24. import android.text.Html;
  25. import android.text.TextUtils;
  26. import android.text.method.ScrollingMovementMethod;
  27. import android.util.Log;
  28. import android.view.Menu;
  29. import android.view.MenuInflater;
  30. import android.view.MenuItem;
  31. import android.widget.TextView;
  32. public class OmnomIRCActivity extends Activity {
  33. /** Called when the activity is first created. */
  34. String signature;
  35. String username = "Guest";
  36. String channel;
  37. String curLine;
  38. Boolean connected = false;
  39. AsyncHttpClient httpclient = new AsyncHttpClient();
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main);
  44. TextView text = (TextView)findViewById(R.id.textOutput);
  45. text.setMovementMethod(new ScrollingMovementMethod());
  46. connect();
  47. }
  48. @Override
  49. public boolean onCreateOptionsMenu(Menu menu) {
  50. MenuInflater inflater = getMenuInflater();
  51. inflater.inflate(R.menu.menu, menu);
  52. return true;
  53. }
  54. @Override
  55. public boolean onOptionsItemSelected(MenuItem item) {
  56. // Handle item selection
  57. switch (item.getItemId()) {
  58. case R.id.reconnect:
  59. connected = false;
  60. connect();
  61. return true;
  62. case R.id.prefs:
  63. Intent myIntent = new Intent(getBaseContext(), Prefs.class);
  64. startActivityForResult(myIntent, 0);
  65. return true;
  66. default:
  67. return super.onOptionsItemSelected(item);
  68. }
  69. }
  70. public void connect()
  71. {
  72. TextView text = (TextView)findViewById(R.id.textOutput);
  73. SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
  74. String user = app_preferences.getString("username", "");
  75. String pass = app_preferences.getString("password", "");
  76. channel = app_preferences.getString("channel", "#omnimaga");
  77. Auth(user, pass);
  78. if(signature.equals("") && username.equals("Guest"))
  79. {
  80. text.append("Login failed. Wrong username or password.\n");
  81. }
  82. else
  83. {
  84. text.append("Successfully logged in. Welcome "+username+"!\n");
  85. load();
  86. }
  87. }
  88. public void Auth(String user, String pass)
  89. {
  90. DefaultHttpClient httpclient = new DefaultHttpClient();
  91. try {
  92. HttpGet httpget = new HttpGet("http://www.omnimaga.org/index.php?action=login");
  93. HttpResponse response = httpclient.execute(httpget);
  94. HttpEntity entity = response.getEntity();
  95. Log.d("OmnomAuth", "Login form get: " + response.getStatusLine());
  96. //EntityUtils.consume(entity);
  97. Log.d("OmnomAuth", "Initial set of cookies:");
  98. List<Cookie> cookies = httpclient.getCookieStore().getCookies();
  99. if (cookies.isEmpty()) {
  100. Log.d("OmnomAuth", "None");
  101. } else {
  102. for (int i = 0; i < cookies.size(); i++) {
  103. Log.d("OmnomAuth", "- " + cookies.get(i).toString());
  104. }
  105. }
  106. HttpPost httpost = new HttpPost("http://www.omnimaga.org/index.php?action=login2");
  107. List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  108. nvps.add(new BasicNameValuePair("user", user));
  109. nvps.add(new BasicNameValuePair("passwrd", pass));
  110. nvps.add(new BasicNameValuePair("cookieneverexp", "true"));
  111. httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
  112. response = httpclient.execute(httpost);
  113. entity = response.getEntity();
  114. Log.d("OmnomAuth", "Login form get: " + response.getStatusLine());
  115. //EntityUtils.consume(entity);
  116. Log.d("OmnomAuth", "Post logon cookies:");
  117. cookies = httpclient.getCookieStore().getCookies();
  118. if (cookies.isEmpty()) {
  119. Log.d("OmnomAuth", "None");
  120. } else {
  121. for (int i = 0; i < cookies.size(); i++) {
  122. Log.d("OmnomAuth", "- " + cookies.get(i).toString());
  123. }
  124. }
  125. HttpGet httpget2 = new HttpGet("http://www.omnimaga.org/checkLogin.php?txt");
  126. response = httpclient.execute(httpget2);
  127. entity = response.getEntity();
  128. String page = EntityUtils.toString(entity);
  129. signature = page.split("\n")[0];
  130. username = page.split("\n")[1];
  131. Log.d("OmnomAuth", "Signature: "+signature+"\n");
  132. Log.d("OmnomAuth", "Username: "+username+"\n");
  133. } catch (Exception e) {
  134. Log.e("OmnomAuth", e.getMessage());
  135. } finally {
  136. // When HttpClient instance is no longer needed,
  137. // shut down the connection manager to ensure
  138. // immediate deallocation of all system resources
  139. httpclient.getConnectionManager().shutdown();
  140. }
  141. }
  142. public void load()
  143. {
  144. SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
  145. String linesNum = "50"; //app_preferences.getString("scrollback", "50");
  146. RequestParams params = new RequestParams();
  147. params.put("count", linesNum);
  148. params.put("channel", new String(Base64.encodeBytes(channel.getBytes())));
  149. params.put("nick", new String(Base64.encodeBytes(username.getBytes())));
  150. params.put("signature", new String(Base64.encodeBytes(signature.getBytes())));
  151. httpclient.get("http://omnomirc.www.omnimaga.org/load.php", params, new AsyncHttpResponseHandler()
  152. {
  153. @Override
  154. public void onSuccess(String response)
  155. {
  156. String lines[] = response.split(";");
  157. for(int i=0; i<lines.length; i++)
  158. {
  159. if(lines[i].startsWith("addLine('"))
  160. {
  161. addLine(lines[i].substring(9, lines[i].length()-2));
  162. }
  163. }
  164. connected = true;
  165. update();
  166. }
  167. });
  168. /*
  169. try {
  170. HttpGet httpget = new HttpGet("http://omnomirc.www.omnimaga.org/load.php?"+"&signature="+);
  171. HttpResponse response = httpclient.execute(httpget);
  172. HttpEntity entity = response.getEntity();
  173. String page = EntityUtils.toString(entity);
  174. String lines[] = page.split(";");
  175. for(int i=0; i<lines.length; i++)
  176. {
  177. if(lines[i].startsWith("addLine('"))
  178. {
  179. addLine(lines[i].substring(9, lines[i].length()-2));
  180. }
  181. }
  182. } catch (Exception e) {
  183. Log.e("OmnomLoad", "Error: "+e.getMessage());
  184. } finally {
  185. // When HttpClient instance is no longer needed,
  186. // shut down the connection manager to ensure
  187. // immediate deallocation of all system resources
  188. httpclient.getConnectionManager().shutdown();
  189. }*/
  190. }
  191. AsyncHttpResponseHandler updateHandler = new AsyncHttpResponseHandler()
  192. {
  193. @Override
  194. public void onSuccess(String response)
  195. {
  196. String lines[] = response.split("\n");
  197. for(int i=0; i<lines.length; i++)
  198. {
  199. addLine(lines[i]);
  200. }/*
  201. try {
  202. //wait(1000);
  203. } catch (InterruptedException e) {
  204. // TODO Auto-generated catch block
  205. e.printStackTrace();
  206. }*/
  207. update();
  208. }
  209. public void onFailure(Throwable t)
  210. {
  211. TextView text = (TextView)findViewById(R.id.textOutput);
  212. connected = false;
  213. text.append(Html.fromHtml("<font color=\"#C73232\">Connection lost. Please reconnect.</font><br/>"));
  214. Log.e("update", t.getMessage(), t);
  215. }
  216. };
  217. public void update()
  218. {
  219. Log.d("update", "Updating");
  220. RequestParams params = new RequestParams();
  221. params.put("lineNum", curLine);
  222. params.put("channel", new String(Base64.encodeBytes(channel.getBytes())));
  223. params.put("nick", new String(Base64.encodeBytes(username.getBytes())));
  224. params.put("signature", new String(Base64.encodeBytes(signature.getBytes())));
  225. httpclient.get("http://omnomirc.www.omnimaga.org/update.php", params, updateHandler);
  226. Log.d("update", "Update complete");
  227. /*
  228. try {
  229. HttpGet httpget = new HttpGet("http://omnomirc.www.omnimaga.org/update.php?"+"&channel="+new String(Base64.encodeBytes(channel.getBytes()))+"&nick="+new String(Base64.encodeBytes(username.getBytes()))+"&signature="+new String(Base64.encodeBytes(signature.getBytes())));
  230. HttpResponse response = httpclient.execute(httpget);
  231. HttpEntity entity = response.getEntity();
  232. String page = EntityUtils.toString(entity);
  233. String lines[] = page.split("\n");
  234. for(int i=0; i<lines.length; i++)
  235. {
  236. addLine(lines[i]);
  237. }
  238. } catch (Exception e) {
  239. Log.e("OmnomLoad", "Error: "+e.getMessage());
  240. } finally {
  241. // When HttpClient instance is no longer needed,
  242. // shut down the connection manager to ensure
  243. // immediate deallocation of all system resources
  244. httpclient.getConnectionManager().shutdown();
  245. }
  246. */
  247. }
  248. public void addLine(String message)
  249. {
  250. TextView text = (TextView)findViewById(R.id.textOutput);
  251. String[] messageParts = message.split(":");
  252. if(messageParts.length >= 4)
  253. {
  254. for (int i=4;i<messageParts.length;i++)
  255. {
  256. try {
  257. messageParts[i] = new String(Base64.decode(messageParts[i].replace(',', '='), Base64.URL_SAFE));
  258. } catch (IOException e) {
  259. Log.e("Base64Decode", e.getMessage());
  260. }
  261. }
  262. curLine = messageParts[0]; //This is a global var for a reason
  263. String type = messageParts[1];
  264. String online = messageParts[2];
  265. Date time = new Date(Long.parseLong(messageParts[3])*1000);
  266. Log.d("addLine", message);
  267. if(!type.equals("curline"))
  268. text.append("[" + DateFormat.getTimeInstance(DateFormat.MEDIUM).format(time) + "] ");
  269. if(type.equals("pm"))
  270. {
  271. text.append("[PM]Ê");
  272. //TODO: got ping'd, do something
  273. }
  274. if(type.equals("message") || type.equals("pm"))
  275. {
  276. text.append(Html.fromHtml("&lt;<font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font>&gt; " + TextUtils.htmlEncode(messageParts[5])));
  277. }
  278. if(type.equals("message") || type.equals("action"))
  279. {
  280. if(messageParts[5].contains(username))
  281. {
  282. //TODO: got ping'd, do something
  283. }
  284. }
  285. if(type.equals("action"))
  286. {
  287. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> " + TextUtils.htmlEncode(messageParts[5])));
  288. }
  289. if(type.equals("join"))
  290. {
  291. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#2A8C2A\">has joined "+channel+"</font>"));
  292. }
  293. if(type.equals("quit"))
  294. {
  295. if(messageParts.length >= 6)
  296. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#66361F\">has quit IRC ("+ TextUtils.htmlEncode(messageParts[5]) +")</font>"));
  297. else
  298. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#66361F\">has quit IRC</font>"));
  299. }
  300. if(type.equals("part"))
  301. {
  302. if(messageParts.length >= 6)
  303. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#66361F\">has left "+channel+" ("+ TextUtils.htmlEncode(messageParts[5]) +")</font>"));
  304. else
  305. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#66361F\">has left "+channel+"</font>"));
  306. }
  307. if(type.equals("mode"))
  308. {
  309. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#2A8C2A\">set "+channel+" mode "+ TextUtils.htmlEncode(messageParts[5]) +"</font>"));
  310. }
  311. if(type.equals("nick"))
  312. {
  313. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#2A8C2A\">has changed his nick to</font> <font color=\"" + color_of(messageParts[5]) + "\">"+ TextUtils.htmlEncode(messageParts[5]) +"</font>"));
  314. }
  315. if(type.equals("kick"))
  316. {
  317. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> <font color=\"#C73232\">has kicked</font> <font color=\"" + color_of(messageParts[5]) + "\">" + TextUtils.htmlEncode(messageParts[5]) + "</font> <font color=\"#C73232\">from "+channel+" ("+ TextUtils.htmlEncode(messageParts[6]) +")</font>"));
  318. }
  319. if(type.equals("topic"))
  320. {
  321. text.append(Html.fromHtml("* <font color=\"" + color_of(messageParts[4]) + "\">" + TextUtils.htmlEncode(messageParts[4]) + "</font> set topic to " + TextUtils.htmlEncode(messageParts[5])));
  322. }
  323. text.append("\n");
  324. }
  325. }
  326. public String color_of(String name)
  327. {
  328. String rcolors[] = { "#2A8C2A", "#C33B3B", "#80267F", "#D9A641", "#3DCC3D", "#1A5555", "#2F8C74", "#4545E6", "#B037B0" };
  329. int sum = 0;
  330. for (int i = 0; i < name.length(); i++)
  331. sum += name.getBytes()[i];
  332. sum %= rcolors.length;
  333. return rcolors[sum];
  334. }
  335. }