ircprotocol.cpp

Go to the documentation of this file.
00001 /*
00002 #########################################################################
00003 #
00004 #  This file is part of trustyRC.
00005 #
00006 #  trustyRC, fully modular IRC robot 
00007 #  Copyright (C) 2006-2008 Nicoleau Fabien 
00008 #
00009 #  trustyRC is free software: you can redistribute it and/or modify
00010 #  it under the terms of the GNU General Public License as published by
00011 #  the Free Software Foundation, either version 3 of the License, or
00012 #  (at your option) any later version.
00013 #
00014 #  trustyRC is distributed in the hope that it will be useful,
00015 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 #  GNU General Public License for more details.
00018 #
00019 #  You should have received a copy of the GNU General Public License
00020 #  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
00021 #
00022 #########################################################################
00023 */
00024 
00029 #include "ircprotocol.h"
00030 
00034 IRCProtocol::IRCProtocol()
00035 {
00036 }
00037 
00041 IRCProtocol::~IRCProtocol()
00042 {
00043 }
00044 
00053 vector<string> IRCProtocol::identify(string pass,string ident,string name,string nick) 
00054 {
00055         vector<string> ret;
00056         if ( pass != "") 
00057                 ret.push_back("PASS "+pass);
00058         ret.push_back(IRCProtocol::changeNick(nick));
00059    ret.push_back("USER " + ident + " 0 * :" + name);
00060         return ret;
00061 }
00062         
00068 string IRCProtocol::quitServer(string reason) 
00069 {
00070    return("QUIT :"+reason);
00071 }
00072         
00078 string IRCProtocol::joinChannel(string channel) 
00079 {
00080    return("JOIN " + channel);
00081 }
00082         
00089 string IRCProtocol::leaveChannel(string channel,string reason) 
00090 {
00091    return("PART " + channel + " :"+reason);
00092 }
00093         
00099 string IRCProtocol::changeNick(string nick) 
00100 {
00101    return("NICK " + nick);
00102 }
00103         
00109 string IRCProtocol::ping(string ping)
00110 {
00111    return("PING :" + ping);
00112 }
00113         
00119 string IRCProtocol::pong(string pong) 
00120 {
00121    return("PONG " + pong);
00122 }
00123         
00130 string IRCProtocol::sendMsg(string destination,string message) 
00131 {
00132    return("PRIVMSG " + destination + " :" + message);
00133 }
00134 
00141 vector<string> IRCProtocol::sendMsg(string destination,vector<string> messages) 
00142 {
00143    vector<string> msgs;
00144    for ( unsigned int i = 0 ; i < messages.size(); i ++ )
00145       msgs.push_back("PRIVMSG " + destination + " :" + messages[i]);
00146    return(msgs);
00147 }
00148 
00156 string IRCProtocol::sendAction(string channel,string action)
00157 {
00158    return "PRIVMSG "+channel+" :"+"\x01"+"ACTION "+action+"\x01";
00159 }
00160 
00167 string IRCProtocol::changeTopic(string channel,string topic) 
00168 {
00169    return("TOPIC " + channel + " :" + topic);
00170 }
00171 
00181 vector<string> IRCProtocol::applyModes(string channel,vector<string> users_list,char sign,char mode,unsigned int limit)
00182 {
00183    vector<string> ret;
00184         string query,users;
00185         unsigned int i = 0 ;
00186         query = "MODE "+channel+ " "+sign;
00187         while ( i < users_list.size() ) {
00188                 query += mode;
00189                 users += " "+users_list[i];
00190                 if ( (i+1)%limit == 0 ) {
00191                         query += users;
00192                         ret.push_back(query);
00193                         query = "MODE "+channel+ " "+sign;
00194                         users = "";
00195                 }
00196                 i++;
00197         }
00198         if ( users_list.size()%limit != 0 ) {
00199                 query += users;
00200                 ret.push_back(query);
00201         }
00202         return ret;
00203 }
00204         
00212 vector<string> IRCProtocol::op(vector<string> vectorNicks,string channel) 
00213 {
00214         vector<string> ret;
00215         string query,nicks;
00216         unsigned int i = 0 ;
00217         query = "MODE "+channel+ " +";
00218         while ( i < vectorNicks.size() )
00219         {
00220                 query += "o";
00221                 nicks += " "+vectorNicks[i];
00222                 if ( (i+1)%4 == 0 )
00223                 {
00224                         query += nicks;
00225                         ret.push_back(query);
00226                         query = "MODE "+channel+ " +";
00227                         nicks = "";
00228                 }
00229                 i++;
00230         }
00231         if ( vectorNicks.size()%4 != 0 )
00232         {
00233                 query += nicks;
00234                 ret.push_back(query);
00235         }
00236         return ret;
00237 }
00238 
00246 string IRCProtocol::op(string nick,string channel) 
00247 {
00248    return ("MODE "+channel+" +o "+nick) ;
00249 }
00250         
00258 vector<string> IRCProtocol::unop(vector<string> vectorNicks,string channel) 
00259 {
00260         vector<string> ret;
00261         string query,nicks;
00262         unsigned int i = 0 ;
00263         query = "MODE "+channel+ " -";
00264         while ( i < vectorNicks.size() )
00265         {
00266                 query += "o";
00267                 nicks += " "+vectorNicks[i];
00268                 if ( (i+1)%4 == 0 )
00269                 {
00270                         query += nicks;
00271                         ret.push_back(query);
00272                         query = "MODE "+channel+ " -";
00273                         nicks = "";
00274                 }
00275                 i++;
00276         }
00277         if ( vectorNicks.size()%4 != 0 )
00278         {
00279                 query += nicks;
00280                 ret.push_back(query);
00281         }
00282         return ret;
00283 }
00284 
00291 string IRCProtocol::unop(string nick,string channel) 
00292 {
00293    return ("MODE "+channel+" -o "+nick) ;
00294 }
00295 
00302 string IRCProtocol::ban(string mask,string channel)
00303 {
00304    return ("MODE "+channel+" +b "+mask);
00305 }       
00306 
00313 string IRCProtocol::unban(string mask,string channel)
00314 {
00315    return ("MODE "+channel+" -b "+mask);
00316 }
00317 
00325 vector<string> IRCProtocol::voice(vector<string> vectorNicks,string channel) 
00326 {
00327         vector<string> ret;
00328         string query,nicks;
00329         unsigned int i = 0 ;
00330         query = "MODE "+channel+ " +";
00331         while ( i < vectorNicks.size() )
00332         {
00333                 query += "v";
00334                 nicks += " "+vectorNicks[i];
00335                 if ( (i+1)%4 == 0 )
00336                 {
00337                         query += nicks;
00338                         ret.push_back(query);
00339                         query = "MODE "+channel+ " +";
00340                         nicks = "";
00341                 }
00342                 i++;
00343         }
00344         if ( vectorNicks.size()%4 != 0 )
00345         {
00346                 query += nicks;
00347                 ret.push_back(query);
00348         }
00349         return ret;
00350 }
00351         
00358 string IRCProtocol::voice(string nick,string channel) 
00359 {
00360    return ("MODE "+channel+" +v "+nick) ;
00361 }
00362         
00370 vector<string> IRCProtocol::unvoice(vector<string> vectorNicks,string channel) 
00371 {
00372         vector<string> ret;
00373         string query,nicks;
00374         unsigned int i = 0 ;
00375         query = "MODE "+channel+ " -";
00376         while ( i < vectorNicks.size() )
00377         {
00378                 query += "v";
00379                 nicks += " "+vectorNicks[i];
00380                 if ( (i+1)%4 == 0 )
00381                 {
00382                         query += nicks;
00383                         ret.push_back(query);
00384                         query = "MODE "+channel+ " -";
00385                         nicks = "";
00386                 }
00387                 i++;
00388         }
00389         if ( vectorNicks.size()%4 != 0 )
00390         {
00391                 query += nicks;
00392                 ret.push_back(query);
00393         }
00394         return ret;
00395 }
00396 
00403 string IRCProtocol::unvoice(string nick,string channel) 
00404 {
00405    return ("MODE "+channel+" -v "+nick) ;
00406 }
00407         
00414 string IRCProtocol::sendNotice(string destination,string notice) 
00415 {
00416    return("NOTICE " + destination +" :" + notice );
00417 }
00418 
00425 vector<string> IRCProtocol::sendNotices(string destination,vector<string> notices) 
00426 {
00427    vector<string> back;
00428    for ( unsigned int i = 0 ; i  < notices.size() ; i ++ )
00429       back.push_back("NOTICE " + destination +" :" + notices[i]);
00430    return(back);
00431 }
00432 
00439 string IRCProtocol::who(string channel,string params)
00440 {
00441    if ( params == "" )
00442       return "WHO "+channel;
00443    else
00444       return "WHO "+channel+" %"+params;
00445 }
00446         
00454 string IRCProtocol::kick(string nick,string chan,string reason) 
00455 {
00456    return("KICK " + chan + " " + nick + " :"+reason);
00457 }
00458 
00465 string IRCProtocol::invite(string channel,string nick)
00466 {
00467    return "INVITE "+channel+" "+nick;
00468 }

Generated on Sun Aug 16 15:28:27 2009 for trustyRC by  doxygen 1.5.8