📄 pocketsmdlg.cpp
字号:
for(i=0; i < m_strUserName.GetLength(); i++)
userName[i] = (char)(m_strUserName.GetAt(i) & 0xFF);
userName[i] = 0;
for(i=0; i < m_strSipSrvAddress.GetLength(); i++)
srvAddress[i] = (char)(m_strSipSrvAddress.GetAt(i) & 0xFF);
srvAddress[i] = 0;
fprintf(f, "%s %s %d\r\n", userName,
srvAddress, m_nSipSrvPort);
int nCount = m_ctlComboTo.GetCount();
for(j=0; nCount!= CB_ERR && j<nCount; j++)
{
m_ctlComboTo.GetLBText(j, lStr);
for(i=0; lStr.GetLength()>0 && i<lStr.GetLength(); i++)
userName[i] = (char)(lStr.GetAt(i) & 0xFF);
userName[i] = 0;
fprintf(f, "%s\r\n", userName);
}
fclose(f);
}
CDialog::OnOK();
}
/**
* display SETUP dialog
*/
void CPocketSMDlg::OnButtonSetup()
{
int i;
CString strBuf;
// TODO: Add your control notification handler code here
// m_TrayIcon.MinimiseToTray(this);
if(m_dlgSetup->DoModal() == IDOK)
{
UpdateWindow();
if(m_bSetupOK)
SendSIPRegister("0", 2);
m_nFlag = 1;
if(m_dlgSetup->getUserName().GetLength() > 0
&& m_dlgSetup->getUserName().Find('@') > 0
&& m_dlgSetup->getUserName().Find(':') < 0)
{
m_strUserName = m_dlgSetup->getUserName();
i = m_strUserName.Find('@');
if (i!=-1)
{
m_strUserPart = m_strUserName.Left(i);
m_strSrvPart = m_strUserName.Right(m_strUserName.GetLength() - i - 1);
}
}
else
{
AfxMessageBox(_T("SIP User ID is invalid.\r\nMust be like:\r\n\"username@sipserver.com\""));
return;
}
if(m_dlgSetup->getPassword().GetLength() > 0)
m_strPassword = m_dlgSetup->getPassword();
if(m_dlgSetup->getServerAddress().GetLength() > 0)
m_strSipSrvAddress = m_dlgSetup->getServerAddress();
else
m_strSipSrvAddress = m_strSrvPart;
if(m_dlgSetup->getServerPort() > 0)
m_nSipSrvPort = m_dlgSetup->getServerPort();
strBuf.Format(_T("<%s> registering ...\r\n outbound proxy:\r\n <%s:%d>\r\n"),
m_strUserName, m_strSipSrvAddress, m_nSipSrvPort);
AddInfoMessage(strBuf);
UpdateDisplayedMessage();
if(SendSIPRegister("3600", 10) != 0)
AddToLastMessage("Not registered. Please try again.\r\n");
else
AddToLastMessage("Registered.\r\n");
UpdateDisplayedMessage();
}
}
/**
* hide the application's window
*/
void CPocketSMDlg::OnButtonHide()
{
// TODO: Add your control notification handler code here
// m_TrayIcon.MinimiseToTray(this);
if(!m_bDlgHide)
{
ShowWindow(SW_HIDE);
m_bDlgHide = true;
}
}
/**
* add the icon in system tray
*/
void CPocketSMDlg::AddSystemTrayIcon()
{
// handle to icon
HICON hIcon;
HINSTANCE hInst =
AfxFindResourceHandle(MAKEINTRESOURCE(IDR_MAINFRAME),
RT_GROUP_ICON);
hIcon = (HICON)LoadImage( hInst,
MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON,
16,
16,
LR_DEFAULTCOLOR);
// set NOTIFYCONDATA structure
m_nidIconData.cbSize = sizeof(NOTIFYICONDATA);
m_nidIconData.hWnd = m_hWnd;
m_nidIconData.uID = IDR_MAINFRAME;
m_nidIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_nidIconData.uCallbackMessage = WM_ICON_NOTIFY; // my user message
m_nidIconData.hIcon = hIcon;
m_nidIconData.szTip[0] = '\0';
// call to Shell_NotifyIcon with NIM_ADD parameter
Shell_NotifyIcon(NIM_ADD, &m_nidIconData);
// free icon
if (hIcon)
DestroyIcon(hIcon);
}
/**
* remove the icon from system tray
*/
void CPocketSMDlg::RemoveSystemTrayIcon()
{
// call to Shell_NotifyIcon with NIM_DEL parameter
Shell_NotifyIcon(NIM_DELETE, &m_nidIconData);
}
/**
* set a new icon in system tray
*/
void CPocketSMDlg::SetNewTrayIcon()
{
RemoveSystemTrayIcon();
// handle to icon
HICON hIcon;
HINSTANCE hInst =
AfxFindResourceHandle(MAKEINTRESOURCE(IDI_ICON_NEW),
RT_GROUP_ICON);
hIcon = (HICON)LoadImage( hInst,
MAKEINTRESOURCE(IDI_ICON_NEW),
IMAGE_ICON,
16,
16,
LR_DEFAULTCOLOR);
// set NOTIFYCONDATA structure
m_nidIconData.cbSize = sizeof(NOTIFYICONDATA);
m_nidIconData.hWnd = m_hWnd;
m_nidIconData.uID = IDI_ICON_NEW;
m_nidIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_nidIconData.uCallbackMessage = WM_ICON_NOTIFY; // my user message
m_nidIconData.hIcon = hIcon;
m_nidIconData.szTip[0] = '\0';
// call to Shell_NotifyIcon with NIM_ADD parameter
Shell_NotifyIcon(NIM_ADD, &m_nidIconData);
// free icon
if (hIcon)
DestroyIcon(hIcon);
}
/**
* set the old icon in system tray
*/
void CPocketSMDlg::SetOldTrayIcon()
{
RemoveSystemTrayIcon();
// handle to icon
HICON hIcon;
HINSTANCE hInst =
AfxFindResourceHandle(MAKEINTRESOURCE(IDR_MAINFRAME),
RT_GROUP_ICON);
hIcon = (HICON)LoadImage( hInst,
MAKEINTRESOURCE(IDR_MAINFRAME),
IMAGE_ICON,
16,
16,
LR_DEFAULTCOLOR);
// set NOTIFYCONDATA structure
m_nidIconData.cbSize = sizeof(NOTIFYICONDATA);
m_nidIconData.hWnd = m_hWnd;
m_nidIconData.uID = IDR_MAINFRAME;
m_nidIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_nidIconData.uCallbackMessage = WM_ICON_NOTIFY; // my user message
m_nidIconData.hIcon = hIcon;
m_nidIconData.szTip[0] = '\0';
// call to Shell_NotifyIcon with NIM_ADD parameter
Shell_NotifyIcon(NIM_ADD, &m_nidIconData);
// free icon
if (hIcon)
DestroyIcon(hIcon);
}
/**
* system tray icon notification handler
*/
LRESULT CPocketSMDlg::OnIconNotify(WPARAM wParam, LPARAM lParam)
{
UINT uMouseMsg = (UINT) lParam;
switch (uMouseMsg)
{
case WM_LBUTTONDOWN:
if(m_bDlgHide)
{
m_bDlgHide = false;
ShowWindow(SW_SHOW);
SetActiveWindow();
}
SetForegroundWindow();
if(m_bTrayIcon)
{
m_bTrayIcon = false;
SetOldTrayIcon();
}
//ShowTrayMenu();
break;
default: break;
}
return 0;
}
void CPocketSMDlg::OnButtonSend()
{
// TODO: Add your control notification handler code here
int nIndex = m_ctlComboTo.GetCurSel();
CString lStr, lStr1;
m_ctlComboTo.GetWindowText(lStr);
lStr.TrimLeft();
lStr.TrimRight();
if(nIndex == CB_ERR)
{
if(lStr.GetLength() > 3 && lStr.Find('@')>0)
{
if(m_ctlComboTo.FindStringExact(0, (LPCTSTR)lStr) == CB_ERR)
{
nIndex = m_ctlComboTo.GetCount();
if(nIndex != CB_ERR && nIndex >= MAX_CONTACTS)
m_ctlComboTo.DeleteString(nIndex-1);
m_ctlComboTo.InsertString(0, lStr);
}
}
else
{
lStr1.Format(_T("\"%s\" might be invalid SIP address!"), lStr);
AfxMessageBox(lStr1);
return;
}
}
else
{
if(nIndex != 0)
{
m_ctlComboTo.DeleteString(nIndex);
m_ctlComboTo.InsertString(0, lStr);
m_ctlComboTo.SetCurSel(0);
}
}
m_ctlEditMsg.GetWindowText(m_strEditMsg);
m_strEditMsg.TrimLeft();
m_strEditMsg.TrimRight();
if(m_strEditMsg.GetLength() <= 0)
{
AfxMessageBox(_T("No message to send!"));
return;
}
if(SendSIPMessage(lStr, m_strEditMsg, 5) != 0)
{
AfxMessageBox(_T("The message could not be sent!"));
return;
}
else
{
m_strEditMsg = "";
m_ctlEditMsg.SetWindowText(m_strEditMsg);
}
}
void CPocketSMDlg::AddInfoMessage(CString s)
{
msgList[last] = ".\r\n";
msgList[last] += s;
last = (last+1)%MSG_LIST_SIZE;
if(size < MSG_LIST_SIZE)
size++;
else
first = (first+1)%MSG_LIST_SIZE;
}
void CPocketSMDlg::AddSentMessage(CString t, CString s)
{
msgList[last].Format(_T(".To: <%s>\r\n%s\r\n"),
t, s);
last = (last+1)%MSG_LIST_SIZE;
if(size < MSG_LIST_SIZE)
size++;
else
first = (first+1)%MSG_LIST_SIZE;
}
void CPocketSMDlg::AddRecvMessage(CString f, CString s, CTime t)
{
msgList[last].Format(_T(".From: %s\r\n [%d-%02d-%02d %02d:%02d:%02d]\r\n%s\r\n"),
f, t.GetYear(), t.GetMonth(), t.GetDay(),
t.GetHour(), t.GetMinute(), t.GetSecond(), s);
last = (last+1)%MSG_LIST_SIZE;
if(size < MSG_LIST_SIZE)
size++;
else
first = (first+1)%MSG_LIST_SIZE;
}
void CPocketSMDlg::AddToLastMessage(CString s)
{
if(size == 0)
{
AddInfoMessage(s);
return;
}
msgList[(last+MSG_LIST_SIZE-1)%MSG_LIST_SIZE] += s;
}
void CPocketSMDlg::UpdateDisplayedMessage()
{
int n,i;
if(size == 0)
return;
n = size;
i = last-1;
m_strEditView="";
while(n>0)
{
m_strEditView += msgList[i] + "\r\n";
i = (i+MSG_LIST_SIZE-1)%MSG_LIST_SIZE;
n--;
}
UpdateData(FALSE);
}
void CPocketSMDlg::OnButtonExit()
{
// TODO: Add your control notification handler code here
OnOK();
}
/***
void CPocketSMDlg::OnButtonDeleteContact()
{
// TODO: Add your control notification handler code here
int nIndex = m_ctlComboTo.GetCurSel();
if(nIndex == CB_ERR)
{
AfxMessageBox(_T("No contact selected from the list."));
return;
}
if(m_ctlComboTo.DeleteString(nIndex) == CB_ERR)
{
AfxMessageBox(_T("The contact could not be deleted."));
return;
}
m_ctlComboTo.SetCurSel(0);
}
***/
int CPocketSMDlg::computeAuthHeader( CString sipRepl )
{
CString wwwAuth;
CString authHdr;
CString newSipReq;
int h,t,l,n;
int quote;
CString s;
CString strTmp;
CString par;
CString cnonce = "SAIDF3W2R8FAETR329FAHSDF34";
/* header params */
CString param[]={"realm","nonce","opaque","algorithm","qop","stale"};
CString vals[]={"","","","","",""};
CString strURI = "sip:";
int quoted[]={1,1,1,0,1,0};
HASHHEX hashHex1;
HASHHEX hashHex2;
str strMethod = {"REGISTER", 8};
char userBuf[30], passwdBuf[30], nonceBuf[50], cnonceBuf[50], uriBuf[50], realmBuf[50];
str sUser = {userBuf, 30}, sPasswd = {passwdBuf, 30}, sRealm = {realmBuf, 50},
sNonce = {nonceBuf, 50}, sCnonce = {cnonceBuf, 50}, sURI = {uriBuf, 50};
m_strAuthHdr = "";
strURI += m_strSrvPart;
//AddInfoMessage(sipRepl);
//UpdateDisplayedMessage();
/* get the www-auth header from reply */
h = sipRepl.Find(_T("WWW-Authenticate"));
if (h==-1)
return -1;
strTmp = sipRepl.Right(sipRepl.GetLength()-h);
t = strTmp.Find('\n');
if(strTmp.GetAt(t-1) == '\r')
t--;
wwwAuth = sipRepl.Mid(h,t);
/* parse the www_auth header */
s = wwwAuth;
h = s.Find(_T("Digest"));
if (h==-1)
return -2;
h += 6;
/* comput the length of useful part */
l = s.GetLength();
while (s.GetAt(l-1)=='\n' || s.GetAt(l-1)=='\r')
l--;
/* look for all the params */
while(h<l) {
/* search the begining of the next param */
while( h<l && (s.GetAt(h)==' ' || s.GetAt(h)=='\t') )
h++;
if (h==l)
return -3;
/* get the param name */
t = h;
while( t<l && s.GetAt(t)!=' ' && s.GetAt(t)!='=')
t++;
if (t==l || t==h)
return -4;
par = s.Mid(h,t-h);
h=t;
/* are we looking for this parameter? */
for(n=0; n<6; n++)
if(!param[n].CompareNoCase(par))
break;
if(n==6)
n = -1;
/* eat spaces */
while( h<l && (s.GetAt(h)==' ' || s.GetAt(h)=='\t') )
h++;
/* do we have a "="? */
if (h==l || s.GetAt(h)!='=' || ++h==l)
return -5;
/* eat spaces */
while( h<l && (s.GetAt(h)==' ' || s.GetAt(h)=='\t') )
h++;
/* param's value */
if ( n!=-1 && ( (quoted[n]==1 && s.GetAt(h)!='"') ||
(quoted[n]==0 && s.GetAt(h)=='"')) )
return -6;
if(n!=-1 && quoted[n]==1)
h++;
quote=(n==-1&&s.GetAt(h)=='"')||(n!=-1&"ed[n]==1)?1:0;
/* look for the end of value */
t = h;
while( t<l && (quote && s.GetAt(t)!='"' ||
!quote && s.GetAt(t)!=' ' && s.GetAt(t)!=',') )
t++;
if (t==h)
return -7;
if (n!=-1) {
vals[n] = s.Mid(h,t-h);
}
h = t + (quote?1:0);
/* eat spaces */
while( h<l && (s.GetAt(h)==' ' || s.GetAt(h)=='\t') )
h++;
if (h==l) break;
/* here must be a ',' */
if (s.GetAt(h)!=',' || ++h==l)
return -8;
}
/* some checkings */
if (vals[0]=="")
return -9;
if (vals[1]=="")
return -10;
/* comput the authentication */
translateToStr(m_strUserPart, &sUser);
translateToStr(m_strPassword, &sPasswd);
translateToStr(vals[0], &sRealm);
translateToStr(vals[1], &sNonce);
translateToStr(cnonce, &sCnonce);
translateToStr(strURI, &sURI);
calc_HA1(HA_MD5, &sUser, &sRealm, &sPasswd, &sNonce, &sCnonce, hashHex1);
calc_response(hashHex1, &sNonce, NULL, &sCnonce, NULL, 0, &strMethod, &sURI, NULL, hashHex2);
/* build authorization header */
m_strAuthHdr = "Authorization: Digest";
/* add username */
m_strAuthHdr += " username=\""+m_strUserPart+"\"";
/* add realm */
m_strAuthHdr += ", realm=\""+vals[0]+"\"";
/* add uri */
m_strAuthHdr += ", uri=\"";
m_strAuthHdr += strURI;
m_strAuthHdr += "\"";
/* add algorithm */
m_strAuthHdr += ", algorithm="+vals[3]+"";
/* add nonce */
m_strAuthHdr += ", nonce=\""+vals[1]+"\"";
/* add opaque, if needed */
if (vals[2]!="")
m_strAuthHdr += ", opaque=\""+vals[2]+"\"";
/* add qop, if needed */
if (vals[4]!="") {
m_strAuthHdr += ", qop=\""+vals[4]+"\"";
m_strAuthHdr += ", cnonce=\""+cnonce+"\"";
}
/* add response */
m_strAuthHdr += ", response=\"";
m_strAuthHdr += hashHex2;
m_strAuthHdr += "\"";
return 0;
}
int CPocketSMDlg::translateToStr(CString s, str *ps)
{
int i,l;
if(ps == NULL)
return -1;
// convert from UNICODE to ASCII
l = s.GetLength();
if(l>ps->len)
return -2;
ps->len = l;
for(i=0; i<l; i++)
ps->s[i] = (char)(s.GetAt(i) & 0xFF);
return 0;
}
void CPocketSMDlg::UpdateCallID()
{
m_nCallID = (m_nCallID+1)%100000;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -