За последние 24 часа нас посетил 18641 программист и 1660 роботов. Сейчас ищут 963 программиста ...

Помогите написать xmpp-бота на готовом серваке!

Тема в разделе "Прочие вопросы по PHP", создана пользователем jeka_ua, 22 авг 2009.

  1. jeka_ua

    jeka_ua Активный пользователь

    С нами с:
    21 авг 2009
    Сообщения:
    3
    Симпатии:
    0
    Адрес:
    Херсон
    Хочу чтобы слал уведомления на клиенты о новых событиях.
    В серваке есть такие функции:
    Код (Text):
    1. <?
    2. class CXMPPUtility
    3. {
    4.     function GetUserByJId($jId)
    5.     {
    6.         if (StrLen($jId) <= 0)
    7.             return false;
    8.  
    9.         $pos = StrPos($jId, "@");
    10.         if ($pos !== false)
    11.             $login = SubStr($jId, 0, $pos);
    12.         else
    13.             $login = $jId;
    14.  
    15.         if (StrLen($login) <= 0)
    16.             return false;
    17.  
    18.         $dbUsers = CUser::GetList(($by = "ID"), ($order = "desc"), array("LOGIN_EQUAL_EXACT" => $login));
    19.         $arUser = $dbUsers->Fetch();
    20.         if (!$arUser)
    21.             return false;
    22.  
    23.         return $arUser;
    24.     }
    25.  
    26.     function GetJIdByUserId($userId)
    27.     {
    28.         if (IntVal($userId) <= 0)
    29.             return false;
    30.  
    31.         $dbUsers = CUser::GetList(($by = "ID"), ($order = "desc"), array("ID_EQUAL_EXACT" => $userId));
    32.         $arUser = $dbUsers->Fetch();
    33.         if (!$arUser)
    34.             return false;
    35.  
    36.         return CXMPPUtility::GetJId($arUser);
    37.     }
    38.  
    39.     function GetJId($arUser)
    40.     {
    41.         $domain = CXMPPServer::GetDomain();
    42.  
    43.         $login = preg_replace("/[^a-zA-Z0-9._-]/i", "_", $arUser["LOGIN"]);
    44.  
    45.         return $login."@".$domain;
    46.     }
    47.  
    48.     function Show($str, $level = 0)
    49.     {
    50.         if (CXMPPServer::IsServerStarted())
    51.         {
    52.             $server = CXMPPServer::GetServer();
    53.             $server->WriteToLog($str, 0);
    54.         }
    55.     }
    56.  
    57.     function _SendToServer($arMessage, &$errorNo, &$errorStr)
    58.     {
    59.         if ($f = @fsockopen("127.0.0.1", 5222, $errNo, $errStr, 10))    //CXMPPServer::GetDomain()
    60.         {
    61.             $arMessage['server'] = array(
    62.                 '.' => array('uniid' => CXMPPUtility::GetUniid()),
    63.                 '#' => '',
    64.             );
    65.  
    66.             $message = CXMPPParser::ToXml($arMessage);
    67.  
    68.             fwrite($f, $message);
    69.  
    70.             $responce = "";
    71.             while (!feof($f))
    72.                 $responce .= trim(fread($f, 8192));
    73.  
    74.             fclose($f);
    75.  
    76.             $arResponce = CXMPPParser::ToArray($responce);
    77.  
    78.             return $arResponce;
    79.         }
    80.  
    81.         $errorNo = $errNo;
    82.         $errorStr = $errStr;
    83.  
    84.         return false;
    85.     }
    86.  
    87.     function SendToServer($arMessage)
    88.     {
    89.         $arResponce = CXMPPUtility::_SendToServer($arMessage, $errorNo, $errorStr);
    90.         return ($arResponce && $arResponce['result']['.']['type'] == 'success');
    91.     }
    92.  
    93.     function _SendToServerXML($message, &$errorNo, &$errorStr)
    94.     {
    95.         if ($f = @fsockopen("127.0.0.1", 5222, $errNo, $errStr, 10))    //CXMPPServer::GetDomain()
    96.         {
    97.             $message .= '<server uniid="'.CXMPPUtility::GetUniid().'" />';
    98.  
    99.             fwrite($f, $message);
    100.  
    101.             $responce = "";
    102.             while (!feof($f))
    103.                 $responce .= trim(fread($f, 8192));
    104.  
    105.             fclose($f);
    106.  
    107.             return $responce;
    108.         }
    109.  
    110.         $errorNo = $errNo;
    111.         $errorStr = $errStr;
    112.  
    113.         return false;
    114.     }
    115.  
    116.     function GetUniid()
    117.     {
    118.         return md5($GLOBALS["APPLICATION"]->GetServerUniqID());
    119.         //global $DB;
    120.         //return md5($DB->DBHost.$DB->DBName.$DB->DBPassword);
    121.     }
    122.  
    123.  
    124.     function GetMessageArray($senderJId, $receiverJId, $messageType, $body)
    125.     {
    126.         if (StrLen($receiverJId) <= 0)
    127.             return false;
    128.  
    129.         // chat - The message is sent in the context of a one-to-one chat conversation.
    130.         // error - An error has occurred related to a previous message sent by the sender.
    131.         // groupchat - The message is sent in the context of a multi-user chat environment.
    132.         // headline - The message is probably generated by an automated service that delivers or broadcasts content.
    133.         // normal - The message is a single message that is sent outside the context of a one-to-one conversation or
    134.         //          groupchat, and to which it is expected that the recipient will reply.
    135.         $arAllowableMessageTypes = array("chat", "groupchat", "headline", "normal");
    136.         if (!In_Array($messageType, $arAllowableMessageTypes))
    137.             return false;
    138.  
    139.         $arResult = array(
    140.             "message" => array(
    141.                 "." => array(
    142.                     "from" => $senderJId,
    143.                     "to" => $receiverJId,
    144.                     "type" => $messageType,
    145.                     "id" => "u".Rand(1000, 9999),
    146.                 ),
    147.                 "body" => array(
    148.                     "#" => $body,
    149.                 ),
    150.             ),
    151.         );
    152.  
    153.         return $arResult;
    154.     }
    155.  
    156.  
    157.     function GetErrorArray($receiverJId, $stanzaKind, $errorType, $condition, $senderJId = "", $id = "", $text = "")
    158.     {
    159.         if (StrLen($receiverJId) <= 0)
    160.             return false;
    161.  
    162.         $arAllowableStanzaKinds = array("message", "presence", "iq");
    163.         if (!In_Array($stanzaKind, $arAllowableStanzaKinds))
    164.             return false;
    165.  
    166.         // cancel - do not retry (the error is unrecoverable)
    167.         // continue - proceed (the condition was only a warning)
    168.         // modify - retry after changing the data sent
    169.         // auth - retry after providing credentials
    170.         // wait - retry after waiting (the error is temporary)
    171.         $arAllowableErrorTypes = array("cancel", "continue", "modify", "auth", "wait");
    172.         if (!In_Array($errorType, $arAllowableErrorTypes))
    173.             return false;
    174.  
    175.         switch ($condition)
    176.         {
    177.             // the sender has sent XML that is malformed or that cannot be processed (e.g., an IQ stanza that includes an
    178.             // unrecognized value of the 'type' attribute); the associated error type SHOULD be "modify".
    179.             case "bad-request":
    180.                 $errorType = "modify";
    181.                 break;
    182.             // access cannot be granted because an existing resource or session exists with the same name or address; the
    183.             // associated error type SHOULD be "cancel".
    184.             case "conflict":
    185.                 $errorType = "cancel";
    186.                 break;
    187.             // the feature requested is not implemented by the recipient or server and therefore cannot be
    188.             // processed; the associated error type SHOULD be "cancel".
    189.             case "feature-not-implemented":
    190.                 $errorType = "cancel";
    191.                 break;
    192.             // the requesting entity does not possess the required permissions to perform the action; the associated
    193.             // error type SHOULD be "auth".
    194.             case "forbidden":
    195.                 $errorType = "auth";
    196.                 break;
    197.             // the recipient or server can no longer be contacted at this address (the error stanza MAY contain a new address in the
    198.             // XML character data of the <gone/> element); the associated error type SHOULD be "modify".
    199.             case "gone":
    200.                 $errorType = "modify";
    201.                 break;
    202.             // the server could not process the stanza because of a misconfiguration or an otherwise-undefined
    203.             // internal server error; the associated error type SHOULD be "wait".
    204.             case "internal-server-error":
    205.                 $errorType = "wait";
    206.                 break;
    207.             // the addressed JID or item requested cannot be found; the associated error type SHOULD be "cancel".
    208.             case "item-not-found":
    209.                 $errorType = "cancel";
    210.                 break;
    211.             // the sending entity has provided or communicated an XMPP address (e.g., a value of the 'to' attribute)
    212.             // or aspect thereof (e.g., a resource identifier) that does not adhere to the syntax defined in
    213.             // Addressing Scheme; the associated error type SHOULD be "modify".
    214.             case "jid-malformed":
    215.                 $errorType = "modify";
    216.                 break;
    217.             // the recipient or server understands the request but is refusing to process it because it does not meet
    218.             // criteria defined by the recipient or server (e.g., a local policy regarding acceptable words in
    219.             // messages); the associated error type SHOULD be "modify".
    220.             case "not-acceptable":
    221.                 $errorType = "modify";
    222.                 break;
    223.             // the recipient or server does not allow any entity to perform the action; the associated
    224.             // error type SHOULD be "cancel".
    225.             case "not-allowed":
    226.                 $errorType = "cancel";
    227.                 break;
    228.             // the sender must provide proper credentials before being allowed to perform the action,
    229.             // or has provided improper credentials; the associated error type SHOULD be "auth".
    230.             case "not-authorized":
    231.                 $errorType = "auth";
    232.                 break;
    233.             // the requesting entity is not authorized to access the requested service because payment is
    234.             // required; the associated error type SHOULD be "auth".
    235.             case "payment-required":
    236.                 $errorType = "auth";
    237.                 break;
    238.             // the intended recipient is temporarily unavailable; the associated error type SHOULD
    239.             // be "wait" (note: an application MUST NOT return this error if doing so would provide
    240.             // information about the intended recipient's network availability to
    241.             // an entity that is not authorized to know such information).
    242.             case "recipient-unavailable":
    243.                 $errorType = "wait";
    244.                 break;
    245.             // the recipient or server is redirecting requests for this information to another entity,
    246.             // usually temporarily (the error stanza SHOULD contain the alternate address, which MUST be
    247.             // a valid JID, in the XML character data of the <redirect/> element); the
    248.             // associated error type SHOULD be "modify".
    249.             case "redirect":
    250.                 $errorType = "modify";
    251.                 break;
    252.             // the requesting entity is not authorized to access the requested service because registration
    253.             // is required; the associated error type SHOULD be "auth".
    254.             case "registration-required":
    255.                 $errorType = "auth";
    256.                 break;
    257.             // a remote server or service specified as part or all of the JID of the intended recipient
    258.             // does not exist; the associated error type SHOULD be "cancel".
    259.             case "remote-server-not-found":
    260.                 $errorType = "cancel";
    261.                 break;
    262.             // a remote server or service specified as part or all of the JID of the intended recipient
    263.             // (or required to fulfill a request) could not be contacted within a reasonable
    264.             // amount of time; the associated error type SHOULD be "wait".
    265.             case "remote-server-timeout":
    266.                 $errorType = "wait";
    267.                 break;
    268.             // the server or recipient lacks the system resources necessary to service the request; the
    269.             // associated error type SHOULD be "wait".
    270.             case "resource-constraint":
    271.                 $errorType = "wait";
    272.                 break;
    273.             // the server or recipient does not currently provide the requested service; the associated
    274.             // error type SHOULD be "cancel".
    275.             case "service-unavailable":
    276.                 $errorType = "cancel";
    277.                 break;
    278.             // the requesting entity is not authorized to access the requested service because a subscription
    279.             // is required; the associated error type SHOULD be "auth".
    280.             case "subscription-required":
    281.                 $errorType = "auth";
    282.                 break;
    283.             // the error condition is not one of those defined by the other conditions in this list;
    284.             // any error type may be associated with this condition, and it SHOULD be used only in
    285.             // conjunction with an application-specific condition.
    286.             case "undefined-condition":
    287.                 break;
    288.             // the recipient or server understood the request but was not expecting it at this
    289.             // time (e.g., the request was out of order); the associated error type SHOULD be "wait".
    290.             case "unexpected-request":
    291.                 $errorType = "wait";
    292.                 break;
    293.             default:
    294.                 return false;
    295.                 break;
    296.         }
    297.  
    298.         $arResult = array(
    299.             $stanzaKind => array(
    300.                 "." => array(
    301.                     "to" => $receiverJId,
    302.                     "type" => "error",
    303.                 ),
    304.                 "error" => array(
    305.                     "." => array(
    306.                         "type" => $errorType,
    307.                     ),
    308.                     $condition => array(
    309.                         "." => array(
    310.                             "xmlns" => "urn:ietf:params:xml:ns:xmpp-stanzas",
    311.                         ),
    312.                     ),
    313.                 ),
    314.             ),
    315.         );
    316.  
    317.         if (StrLen($senderJId) > 0)
    318.             $arResult[$stanzaKind]["."]["from"] = $senderJId;
    319.  
    320.         if (StrLen($id) > 0)
    321.             $arResult[$stanzaKind]["."]["id"] = $id;
    322.  
    323.         if (StrLen($text) > 0)
    324.         {
    325.             $arResult[$stanzaKind]["error"]["text"] = array(
    326.                 "." => array(
    327.                     "xmlns" => "urn:ietf:params:xml:ns:xmpp-stanzas",
    328.                     "xml:lang" => "en",
    329.                 ),
    330.                 "#" => $text,
    331.             );
    332.         }
    333.  
    334.         return $arResult;
    335.     }
    336.  
    337.     function GetServerErrorArray($errorMessage)
    338.     {
    339.         return array(
    340.             'result' => array(
    341.                 "." => array(
    342.                     "type" => "error",
    343.                 ),
    344.                 "message" => array(
    345.                     "#" => $errorMessage
    346.                 ),
    347.             ),
    348.         );
    349.     }
    350.  
    351. }
    352. ?>
    Попробывал отправить сообщение, сервер молчит
    Код (Text):
    1. $message=CXMPPUtility::GetMessageArray(CXMPPUtility::GetJIdByUserId(479),CXMPPUtility::GetJIdByUserId(1),"hi!");
    2. $temp=CXMPPUtility::_SendToServer($message, $errorNo, $errorStr);//_SendToServerXML($message, $errorNo, $errorStr);
    3. if($temp){echo "работает";}else
    4. {
    5. echo $temp;
    6. echo $errorNo;
    7. echo $errorStr;
    8. };
    Пробывал ручками все прописывать НЕ РАБОТАЕТ.
    Помогите пожалуйста.