channel.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var channel = function(){
  2. this.name = null;
  3. this.TS = null;
  4. this.topic = { 'setter': null, 'TS': null, 'text': null };
  5. this.modes = {}; // indexed by mode name
  6. this.statusModes = {}; // indexed by UID
  7. this.listModes = {}; // indexed by mode name
  8. this.users = [];
  9. this.metadata = {};
  10. this.events = {};
  11. this.setEvents = function(events){
  12. this.events = events;
  13. };
  14. this.setTS = function(TS){
  15. this.TS = TS;
  16. };
  17. this.changeModes = function(modes){
  18. for(var mode in modes){
  19. if(mode == 'status'){
  20. for(var i=0; i<modes[mode].length; i++){
  21. var statusMode = modes[mode][i];
  22. this.statusModes[statusMode.user.uid][statusMode.name] = statusMode.status;
  23. console.log((statusMode.status?'Setting':'Unsetting')+' '+statusMode.name+' for '+statusMode.user.name+' on '+this.name);
  24. this.events.doEvent('channelStatusMode', this, statusMode);
  25. }
  26. } else if(mode == 'list'){
  27. for(var i=0; i<modes[mode].length; i++){
  28. var listMode = modes[mode][i];
  29. if(!(listMode.name in this.listModes)){
  30. this.listModes[listMode.name] = [];
  31. }
  32. if(listMode.status){ // add
  33. if(this.listModes[listMode.name].indexOf(listMode.value) < 0){
  34. this.listModes[listMode.name].push(listMode.value);
  35. }
  36. } else { // remove
  37. for(var j=this.listModes[listMode.name].length-1; j >= 0; j--){
  38. if(this.listModes[listMode.name][j] == listMode.value){
  39. this.listModes[listMode.name].splice(j, 1);
  40. }
  41. }
  42. }
  43. this.events.doEvent('channelListMode', this, listMode);
  44. }
  45. } else {
  46. this.modes[mode] = modes[mode];
  47. this.events.doEvent('channelModes', this);
  48. }
  49. }
  50. };
  51. this.joinUser = function(user){
  52. if(this.users.indexOf(user) >= 0) return;
  53. this.users.push(user);
  54. this.statusModes[user.uid] = {};
  55. user.joinChannel(this);
  56. this.events.doEvent('channelJoin', this, user);
  57. };
  58. this.removeUser = function(user){
  59. if(user.uid in this.statusModes){
  60. delete this.statusModes[user.uid]; // remove status modes for this user
  61. }
  62. for(var i=0; i<this.users.length; i++){
  63. if(this.users[i] == user){
  64. user.leaveChannel(this);
  65. this.users.splice(i, 1);
  66. this.events.doEvent('channelLeave', this, user);
  67. break;
  68. }
  69. }
  70. };
  71. this.setMetadata = function(name, visibility, value){
  72. if(value){
  73. this.metadata[name] = { 'visibility': visibility, 'value': value };
  74. } else {
  75. if(name in this.metadata){
  76. delete this.metadata[name];
  77. }
  78. }
  79. this.events.doEvent('channelMetadata', this, name);
  80. };
  81. this.setTopic = function(topic){
  82. this.topic = topic;
  83. this.events.doEvent('channelTopic', this);
  84. };
  85. }
  86. module.exports = channel;