~ubuntu-branches/ubuntu/raring/quassel/raring-proposed

« back to all changes in this revision

Viewing changes to src/core/userinputhandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Hermann
  • Date: 2008-08-27 16:13:38 UTC
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20080827161338-wwscdevtkwm9lq93
Tags: upstream-0.3.0
ImportĀ upstreamĀ versionĀ 0.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include "ircuser.h"
28
28
 
29
29
#include <QDebug>
 
30
#include <QRegExp>
30
31
 
31
32
UserInputHandler::UserInputHandler(NetworkConnection *parent) : BasicHandler(parent) {
32
33
}
37
38
      return;
38
39
    QString cmd;
39
40
    QString msg = msg_;
40
 
    if(!msg.startsWith('/')) {
 
41
    // leading slashes indicate there's a command to call unless there is anothere one in the first section (like a path /proc/cpuinfo)
 
42
    int secondSlashPos = msg.indexOf('/', 1);
 
43
    int firstSpacePos = msg.indexOf(' ');
 
44
    if(!msg.startsWith('/') || (secondSlashPos != -1 && (secondSlashPos < firstSpacePos || firstSpacePos == -1))) {
 
45
      if(msg.startsWith("//"))
 
46
        msg.remove(0, 1); // //asdf is transformed to /asdf
41
47
      cmd = QString("SAY");
42
48
    } else {
43
49
      cmd = msg.section(' ', 0, 0).remove(0, 1).toUpper();
57
63
  Q_UNUSED(bufferInfo)
58
64
 
59
65
  QString awayMsg = msg;
 
66
  IrcUser *me = network()->me();
 
67
 
60
68
  // if there is no message supplied we have to check if we are already away or not
61
69
  if(msg.isEmpty()) {
62
 
    IrcUser *me = network()->me();
63
70
    if(me && !me->isAway())
64
71
      awayMsg = networkConnection()->identity()->awayReason();
65
72
  }
66
 
 
 
73
  if(me)
 
74
    me->setAwayMessage(awayMsg);
 
75
  
67
76
  putCmd("AWAY", serverEncode(awayMsg));
68
77
}
69
78
 
143
152
  emit putCmd("INVITE", serverEncode(params));
144
153
}
145
154
 
146
 
void UserInputHandler::handleJ(const BufferInfo &bufferInfo, const QString &msg) {
147
 
  QString trimmed = msg.trimmed();
148
 
  if(trimmed.length() == 0) return;
149
 
  if(trimmed[0].isLetter()) trimmed.prepend("#");
150
 
  handleJoin(bufferInfo, trimmed);
151
 
}
152
 
 
153
155
void UserInputHandler::handleJoin(const BufferInfo &bufferInfo, const QString &msg) {
154
 
  Q_UNUSED(bufferInfo)
155
 
  QStringList params = msg.trimmed().split(" ");
 
156
  Q_UNUSED(bufferInfo);
 
157
 
 
158
  // trim spaces before chans or keys
 
159
  QString sane_msg = msg;
 
160
  sane_msg.replace(QRegExp(", +"), ",");
 
161
  QStringList params = sane_msg.trimmed().split(" ");
156
162
  QStringList chans = params[0].split(",");
157
163
  QStringList keys;
 
164
  int i;
 
165
  for(i = 0; i < chans.count(); i++) {
 
166
    if(!network()->isChannelName(chans[i]))
 
167
      chans[i].prepend('#');
 
168
  }
 
169
  params[0] = chans.join(",");
158
170
  if(params.count() > 1) keys = params[1].split(",");
159
171
  emit putCmd("JOIN", serverEncode(params)); // FIXME handle messages longer than 512 bytes!
160
 
  int i = 0;
 
172
  i = 0;
161
173
  for(; i < keys.count(); i++) {
162
174
    if(i >= chans.count()) break;
163
175
    networkConnection()->addChannelKey(chans[i], keys[i]);
263
275
  emit putCmd("PART", params);
264
276
}
265
277
 
 
278
void UserInputHandler::handlePing(const BufferInfo &bufferInfo, const QString &msg) {
 
279
  Q_UNUSED(bufferInfo)
 
280
 
 
281
  QString param = msg;
 
282
  if(param.isEmpty())
 
283
    param = QTime::currentTime().toString("hh:mm:ss.zzz");
 
284
 
 
285
  putCmd("PING", serverEncode(param));
 
286
}
 
287
 
266
288
// TODO: implement queries
267
289
void UserInputHandler::handleQuery(const BufferInfo &bufferInfo, const QString &msg) {
268
290
  Q_UNUSED(bufferInfo)
336
358
}
337
359
 
338
360
void UserInputHandler::defaultHandler(QString cmd, const BufferInfo &bufferInfo, const QString &msg) {
339
 
  Q_UNUSED(bufferInfo)
 
361
  for(int i = 0; i < coreSession()->aliasManager().count(); i++) {
 
362
    if(coreSession()->aliasManager()[i].name.toLower() == cmd.toLower()) {
 
363
      expand(coreSession()->aliasManager()[i].expansion, bufferInfo, msg);
 
364
      return;
 
365
    }
 
366
  }
340
367
  emit displayMsg(Message::Error, BufferInfo::StatusBuffer, "", QString("Error: %1 %2").arg(cmd).arg(msg));
341
368
}
342
369
 
 
370
void UserInputHandler::expand(const QString &alias, const BufferInfo &bufferInfo, const QString &msg) {
 
371
  QStringList commands = alias.split(QRegExp("; ?"));
 
372
  QStringList params = msg.split(' ');
 
373
  for(int i = 0; i < commands.count(); i++) {
 
374
    QString command = commands[i];
 
375
    for(int j = params.count(); j > 0; j--) {
 
376
      IrcUser *ircUser = network()->ircUser(params[j - 1]);
 
377
      command = command.replace(QString("$%1:hostname").arg(j), ircUser ? ircUser->host() : QString("*"));
 
378
      command = command.replace(QString("$%1").arg(j), params[j - 1]);
 
379
    }
 
380
    command = command.replace("$0", msg);
 
381
    command = command.replace("$channelname", bufferInfo.bufferName());
 
382
    command = command.replace("$currentnick", network()->myNick());
 
383
    handleUserInput(bufferInfo, command);
 
384
  }
 
385
}
 
386
 
 
387
 
343
388