Skip to content

Instantly share code, notes, and snippets.

@justjanne
Created January 20, 2015 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justjanne/b6e53dd9559660df9c92 to your computer and use it in GitHub Desktop.
Save justjanne/b6e53dd9559660df9c92 to your computer and use it in GitHub Desktop.
/*
* The addIrcUser function in the Network class is called whenever a new
* IRC user appears on a given network.
*/
} else if (className.equals("Network")) {
switch(function) {
case "addIrcUser":
String nick = (String) packedFunc.remove(0).getData();
IrcUser user = new IrcUser();
user.nick = nick.split("!")[0];
//If not done then we can add it right here, if we try to send it we might crash because service don't have the network yet
if (!initComplete) {
networks.get(Integer.parseInt(objectName)).onUserJoined(user);
} else {
handler.obtainMessage(R.id.NEW_USER_ADDED, Integer.parseInt(objectName), 0, user).sendToTarget();
}
sendInitRequest("IrcUser", objectName + "/" + nick.split("!")[0]);
break;
case "setConnectionState":
Log.d(TAG, "Sync: Network -> setConnectionState");
int networkId = Integer.parseInt(objectName);
Network.ConnectionState state = ConnectionState.getForValue((Integer) packedFunc.remove(0).getData());
//If network has no status buffer it is the first time we are connecting to it
if (state == ConnectionState.Connecting && networks.get(networkId).getStatusBuffer() == null) {
//Create the new buffer object for status buffer
QuasselDbHelper dbHelper = new QuasselDbHelper(applicationContext);
BufferInfo info = new BufferInfo();
maxBufferId += 1;
info.id = maxBufferId;
info.networkId = networkId;
info.type = BufferInfo.Type.StatusBuffer;
Buffer buffer = new Buffer(info, dbHelper);
buffers.put(info.id, buffer);
handler.obtainMessage(R.id.SET_STATUS_BUFFER, networkId, 0, buffer).sendToTarget();
}
handler.obtainMessage(R.id.SET_CONNECTION_STATE, networkId, 0, state).sendToTarget();
break;
case "addIrcChannel":
Log.d(TAG, "Sync: Network -> addIrcChannel");
networkId = Integer.parseInt(objectName);
String bufferName = (String) packedFunc.remove(0).getData();
System.out.println(bufferName);
boolean hasBuffer = networks.get(networkId).getBuffers().hasBuffer(bufferName);
if (!hasBuffer) {
//Create the new buffer object
QuasselDbHelper dbHelper = new QuasselDbHelper(applicationContext);
BufferInfo info = new BufferInfo();
info.name = bufferName;
info.id = -1;
info.networkId = networkId;
info.type = BufferInfo.Type.ChannelBuffer;
Buffer buffer = new Buffer(info, dbHelper);
Message msg = handler.obtainMessage(R.id.NEW_BUFFER_TO_SERVICE, buffer);
msg.sendToTarget();
}
sendInitRequest("IrcChannel", objectName + "/" + bufferName);
break;
case "setConnected":
case "setMyNick":
case "setLatency":
case "setNetworkName":
case "setIdentity":
case "setCurrentServer":
try {
SyncMethodMessage p = new SyncMethodMessage();
p.from(className, objectName, function, packedFunc);
Message msg = handler.obtainMessage(R.id.DIRECT_MESSAGE);
msg.obj = p;
msg.sendToTarget();
} catch (ArrayIndexOutOfBoundsException e) {
Log.e(TAG, "Tried to execute "+function+"() on nonexisting object "+objectName);
}
break;
}
} else if (className.equals("IrcUser")) {
switch (function) {
case "partChannel":
Log.d(TAG, "Sync: IrcUser -> partChannel");
String[] tmp = objectName.split("/", 2);
int networkId = Integer.parseInt(tmp[0]);
String userName = tmp[1];
Bundle bundle = new Bundle();
bundle.putString("nick", userName);
bundle.putString("buffer", (String) packedFunc.remove(0).getData());
handler.obtainMessage(R.id.USER_PARTED, networkId, 0, bundle).sendToTarget();
break;
case "quit":
Log.d(TAG, "Sync: IrcUser -> quit");
tmp = objectName.split("/", 2);
networkId = Integer.parseInt(tmp[0]);
userName = tmp[1];
handler.obtainMessage(R.id.USER_QUIT, networkId, 0, userName).sendToTarget();
break;
case "setNick":
case "setServer":
case "setAway":
case "setAwayMessage":
case "setRealName":
Log.d(TAG, "Sync: "+className+" -> "+function);
try {
SyncMethodMessage p = new SyncMethodMessage();
p.from(className, objectName, function, packedFunc);
Message msg = handler.obtainMessage(R.id.DIRECT_MESSAGE);
msg.obj = p;
msg.sendToTarget();
} catch (ArrayIndexOutOfBoundsException e) {
final String networkIdString = objectName.split("/")[0];
Log.e(TAG, "Tried to execute "+function+"() on nonexisting object "+objectName);
Log.e(TAG, "Existing objects in Net: "+Client.getInstance().getNetworks().getNetworkById(Integer.valueOf(objectName.split("/")[0])).getUserList());
Log.e(TAG, "Existing objects in Client: "+Collections2.filter(Client.getInstance().getObjects(objectName).keySet(), new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(networkIdString);
}
}));
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment