]> cgit.babelmonkeys.de Git - mpdbot.git/blob - src/mpdbot.m
Update/add copyright
[mpdbot.git] / src / mpdbot.m
1 /*
2  * Copyright (c) 2010, 2011, Jonathan Schleifer <js@webkeks.org>
3  * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
4  *
5  * http://cgit.babelmonkeys.de/cgit.cgi/mpdbot/
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice is present in all copies.
10  *
11  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
12  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
15  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
16  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
17  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
18  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
19  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  * POSSIBILITY OF SUCH DAMAGE.
22  */
23
24 #import <ObjFW/ObjFW.h>
25 #import <ObjXMPP/ObjXMPP.h>
26
27 #import "PEPThread.h"
28
29 #define NS_DISCO_INFO @"http://jabber.org/protocol/disco#info"
30
31 @interface AppDelegate: OFObject
32 #ifdef OF_HAVE_OPTIONAL_PROTOCOLS
33     <OFApplicationDelegate, XMPPConnectionDelegate>
34 #endif
35
36 OFString *discoID;
37 PEPThread *pepper;
38 @end
39
40 OF_APPLICATION_DELEGATE(AppDelegate)
41
42 @implementation AppDelegate
43 - (void)applicationDidFinishLaunching
44 {
45         XMPPConnection *conn;
46         OFArray *arguments = [OFApplication arguments];
47
48         conn = [[XMPPConnection alloc] init];
49         [conn setDelegate: self];
50
51         if ([arguments count] != 3) {
52                 of_log(@"Invalid count of command line arguments!");
53                 [OFApplication terminateWithStatus: 1];
54         }
55
56         [conn setDomain: [arguments objectAtIndex: 0]];
57         [conn setUsername: [arguments objectAtIndex: 1]];
58         [conn setPassword: [arguments objectAtIndex: 2]];
59         [conn setResource: @"ObjXMPP"];
60
61         @try {
62                 [conn connect];
63                 [conn handleConnection];
64         } @catch (id e) {
65                 of_log(@"%@", e);
66         }
67 }
68
69 - (void)connectionWasAuthenticated: (XMPPConnection*)conn
70 {
71         of_log(@"Auth successful");
72 }
73
74 - (void)connection: (XMPPConnection*)conn
75      wasBoundToJID: (XMPPJID*)jid
76 {
77         XMPPPresence *pres;
78         XMPPIQ *disco;
79
80         of_log(@"Bound to JID: %@", [jid fullJID]);
81
82         pres = [XMPPPresence presence];
83         [pres addPriority: 0];
84         [pres addStatus: @"Hello I'm MPDbot!"];
85         [conn sendStanza: pres];
86
87         discoID = [[conn generateStanzaID] retain];
88         disco = [XMPPIQ IQWithType: @"get"
89                                 ID: discoID];
90         disco.to = [XMPPJID JIDWithString: [[conn JID] bareJID]];
91         [disco addChild: [OFXMLElement
92         elementWithName: @"query"
93               namespace: NS_DISCO_INFO]];
94
95         [conn sendStanza: disco];
96 }
97
98 - (BOOL)connection: (XMPPConnection*)conn
99       didReceiveIQ: (XMPPIQ*)iq
100 {
101         OFXMLElement *query = [iq elementForName: @"query"
102                                        namespace: NS_DISCO_INFO];
103         if ([[iq ID] isEqual: discoID]) {
104                 for (OFXMLElement *identity
105                     in [query elementsForName: @"identity"
106                                     namespace: NS_DISCO_INFO]) {
107                         if ([[[identity attributeForName: @"category"]
108                             stringValue] isEqual: @"pubsub"] &&
109                             [[[identity attributeForName: @"type"] stringValue]
110                             isEqual: @"pep"]) {
111                                 pepper = [[PEPThread alloc]
112                                     initWithObject: conn];
113                                 [pepper start];
114                                 return YES;
115                         }
116                 }
117         }
118
119         return NO;
120 }
121
122 -  (void)connection: (XMPPConnection*)conn
123   didReceiveMessage: (XMPPMessage*)msg
124 {
125         of_log(@"Message: %@", msg);
126 }
127
128 -   (void)connection: (XMPPConnection*)conn
129   didReceivePresence: (XMPPPresence*)pres
130 {
131         of_log(@"Presence: %@", pres);
132 }
133
134 - (void)connectionWasClosed: (XMPPConnection*)conn
135 {
136         of_log(@"Connection was closed!");
137 }
138 @end