OmnomIRCActivity.java 14 KB

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