summary refs log tree commit diff stats
path: root/plugins/mpcinfo/mpcInfo.c
blob: 4ad176899610caeb25faa0b8e0f7ffe4e7b996be (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
/*
 *  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 2 of the License, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

//static int DEBUG=0;
static char *VERSION="0.0.6";

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include "hexchat-plugin.h"
static hexchat_plugin *ph;

#include "functions.c"
#include "mp3Info.c"
#include "oggInfo.c"
#include "theme.c"

static int print_themes (char *word[], char *word_eol[], void *userdata){
       printThemes();
       return HEXCHAT_EAT_ALL;
}

static int mpc_themeReload(char *word[], char *word_eol[], void *userdata){
   themeInit();
   loadThemes();
   return HEXCHAT_EAT_ALL;
}

static int mpc_tell(char *word[], char *word_eol[], void *userdata){
       char *tTitle, *zero, *oggLine, *line;
	   struct tagInfo info;
	   HWND hwnd = FindWindow("MediaPlayerClassicW",NULL);
       if (hwnd==0) {hexchat_print(ph, randomLine(notRunTheme));return HEXCHAT_EAT_ALL;}
       
       tTitle = g_new(char, 1024);
       GetWindowText(hwnd, tTitle, 1024);
       zero = strstr (tTitle, " - Media Player Classic");
	   if (zero != NULL)
	   {
		   zero[0] = 0;
	   }
	   else
	   {
		   g_free(tTitle);
		   hexchat_print(ph, "pattern not found");
		   return HEXCHAT_EAT_ALL;
	   }

       if ((tTitle[1]==':')&&(tTitle[2]=='\\')){
          //hexchat_print(ph,"seams to be full path");
          if (endsWith(tTitle,".mp3")==1){
             //hexchat_print(ph,"seams to be a mp3 file");
             info = readHeader(tTitle);
             
             if ((info.artist!=NULL)&&(strcmp(info.artist,"")!=0)){
                char *mode=MODES[info.mode];
                //hexchat_printf(ph,"mode: %s\n",mode);
                char *mp3Line=randomLine(mp3Theme);
                mp3Line=replace(mp3Line,"%art",info.artist);
                mp3Line=replace(mp3Line,"%tit",info.title);
                mp3Line=replace(mp3Line,"%alb",info.album);
                mp3Line=replace(mp3Line,"%com",info.comment);
                mp3Line=replace(mp3Line,"%gen",info.genre);
                //mp3Line=replace(mp3Line,"%time",pos);
                //mp3Line=replace(mp3Line,"%length",len);
                //mp3Line=replace(mp3Line,"%ver",waVers);
                //mp3Line=intReplace(mp3Line,"%br",br);
                //mp3Line=intReplace(mp3Line,"%frq",frq);
                
                mp3Line=intReplace(mp3Line,"%br",info.bitrate);
                mp3Line=intReplace(mp3Line,"%frq",info.freq);
                mp3Line=replace(mp3Line,"%mode",mode);
                //mp3Line=replace(mp3Line,"%size",size);
                //mp3Line=intReplace(mp3Line,"%perc",perc);
                //mp3Line=replace(mp3Line,"%plTitle",title);
                mp3Line=replace(mp3Line,"%file",tTitle);
				g_free(tTitle);
				hexchat_command(ph, mp3Line);
                return HEXCHAT_EAT_ALL;
             }
          }
          if (endsWith(tTitle,".ogg")==1){
             hexchat_printf(ph,"Ogg detected\n");
             info = getOggHeader(tTitle);
             if (info.artist!=NULL){
                char *cbr;
                if (info.cbr==1) cbr="CBR"; else cbr="VBR";
                oggLine=randomLine(oggTheme);
                //if (cue==1) oggLine=cueLine;
                //hexchat_printf(ph,"ogg-line: %s\n",oggLine);
                oggLine=replace(oggLine,"%art",info.artist);
                oggLine=replace(oggLine,"%tit",info.title);
                oggLine=replace(oggLine,"%alb",info.album);
                oggLine=replace(oggLine,"%com",info.comment);
                oggLine=replace(oggLine,"%gen",info.genre);
                //oggLine=replace(oggLine,"%time",pos);
                //oggLine=replace(oggLine,"%length",len);
                //oggLine=replace(oggLine,"%ver",waVers);
                oggLine=intReplace(oggLine,"%chan",info.mode);
                oggLine=replace(oggLine,"%cbr",cbr);
                oggLine=intReplace(oggLine,"%br",info.bitrate/1000);//br);
                oggLine=intReplace(oggLine,"%frq",info.freq);
                //oggLine=replace(oggLine,"%size",size);
                //oggLine=intReplace(oggLine,"%perc",perc);
                //oggLine=replace(oggLine,"%plTitle",title);
                oggLine=replace(oggLine,"%file",tTitle);
				g_free(tTitle);
				hexchat_command(ph, oggLine);
                return HEXCHAT_EAT_ALL;
             }
          }
       }
       line=randomLine(titleTheme);
       line=replace(line,"%title", tTitle);
	   g_free(tTitle);
	   hexchat_command(ph, line);
       return HEXCHAT_EAT_ALL;
}

int hexchat_plugin_init(hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg){
	ph = plugin_handle;
	*plugin_name = "mpcInfo";
	*plugin_desc = "Information-Script for Media Player Classic"; 
	*plugin_version=VERSION;

	hexchat_hook_command(ph, "mpc", HEXCHAT_PRI_NORM, mpc_tell,"no help text", 0);
	hexchat_hook_command(ph, "mpc_themes", HEXCHAT_PRI_NORM, print_themes,"no help text", 0);
	hexchat_hook_command(ph, "mpc_reloadthemes", HEXCHAT_PRI_NORM, mpc_themeReload,"no help text", 0);
	hexchat_command (ph, "MENU -ishare\\music.png ADD \"Window/Display Current Song (MPC)\" \"MPC\"");

	themeInit();
	loadThemes();
	hexchat_printf(ph, "%s plugin loaded\n", *plugin_name);

	return 1;
}

int
hexchat_plugin_deinit (void)
{
	hexchat_command (ph, "MENU DEL \"Window/Display Current Song (MPC)\"");
	hexchat_print (ph, "mpcInfo plugin unloaded\n");
	return 1;
}
+ 1 : 0); memcpy(request->RWSP, "RWSP", sizeof(request->RWSP)); l = end - (char *)request; /* all requests must be atleast MSPROXY_MINLENGTH it seems. */ if (l < MSPROXY_MINLENGTH) { bzero(end, (size_t)(MSPROXY_MINLENGTH - l)); l = MSPROXY_MINLENGTH; } if ((w = send(s, request, l, 0)) != l) { #ifdef DEBUG_MSPROXY printf ("send_msprequest(): send() failed (%d bytes sent instead of %d\n", w, l); perror ("Error is"); #endif return -1; } state->seq_sent = request->sequence; return w; } static int recv_mspresponse(s, state, response) int s; struct msproxy_state_t *state; struct msproxy_response_t *response; { ssize_t r; do { if ((r = recv (s, response, sizeof (*response), 0)) < MSPROXY_MINLENGTH) { #ifdef DEBUG_MSPROXY printf ("recv_mspresponse(): expected to read atleast %d, read %d\n", MSPROXY_MINLENGTH, r); #endif return -1; } if (state->seq_recv == 0) break; /* not started incrementing yet. */ #ifdef DEBUG_MSPROXY if (response->sequence == state->seq_recv) printf ("seq_recv: %d, dup response, seqnumber: 0x%x\n", state->seq_recv, response->sequence); #endif } while (response->sequence == state->seq_recv); state->seq_recv = response->sequence; return r; } int traverse_msproxy (int sok, char *serverAddr, int port, struct msproxy_state_t *state, netstore *ns_proxy, int csok4, int csok6, int *csok, char bound) { struct msproxy_request_t req; struct msproxy_response_t res; char *data, *p; char hostname[NT_MAXNAMELEN]; char ntdomain[NT_MAXNAMELEN]; char challenge[8]; netstore *ns_client; int clientport; guint32 destaddr; guint32 flags; if (!prefs.hex_net_proxy_auth || !prefs.hex_net_proxy_user[0] || !prefs.hex_net_proxy_pass[0] ) return 1; /* MS proxy protocol implementation currently doesn't support IPv6 */ destaddr = net_getsockaddr_v4 (ns_proxy); if (!destaddr) return 1; state->seq_recv = 0; state->seq_sent = 0; #ifdef DEBUG_MSPROXY printf ("Connecting to %s:%d via MS proxy\n", serverAddr, port); #endif gethostname (hostname, NT_MAXNAMELEN); p = strchr (hostname, '.'); if (p) *p = '\0'; bzero (&req, sizeof(req)); req.clientid = htonl(0x0a000000); /* Initial client ID is always 0x0a */ req.command = htons(MSPROXY_HELLO); /* HELLO command */ req.packet.hello.magic5 = htons(0x4b00); /* Fill in magic values */ req.packet.hello.magic10 = htons(0x1400); req.packet.hello.magic15 = htons(0x0400); req.packet.hello.magic20 = htons(0x5704); req.packet.hello.magic25 = htons(0x0004); req.packet.hello.magic30 = htons(0x0100); req.packet.hello.magic35 = htons(0x4a02); req.packet.hello.magic40 = htons(0x3000); req.packet.hello.magic45 = htons(0x4400); req.packet.hello.magic50 = htons(0x3900); data = req.packet.hello.data; strcpy (data, prefs.hex_net_proxy_user); /* Append a username */ data += strlen (prefs.hex_net_proxy_user)+2; /* +2 automatically creates second empty string */ strcpy (data, MSPROXY_EXECUTABLE); /* Append an application name */ data += strlen (MSPROXY_EXECUTABLE)+1; strcpy (data, hostname); /* Append a hostname */ data += strlen (hostname)+1; if (send_msprequest(sok, state, &req, data) == -1) return 1; if (recv_mspresponse(sok, state, &res) == -1) return 1; if (strcmp(res.RWSP, "RWSP") != 0) { #ifdef DEBUG_MSPROXY printf ("Received mailformed packet (no RWSP signature)\n"); #endif return 1; } if (ntohs(res.command) >> 8 != 0x10) { #ifdef DEBUG_MSPROXY printf ("expected res.command = 10??, is %x", ntohs(res.command)); #endif return 1; } state->clientid = htonl(rand()); state->serverid = res.serverid; #ifdef DEBUG_MSPROXY printf ("clientid: 0x%x, serverid: 0x%0x\n", state->clientid, state->serverid); printf ("packet #2\n"); #endif /* almost identical. */ req.clientid = state->clientid; req.serverid = state->serverid; if (send_msprequest(sok, state, &req, data) == -1) return 1; if (recv_mspresponse(sok, state, &res) == -1) return 1; if (res.serverid != state->serverid) { #ifdef DEBUG_MSPROXY printf ("expected serverid = 0x%x, is 0x%x\n",state->serverid, res.serverid); #endif return 1; } if (res.sequence != 0x01) { #ifdef DEBUG_MSPROXY printf ("expected res.sequence = 0x01, is 0x%x\n", res.sequence); #endif return 1; } if (ntohs(res.command) != MSPROXY_USERINFO_ACK) { #ifdef DEBUG_MSPROXY printf ("expected res.command = 0x%x, is 0x%x\n", MSPROXY_USERINFO_ACK, ntohs(res.command)); #endif return 1; } #ifdef DEBUG_MSPROXY printf ("packet #3\n"); #endif bzero(&req, sizeof(req)); req.clientid = state->clientid; req.serverid = state->serverid; req.command = htons(MSPROXY_AUTHENTICATE); memcpy(req.packet.auth.NTLMSSP, "NTLMSSP", sizeof("NTLMSSP")); req.packet.auth.bindaddr = htonl(0x02000000); req.packet.auth.msgtype = htonl(0x01000000); /* NTLM flags: 0x80000000 Negotiate LAN Manager key 0x10000000 Negotiate sign 0x04000000 Request target 0x02000000 Negotiate OEM 0x00800000 Always sign 0x00020000 Negotiate NTLM */ req.packet.auth.flags = htonl(0x06020000); if (send_msprequest(sok, state, &req, &req.packet.auth.data) == -1) return 1; if (recv_mspresponse(sok, state, &res) == -1) return 1; if (res.serverid != state->serverid) { #ifdef DEBUG_MSPROXY printf ("expected serverid = 0x%x, is 0x%x\n", state->serverid, res.serverid); #endif return 1; } if (ntohs(res.command) != MSPROXY_AUTHENTICATE_ACK) { #ifdef DEBUG_MSPROXY printf ("expected res.command = 0x%x, is 0x%x\n", MSPROXY_AUTHENTICATE_ACK, ntohs(res.command)); #endif return 1; } flags = res.packet.auth.flags & htonl(0x00020000); /* Remember if the server supports NTLM */ memcpy(challenge, &res.packet.auth.challenge, sizeof(challenge)); memcpy(ntdomain, &res.packet.auth.NTLMSSP[res.packet.auth.target.offset], res.packet.auth.target.len); ntdomain[res.packet.auth.target.len] = 0; #ifdef DEBUG_MSPROXY printf ("ntdomain: \"%s\"\n", ntdomain); printf ("packet #4\n"); #endif bzero(&req, sizeof(req)); req.clientid = state->clientid; req.serverid = state->serverid; req.command = htons(MSPROXY_AUTHENTICATE_2); /* Authentication response */ req.packet.auth2.magic3 = htons(0x0200); /* Something */ memcpy(req.packet.auth2.NTLMSSP, "NTLMSSP", sizeof("NTLMSSP")); /* Start of NTLM message */ req.packet.auth2.msgtype = htonl(0x03000000); /* Message type 2 */ req.packet.auth2.flags = flags | htonl(0x02000000); /* Choose authentication method */ data = req.packet.auth2.data; if (flags) { req.packet.auth2.lm_resp.len = 0; /* We are here if NTLM is supported, */ req.packet.auth2.lm_resp.alloc = 0; /* Do not fill in insecure LM response */ req.packet.auth2.lm_resp.offset = data - req.packet.auth2.NTLMSSP; req.packet.auth2.ntlm_resp.len = 24; /* Fill in NTLM response security buffer */ req.packet.auth2.ntlm_resp.alloc = 24; req.packet.auth2.ntlm_resp.offset = data - req.packet.auth2.NTLMSSP; ntlm_smb_nt_encrypt(prefs.hex_net_proxy_pass, challenge, data); /* Append an NTLM response */ data += 24; } else { req.packet.auth2.lm_resp.len = 24; /* Fill in LM response security buffer */ req.packet.auth2.lm_resp.alloc = 24; req.packet.auth2.lm_resp.offset = data - req.packet.auth2.NTLMSSP; ntlm_smb_encrypt(prefs.hex_net_proxy_pass, challenge, data); /* Append an LM response */ data += 24; req.packet.auth2.ntlm_resp.len = 0; /* NTLM response is empty */ req.packet.auth2.ntlm_resp.alloc = 0; req.packet.auth2.ntlm_resp.offset = data - req.packet.auth2.NTLMSSP; } req.packet.auth2.ntdomain_buf.len = strlen(ntdomain); /* Domain name */ req.packet.auth2.ntdomain_buf.alloc = req.packet.auth2.ntdomain_buf.len; req.packet.auth2.ntdomain_buf.offset = data - req.packet.auth2.NTLMSSP; strcpy(data, ntdomain); data += req.packet.auth2.ntdomain_buf.len; req.packet.auth2.username_buf.len = strlen(prefs.hex_net_proxy_user); /* Username */ req.packet.auth2.username_buf.alloc = req.packet.auth2.username_buf.len; req.packet.auth2.username_buf.offset = data - req.packet.auth2.NTLMSSP; strcpy(data, prefs.hex_net_proxy_user); data += req.packet.auth2.username_buf.len; req.packet.auth2.clienthost_buf.len = strlen(hostname); /* Hostname */ req.packet.auth2.clienthost_buf.alloc = req.packet.auth2.clienthost_buf.len; req.packet.auth2.clienthost_buf.offset = data - req.packet.auth2.NTLMSSP; strcpy(data, hostname); data += req.packet.auth2.clienthost_buf.len; req.packet.auth2.sessionkey_buf.len = 0; /* Session key (we don't use it) */ req.packet.auth2.sessionkey_buf.alloc = 0; req.packet.auth2.sessionkey_buf.offset = data - req.packet.auth2.NTLMSSP; if (send_msprequest(sok, state, &req, data) == -1) return 1; if (recv_mspresponse(sok, state, &res) == -1) return 1; if (res.serverid != state->serverid) { #ifdef DEBUG_MSPROXY printf ("expected res.serverid = 0x%x, is 0x%x\n", state->serverid, res.serverid); #endif return 1; } if (res.clientack != 0x01) { #ifdef DEBUG_MSPROXY printf ("expected res.clientack = 0x01, is 0x%x\n", res.clientack); #endif return 1; } if (ntohs(res.command) >> 8 != 0x47) { #ifdef DEBUG_MSPROXY printf ("expected res.command = 47??, is 0x%x\n", ntohs(res.command)); #endif return 1; } if (ntohs(res.command) == MSPROXY_AUTHENTICATE_2_NAK) { #ifdef DEBUG_MSPROXY printf ("Authentication failed\n"); #endif return -1; } #ifdef DEBUG_MSPROXY printf ("packet #5\n"); #endif bzero(&req, sizeof(req)); req.clientid = state->clientid; req.serverid = state->serverid; req.command = htons(MSPROXY_CONNECT); req.packet.connect.magic2 = htons(0x0200); req.packet.connect.magic6 = htons(0x0200); req.packet.connect.destport = htons(port); req.packet.connect.destaddr = destaddr; data = req.packet.connect.executable; strcpy(data, MSPROXY_EXECUTABLE); data += strlen(MSPROXY_EXECUTABLE) + 1; /* * need to tell server what port we will connect from, so we bind our sockets. */ ns_client = net_store_new (); if (!bound) { net_store_fill_any (ns_client); net_bind(ns_client, csok4, csok6); #ifdef DEBUG_MSPROXY perror ("bind() result"); #endif } clientport = net_getsockport(csok4, csok6); if (clientport == -1) { #ifdef DEBUG_MSPROXY printf ("Unable to obtain source port\n"); #endif return 1; } req.packet.connect.srcport = clientport; if (send_msprequest(sok, state, &req, data) == -1) return 1; if (recv_mspresponse(sok, state, &res) == -1) return 1; if (ntohs(res.command) != MSPROXY_CONNECT_ACK) { #ifdef DEBUG_MSPROXY printf ("expected res.command = 0x%x, is 0x%x\n",MSPROXY_CONNECT_ACK, ntohs(res.command)); #endif return 1; } net_store_fill_v4 (ns_client, res.packet.connect.clientaddr, res.packet.connect.clientport); #ifdef DEBUG_MSPROXY printf ("Connecting...\n"); #endif if (net_connect (ns_client, csok4, csok6, csok) != 0) { #ifdef DEBUG_MSPROXY printf ("Failed to connect to port %d\n", htons(res.packet.connect.clientport)); #endif net_store_destroy (ns_client); return 1; } net_store_destroy (ns_client); #ifdef DEBUG_MSPROXY printf ("packet #6\n"); #endif req.clientid = state->clientid; req.serverid = state->serverid; req.command = htons(MSPROXY_USERINFO_ACK); if (send_msprequest(sok, state, &req, req.packet.connack.data) == -1) return 1; return 0; } void msproxy_keepalive (void) { server *serv; GSList *list = serv_list; struct msproxy_request_t req; struct msproxy_response_t res; while (list) { serv = list->data; if (serv->connected && (serv->proxy_sok != -1)) { #ifdef DEBUG_MSPROXY printf ("sending MS proxy keepalive packet\n"); #endif bzero(&req, sizeof(req)); req.clientid = serv->msp_state.clientid; req.serverid = serv->msp_state.serverid; req.command = htons(MSPROXY_HELLO); if (send_msprequest(serv->proxy_sok, &serv->msp_state, &req, req.packet.hello.data) == -1) continue; recv_mspresponse(serv->proxy_sok, &serv->msp_state, &res); #ifdef DEBUG_MSPROXY if (ntohs(res.command) != MSPROXY_USERINFO_ACK) printf ("expected res.command = 0x%x, is 0x%x\n", MSPROXY_USERINFO_ACK, ntohs(res.command)); #endif } list = list->next; } } #endif