Browse Source

Better JavaScript API. Allow channel managment

Nathaniel van Diepen 10 years ago
parent
commit
1767180fa7
4 changed files with 245 additions and 21 deletions
  1. 121 2
      lib/irc.php
  2. 34 3
      site/api/index.php
  3. 23 4
      site/index.php
  4. 67 12
      site/js/index.js

+ 121 - 2
lib/irc.php

@@ -97,7 +97,7 @@
 		}
 		return $r;
 	}
-	function ircclose($code=0,$message=null){
+	function ircclose($code=0,$message=null,$ret_type='string'){
 		global $msg;
 		global $irc;
 		global $ircret;
@@ -113,7 +113,15 @@
 			}
 		}
 		fclose($irc);
-		$ircret = '{"code":'.$code.',"message":"'.$message.'","log":'.json_encode($msg).'}';
+		if($ret_type == 'string'){
+			$ircret = '{"code":'.$code.',"message":"'.$message.'","log":'.json_encode($msg).'}';
+		}else{
+			$ircret = Array(
+				'code'=>$code,
+				'message'=>$message,
+				'log'=>$msg
+			);
+		}
 		return $ircret;
 	}
 	function isval($src,$prop,$val){
@@ -204,4 +212,115 @@
 		}
 		return '{"code":0,"message":"'.__('Rehashed. View console for output.').'","log":'.json_encode($msg).'}';
 	}
