summary refs log tree commit diff stats
path: root/plugins/perl/lib/IRC.pm
blob: e642fa3dd76020326c1c82dd20386ddd6f16af5d (plain) (blame)
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
package IRC;
sub IRC::register {
  my ($script_name, $version, $callback) = @_;
  my $package = caller;
  $callback = Xchat::Embed::fix_callback( $package, undef, $callback) if $callback;
  Xchat::register( $script_name, $version, undef, $callback );
}


sub IRC::add_command_handler {
  my ($command, $callback) = @_;
  my $package = caller;

  $callback = Xchat::Embed::fix_callback( $package, undef, $callback );

  # starting index for word_eol array
  # this is for compatibility with '' as the command
  my $start_index = $command ? 1 : 0;

  Xchat::hook_command( $command,
		       sub {
			 no strict 'refs';
			 return &{$callback}($_[1][$start_index]);
		       }
		     );
  return;
}

sub IRC::add_message_handler {
  my ($message, $callback) = @_;
  my $package = caller;
  $callback = Xchat::Embed::fix_callback( $package, undef, $callback );

  Xchat::hook_server( $message,
		      sub {
			no strict 'refs';
			return &{$callback}( $_[1][0] );
		      }
		    );
  return;
}

sub IRC::add_print_handler {
  my ($event, $callback) = @_;
  my $package = caller;
  $callback = Xchat::Embed::fix_callback( $package, undef, $callback );
  Xchat::hook_print( $event,
		     sub {
		       my @word = @{$_[0]};
		       no strict 'refs';
		       return &{$callback}( join( ' ', @word[0..3] ), @word );
		     }
		   );
  return;
}

sub IRC::add_timeout_handler {
  my ($timeout, $callback) = @_;
  my $package = caller;
  $callback = Xchat::Embed::fix_callback( $package, undef, $callback );
  Xchat::hook_timer( $timeout,
		     sub {
		       no strict 'refs';
		       &{$callback};
		       return 0;
		     }
		   );
  return;
}

sub IRC::command {
  my $command = shift;
  if( $command =~ m{^/} ) {
    $command =~ s{^/}{};
    Xchat::command( $command );
  } else {
    Xchat::command( qq[say $command] );
  }
}

sub IRC::command_with_channel {
  my ($command, $channel, $server) = @_;
  my $old_ctx = Xchat::get_context;
  my $ctx = Xchat::find_context( $channel, $server );

  if( $ctx ) {
    Xchat::set_context( $ctx );
    IRC::command( $command );
    Xchat::set_context( $ctx );
  }
}

sub IRC::command_with_server {
  my ($command, $server) = @_;
  my $old_ctx = Xchat::get_context;
  my $ctx = Xchat::find_context( undef, $server );

  if( $ctx ) {
    Xchat::set_context( $ctx );
    IRC::command( $command );
    Xchat::set_context( $ctx );
  }
}

sub IRC::dcc_list {
  my @dccs;
  for my $dcc ( Xchat::get_list( 'dcc' ) ) {
    push @dccs, $dcc->{nick};
    push @dccs, $dcc->{file} ? $dcc->{file} : '';
    push @dccs, @{$dcc}{qw(type status cps size)};
    push @dccs, $dcc->{type} == 0 ? $dcc->{pos} : $dcc->{resume};
    push @dccs, $dcc->{address32};
    push @dccs, $dcc->{destfile} ? $dcc->{destfile} : '';
  }
  return @dccs;
}

sub IRC::channel_list {
  my @channels;
  for my $channel ( Xchat::get_list( 'channels' ) ) {
    push @channels, @{$channel}{qw(channel server)},
      Xchat::context_info( $channel->{context} )->{nick};
  }
  return @channels;
}

sub IRC::get_info {
  my $id = shift;
  my @ids = qw(version nick channel server configdir xchatdir away network host topic);
  
  if( $id >= 0 && $id <= 8 && $id != 5 ) {
    my $info = Xchat::get_info($ids[$id]);
    return defined $info ? $info : '';
  } else {
    if( $id == 5 ) {
      return Xchat::get_info( 'away' ) ? 1 : 0;
    } else {
      return 'Error2';
    }
  }
}

