| 1 | /* |
| 2 | * parser.cpp |
| 3 | */ |
| 4 | |
| 5 | /* There are a lot of possibilities for segmentation faults in this |
| 6 | * file that need to be fixed, places where the dispatch is null */ |
| 7 | |
| 8 | #include "parser.h" |
| 9 | #include "networkconnection.h" |
| 10 | #include "igsc.h" |
| 11 | #include "networkdispatch.h" |
| 12 | #include "consoledispatch.h" |
| 13 | #include "roomdispatch.h" |
| 14 | #include "boarddispatch.h" |
| 15 | #include "gamedialogdispatch.h" |
| 16 | #include "talkdispatch.h" |
| 17 | |
| 18 | // Parsing of Go Server messages |
| 19 | Parser::Parser() //: QObject()//, Misc<QString>() |
| 20 | { |
| 21 | // generate buffers |
| 22 | memory = 0; |
| 23 | created_match_request = 0; |
| 24 | memory_str = QString(); |
| 25 | myname = QString(); |
| 26 | |
| 27 | // init |
| 28 | gsName = GS_UNKNOWN; |
| 29 | // cmd = NONE; |
| 30 | aMove = new MoveRecord(); |
| 31 | aGameRecord = new GameRecord(); |
| 32 | aTime = new TimeRecord(); |
| 33 | aGameResult = new GameResult(); |
| 34 | /* Player needs initalization stuff... and more, |
| 35 | * maybe we need to track bit changes ??*/ |
| 36 | gameListA = new QList <unsigned int>(); |
| 37 | gameListB = new QList <unsigned int>(); |
| 38 | scoring_game_id = -1; |
| 39 | } |
| 40 | |
| 41 | Parser::~Parser() |
| 42 | { |
| 43 | delete aMove; |
| 44 | delete aGameRecord; |
| 45 | delete aTime; |
| 46 | delete aGameResult; |
| 47 | delete gameListA; |
| 48 | delete gameListB; |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * extratcs and returns the element between 2 delimiters, at a given delimiter count |
| 53 | */ |
| 54 | QString Parser::element(const QString &line, int index, const QString &del1, const QString &del2, bool killblanks) |
| 55 | { |
| 56 | int len = line.length(); |
| 57 | int idx = index; |
| 58 | int i; |
| 59 | QString sub; |
| 60 | |
| 61 | // kill trailing white spaces |
| 62 | while (/*(int)*/ line[len-1] < 33 && len > 0) |
| 63 | len--; |
| 64 | |
| 65 | // right delimiter given? |
| 66 | if (del2.isEmpty()) |
| 67 | { |
| 68 | // ... no right delimiter |
| 69 | // element("a b c", 0, " ") -> "a" |
| 70 | // element(" a b c", 0, " ") -> "a" spaces at the beginning are skipped |
| 71 | // element("a b c", 1, " ") -> "b" |
| 72 | // element(" a b c", 1, " ") -> "b", though two spaces between "a" and "b" |
| 73 | |
| 74 | // skip (delimiters) spaces at the beginning |
| 75 | i = 0; |
| 76 | // while (i < len && line[i] == del1) |
| 77 | while (i < len && line[i] == ' ') |
| 78 | i++; |
| 79 | |
| 80 | for (; idx != -1 && i < len; i++) |
| 81 | { |
| 82 | // skip multiple (delimiters) spaces before wanted sequence starts |
| 83 | // while (idx > 0 && line[i] == del1 && i < len-1 && line[i+1] == del1) |
| 84 | while (idx > 0 && line[i] == ' ' && i < len-1 && line[i+1] == ' ') |
| 85 | i++; |
| 86 | |
| 87 | // look for delimiters, maybe more in series |
| 88 | if (line.mid(i,del1.length()) == del1) |
| 89 | idx--; |
| 90 | else if (idx == 0) |
| 91 | sub += line[i]; |
| 92 | } |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | // ... with right delimiter |
| 97 | // element("a b c", 0, " ", " ") -> "b" |
| 98 | // element("(a) (b c)", 0, "(", ")") -> "a" |
| 99 | // element("(a) (b c)", 1, "(", ")") -> "b c" |
| 100 | // element("(a) (b c)", 0, " ", ")") -> "(b c" |
| 101 | // element("(a) (b c)", 1, " ", ")") -> "c" |
| 102 | // element("(a) (b c)", 1, "(", "EOL") -> "b c)" |
| 103 | |
| 104 | // skip spaces at the beginning |
| 105 | i = 0; |
| 106 | while (i < len && line[i] == ' ') |
| 107 | i++; |
| 108 | |
| 109 | // seek left delimiter |
| 110 | idx++; |
| 111 | |
| 112 | for (; idx != -1 && i < len; i++) |
| 113 | { |
| 114 | // skip multiple (delimiters) spaces before wanted sequence starts |
| 115 | // while (idx > 0 && line[i] == del1 && i < len-1 && line[i+1] == del1) |
| 116 | while (idx > 0 && line[i] == ' ' && i < len-1 && line[i+1] == ' ') |
| 117 | i++; |
| 118 | |
| 119 | if ((idx != 0 && line.mid(i,del1.length()) == del1) || |
| 120 | (idx == 0 && line.mid(i,del2.length()) == del2)) |
| 121 | { |
| 122 | idx--; |
| 123 | } |
| 124 | else if (idx == 0) |
| 125 | { |
| 126 | // EOL (End Of Line)? |
| 127 | if (del2 == QString("EOL")) |
| 128 | { |
| 129 | // copy until end of line |
| 130 | for (int j = i; j < len; j++) |
| 131 | if (!killblanks || line[j] != ' ') |
| 132 | sub += line[j]; |
| 133 | |
| 134 | // leave loop |
| 135 | idx--; |
| 136 | } |
| 137 | else if (!killblanks || line[i] != ' ') |
| 138 | sub += line[i]; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | return sub; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * put a line from host to parser |
| 148 | * if info is recognized, a signal is sent, and, however, |
| 149 | * the return type indicates the type of information |
| 150 | */ |
| 151 | InfoType Parser::put_line(const QString &txt, class NetworkConnection * connection, GSName gsName) |
| 152 | { |
| 153 | |
| 154 | QString line = txt.trimmed(); |
| 155 | int pos; |
| 156 | class BoardDispatch * boarddispatch = 0; |
| 157 | class RoomDispatch * roomdispatch = connection->getDefaultRoomDispatch(); |
| 158 | class NetworkDispatch * dispatch = connection->getDefaultDispatch(); |
| 159 | class ConsoleDispatch * console = connection->getConsoleDispatch(); |
| 160 | qDebug(line.toLatin1().constData()); |
| 161 | if (line.length() == 0) |
| 162 | { |
| 163 | // skip empty lines but write message if |
| 164 | // a) not logged in |
| 165 | // b) help files |
| 166 | if ((gsName == GS_UNKNOWN) || (!memory_str.isEmpty() && memory_str.contains("File"))) |
| 167 | { |
| 168 | console->recvText(txt.toLatin1().constData()); |
| 169 | return MESSAGE; |
| 170 | } |
| 171 | |
| 172 | // white space only |
| 173 | return WS; |
| 174 | } |
| 175 | |
| 176 | // skip console commands |
| 177 | if (line.indexOf(CONSOLECMDPREFIX,0) != -1) |
| 178 | return NONE; |
| 179 | |
| 180 | // check for connection status |
| 181 | if (line.indexOf("Connection closed",0,Qt::CaseInsensitive) != -1) |
| 182 | { |
| 183 | emit signal_connexionClosed(); |
| 184 | console->recvText(txt.toLatin1().constData()); |
| 185 | gsName = GS_UNKNOWN; |
| 186 | return IT_OTHER; |
| 187 | } |
| 188 | else if (line.indexOf("IGS entry on",0) != -1) |
| 189 | { |
| 190 | connection->onReady(); |
| 191 | return SERVERNAME; |
| 192 | } |
| 193 | |
| 194 | // |
| 195 | // LOGON MODE ---------------------- |
| 196 | // |
| 197 | // try to find out server and set mode |
| 198 | #ifdef OLD |
| 199 | // why would we not know the server type? |
| 200 | if (gsName == GS_UNKNOWN) |
| 201 | { |
| 202 | if (line.indexOf("IGS entry on",0) != -1) |
| 203 | { |
| 204 | gsName = IGS; |
| 205 | emit signal_svname(gsName); |
| 206 | return SERVERNAME; |
| 207 | } |
| 208 | |
| 209 | if (line.indexOf("LGS #",0) != -1) |
| 210 | { |
| 211 | gsName = LGS; |
| 212 | emit signal_svname(gsName); |
| 213 | return SERVERNAME; |
| 214 | } |
| 215 | |
| 216 | if (line.indexOf("NNGS #",0) != -1) |
| 217 | { |
| 218 | gsName = NNGS; |
| 219 | emit signal_svname(gsName); |
| 220 | return SERVERNAME; |
| 221 | } |
| 222 | |
| 223 | // suggested by Rod Assard for playing with NNGS version 1.1.14 |
| 224 | if (line.indexOf("Server (NNGS)",0) != -1) |
| 225 | { |
| 226 | gsName = NNGS; |
| 227 | emit signal_svname(gsName); |
| 228 | return SERVERNAME; |
| 229 | } |
| 230 | |
| 231 | if (line.indexOf("WING #",0) != -1) |
| 232 | { |
| 233 | gsName = WING; |
| 234 | emit signal_svname(gsName); |
| 235 | return SERVERNAME; |
| 236 | } |
| 237 | |
| 238 | if (line.indexOf("CTN #",0) != -1) |
| 239 | { |
| 240 | gsName = CTN; |
| 241 | emit signal_svname(gsName); |
| 242 | return SERVERNAME; |
| 243 | } |
| 244 | |
| 245 | // adapted from NNGS, chinese characters |
| 246 | if (line.indexOf("CWS #",0) != -1 || line.indexOf("==CWS",0) != -1) |
| 247 | { |
| 248 | gsName = CWS; |
| 249 | emit signal_svname(gsName); |
| 250 | return SERVERNAME; |
| 251 | } |
| 252 | |
| 253 | // critical: TO BE WATCHED.... |
| 254 | if (line.indexOf("#>",0) != -1) |
| 255 | { |
| 256 | gsName = DEFAULT; |
| 257 | emit signal_svname(gsName); |
| 258 | return SERVERNAME; |
| 259 | } |
| 260 | |
| 261 | // account name |
| 262 | if (line.indexOf("Your account name is",0) != -1) |
| 263 | { |
| 264 | buffer = line.right(line.length() - 21); |
| 265 | buffer.replace(QRegExp("[\".]"), ""); |
| 266 | emit signal_accname(buffer); |
| 267 | return ACCOUNT; |
| 268 | } |
| 269 | |
| 270 | // account name as sent from telnet.cpp |
| 271 | if (line.indexOf("...sending:") != -1) |
| 272 | { |
| 273 | if ((buffer = element(line, 0, "{", "}")) == NULL) |
| 274 | return IT_OTHER; |
| 275 | emit signal_accname(buffer); |
| 276 | return ACCOUNT; |
| 277 | } |
| 278 | |
| 279 | if ((line.indexOf("guest account",0) != -1) || line.contains("logged in as a guest")) |
| 280 | { |
| 281 | emit signal_status(GUEST); |
| 282 | return STATUS; |
| 283 | } |
| 284 | |
| 285 | if (line.at(0) != '9' && !memory) |
| 286 | { |
| 287 | //emit signal_message(txt); |
| 288 | console->recvText(txt.toLatin1().constData()); |
| 289 | return MESSAGE; |
| 290 | } |
| 291 | } |
| 292 | #endif //OLD |
| 293 | |
| 294 | // |
| 295 | // LOGON HAS DONE, now parse: ---------------------- |
| 296 | // |
| 297 | // get command type: |
| 298 | bool ok; |
| 299 | int cmd_nr = element(line, 0, " ").toInt(&ok); |
| 300 | if (!ok && !memory_str.isEmpty() && memory_str.contains("CHANNEL")) |
| 301 | { |
| 302 | connection->onReady(); |
| 303 | // special case: channel info |
| 304 | cmd_nr = 9; |
| 305 | } |
| 306 | else if (!ok || !memory_str.isEmpty() && memory_str.contains("File") && !line.contains("File")) |
| 307 | { |
| 308 | // memory_str == "File": This is a help message! |
| 309 | // skip action if entering client mode |
| 310 | if (line.indexOf("Set client to be True", 0, Qt::CaseInsensitive) != -1) |
| 311 | return IT_OTHER; |
| 312 | |
| 313 | if (memory == 14) |
| 314 | // you have message |
| 315 | emit signal_msgBox(line); |
| 316 | else |
| 317 | console->recvText(txt.toLatin1().constData()); |
| 318 | |
| 319 | if (line.indexOf("#>") != -1 && !memory_str.isEmpty() && !memory_str.contains("File")) |
| 320 | return NOCLIENTMODE; |
| 321 | |
| 322 | return MESSAGE; |
| 323 | } |
| 324 | else |
| 325 | { |
| 326 | // remove command number |
| 327 | line = line.remove(0, 2).trimmed(); |
| 328 | } |
| 329 | //qDebug("%s", line.toLatin1().constData()); |
| 330 | |
| 331 | // correct cmd_nr for special case; if quiet is set to false a game may not end... |
| 332 | if (cmd_nr == 9 && line.contains("{Game")) |
| 333 | { |
| 334 | qDebug("command changed: 9 -> 21 !!!"); |
| 335 | cmd_nr = 21; |
| 336 | } |
| 337 | |
| 338 | // case 42 is equal to 7 |
| 339 | if (cmd_nr == 42 && gsName != IGS) |
| 340 | { |
| 341 | // treat as game info |
| 342 | cmd_nr = 7; |
| 343 | qDebug("command changed: 42 -> 7 !!!"); |
| 344 | } |
| 345 | |
| 346 | // process for different servers has particular solutions |
| 347 | // command mode -> expect result |
| 348 | switch (cmd_nr) |
| 349 | { |
| 350 | // PROMPT |
| 351 | |
| 352 | |
| 353 | |
| 354 | // IGS:48 Game 354 qGoDev requests an adjournment |
| 355 | case 48: |
| 356 | // have a look at case 9 |
| 357 | |
| 358 | break; |
| 359 | // 51 Say in game 583 |
| 360 | case 51: |
| 361 | // we could get the game id number here and store it |
| 362 | // its weird that there's also a case 11 message |
| 363 | break; |
| 364 | // IGS review protocol |
| 365 | // 56 CREATE 107 |
| 366 | // 56 DATA 107 |
| 367 | // 56 OWNER 107 eb5 3k |
| 368 | // 56 BOARDSIZE 107 19 |
| 369 | // 56 OPEN 107 1 |
| 370 | // 56 KIBITZ 107 1 |
| 371 | // 56 KOMI 107 0.50 |
| 372 | // 56 TITLE 107 yfh22-eb5(B) IGS |
| 373 | // 56 SGFNAME 107 yfh22-eb5-09-03-24 |
| 374 | // 56 WHITENAME 107 yfh22 |
| 375 | // 56 WHITERANK 107 1d? |
| 376 | // 56 BLACKNAME 107 eb5 |
| 377 | // 56 BLACKRANK 107 3k |
| 378 | // 56 GAMERESULT 107 B+Resign |
| 379 | // 56 NODE 107 1 0 0 0 |
| 380 | // 56 NODE 107 2 1 16 4 |
| 381 | // 56 NODE 107 3 2 16 16 |
| 382 | // 56 NODE 107 4 1 4 4 |
| 383 | // 56 NODE 107 5 2 4 16 |
| 384 | // 56 NODE 107 1 0 0 0 |
| 385 | // 56 CONTROL 107 eb5 |
| 386 | // 56 DATAEND 107 |
| 387 | // 56 ERROR That user's client does not support review. |
| 388 | // 56 INVITED_PLAY 58 yfh2test |
| 389 | |
| 390 | case 56: |
| 391 | #ifdef FIXME |
| 392 | if (line.contains("CREATE")) |
| 393 | aGame->number = element(line, 1, " ").toInt(); |
| 394 | if (line.contains("OWNER")) |
| 395 | aGame->player = element(line, 2, " "); |
| 396 | if (line.contains("BOARDSIZE")) |
| 397 | { |
| 398 | aGame->board_size = element(line, 2, " ").toInt(); |
| 399 | memory = aGame->board_size; |
| 400 | } |
| 401 | if (line.contains("KOMI")) |
| 402 | aGame->K = element(line, 2, " "); |
| 403 | if (line.contains("WHITENAME")) |
| 404 | aGame->wname = element(line, 2, " "); |
| 405 | if (line.contains("WHITERANK")) |
| 406 | { |
| 407 | aGame->white_rank = element(line, 2, " "); |
| 408 | fixRankString(&(aGame->white_rank)); |
| 409 | aGame->white_rank_score = rankToScore(aGame->white_rank); |
| 410 | } |
| 411 | if (line.contains("BLACKNAME")) |
| 412 | aGame->black_name = element(line, 2, " "); |
| 413 | if (line.contains("BLACKRANK")) |
| 414 | { |
| 415 | aGame->black_rank = element(line, 2, " "); |
| 416 | fixRankString(&(aGame->black_rank)); |
| 417 | aGame->black_rank_score = rankToScore(aGame->black_rank); |
| 418 | } |
| 419 | if (line.contains("GAMERESULT")) |
| 420 | { |
| 421 | aGame->res = element(line, 2, " "); |
| 422 | emit signal_gameReview(aGame); |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | if (line.contains("INVITED_PLAY")) |
| 427 | { |
| 428 | emit signal_reviewInvite(element(line, 1, " "), element(line, 2, " ")); |
| 429 | break ; |
| 430 | } |
| 431 | |
| 432 | if (line.contains("NODE")) |
| 433 | { |
| 434 | |
| 435 | StoneColor sc = stoneNone; |
| 436 | int c = element(line, 3, " ").toInt(); |
| 437 | |
| 438 | if (c==1) |
| 439 | sc = stoneBlack; |
| 440 | else if(c==2) |
| 441 | sc=stoneWhite; |
| 442 | |
| 443 | |
| 444 | emit signal_reviewNode(element(line, 1, " ").toInt(), element(line, 2, " ").toInt(), sc, element(line, 4, " ", "EOL").toInt() ,element(line, 3, " ").toInt() ); |
| 445 | break ; |
| 446 | } |
| 447 | #endif //FIXME |
| 448 | |
| 449 | |
| 450 | |
| 451 | break; |
| 452 | |
| 453 | |
| 454 | } |
| 455 | |
| 456 | return IT_OTHER; |
| 457 | } |
| 458 | |
| 459 | unsigned int Parser::idleTimeToSeconds(QString time) |
| 460 | { |
| 461 | QString i = time; |
| 462 | |
| 463 | |
| 464 | /* I guess its either minutes or seconds here, not both */ |
| 465 | if(time.contains("m")) |
| 466 | { |
| 467 | i.replace(QRegExp("[m*]"), ""); |
| 468 | return (60 * i.toInt()); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | i.replace(QRegExp("s"), ""); |
| 473 | return i.toInt(); |
| 474 | } |
| 475 | /*m1 = time1; m2 = time2; |
| 476 | s1 = time1; s2 = time2; |
| 477 | m1.replace(QRegExp("[m*]"), ""); |
| 478 | m2.replace(QRegExp("[m*]"), ""); |
| 479 | s1.replace(QRegExp("[*m]"), ""); |
| 480 | s1.replace(QRegExp("s"), ""); |
| 481 | s2.replace(QRegExp("[*m]"), ""); |
| 482 | s2.replace(QRegExp("s"), ""); |
| 483 | int seconds1 = (m1.toInt() * 60) + s1.toInt(); |
| 484 | int seconds2 = (m2.toInt() * 60) + s2.toInt();*/ |
| 485 | } |
| 486 | |
| 487 | void Parser::fixRankString(QString * rank) |
| 488 | { |
| 489 | if(rank->at(rank->length() - 1) == '*') |
| 490 | rank->truncate(rank->length() - 1); |
| 491 | else if(*rank == "NR") {} |
| 492 | else |
| 493 | *rank += "?"; |
| 494 | } |
| 495 | |
| 496 | GSName Parser::get_gsname() |
| 497 | { |
| 498 | return gsName; |
| 499 | } |
| 500 | |
| 501 | void Parser::set_gsname(const GSName gs) |
| 502 | { |
| 503 | gsName = gs; |
| 504 | } |