+	function irccommands($commands,$runas="RehashServ"){
+		global $msg;
+		global $irc;
+		global $ircret;
+		global $u;
+		global $user;
+		$ircret = Array(
+			'code'=>1
+		);
+		if(!isset($u)){
+			$u = $user;
+		}
+		$msg = '';
+		if(!$irc = fsockopen(get_conf('irc-server'),get_conf('irc-port'))){return ircclose(1,__("Could not connect."),'array');}
+		stream_set_timeout($irc,1) or ircclose(2,__("Could not set timeout."),'array');
+		while(!feof($irc)&&!$msg = fgets($irc,128)){}
+		if($runas == 'RehashServ'){
+			if(!ircputs("NICK RehashServ\r\n")){return $ircret;}
+			if(!ircputs("USER RehashServ omni.irc.omnimaga.org RehashServ :RehashServ\r\n")){return $ircret;}
+		}else{
+			if(!ircputs("NICK RunServ\r\n")){return $ircret;}
+			if(!ircputs("USER {$runas} omni.irc.omnimaga.org {$runas} :{$runas}\r\n")){return $ircret;}
+		}
+		while(!feof($irc)){
+			$line = fgets($irc,128);
+			if(is_string($line)){
+				$msg .= $line;
+				$data = explode(' ',$line);
+				if(isval($data,1,'433')){
+					return ircclose(4,__("RunServ is already running."),'array');
+				}elseif(strrpos($line,'ERROR :Closing Link:') !== false){
+					return ircclose(3,__("IRC Server refused the connection."),'array');
+				}elseif($data[0] == 'PING'){
+					if(!ircputs("PONG {$data[1]}")){return $ircret;}
+				}elseif(isval($data,1,'001')){
+					break;
+				}
+			}
+		}
+		if($runas == 'RehashServ'){
+			if(!ircputs("OPER RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
+			if(!ircputs("IDENTIFY RehashServ ".get_conf('rehash-pass','string')."\r\n")){return $ircret;}
+		}else{
+			if(!ircputs("IDENTIFY {$runas} ".$_SESSION['password']."\r\n")){return $ircret;}
+		}
+		while(!feof($irc)){
+			$line = fgets($irc,128);
+			if(is_string($line)){
+				$msg .= $line;
+				$data = explode(' ',$line);
+				if(isval($data,1,'433')){
+					return ircclose(4,__("RunServ is already running."),'array');
+				}elseif(isval($data,1,'375')){
+					while(!feof($irc)){
+						$line = fgets($irc,128);
+						if(is_string($line)){
+							$msg .= $line;
+							$data = explode(' ',$line);
+							if(isval($data,1,'376')){
+								break;
+							}
+						}
+					}
+				}elseif(strrpos($line,'ERROR :Closing Link:') !== false){
+					return ircclose(3,__("IRC Server refused the connection."),'array');
+				}elseif(strrpos($line,":You are now identified for") !== false){
+					break;
+				}elseif(strrpos($line,'Password incorrect.') !== false){
+					return ircclose(5,__("Failed to authenticate with NickServ"),'array');
+				}
+			}
+		}
+		if($runas == 'RehashServ'){
+			if(!ircputs("HS ON\r\n")){return $ircret;}
+			while(!feof($irc)){
+				$line = fgets($irc,128);
+				if(is_string($line)){
+					$msg .= $line;
+					$data = explode(' ',$line);
+					if(isval($data,1,'433')){
+						return ircclose(4,__("RunServ is already running."),'array');
+					}elseif(strrpos($line,'ERROR :Closing Link:') !== false){
+						return ircclose(3,__("IRC Server refused the connection."),'array');
+					}elseif(strrpos($line,':Your vhost of') !== false && strrpos($line,'is now activated') !== false){
+						break;
+					}elseif(strrpos($line,"Please contact an Operator to get a vhost assigned to this nick") !== false){
+						return ircclose(6,__("vhost not set."),'array');
+					}
+				}
+			}
+		}
+		foreach($commands as $k => $command){
+			if(!ircputs($command."\r\n")){return $ircret;}
+		}
+		try{
+			error_reporting(0);
+			$msg .= 'QUIT :'.$message;
+			fputs($irc,'QUIT :'.$message);
+			error_reporting(E_ALL);
+		}catch(Exception $e){}
+		while(!feof($irc) && $line = fgets($irc,128)){
+			if(is_string($line)){
+				$msg .= $line;
+			}
+		}
+		fclose($irc);
+		return array(
+			'code'=>0,
+			'log'=>$msg
+		);
+	}
 ?>

+ 34 - 3
site/api/index.php

@@ -11,8 +11,7 @@
 		case 'test':
 			//$u or die();
 			//print_r(atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'topic','#omnimaga'));
-			echo mkpasswd('root');
-			die();
+			//echo mkpasswd('root');
 		break;
 		case 'lang':
 			echo file_get_contents(DIR.'/lang/'.LOCALE.'/C/LC_MESSAGES/omninet.po');
@@ -169,10 +168,42 @@
 			isset($_GET['id']) or die('{"code":1,"message":"'.__('No id given').'"}');
 			$res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'MemoServ','delete',Array($_GET['id']));
 			if(!$res[0]){
-				die('{"code":1,"message":"'.__('Cannot send memo').': '+$res[1]+'"}');
+				die('{"code":1,"message":"'.__('Cannot delete memo').': '+$res[1]+'"}');
+			}
+			die('{"code":0}');
+		break;
+		case 'delete-channel':
+			$u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
+			isset($_GET['channel']) or die('{"code":1,"message":"'.__('No channel given').'"}');
+			$res = atheme_command(get_conf('xmlrpc-server'),get_conf('xmlrpc-port'),get_conf('xmlrpc-path'),USER_IP,$_COOKIE['user'],$_SESSION['password'],'ChanServ','drop',Array($_GET['channel']));
+			if(!$res[0]){
+				die('{"code":1,"message":"'.__('Cannot drop channel').': '+$res[1]+'"}');
 			}
 			die('{"code":0}');
 		break;
+		case 'register-channel':
+			$u or die('{"code":1,"message":"'.__('You have been logged out').'"}');
+			isset($_GET['channel']) or die('{"code":1,"message":"'.__('No channel given').'"}');
+			$ret = irccommands(array(
+				'join '.$_GET['channel'],
+				'samode '.$_GET['channel'].' +o RehashServ',
+				'cs register '.$_GET['channel'],
+				'cs set '.$_GET['channel'].' keeptopic on',
+				'cs set '.$_GET['channel'].' founder '.$_COOKIE['user']
+			));
+			if($ret['code'] !== 0){
+				die(json_encode($ret));
+			}
+			$ret2 = irccommands(array(
+				'join '.$_GET['channel'],
+				'cs set '.$_GET['channel'].' founder '.$_COOKIE['user']
+			),$_COOKIE['user']);
+			if($ret2['code'] !== 0){
+				$ret2['message'] = 'Failed to register channel. See log for information.';
+			}
+			$ret2['log'] = $ret['log']."\r\n".$ret2['log'];
+			die(json_encode($ret));
+		break;
 		case 'persona-login':
 			if($u){
 				$register = true;

+ 23 - 4
site/index.php

@@ -110,10 +110,10 @@
 										{{html this.body}}
 									</span>
 								</div>
-								<button class="button" value="<?php echo __('Reply'); ?>" onclick="return window.ReplyToMemoFromButton.call(this);">
+								<button class="button" value="<?php echo __('Reply'); ?>" onclick="return window.ReplyToMemo('{{this.from}}');">
 									<?php echo __('Reply'); ?>
 								</button>
-								<button style="background-color:red;background-image:none;" class="button" value="<?php echo __('Delete'); ?>" onclick="return window.DeleteMemoFromButton.call(this);">
+								<button style="background-color:red;background-image:none;" class="button" value="<?php echo __('Delete'); ?>" onclick="return window.DeleteMemo({{this.id}});">
 									<?php echo __('Delete'); ?>
 								</button>
 							</div>
@@ -154,7 +154,7 @@
 						<button value="<?php echo __('Refresh'); ?>" onclick="window.FetchChannels(true);">
 							<?php echo __('Refresh'); ?>
 						</button>
-						<button value="<?php echo __('New Channel'); ?>" style="background-color:green;background-image:none;" onclick="">
+						<button value="<?php echo __('New Channel'); ?>" style="background-color:green;background-image:none;" onclick="$('#channel-diag').dialog('open');">
 							<?php echo __('New Channel'); ?>
 						</button>
 						{{#each channels}}
@@ -167,7 +167,7 @@
 										<li>{{this.name}}</li>
 									{{/each}}
 								</ul>
-								<button value="<?php echo __('Delete'); ?>" style="background-color:red;background-image:none;" onclick="">
+								<button value="<?php echo __('Delete'); ?>" style="background-color:red;background-image:none;" onclick="window.DeleteChannel('{{this.name}}');">
 									<?php echo __('Delete'); ?>
 								</button>
 							</div>
@@ -246,6 +246,25 @@
 							)
 						)
 					));
+					array_push($dialogs,array(
+						'id'=>'channel-diag',
+						'type'=>'form',
+						'form_id'=>'channel',
+						'form_submit_label'=>'Register',
+						'form_fields'=>array(
+							array(
+								'name'=>'channel',
+								'label'=>__('Channel Name'),
+								'type'=>'string',
+								'value'=>''
+							),
+							array(
+								'name'=>'action',
+								'type'=>'hidden',
+								'value'=>'register-channel'
+							)
+						)
+					));
 				} ?>
 				<div id="profile">
 					<?php

+ 67 - 12
site/js/index.js

@@ -342,6 +342,12 @@ $(function(){
 				of: window
 			}
 		}).dialog('open');
+		if(typeof $.cookie('user') != 'undefined'){
+			$('#login').find('input[name=username]').val($.cookie('user'));
+		}
+		if(typeof $.cookie('type') != 'undefined'){
+			$('#login').find('select[name=type]').val($.cookie('type'));
+		}
 		$('#verify-diag').dialog('option','close',logout);
 		$('.accordion').accordion({
 			collapsible: true,
@@ -441,24 +447,16 @@ $(function(){
 				setTimeout(window.ServerPing,1000*60); // Every minute
 			}
 		};
-		window.DeleteMemoFromButton = function(){
-			window.DeleteMemo($(this).parent());
-		};
-		window.ReplyToMemoFromButton = function(){
-			window.ReplyToMemo($(this).parent());
-		};
-		window.ReplyToMemo = function(div){
-			var from = div.find('.memo-from').text().trim();
+		window.ReplyToMemo = function(from){
 			$('#memo-diag').dialog('open').find('input[name=to]').val(from);
 			$('#memo-diag').find('input[name=message]').select();
 		};
 		window.DeleteMemos = function(){
-			window.DeleteMemo($('<div>').attr('id','memo-all'),function(){
+			window.DeleteMemo('all',function(){
 				window.FetchMemos(true);
 			});
 		};
-		window.DeleteMemo = function(div,callback){
-			var id = $(div).attr('id').substr(5);
+		window.DeleteMemo = function(id,callback){
 			console.log(_("Deleting memo")+": "+id);
 			$.ajax(__HOSTNAME__+'site/api/?action=delete-memo&id='+id,{
 				success: function(d){
@@ -471,7 +469,7 @@ $(function(){
 					if(d.code!==0){
 						location.reload();
 					}
-					div.remove();
+					$('#memo-'+id).remove();
 					if(typeof callback != 'undefined'){
 						callback();
 					}
@@ -562,6 +560,63 @@ $(function(){
 				setTimeout(window.ServerPing,1000*60); // Every minute
 			}
 		};
+		window.DeleteChannel = function(channel){
+			if(confirm(_('Are you sure you want to delete channel')+' '+channel)){
+				console.log(_("Deleting channel")+": "+channel);
+				$.ajax(__HOSTNAME__+'site/api/?action=delete-channel',{
+					data: {
+						channel: channel
+					},
+					success: function(d){
+						if(d.log){
+							console.log(d.log);
+						}
+						if(d.message){
+							alert(d.message);
+						}
+						if(d.code!==0){
+							location.reload();
+						}
+						$('[id=channel-'+channel+']').remove();
+						if(typeof callback != 'undefined'){
+							callback();
+						}
+					},
+					error: function(xhr,msg,e){
+						console.error(e);
+						alert(_("Could not ping server")+": "+msg);
+						location.reload();
+					},
+					dataType: 'json'
+				});
+			}
+		};
+		window.RegisterChannel = function(channel){
+			console.log(_("Registering channel")+": "+channel);
+				$.ajax(__HOSTNAME__+'site/api/?action=register-channel',{
+					data: {
+						channel: channel
+					},
+					success: function(d){
+						if(d.log){
+							console.log(d.log);
+						}
+						if(d.message){
+							alert(d.message);
+						}
+						if(d.code!==0){
+							location.reload();
+						}
+						window.FetchChannels(true);
+					},
+					error: function(xhr,msg,e){
+						console.error(e);
+						alert(_("Could not ping server")+": "+msg);
+						location.reload();
+					},
+					dataType: 'json'
+				});
+		};
 		setInterval(function(){
 			if(LANG != window.navigator.language){
 				console.log(_('Language change detected'));