sub IRC::get_prefs {
  return 'Unknown variable' unless defined $_[0];
  my $result = Xchat::get_prefs(shift);
  return defined $result ? $result : 'Unknown variable';
}

sub IRC::ignore_list {
  my @ignores;
  for my $ignore ( Xchat::get_list( 'ignore' ) ) {
    push @ignores, $ignore->{mask};
    my $flags = $ignore->{flags};
    push @ignores, $flags & 1, $flags & 2, $flags & 4, $flags & 8, $flags & 16,
      $flags & 32, ':';
  }
  return @ignores;
}

sub IRC::print {
  Xchat::print( $_ ) for @_;
  return;
}

sub IRC::print_with_channel {
  Xchat::print( @_ );
}

sub IRC::send_raw {
  Xchat::commandf( qq[quote %s], shift );
}

sub IRC::server_list {
  my @servers;
  for my $channel ( Xchat::get_list( 'channels' ) ) {
    push @servers, $channel->{server} if $channel->{server};
  }
  return @servers;
}

sub IRC::user_info {
  my $user;
  if( @_ > 0 ) {
    $user = Xchat::user_info( shift );
  } else {
    $user = Xchat::user_info();
  }

  my @info;
  if( $user ) {
    push @info, $user->{nick};
    if( $user->{host} ) {
      push @info, $user->{host};
    } else {
      push @info, 'FETCHING';
    }
    push @info, $user->{prefix} eq '@' ? 1 : 0;
    push @info, $user->{prefix} eq '+' ? 1 : 0;
  }
  return @info;
}

sub IRC::user_list {
  my ($channel, $server) = @_;
  my $ctx = Xchat::find_context( $channel, $server );
  my $old_ctx = Xchat::get_context;

  if( $ctx ) {
    Xchat::set_context( $ctx );
    my @users;
    for my $user ( Xchat::get_list( 'users' ) ) {
      push @users, $user->{nick};
      if( $user->{host} ) {
	push @users, $user->{host};
      } else {
	push @users, 'FETCHING';
      }
      push @users, $user->{prefix} eq '@' ? 1 : 0;
      push @users, $user->{prefix} eq '+' ? 1 : 0;
      push @users, ':';
    }
    Xchat::set_context( $old_ctx );
    return @users;
  } else {
    return;
  }
}

sub IRC::user_list_short {
  my ($channel, $server) = @_;
  my $ctx = Xchat::find_context( $channel, $server );
  my $old_ctx = Xchat::get_context;

  if( $ctx ) {
    Xchat::set_context( $ctx );
    my @users;
    for my $user ( Xchat::get_list( 'users' ) ) {
      my $nick = $user->{nick};
      my $host = $user->{host} || 'FETCHING';
      push @users, $nick, $host;
    }
    Xchat::set_context( $old_ctx );
    return @users;
  } else {
    return;
  }

}

sub IRC::add_user_list {}
sub IRC::sub_user_list {}
sub IRC::clear_user_list {}
sub IRC::notify_list {}
sub IRC::perl_script_list {}

