/*
 *	IRC.app -- IRC client program for NEXTSTEP
 *	Copyright (C)1995 Norihiro Itoh
 *
 *	このソースはIRC.appの一部です。
 *	This file is part of IRC.app.
 *
 *	本プログラムはフリー・ソフトウェアです。あなたは、Free Software 
 *	Foundationが公表したGNU一般公有使用許諾の「バージョン２」或いは
 *	それ以降の各バージョンの中からいずれかを選択し、そのバージョンが
 *	定める条項に従って本プログラムを再頒布または変更することができま
 *	す。
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2 of the License, or
 *	(at your option) any later version.
 *
 *	本プログラムは有用とは思いますが、頒布にあたっては、市場性及び特
 *	定目的適合性についての暗黙の保証を含めて、いかなる保証も行ないま
 *	せん。詳細についてはGNU一般公有使用許諾書をお読みください。
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	あなたは、本プログラムと一緒にGNU一般公有使用許諾の写しを受け取っ
 *	ているはずです。そうでない場合は、Free Software Foundation, Inc.,
 *	675 Mass Ave, Cambridge, MA 02139, USAへ手紙を書いてください。
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *	作者のメールアドレス: nito@scorpio.bekkoame.or.jp
 *	Author's e-mail address: nito@scorpio.bekkoame.or.jp
 */	

#import "IRCManager.h"

@implementation IRCManager

- appDidInit:sender
{
    char	path[MAXPATHLEN+1];

    if ([[NXBundle mainBundle] getPath:path
		forResource:"message" ofType:"strings"]) {
	[messageTable readFromFile:path];
    }
    channelTable = [[HashTable alloc] initKeyDesc:"*" valueDesc:"@"];
    head = 3;
    kind = JAPANESE_CHANNEL_ONLY;
    listStorage = [[Storage alloc] initCount:30 elementSize:50
    			description:"*"];
    [channelTable empty];
    [listStorage empty];
    [channelBrowser loadColumnZero];
    if ([chatManager connect:nil]) {
    	[self showChannelPanel:nil];
    } else {
    	[self showPreference:nil];
    }
    return self;
}

- terminate:sender
{
    [chatManager disconnect:nil];
    [super terminate:self];
    return self;
}

- showPreference:sender
{
    if (preferenceManager == nil) {
	preferenceManager = [[PreferenceManager alloc] init];
    }
    [preferenceManager showPreference:self];
    return self;
}

- showChannelPanel:sender
{
    [channelPanel makeKeyAndOrderFront:nil];
    [channelName selectText:nil];
    return self;
}

- list:sender
{
    [chatManager sendLIST];
    return self;
}

- openChannel:sender
{
    id			manager;
    const char		*name;
    
    name = NXCopyStringBuffer([channelName stringValue]);
    if ((strlen(name) <= 1) || (*name != '#')) {
    	NXBeep();
    	[channelName selectText:nil];
	return nil;
    }
    if ([channelTable isKey:name] == YES) return nil;
    
    [chatManager sendJOIN:(char *)name];
    manager = [[[ChannelManager alloc] init] showChannel:name];
    if (manager) {
    	[channelTable insertKey:name value:manager];
	[inputPanel makeKeyAndOrderFront:nil];
	[inputField selectText:nil];
    } else {
    	[manager free];
    }
    return self;
}

- closeChannel:(const char *)channel
{
    [channelTable removeKey:channel];
    if ([channelTable count] == 0) [inputPanel orderOut:nil];
    return nil;
}

- sendMessage:sender
{
    id		window;
    id		manager;
    
    if ((window = [NXApp mainWindow]) == nil) {
    	NXBeep();
	return nil;
    }

    manager = [window delegate];
    [manager writeMessage:(char *)[inputField stringValue]
    		from:(char *)[chatManager takeNickName]];
    [chatManager sendPRIVMSG:(char *)[inputField stringValue]
    		to:[manager channelName]];
    [inputField selectText:nil];
    return self;
}

- sendAway:sender
{
    if ([awayButton state]) {
	[inputField setEnabled:NO];
	[chatManager sendAWAY:"I'm away now"];
    } else {
	[[inputField setEnabled:YES] selectText:nil];
	[chatManager sendAWAY];
    }
    return self;
}

- chatManager
{
    return chatManager;
}

- managerOf:(char *)channel
{
    if ([channelTable isKey:channel] == NO) return nil;
    return (id)[channelTable valueForKey:channel];
}

- startList
{
    head = [count intValue];
    kind = [channelKind selectedTag];
    [listStorage empty];
    [listButton setEnabled:NO];
    return self;
}

- setList:(char *)item
{
    char		*cname;
    int			chead, jflag, i, imax;
    
    strtok(item, " \r\n");
    cname = strtok(NULL, " \r\n");
    chead = atoi(strtok(NULL, " \r\n"));

    if (chead > head) return nil;
    if (kind == JAPANESE_CHANNEL_ONLY) {
    	jflag = 0;
	imax = strlen(cname);
    	for (i = 0; i < imax; i++) {
	    if ((unsigned char)(*(cname + i)) > 0x7F) {
	    	jflag = 1;
		break;
	    }
	}
	if (jflag == 0) return nil;
    }
	    
    [listStorage addElement:(void *)cname];
    return self;
}

- endList
{
    [channelBrowser loadColumnZero];
    [listButton setEnabled:YES];
    return self;
}


- (int)browser:sender fillMatrix:matrix inColumn:(int)column
{
    int			row = 0;
    int			i;
    int			numOfRecords;
    NXBrowserCell	*cell;

    numOfRecords = [listStorage count];
    for(i = 0; i < numOfRecords; i++) {
	[matrix addRow];
	cell = [matrix cellAt:row++ :column];
	[cell setStringValue:(const char *)[listStorage elementAt:i]];
	[cell setLoaded:YES];
	[cell setLeaf:YES];
    }
    return row;
}

@end
