12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289 |
- /*
- * The contents of this file are subject to the Mozilla Public
- * License Version 1.1 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of
- * the License at http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS
- * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
- * implied. See the License for the specific language governing
- * rights and limitations under the License.
- *
- * The Original Code is Vision.
- *
- * The Initial Developer of the Original Code is The Vision Team.
- * Portions created by The Vision Team are
- * Copyright (C) 1999, 2000, 2001 The Vision Team. All Rights
- * Reserved.
- *
- * Contributor(s): Wade Majors <wade@ezri.org>
- * Rene Gollent
- * Todd Lair
- * Andrew Bazan
- * Jamie Wilkinson
- * Alan Ellis <alan@cgsoftware.org>
- */
- #include <File.h>
- #include <FilePanel.h>
- #include <Path.h>
- #include <Roster.h>
- #include <FindDirectory.h>
- #include <arpa/inet.h>
- #include <ctype.h>
- #include <netdb.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/socket.h>
- #include <map>
- #include "Vision.h"
- #include "VisionBase.h"
- #include "ServerAgent.h"
- #include "ChannelAgent.h"
- #include "MessageAgent.h"
- #include "Names.h"
- #include "Utilities.h"
- #include "ClientAgent.h"
- #include "ClientWindow.h"
- #include "RunView.h"
- #include "WindowList.h"
- bool
- ClientAgent::ParseCmd (const char *data)
- {
- BString firstWord (GetWord(data, 1).ToUpper());
- BMessage sendMsg (M_SERVER_SEND);
- if (vision_app->HasAlias(firstWord))
- {
- return ParseCmd(vision_app->ParseAlias(data, fId).String());
- }
-
- if (firstWord == "/ADDALIAS")
- {
- vision_app->AddAlias(GetWord(data, 2).ToUpper(), RestOfString(data, 3));
- return true;
- }
-
- if (firstWord == "/DELALIAS")
- {
- vision_app->RemoveAlias(GetWord(data, 2).ToUpper());
- return true;
- }
- if (firstWord == "/WALLOPS" // we need to insert a ':' before parm2
- || firstWord == "/SQUIT" // for the user
- || firstWord == "/PRIVMSG")
- {
- BString theCmd (firstWord.RemoveAll ("/")),
- theRest (RestOfString (data, 2));
- AddSend (&sendMsg, theCmd);
- if (theRest != "-9z99")
- {
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theRest);
- }
- AddSend (&sendMsg, endl);
- return true;
- }
- if (firstWord == "/KILL") // we need to insert a ':' before parm3
- { // for the user
- BString theCmd (firstWord.RemoveAll ("/")),
- theTarget (GetWord (data, 2)),
- theRest (RestOfString (data, 3));
- AddSend (&sendMsg, theCmd);
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, theTarget);
- if (theRest != "-9z99")
- {
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theRest);
- }
- AddSend (&sendMsg, endl);
- return true;
- }
- // some quick aliases for scripts, these will of course be
- // moved to an aliases section eventually
- if (firstWord == "/SOUNDPLAY"
- || firstWord == "/CL-AMP")
- {
- app_info ai;
- be_app->GetAppInfo (&ai);
- BEntry entry (&ai.ref);
- if (entry.InitCheck() != B_OK)
- return true;
- BPath path;
- entry.GetPath (&path);
- path.GetParent (&path);
- path.Append ("scripts");
-
- if (firstWord == "/SOUNDPLAY")
- path.Append ("soundplay-hey");
- else
- path.Append ("cl-amp-clr");
- BString *theCmd (new BString (path.Path()));
-
- BMessage *execmsg (new BMessage);
- execmsg->AddPointer ("exec", theCmd);
- execmsg->AddPointer ("agent", this);
- thread_id execThread = spawn_thread (
- ExecPipe,
- "exec_thread",
- B_LOW_PRIORITY,
- execmsg);
- resume_thread (execThread);
- return true;
- }
- if (firstWord == "/ABOUT")
- {
- vision_app->PostMessage (B_ABOUT_REQUESTED);
- return true;
- }
- if (firstWord == "/GAWAY")
- {
- BString theReason (RestOfString (data, 2)),
- tempString;
- if (theReason == "-9z99")
- theReason = "BRB"; // Todo: make a default away msg option
-
- BString theCmd ("/AWAY ");
- theCmd += theReason;
- BMessage cmdMsg (M_SUBMIT);
- cmdMsg.AddBool("clear", true);
- cmdMsg.AddBool("history", true);
- cmdMsg.AddString("input", theCmd.String());
- vision_app->pClientWin()->ServerBroadcast(&cmdMsg);
- return true;
- }
-
- if (firstWord == "/AWAY")
- {
- BString theReason (RestOfString (data, 2)),
- tempString;
- if (theReason == "-9z99")
- theReason = "BRB"; // Todo: make a default away msg option
- const char *expansions[1];
- expansions[0] = theReason.String();
- tempString = ExpandKeyed (vision_app->GetCommand (CMD_AWAY).String(), "R",
- expansions);
- tempString.RemoveFirst("\n");
- AddSend (&sendMsg, "AWAY");
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theReason.String());
- AddSend (&sendMsg, endl);
- if (fId != fServerName)
- ActionMessage (tempString.String(), fMyNick.String());
- return true;
- }
-
- if (firstWord == "/GBACK")
- {
- BString theCmd ("/BACK");
- BMessage cmdMsg (M_SUBMIT);
- cmdMsg.AddBool("clear", true);
- cmdMsg.AddBool("history", true);
- cmdMsg.AddString("input", theCmd.String());
- vision_app->pClientWin()->ServerBroadcast(&cmdMsg);
- return true;
- }
- if (firstWord == "/BACK")
- {
- AddSend (&sendMsg, "AWAY");
- AddSend (&sendMsg, endl);
- if (fId != fServerName)
- ActionMessage (vision_app->GetCommand (CMD_BACK).String(), fMyNick.String());
- return true;
- }
-
-
- if (firstWord == "/CLEAR")
- {
- fText->Clear ();
- return true;
- }
- if (firstWord == "/NETCLEAR")
- {
- BLooper *looper (NULL);
- ServerAgent *currentserver (dynamic_cast<ServerAgent *>(fSMsgr.Target(&looper)));
- if (currentserver != NULL)
- {
- BMessage msg (M_SUBMIT);
- msg.AddString("input", "/clear");
- msg.AddBool("clear", true);
- msg.AddBool("history", false);
- currentserver->Broadcast(&msg, true);
- }
- return true;
- }
-
- if (firstWord == "/ACLEAR")
- {
- BMessage msg (M_SUBMIT);
- msg.AddString("input", "/netclear");
- msg.AddBool("clear", true);
- msg.AddBool("history", true);
- vision_app->pClientWin()->ServerBroadcast(&msg);
- return true;
- }
- if (firstWord == "/FIND" || firstWord == "/SEARCH")
- {
- BString buffer (RestOfString(data, 2));
- if (buffer != "-9z99")
- {
- fText->FindText(buffer.String());
- }
- return true;
- }
- if (firstWord == "/CTCP")
- {
- BString theTarget (GetWord (data, 2)),
- theAction (RestOfString (data, 3));
- if (theAction != "-9z99")
- {
- theAction.ToUpper();
- if (theAction.ICompare ("PING") == 0)
- {
- time_t now (time (0));
- theAction << " " << now;
- }
- CTCPAction (theTarget, theAction);
- AddSend (&sendMsg, "PRIVMSG ");
- AddSend (&sendMsg, theTarget << " :\1" << theAction << "\1");
- AddSend (&sendMsg, endl);
- }
- else
- Display ("[x] /ctcp: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/DCC")
- {
- BString secondWord (GetWord (data, 2)),
- theNick (GetWord (data, 3)),
- theFile (RestOfString(data, 4));
- if (secondWord.ICompare ("SEND") == 0
- && theNick != "-9z99")
- {
- BMessage *msg (new BMessage (M_CHOSE_FILE));
- msg->AddString ("nick", theNick.String());
- BPath sendPath;
- if (theFile != "-9z99")
- {
- if (theFile.ByteAt (0) != '/')
- {
- find_directory (B_USER_DIRECTORY, &sendPath, false);
- sendPath.Append (theFile.String(), true);
- }
- else
- sendPath.SetTo (theFile.String(), NULL, true);
- // the BFile is used to verify if the file exists
- // based off the documentation get_ref_for_path *should*
- // return something other than B_OK if the file doesn't exist
- // but that doesn't seem to be working correctly
- BFile sendFile (sendPath.Path(), B_READ_ONLY);
- // if the file exists, sendMsg, otherwise drop to the file panel
- if (sendFile.InitCheck() == B_OK)
- {
- sendFile.Unset();
- entry_ref ref;
- get_ref_for_path(sendPath.Path(), &ref);
- msg->AddRef("refs", &ref);
- fSMsgr.SendMessage(msg);
- return true;
- }
- }
- BFilePanel *myPanel (new BFilePanel (B_OPEN_PANEL, NULL, NULL, 0, false));
- BString myTitle (S_PCMD_SEND_TITLE);
- myTitle.Append (theNick);
- myPanel->Window()->SetTitle (myTitle.String());
- myPanel->SetTarget (fSMsgr);
- myPanel->SetMessage (msg);
- myPanel->SetButtonLabel (B_DEFAULT_BUTTON, S_PCMD_SEND_BUTTON);
- myPanel->Show();
- }
- if (secondWord.ICompare ("CHAT") == 0
- && theNick != "-9z99")
- {
- if (theNick.ICompare(fMyNick) == 0)
- return false;
- BString thePort (GetWord (data, 4));
- BMessage msg (M_CHAT_ACTION);
- msg.AddString ("nick", theNick.String());
- if (thePort != "-9z99")
- msg.AddString ("port", thePort.String());
- fSMsgr.SendMessage (&msg);
- }
- return true;
- }
- if (firstWord == "/DOP" || firstWord == "/DEOP" || firstWord == "/DEVOICE")
- {
- BString theNick (RestOfString (data, 2));
- int32 current (2),
- last (2);
- if (theNick != "-9z99")
- {
- BString command ("MODE ");
- command += fId;
- command += " -";
- while (GetWord(data, current) != "-9z99")
- {
- AddSend (&sendMsg, command.String());
- for (; GetWord(data, current) != "-9z99" && (current - last != 4); current++)
- AddSend (&sendMsg, ((firstWord == "/DEVOICE") ? "v" : "o"));
- AddSend (&sendMsg, " ");
- for (; last < current; last++)
- {
- BString curNick (GetWord(data, last));
- if (curNick != "-9z99")
- {
- AddSend (&sendMsg, curNick);
- AddSend (&sendMsg, " ");
- }
- }
- AddSend (&sendMsg, endl);
- sendMsg.MakeEmpty();
- }
- }
- else
- {
- BString dispString ("[x] ");
- dispString += firstWord.ToLower();
- dispString += ": ";
- dispString += S_PCMD_PARAMETER_ERROR "\n";
- Display (dispString.String(), C_ERROR);
- }
- return true;
- }
- if (firstWord == "/DNS")
- {
- BString parms (GetWord(data, 2));
- ChannelAgent *channelagent;
- MessageAgent *messageagent;
- if ((channelagent = dynamic_cast<ChannelAgent *>(this)))
- {
- const NamesView *namesList (channelagent->pNamesList());
- int32 count (namesList->CountItems());
- for (int32 i = 0; i < count; ++i)
- {
- NameItem *item ((NameItem *)(namesList->ItemAt (i)));
- if (!item->Name().ICompare (parms.String(), strlen (parms.String()))) //nick
- {
- AddSend (&sendMsg, "USERHOST ");
- AddSend (&sendMsg, item->Name().String());
- AddSend (&sendMsg, endl);
- return true;
- }
- }
- }
- else if ((messageagent = dynamic_cast<MessageAgent *>(this)))
- {
- BString eid (fId);
- eid.RemoveLast (" [DCC]");
- if (!ICompare(eid, parms) || !ICompare(fMyNick, parms))
- {
- AddSend (&sendMsg, "USERHOST ");
- AddSend (&sendMsg, parms.String());
- AddSend (&sendMsg, endl);
- return true;
- }
- }
- if (parms != "-9z99")
- {
- BMessage *lookupmsg (new BMessage);
- lookupmsg->AddString ("lookup", parms.String());
- lookupmsg->AddPointer ("agent", this);
- thread_id lookupThread = spawn_thread (
- DNSLookup,
- "dns_lookup",
- B_LOW_PRIORITY,
- lookupmsg);
- resume_thread (lookupThread);
- }
- else
- Display ("[x] /dns: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
-
- if (firstWord == "/PEXEC" || firstWord == "/RRUN") // piped exec
- {
- BString *theCmd (new BString (RestOfString (data, 2)));
- if (*theCmd != "-9z99")
- {
- BMessage *msg (new BMessage);
- msg->AddPointer ("exec", theCmd);
- msg->AddPointer ("agent", this);
- thread_id execThread = spawn_thread (
- ExecPipe,
- "exec_thread",
- B_LOW_PRIORITY,
- msg);
- resume_thread (execThread);
- }
- else
- {
- Display ("[x] /pexec: Error: Invalid parameters\n", C_ERROR);
- delete theCmd;
- }
- return true;
- }
- #if 1
- if (firstWord == "/EXCLUDE")
- {
- {
- BString second (GetWord (data, 2)),
- rest (RestOfString (data, 3));
- if (rest != "-9z99" && rest != "-9z99")
- {
- BMessage msg (M_EXCLUDE_ADD);
- msg.AddString ("second", second.String());
- msg.AddString ("cmd", rest.String());
- msg.AddString ("server", fServerName.String());
- msg.AddRect ("frame", Frame());
- vision_app->PostMessage (&msg);
- }
- }
- return true;
- }
- #endif
-
- if (firstWord == "/EXIT")
- {
- Window()->PostMessage (B_QUIT_REQUESTED);
- return true;
- }
-
- if (firstWord == "/IGNORE")
- {
- BString rest (RestOfString (data, 2));
-
- // strip trailing spaces
- int32 count (rest.Length() - 1);
- while (rest[count--] == ' ')
- rest.RemoveLast(" ");
- if (rest != "-9z99")
- {
- BMessage msg (M_IGNORE_ADD);
- msg.AddString ("cmd", rest.String());
- fSMsgr.SendMessage(&msg);
- }
- return true;
- }
- if (firstWord == "/INVITE" || firstWord == "/I")
- {
- BString theUser (GetWord (data, 2));
- if (theUser != "-9z99")
- {
- BString theChan (GetWord (data, 3));
- if (theChan == "-9z99")
- theChan = fId;
- AddSend (&sendMsg, "INVITE ");
- AddSend (&sendMsg, theUser << " " << theChan);
- AddSend (&sendMsg, endl);
- }
- else
- Display ("[x] /invite: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/JOIN" || firstWord == "/J")
- {
- // Bugs: Will not handle passing more than one channel key
- BString channel (GetWord (data, 2));
- if (channel != "-9z99")
- {
- if (channel[0] != '#'
- && channel[0] != '!'
- && channel[0] != '&'
- && channel[0] != '+')
- channel.Prepend ("#");
- AddSend (&sendMsg, "JOIN ");
- AddSend (&sendMsg, channel);
- BString key (GetWord (data, 3));
- if (key != "-9z99")
- {
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, key);
- }
- AddSend (&sendMsg, endl);
-
-
- if (key != "-9z99")
- {
- // used for keeping track of channel keys on u2 ircds
- // (not included as part of the mode reply on join)
- ServerAgent *fatherServer (vision_app->pClientWin()->GetTopServer (fAgentWinItem));
- if (fatherServer != NULL)
- {
- if (fatherServer->IRCDType() == IRCD_UNDERNET)
- {
- vision_app->pClientWin()->joinStrings.Append (",");
- vision_app->pClientWin()->joinStrings.Append (data);
- }
- }
- }
- }
- else
- Display ("[x] /join: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/KICK" || firstWord == "/K")
- {
- BString theNick (GetWord (data, 2));
- if (theNick != "-9z99")
- {
- BString theReason (RestOfString (data, 3));
- if (theReason == "-9z99")
- {
- // No expansions
- theReason = vision_app->GetCommand (CMD_KICK);
- }
- AddSend (&sendMsg, "KICK ");
- AddSend (&sendMsg, fId);
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, theNick);
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theReason);
- AddSend (&sendMsg, endl);
- }
- else
- Display ("[x] /kick: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/LIST")
- {
- BString theArgs (RestOfString (data, 2));
-
- BMessage msg (M_LIST_COMMAND);
- msg.AddString ("cmd", theArgs);
- fSMsgr.SendMessage (&msg);
- return true;
- }
- if (firstWord == "/M")
- {
- BString theMode (RestOfString (data, 2));
- AddSend (&sendMsg, "MODE ");
- if (fId == fServerName)
- AddSend (&sendMsg, fMyNick);
- else if (fId[0] == '#' || fId[0] == '!' || fId[0] == '&' || fId[0] == '+')
- AddSend (&sendMsg, fId);
- else
- AddSend (&sendMsg, fMyNick);
-
- if (theMode != "-9z99")
- {
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, theMode);
- }
- AddSend (&sendMsg, endl);
- return true;
- }
- if (firstWord == "/ME")
- {
- BString theAction (RestOfString (data, 2));
- if (theAction != "-9z99")
- ActionMessage (theAction.String(), fMyNick.String());
- else
- Display ("[x] /me: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
-
- if (firstWord == "/MODE")
- {
- BString theMode (RestOfString (data, 3)),
- theTarget (GetWord (data, 2));
- if (theTarget != "-9z99")
- {
- AddSend (&sendMsg, "MODE ");
- if (theMode == "-9z99")
- AddSend (&sendMsg, theTarget);
- else
- AddSend (&sendMsg, theTarget << " " << theMode);
- AddSend (&sendMsg, endl);
- }
- else
- Display ("[x] /mode: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/MSG")
- {
- BString theRest (RestOfString (data, 3));
- BString theNick (GetWord (data, 2));
- if (theRest != "-9z99")
- {
- if (vision_app->GetBool ("queryOnMsg"))
- {
- BMessage msg (M_OPEN_MSGAGENT);
- BMessage buffer (M_SUBMIT);
- buffer.AddString ("input", theRest.String());
- msg.AddMessage ("msg", &buffer);
- msg.AddString ("nick", theNick.String());
- fSMsgr.SendMessage (&msg);
- }
- else
- {
- BString tempString;
- tempString << "[M]-> " << theNick << " > " << theRest << "\n";
- Display (tempString.String());
- AddSend (&sendMsg, "PRIVMSG ");
- AddSend (&sendMsg, theNick);
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theRest);
- AddSend (&sendMsg, endl);
- }
- }
- return true;
- }
- if (firstWord == "/NICK")
- {
- BString newNick (GetWord (data, 2));
- if (newNick != "-9z99")
- {
- BString tempString (S_PCMD_TRY_NEW_NICK);
- tempString << newNick << ".\n";
- Display (tempString.String());
- AddSend (&sendMsg, "NICK ");
- AddSend (&sendMsg, newNick);
- AddSend (&sendMsg, endl);
- }
- else
- Display ("[x] /nick: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/NOTICE")
- {
- BString theTarget (GetWord (data, 2)),
- theMsg (RestOfString (data, 3));
- if (theMsg != "-9z99")
- {
- AddSend (&sendMsg, "NOTICE ");
- AddSend (&sendMsg, theTarget);
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theMsg);
- AddSend (&sendMsg, endl);
- BString tempString ("[N]-> ");
- tempString += theTarget;
- tempString += " -> ";
- tempString += theMsg;
- tempString += '\n';
- Display (tempString.String());
- }
- else
- Display ("[x] /notice: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/NOTIFY")
- {
- BString rest (RestOfString (data, 2));
-
- // strip trailing spaces
- int32 count (rest.Length() - 1);
- while (rest[count--] == ' ')
- rest.RemoveLast(" ");
- if (rest != "-9z99")
- {
- BMessage msg (M_NOTIFYLIST_ADD);
- msg.AddString ("cmd", rest.String());
- fSMsgr.SendMessage(&msg);
- }
- return true;
- }
-
- if (firstWord == "/OP" || firstWord == "/VOICE")
- {
- BString theNick (RestOfString (data, 2));
- int32 current (2),
- last (2);
- if (theNick != "-9z99")
- {
- BString command ("MODE ");
- command += fId;
- command += " +";
- while (GetWord(data, current) != "-9z99")
- {
- AddSend (&sendMsg, command.String());
- for (; GetWord(data, current) != "-9z99" && (current - last != 4); current++)
- AddSend (&sendMsg, ((firstWord == "/OP") ? "o" : "v"));
- AddSend (&sendMsg, " ");
- for (; last < current; last++)
- {
- BString curNick (GetWord(data, last));
- if (curNick != "-9z99")
- {
- AddSend (&sendMsg, curNick);
- AddSend (&sendMsg, " ");
- }
- }
- AddSend (&sendMsg, endl);
- sendMsg.MakeEmpty();
- }
- }
- else
- {
- BString dispString ("[x] ");
- dispString += firstWord.ToLower();
- dispString += ": ";
- dispString += S_PCMD_PARAMETER_ERROR "\n";
- Display ( dispString.String(), C_ERROR);
- }
- return true;
- }
-
- if (firstWord == "/PART")
- {
- BMessage msg (M_CLIENT_QUIT);
- msg.AddBool ("vision:part", true);
- BString secondWord (GetWord(data, 2));
- BString partmsg;
- if (secondWord == fId)
- {
- partmsg = RestOfString(data, 3);
- }
- else
- {
- partmsg = RestOfString(data, 2);
- }
-
- if (partmsg != "-9z99")
- {
- msg.AddString("vision:partmsg", partmsg);
- }
- fMsgr.SendMessage (&msg);
- return true;
- }
- if (firstWord == "/PING")
- {
- BString theNick (GetWord (data, 2));
- if (theNick != "-9z99")
- {
- long theTime (time (0));
- BString tempString ("/CTCP ");
- tempString << theNick << " PING " << theTime;
- ParseCmd (tempString.String());
- }
- return true;
- }
- if (firstWord == "/QUERY" || firstWord == "/Q")
- {
- BString theNick (GetWord (data, 2)),
- theMsg (RestOfString (data, 3));
-
- if (theNick != "-9z99")
- {
- BMessage msg (M_OPEN_MSGAGENT);
- msg.AddString ("nick", theNick.String());
- if (theMsg != "-9z99")
- {
- BMessage buffer (M_SUBMIT);
- buffer.AddString ("input", theMsg.String());
- msg.AddMessage ("msg", &buffer);
- }
-
- fSMsgr.SendMessage (&msg);
- }
- return true;
- }
- if (firstWord == "/QUIT")
- {
- BString theRest (RestOfString (data, 2)),
- buffer;
-
- if (theRest != "-9z99")
- {
- buffer += "QUIT :";
- buffer += theRest;
- }
- BMessage msg (M_CLIENT_QUIT);
- msg.AddString ("vision:quit", buffer.String());
- if (fSMsgr.IsValid())
- fSMsgr.SendMessage (&msg);
- return true;
- }
- if (firstWord == "/RAW" || firstWord == "/QUOTE")
- {
- BString theRaw (RestOfString (data, 2));
-
- if (theRaw != "-9z99")
- {
- AddSend (&sendMsg, theRaw);
- AddSend (&sendMsg, endl);
- BString tempString ("[R]-> ");
- tempString << theRaw << '\n';
- Display (tempString.String());
- }
- else
- Display ("[x] /raw: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- return true;
- }
- if (firstWord == "/RECONNECT")
- {
- fSMsgr.SendMessage (M_SLASH_RECONNECT);
- return true;
- }
- if (firstWord == "/SETBOOL")
- {
- BString var (GetWord (data, 2)),
- value (GetWord (data, 3));
- bool newvalue (false),
- caught (false);
- value.ToLower();
- if (value != "-9z99")
- {
- if (value == "true")
- {
- caught = true;
- newvalue = true;
- }
- else if (value == "false")
- {
- caught = true;
- newvalue = false;
- }
- }
- if (!caught || value == "-9z99")
- {
- Display ("[x] /setbool: " S_PCMD_PARAMETER_ERROR "\n", C_ERROR);
- }
- else
- {
- status_t returned (vision_app->SetBool (var.String(), newvalue));
- if (returned == B_OK)
- Display ("[x] /setbool: " S_PCMD_SET_BOOL_SUCCESS "\n", C_ERROR);
- else
- Display ("[x] /setbool: " S_PCMD_SET_BOOL_FAILURE "\n", C_ERROR);
- }
- return true;
- }
-
- if (firstWord == "/SLEEP")
- {
- BString rest (RestOfString (data, 2));
- if (rest != "-9z99")
- {
- // this basically locks up the window its called from,
- // but I can't think of a better way with our current
- // commands implementation
- int32 sleeptime = atoi(rest.String());
- snooze (sleeptime * 1000 * 100); // deciseconds? 10 = one second
- }
- return true;
- }
- if (firstWord == "/TOPIC")
- {
- BString theChan (GetWord(data, 2));
- BString theTopic (RestOfString (data, 3));
- if (theChan != "-9z99")
- {
- AddSend (&sendMsg, "TOPIC ");
- AddSend (&sendMsg, theChan);
- if (theTopic != "-9z99")
- {
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, theTopic);
- }
- AddSend (&sendMsg, endl);
- }
- // TODO: print nice error message about topic parameters here
- return true;
- }
- if (firstWord == "/UNIGNORE")
- {
- BString rest (RestOfString (data, 2));
-
- // strip trailing spaces
- int32 count (rest.Length() - 1);
- while (rest[count--] == ' ')
- rest.RemoveLast(" ");
- if (rest != "-9z99")
- {
- BMessage msg (M_IGNORE_REMOVE);
- msg.AddString ("cmd", rest.String());
- fSMsgr.SendMessage(&msg);
- }
- return true;
- }
- if (firstWord == "/UNNOTIFY")
- {
- {
- BString rest (RestOfString (data, 2));
-
- // strip trailing spaces
- int32 count (rest.Length() - 1);
- while (rest[count--] == ' ')
- rest.RemoveLast(" ");
- if (rest != "-9z99")
- {
- BMessage msg (M_NOTIFYLIST_REMOVE);
- msg.AddString ("cmd", rest.String());
- fSMsgr.SendMessage (&msg);
- }
- }
- return true;
- }
- if (firstWord == "/VUPTIME")
- {
- BString parms (GetWord(data, 2)),
- clientUptime (DurationString(vision_app->VisionUptime())),
- expandedString (S_PCMD_VIS_UPTIME);
- expandedString += clientUptime;
- if ((fId != fServerName) && (parms == "-9z99"))
- {
- AddSend (&sendMsg, "PRIVMSG ");
- AddSend (&sendMsg, fId);
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, expandedString.String());
- AddSend (&sendMsg, endl);
-
- ChannelMessage (expandedString.String(), fMyNick.String());
- }
- else if ((parms == "-l") || (fId == fServerName)) // echo locally
- {
- BString tempString;
- tempString << "Vision Uptime: " << clientUptime.String() << "\n";
- Display (tempString.String(), C_WHOIS);
- }
- return true;
- }
- if (firstWord == "/UPTIME")
- {
- BString parms (GetWord(data, 2)),
- uptime (DurationString (system_time())),
- expandedString;
-
- const char *expansions[1];
- expansions[0] = uptime.String();
- expandedString = ExpandKeyed (vision_app->GetCommand (CMD_UPTIME).String(), "U",
- expansions);
- expandedString.RemoveFirst("\n");
- if ((fId != fServerName) && (parms == "-9z99"))
- {
- AddSend (&sendMsg, "PRIVMSG ");
- AddSend (&sendMsg, fId);
- AddSend (&sendMsg, " :");
- AddSend (&sendMsg, expandedString.String());
- AddSend (&sendMsg, endl);
-
- ChannelMessage (expandedString.String(), fMyNick.String());
- }
- else if ((parms == "-l") || (fId == fServerName)) // echo locally
- {
- BString tempString;
- tempString << "Uptime: " << expandedString << "\n";
- Display (tempString.String(), C_WHOIS);
- }
- return true;
- }
- if (firstWord == "/VERSION"
- || firstWord == "/TIME")
- {
- BString theCmd (firstWord.RemoveFirst ("/")),
- theNick (GetWord (data, 2));
- theCmd.ToUpper();
- // the "." check is because the user might specify a server name
- if (theNick != "-9z99" && theNick.FindFirst(".") < 0)
- {
- BString tempString ("/CTCP ");
- tempString << theNick << " " << theCmd;
- ParseCmd (tempString.String());
- }
- else
- {
- AddSend (&sendMsg, theCmd);
- if (theNick != "-9z99")
- {
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, theNick);
- }
- AddSend (&sendMsg, endl);
- }
- return true;
- }
- if (firstWord != "" && firstWord[0] == '/')
- // != "" is required to prevent a nasty crash with firstWord[0]
- {
- BString theCmd (firstWord.RemoveAll ("/")),
- theRest (RestOfString (data, 2));
- if (theCmd == "W")
- theCmd = "WHOIS";
- AddSend (&sendMsg, theCmd);
- if (theRest != "-9z99")
- {
- AddSend (&sendMsg, " ");
- AddSend (&sendMsg, theRest);
- }
-
- AddSend (&sendMsg, endl);
- return true;
- }
- return false; // we couldn't handle this message
- }
- int32
- ClientAgent::ExecPipe (void *arg)
- {
- BMessage *msg (reinterpret_cast<BMessage *>(arg));
- BString *exec;
- ClientAgent *agent;
- if ((msg->FindPointer ("exec", reinterpret_cast<void **>(&exec)) != B_OK)
- || (msg->FindPointer ("agent", reinterpret_cast<void **>(&agent)) != B_OK))
- {
- printf (":ERROR: couldn't find valid data in BMsg to ExecPipe() -- bailing\n");
- return B_ERROR;
- }
- // re use message
- msg->MakeEmpty();
- msg->what = M_SUBMIT;
- msg->AddString("input", "");
- msg->AddBool("clear", false);
- msg->AddBool("add2history", false);
- BMessenger self_destruct_in_15_seconds(agent);
- FILE *fp = popen (exec->String(), "r");
-
- if(fp == NULL)
- {
- msg->what = M_DISPLAY;
- PackDisplay(msg, "[x] " S_PCMD_PEXEC_ERROR "\n", C_ERROR);
- self_destruct_in_15_seconds.SendMessage(msg);
- }
- else
- {
- char data[768]; // should be long enough for any line...
-
- // read one less just in case we need to offset by a char (prepended '/')
- while (fgets(data, 767, fp))
- {
- data[strlen(data)-1] = '\0'; // strip termination
- if (data[0] == '/')
- {
- memmove(data + 1, data, strlen(data));
- data[0] = ' ';
- }
-
- // ship off to agent
- msg->ReplaceString("input", data);
- self_destruct_in_15_seconds.SendMessage(msg);
- }
- pclose(fp);
- }
- delete exec;
- delete msg;
- return B_OK;
- }
- int32
- ClientAgent::DNSLookup (void *arg)
- {
- BMessage *msg (reinterpret_cast<BMessage *>(arg));
- const char *lookup;
- ClientAgent *agent;
- if ((msg->FindString ("lookup", &lookup) != B_OK)
- || (msg->FindPointer ("agent", reinterpret_cast<void **>(&agent)) != B_OK))
- {
- printf (":ERROR: couldn't find valid data in BMsg to DNSLookup() -- bailing\n");
- return B_ERROR;
- }
- BString resolve (lookup),
- output ("[x] ");
- if (isalpha (resolve[0]))
- {
- hostent *hp = gethostbyname (resolve.String());
- if (hp)
- {
- // ip address is in hp->h_addr_list[0];
- char addr_buf[16];
- in_addr *addr = (in_addr *)hp->h_addr_list[0];
- strcpy(addr_buf, inet_ntoa(*addr));
- output += S_PCMD_DNS1;
- output += resolve.String();
- output += S_PCMD_DNS2;
- output += addr_buf;
- }
- else
- {
- output += S_PCMD_DNS_ERROR;
- output += resolve.String();
- }
- }
- else
- {
- ulong addr = inet_addr (resolve.String());
- hostent *hp = gethostbyaddr ((const char *)&addr, 4, AF_INET);
-
- if (hp)
- {
- output += S_PCMD_DNS1;
- output += resolve.String();
- output += S_PCMD_DNS2;
- output += hp->h_name;
- }
- else
- {
- output += S_PCMD_DNS_ERROR;
- output += resolve.String();
- }
- }
- output += "\n";
- delete msg;
- BMessage dnsMsg (M_DISPLAY);
- agent->PackDisplay (&dnsMsg, output.String(), C_WHOIS);
- agent->fMsgr.SendMessage (&dnsMsg);
- return B_OK;
- }
|