1
class="w"> else { ShowBalloon(g_hXchatWnd, 1, szTemp, "Alert", iTime, NIIF_INFO); } } free(szTemp); return XCHAT_EAT_ALL; } LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_CLOSE: { if((g_dwPrefs & (1<<PREF_MIOC)) && (g_bCanQuit == false)) { /*******************************************/ /**** to autoaway or not to autoaway... ***/ /*******************************************/ if(g_dwPrefs & (1<<PREF_AOM)) { xchat_globally_away(g_szAway); } /**************************************************/ /**** Win32 API call to hide the window and **/ /**** save the fact that its minimized for later **/ /**************************************************/ g_iIsActive = 0; ShowWindow(hWnd, SW_HIDE); return 0; } else { if(g_hPrefDlg != NULL) { DestroyWindow(g_hPrefDlg); } StopBlink(hWnd, 1, g_hIcons[0]); if(sdAlertNum()) { sdCloseAlerts(); HoldClose(); return 0; } } } break; case WM_SIZE: { /******************************************/ /***** User wants to minimize xChat, ******/ /***** are we allowed to go to tray? ******/ /******************************************/ if((g_dwPrefs & (1<<PREF_TOT)) && (wparam == SIZE_MINIMIZED)) { /*******************************************/ /**** to autoaway or not to autoaway... ***/ /*******************************************/ if(g_dwPrefs & (1<<PREF_AOM)) { xchat_globally_away(g_szAway); } /**************************************************/ /**** Win32 API call to hide the window and **/ /**** save the fact that its minimized for later **/ /**************************************************/ g_iIsActive = 0; ShowWindow(hWnd, SW_HIDE); } } break; /**********************************/ /*** user clicked the tray icon ***/ /**********************************/ case WM_TRAYMSG: { switch(lparam) { case WM_LBUTTONDOWN: { if(!g_iIsActive) { /*********************************************************/ /*** 0: its hiden, restore it and show it, if autoaway ***/ /*** is on, set us as back ***/ /*********************************************************/ SendMessage(hWnd, WM_SYSCOMMAND, SC_RESTORE, 0); SetForegroundWindow(hWnd); g_iIsActive = 1; if(g_dwPrefs & (1<<PREF_AOM)) { xchat_globally_back(); } } else { SendMessage(hWnd, WM_SYSCOMMAND, SC_MINIMIZE, 0); } } break; case WM_RBUTTONDOWN: { /******************************************/ /*** user wants to see the menu find out **/ /*** where the mouse is and show it **/ /******************************************/ POINT pt; int iRet; GetCursorPos(&pt); SetForegroundWindow(hWnd); ModifyMenu(g_hTrayMenu, 2, (MF_POPUP | MF_BYPOSITION), (UINT)setServerMenu(), _T("Away")); Sleep(175); iRet = TrackPopupMenuEx(g_hTrayMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), pt.x, pt.y, hWnd, NULL); /***********************************/ /*** nRet is the users selection, **/ /*** process it **/ /***********************************/ sdTrayProc(hWnd, iRet); } break; } } break; default: { /*****************************************************/ /*** the taskbar has been restarted, re-add our icon */ /*****************************************************/ if(msg == RegisterWindowMessage(_T("TaskbarCreated"))) { char szVersion[64]; _snprintf(szVersion, 64, "XChat-WDK [%s]", xchat_get_info(ph, "version")); AddIcon(g_hXchatWnd, 1, g_hIcons[0], szVersion, (NIF_ICON | NIF_MESSAGE | NIF_TIP), WM_TRAYMSG); } } break; } return CallWindowProc(g_hOldProc, hWnd, msg, wparam, lparam); } /****************************************************/ /*** process messages from the tray menu ************/ /****************************************************/ LRESULT CALLBACK sdTrayProc(HWND hWnd, int msg) { switch(msg) { case ACT_EXIT: { g_bCanQuit = true; PostMessage(hWnd, WM_CLOSE, 0, 0); } break; case ACT_RESTORE: { /***********************************************/ /** user wants us to restore the xchat window **/ /** and of autoaway is on, set as back **/ /***********************************************/ SendMessage(g_hXchatWnd, WM_SYSCOMMAND, SC_RESTORE, 0); SetForegroundWindow(hWnd); if((!g_iIsActive) && (g_dwPrefs & (1<<PREF_AOM))) { xchat_globally_back(); g_iIsActive = 1; } } break; case ACT_SETTINGS: { ShowWindow(g_hPrefDlg, SW_SHOW); } break; case ACT_AWAY: { xchat_globally_away(g_szAway); } break; case ACT_BACK: { xchat_globally_back(); } break; default: { if(msg > 0) { xchat_set_context(ph, xchat_find_server(msg-1)); if(!xchat_get_info(ph, "away")) { xchat_away(g_szAway); } else { xchat_back(); } } } break; } return 1; } int CALLBACK PrefProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_INITDIALOG: { TCITEM tci1; TCITEM tci2; TCITEM tci3; TCITEM tci4; tci1.mask = TCIF_TEXT; tci1.pszText = _T("Settings"); tci1.cchTextMax = strlen("Settings"); SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_INSERTITEM, 0, (LPARAM)&tci1); tci2.mask = TCIF_TEXT; tci2.pszText = _T("Alerts"); tci2.cchTextMax = strlen("Alerts"); SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_INSERTITEM, 1, (LPARAM)&tci2); tci3.mask = TCIF_TEXT; tci3.pszText = _T("Events"); tci3.cchTextMax = strlen("Events"); SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_INSERTITEM, 2, (LPARAM)&tci3); tci4.mask = TCIF_TEXT; tci4.pszText = _T("About"); tci4.cchTextMax = strlen("About"); SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_INSERTITEM, 3, (LPARAM)&tci4); /***********************************************************************************/ /***********************************************************************************/ /***********************************************************************************/ g_hPrefTabSettings = CreateDialog((HINSTANCE)g_hInstance, MAKEINTRESOURCE(IDD_SETTINGS), hWnd, (DLGPROC)SettingsProc); SetDialog(g_hPrefTabSettings, IDD_SETTINGS); g_hPrefTabAlerts = CreateDialog((HINSTANCE)g_hInstance, MAKEINTRESOURCE(IDD_ALERTS), hWnd, (DLGPROC)AlertsProc); SetDialog(g_hPrefTabAlerts, IDD_ALERTS); g_hPrefTabEvents = CreateDialog((HINSTANCE)g_hInstance, MAKEINTRESOURCE(IDD_EVENTS), hWnd, (DLGPROC)EventsProc); SetDialog(g_hPrefTabEvents, IDD_EVENTS); g_hPrefTabAbout = CreateDialog((HINSTANCE)g_hInstance, MAKEINTRESOURCE(IDD_ABOUT), hWnd, (DLGPROC)AboutProc); } break; case WM_SHOWWINDOW: { if(wparam) { SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_SETCURSEL, 0, 0); ShowWindow(g_hPrefTabSettings, SW_SHOW); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); } } break; case WM_NOTIFY: { NMHDR *pData = (NMHDR *)lparam; switch(pData->code) { case TCN_SELCHANGE: { switch(SendDlgItemMessage(hWnd, IDC_TAB_CONTROL, TCM_GETCURSEL, 0, 0)) { case 0: { ShowWindow(g_hPrefTabSettings, SW_SHOW); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); } break; case 1: { ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_SHOW); ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); } break; case 2: { ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabEvents, SW_SHOW); ShowWindow(g_hPrefTabAbout, SW_HIDE); } break; case 3: { ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_SHOW); } break; } } break; } } break; case WM_CLOSE: { ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); ShowWindow(hWnd, SW_HIDE); return TRUE; } break; case WM_COMMAND: { switch(wparam) { case IDC_PREF_OK: { CheckPrefs(g_hPrefTabEvents, IDD_EVENTS); CheckPrefs(g_hPrefTabSettings, IDD_SETTINGS); CheckPrefs(g_hPrefTabAlerts, IDD_ALERTS); SavePrefs(0); ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); ShowWindow(hWnd, SW_HIDE); return TRUE; } break; case IDC_PREF_CANCEL: { ShowWindow(g_hPrefTabEvents, SW_HIDE); ShowWindow(g_hPrefTabSettings, SW_HIDE); ShowWindow(g_hPrefTabAlerts, SW_HIDE); ShowWindow(g_hPrefTabAbout, SW_HIDE); ShowWindow(hWnd, SW_HIDE); return TRUE; } break; case IDC_PREF_APPLY: { CheckPrefs(g_hPrefTabEvents, IDD_EVENTS); CheckPrefs(g_hPrefTabSettings, IDD_SETTINGS); CheckPrefs(g_hPrefTabAlerts, IDD_ALERTS); SavePrefs(0); return FALSE; } break; } } break; case WM_DESTROY: { SendMessage(g_hPrefTabEvents, WM_CLOSE, 0, 0); SendMessage(g_hPrefTabSettings, WM_CLOSE, 0, 0); SendMessage(g_hPrefTabAbout, WM_CLOSE, 0, 0); SendMessage(g_hPrefTabAlerts, WM_CLOSE, 0, 0); } break; } return FALSE; } /****************************************************/ /****************************************************/ /****************************************************/ LRESULT CALLBACK AlertsProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { switch(msg) { case WM_CLOSE: { DestroyWindow(hWnd); return TRUE; break; } break; case WM_COMMAND: { switch(LOWORD(wparam)) { case PREF_AMAE: { SetToggle(hWnd, PREF_OSBWM, PREF_AMAE, TRUE); SetToggle(hWnd, PREF_UWIOB, PREF_AMAE, TRUE); SetToggle(hWnd, PREF_KAOI, PREF_AMAE, TRUE); if(IsDlgButtonChecked(hWnd, PREF_AMAE)) { SetToggle(hWnd, IDC_ALERT_HOTKEY, PREF_UWIOB, TRUE); SetToggle(hWnd, IDC_ALERT_HOTKEY_TEXT, PREF_UWIOB, TRUE); SetToggle(hWnd, IDC_ALERT_TIME, PREF_KAOI, FALSE); SetToggle(hWnd, IDC_ALERT_TIME_TEXT, PREF_KAOI, FALSE); } else { SetToggle(hWnd, IDC_ALERT_HOTKEY, PREF_AMAE, TRUE); SetToggle(hWnd, IDC_ALERT_HOTKEY_TEXT, PREF_AMAE, TRUE); SetToggle(hWnd, IDC_ALERT_TIME, PREF_AMAE, TRUE); SetToggle(hWnd, IDC_ALERT_TIME_TEXT, PREF_AMAE, TRUE); } } break; case PREF_UWIOB: { SetToggle(hWnd, IDC_ALERT_HOTKEY, PREF_UWIOB, TRUE); SetToggle(hWnd, IDC_ALERT_HOTKEY_TEXT, PREF_UWIOB, TRUE); } break; case PREF_KAOI: { SetToggle(hWnd, IDC_ALERT_TIME, PREF_KAOI, FALSE); SetToggle(hWnd, IDC_ALERT_TIME_TEXT, PREF_KAOI, FALSE); } break; } break; } } return FALSE; } /****************************************************/ /****************************************************/ /****************************************************/ LRESULT CALLBACK AboutProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { if(msg == WM_CLOSE) { DestroyWindow(hWnd); return true; } return FALSE; } /*****************************************************/ /** Process the events for our event dialog **********/ /*****************************************************/ LRESULT CALLBACK EventsProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { if(msg == WM_CLOSE) { DestroyWindow(hWnd); return true; } return FALSE; } /*****************************************************/ /** Process the events for our settings dialog this **/ /** is alot more complicated because options are **/ /** enabled/disabled based on the state of others **/ /*****************************************************/ LRESULT CALLBACK SettingsProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { if(msg == WM_CLOSE) { DestroyWindow(hWnd); return true; } return FALSE; } /*****************************************************/ /** this is the hotkey message processing function **/ /** this window is always open and ready to be told **/ /** if someone has hit the hotkey, if they did, we **/ /** need to close out all of the tray alerts, for **/ /** this I wrote sdCloseAlerts, more info there **/ /*****************************************************/ LRESULT CALLBACK HotKeyProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam) { if(msg == WM_CLOSE) { DestroyWindow(hWnd); return true; } else if(msg == WM_HOTKEY) { sdCloseAlerts(); } return FALSE; }