File: xIrcConnect3.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (380 lines) | stat: -rw-r--r-- 11,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/***************************************************************************
**    xIrcConnect3.cpp  $Revision: 1.10 $ - $Name: V2-0 $ 
**    Class for making the server connections Part 3
**
** Copyright (C) 1995, 1996  Joseph Croft <jcroft@unicomp.net>  
** 
** 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 1, or (at your option)
** any later version.
** 
** 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.
** 
** 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.
**
 ***************************************************************************/
#include <unistd.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <qkeycode.h>
#include <qmenubar.h>
#include <xLineEditQuery.h>
#include <xMessageBox.h>
#include "xIrcCommands.h"
#include "xIrcErrorBox.h"
#include "xIrcMsgDispatch.h"
#include "xIrcPeopleEdit.h"
#include "xIrcPeopleEntry.h"
#include "xDefaults.h"
#include "xIrcConnect.h"

static int dbg = 0;

extern xLineEditQuery *KickQuery;
extern xIrcCommands ircResponses;
extern xIrcMsgDispatch Dispatcher;

void xIrcConnect::nickActionHandler(xMultiLineTextSelection txtSel)
{
   int err;
   char buf[256];
   const char *cp;
   xIrcMessage msg;
   xIrcMessageFrame *pMsg;
   xIrcDccChatFrame *pDccChatFrame;

   switch (txtSel.iData)
   {
      case xIrcNickActionQuery::DccChat:
         struct sockaddr_in addr = pSocket->socketName();
         sprintf(buf, "%d", addr.sin_addr.s_addr);
         if ((pDccChatFrame = new xIrcDccChatFrame(wdtPrv, NULL, txtSel.text)) != NULL)
         {
            connect(pDccChatFrame, SIGNAL(initiateDCCChat(xIrcDccChatFrame*)),
                    this, SLOT(initiateDCCChat(xIrcDccChatFrame*)));
            if ((err = pDccChatFrame->accept()) == 0)
            {
               connect(pDccChatFrame, SIGNAL(textSelected(xMultiLineTextSelection)),
                       pNickAction, SLOT(wakeUp(xMultiLineTextSelection)));
               pDccChatFrame->show();
            }
            else
            {
               sprintf(buf, "Error Accepting Connection from %s:%s",
                            (const char *)txtSel.text, strerror(err));
               xMessageBox::message("Error", buf);
               disconnect(pDccChatFrame, SIGNAL(initiateDCCChat(xIrcDccChatFrame*)),
                          this, SLOT(initiateDCCChat(xIrcDccChatFrame*)));
               delete pDccChatFrame;
            }
         }
         return;
                  
      case xIrcNickActionQuery::PrivChat:
         if (findMsgFrame(txtSel.text) == NULL)
         {
            if (dbg) fprintf(stdout, "xIrcConnect::gotChannelBoxResults():Creating Message Frame\n");
            if (dbg) fflush(stdout);
            if ((pMsg = makeNewMsgFrame(NULL, txtSel.text)) != NULL)
            {
               if (dbg) fprintf(stdout, "xIrcConnect::gotChannelBoxResults():Showing Message Frame\n");
               if (dbg) fflush(stdout);
               pMsg->show();
               if (dbg) fprintf(stdout, "xIrcConnect::gotChannelBoxResults():Getting channel name\n");
               if (dbg) fflush(stdout);
               cp = txtSel.text;
               if (*cp == '#')
               {
                  if (dbg) fprintf(stdout, "xIrcConnect::gotChannelBoxResults():Joining channel\n");
                  if (dbg) fflush(stdout);
                  sprintf(buf, "JOIN %s\n", (const char *)txtSel.text);
                  emit textOut(buf);
               }
            }     
         }
         else
            xMessageBox::message("Error", "Channel already opened");
         pNickAction->hide();
         return;

      case xIrcNickActionQuery::Kick:
         cp = txtSel.winName;
         if (*cp != '#')
         {
            xMessageBox::message("Error", "Channel not specified!!");
            return;
         }
         else if (KickQuery->exec() != QDialog::Rejected)
         {
            msg.rspCode = ircResponses.code("KICK");
            msg.dstStr = txtSel.winName;
            msg.dstStr += ' ';
            msg.dstStr += txtSel.text;
            msg.msgStr = KickQuery->text();
         }
         else
            return;
         break;

      case xIrcNickActionQuery::WhoIs:
         msg.rspCode = ircResponses.code("WHOIS");
         msg.dstStr = txtSel.text;
         msg.msgStr = "";
         break;

      case xIrcNickActionQuery::Who:
         msg.rspCode = ircResponses.code("WHO");
         msg.dstStr = txtSel.text;
         msg.msgStr = "";
         break;

      case xIrcNickActionQuery::Ping:
         sprintf(buf, "%ld", time(NULL));
         msg.rspCode = ircResponses.code("PRIVMSG");
         msg.dstStr = txtSel.text;
         msg.msgStr = "\x01";
         msg.msgStr += "PING ";
         msg.msgStr += buf;
         msg.msgStr += "\x01";
         break;

      case xIrcNickActionQuery::Ban:
         cp = txtSel.winName;
         if (*cp != '#')
         {
            xMessageBox::message("Error", "Channel not specified!!");
            return;
         }
         else
         {
            banFlag = TRUE;
            banChan = txtSel.winName;
            banNick = txtSel.text;
            msg.rspCode = ircResponses.code("WHO");
            msg.dstStr = txtSel.text;
            msg.msgStr = "";
            Dispatcher.dispatchMsg(this, SLOT(doBanResp(xIrcMessage*)), &msg);
            return;
         }
         break;

      case xIrcNickActionQuery::GiveOps:
         cp = txtSel.winName;
         if (*cp != '#')
         {
            xMessageBox::message("Error", "Channel not specified!!");
            return;
         }
         else
         {
            msg.rspCode = ircResponses.code("MODE");
            msg.dstStr = txtSel.winName;
            msg.dstStr += " +o ";
            msg.dstStr += txtSel.text;
            msg.msgStr = "";
         }
         break;

      case xIrcNickActionQuery::TakeOps:
         cp = txtSel.winName;
         if (*cp != '#')
         {
            xMessageBox::message("Error", "Channel not specified!!");
            return;
         }
         else
         {
            msg.rspCode = ircResponses.code("MODE");
            msg.dstStr = txtSel.winName;
            msg.dstStr += " -o ";
            msg.dstStr += txtSel.text;
            msg.msgStr = "";
         }
         break;

      case xIrcNickActionQuery::WhoWas:
         msg.rspCode = ircResponses.code("WHOWAS");
         msg.dstStr = txtSel.text;
         msg.msgStr = "";
         break;

      case xIrcNickActionQuery::UserInfo:
         msg.rspCode = ircResponses.code("PRIVMSG");
         msg.dstStr = txtSel.text;
         msg.msgStr = "\x01";
         msg.msgStr += "USERINFO";
         msg.msgStr += "\x01";
         break;

      case xIrcNickActionQuery::Time:
         msg.rspCode = ircResponses.code("PRIVMSG");
         msg.dstStr = txtSel.text;
         msg.msgStr = "\x01";
         msg.msgStr += "TIME";
         msg.msgStr += "\x01";
         break;

      case xIrcNickActionQuery::Finger:
         msg.rspCode = ircResponses.code("PRIVMSG");
         msg.dstStr = txtSel.text;
         msg.msgStr = "\x01";
         msg.msgStr += "FINGER";
         msg.msgStr += "\x01";
         break;

      case xIrcNickActionQuery::Version:
         msg.rspCode = ircResponses.code("PRIVMSG");
         msg.dstStr = txtSel.text;
         msg.msgStr = "\x01";
         msg.msgStr += "VERSION";
         msg.msgStr += "\x01";
         break;

      case xIrcNickActionQuery::Close:
         pNickAction->hide();
         return;
         break;

      case xIrcNickActionQuery::Ignore:
         ignoreFlag = TRUE;
         ignoreNick = txtSel.text;
         msg.rspCode = ircResponses.code("WHO");
         msg.dstStr = txtSel.text;
         msg.msgStr = "";
         Dispatcher.dispatchMsg(this, SLOT(doIgnoreResp(xIrcMessage*)), &msg);
         return;
         break;

      case xIrcNickActionQuery::Notify:
         notifyQueryFlag = TRUE;
         notifyNick = txtSel.text;
         msg.rspCode = ircResponses.code("WHO");
         msg.dstStr = txtSel.text;
         msg.msgStr = "";
         Dispatcher.dispatchMsg(this, SLOT(doNotifyResp(xIrcMessage*)), &msg);
         return;
         break;
   }
   Dispatcher.dispatchMsg(NULL, NULL, &msg);
//   emit msgOut(&msg);
//   pNickAction->hide();
}

QString xIrcConnect::buildBanMask(xIrcMessage *pMsg)
{
   QString rv("*!");
   const char *cp;

   if (pMsg->rspCode == 352)
   {
      cp = pMsg->dstStr;
      if (*cp != '#')
      {
         rv += pMsg->dstStr;
         for (cp = pMsg->msgStr; *cp == ' '; cp++);
      }
      else
      {
         for (cp = pMsg->msgStr; *cp == ' '; cp++);
         for (; *cp != ' '; cp++)
            rv += *cp;
      }
      rv += "@";
      for (; *cp == ' '; cp++);
      if (isdigit(*cp))
      {
         for (; *cp != ' '; cp++)
            rv += *cp;
      }
      else
      {
         rv += "*";
         for (; *cp != '.'; cp++);
         for (; *cp != ' '; cp++)
            rv += *cp;
      }
   }
   else
      rv = "";
   return(rv);
}

void xIrcConnect::doIgnoreResp(xIrcMessage *pMsg)
{
   if (dbg) fprintf(stdout, "xIrcConnect::doIgnoreResp():Have Ignore Who Response!\n");
   if (dbg) fflush(stdout);
   if (pMsg->rspCode == 352)
   {
      ignoreFlag = FALSE;
      xIrcPeopleEntry e(pMsg);
      xIrcPeopleEdit editDlg(wdtRes, NULL, "Ignore Entry Edit", &e);
      if (editDlg.exec() == Accepted)
         pIgnore->add(e);
   }
}         

void xIrcConnect::doNotifyResp(xIrcMessage *pMsg)
{
   if (dbg) fprintf(stdout, "xIrcConnect::doNotifyResp():Have Notify Who Response!\n");
   if (dbg) fflush(stdout);
   if (pMsg->rspCode == 352)
   {
      notifyQueryFlag = FALSE;
      xIrcPeopleEntry e(pMsg);
      xIrcPeopleEdit editDlg(wdtRes, NULL, "Notify Entry Edit", &e);
      if (editDlg.exec() == Accepted)
         pNotify->add(e);
   }
}

void xIrcConnect::doBanResp(xIrcMessage *pMsg)
{
   xIrcMessage msg;
   char str[120];
   int x;

   if (pMsg->rspCode == 352)
   {
      if (dbg) fprintf(stdout, "xIrcConnect::doBanResp():Have Ban Who Response!\n");
      if (dbg) fflush(stdout);
      banFlag = FALSE;
      pBanBox->setNick(banNick);
      pBanBox->setChan(banChan);
      pBanBox->setMask(buildBanMask(pMsg));
      if ((x = pBanBox->exec()) != xIrcBanDialog::Rejected)
      {
         msg.rspCode = ircResponses.code("MODE");
         msg.dstStr = pBanBox->chan();
         msg.dstStr += (x == xIrcBanDialog::UnBan) ? " -b " : " +b ";
         msg.dstStr += pBanBox->mask();
         msg.msgStr = "";
         sprintf(str, "\x02*** %sanning from %s: %s\n",
                      (x == xIrcBanDialog::UnBan) ? "Unb" : "B",
                      (const char *)pBanBox->chan(),
                      (const char *)pBanBox->nick());
         pMainWin->pWin->putString(str);
         emit msgOut(&msg);
         banFlag = FALSE;
         if (x == xIrcBanDialog::Kick)
         {
            msg.rspCode = ircResponses.code("KICK");
            msg.dstStr = pBanBox->chan();
            msg.dstStr += " ";
            msg.dstStr += pBanBox->nick();
            msg.msgStr = pBanBox->text();
            emit msgOut(&msg);
         }
      }
   }   
}